Skip to content

Commit 5a14112

Browse files
committed
refactor(core,jni,ui): reform the native method of selecting schemas
1 parent c6140e4 commit 5a14112

File tree

4 files changed

+32
-43
lines changed

4 files changed

+32
-43
lines changed

app/src/main/java/com/osfans/trime/core/Rime.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ public static String getSchemaName() {
554554
public static boolean selectSchema(String schemaId) {
555555
Timber.d("Selecting schemaId=%s", schemaId);
556556
overWriteSchema(schemaId);
557-
boolean b = select_schema(schemaId);
557+
boolean b = selectRimeSchema(schemaId);
558558
getContexts();
559559
return b;
560560
}
@@ -565,8 +565,8 @@ public static void applySchemaChange() {
565565
// 实测直接select_schema(schema_id)方案没有重新载入,切换到不存在的方案,再切回去(会产生1秒的额外耗时).需要找到更好的方法
566566
// 不发生覆盖则不生效
567567
if (overWriteSchema(schema_id)) {
568-
select_schema("nill");
569-
select_schema(schema_id);
568+
selectRimeSchema("nill");
569+
selectRimeSchema(schema_id);
570570
}
571571
getContexts();
572572
}
@@ -769,7 +769,7 @@ public static boolean deploy_config_file(String name, boolean skipIfExists) {
769769
@NonNull
770770
public static native String getCurrentRimeSchema();
771771

772-
public static native boolean select_schema(String schema_id);
772+
public static native boolean selectRimeSchema(@NonNull String schemaId);
773773

774774
// configuration
775775
public static native Boolean config_get_bool(String name, String key);
@@ -852,7 +852,7 @@ public static native Map<String, Object> getRimeConfigMap(
852852
@NonNull
853853
public static native SchemaListItem[] getSelectedRimeSchemaList();
854854

855-
public static native boolean select_schemas(String[] schema_id_list);
855+
public static native boolean selectRimeSchemas(@NonNull String[] schemaIds);
856856

857857
// opencc
858858
public static native String get_opencc_version();

app/src/main/java/com/osfans/trime/ui/main/Pickers.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ suspend fun Context.schemaPicker(
8585
}
8686
postiveDispatcher = Dispatchers.Default
8787
onOKButton {
88-
Rime.select_schemas(
88+
Rime.selectRimeSchemas(
8989
items.filterIndexed { index, _ ->
9090
checkedItems[index]
9191
}.map { it.toString() }.toTypedArray()

app/src/main/jni/librime_jni/levers.cc

+16-24
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,22 @@ Java_com_osfans_trime_core_Rime_getSelectedRimeSchemaList(JNIEnv *env, jclass /*
107107

108108
extern "C"
109109
JNIEXPORT jboolean JNICALL
110-
Java_com_osfans_trime_core_Rime_select_1schemas(JNIEnv *env, jclass /* thiz */, jobjectArray stringArray) {
111-
if (stringArray == nullptr) return false;
112-
int count = env->GetArrayLength(stringArray);
113-
if (count == 0) return false;
114-
const char** schema_id_list = new const char*[count];
115-
for (int i = 0; i < count; i++) {
116-
auto string = (jstring) env->GetObjectArrayElement(stringArray, i);
117-
const char *rawString = env->GetStringUTFChars(string, nullptr);
118-
schema_id_list[i] = rawString;
110+
Java_com_osfans_trime_core_Rime_selectRimeSchemas(JNIEnv *env, jclass /* thiz */, jobjectArray array) {
111+
int schemaIdsLength = env->GetArrayLength(array);
112+
const char* entries[schemaIdsLength];
113+
for (int i = 0; i < schemaIdsLength; i++) {
114+
auto string = JRef<jstring>(env, env->GetObjectArrayElement(array, i));
115+
entries[i] = env->GetStringUTFChars(string, nullptr);
119116
}
120-
RimeLeversApi* api_ = get_levers();
121-
RimeSwitcherSettings* settings_ = api_->switcher_settings_init();
122-
auto *custom_settings_ = (RimeCustomSettings *) settings_;
123-
Bool b = api_->load_settings(custom_settings_);
124-
if (b) {
125-
b = api_->select_schemas(settings_, schema_id_list, count);
126-
api_->save_settings(custom_settings_);
127-
api_->custom_settings_destroy(custom_settings_);
128-
}
129-
for (int i = 0; i < count; i++) {
130-
auto string = (jstring) env->GetObjectArrayElement(stringArray, i);
131-
const char *rawString = schema_id_list[i];
132-
env->ReleaseStringUTFChars(string, rawString);
117+
auto levers = get_levers();
118+
auto switcher = levers->switcher_settings_init();
119+
levers->load_settings((RimeCustomSettings *) switcher);
120+
levers->select_schemas(switcher, entries, schemaIdsLength);
121+
levers->save_settings((RimeCustomSettings *) switcher);
122+
levers->custom_settings_destroy((RimeCustomSettings *) switcher);
123+
for (int i = 0; i < schemaIdsLength; i++) {
124+
auto string = JRef<jstring>(env, env->GetObjectArrayElement(array, i));
125+
env->ReleaseStringUTFChars(string, entries[i]);
133126
}
134-
delete[] schema_id_list;
135-
return b;
127+
return true;
136128
}

app/src/main/jni/librime_jni/rime_jni.cc

+10-13
Original file line numberDiff line numberDiff line change
@@ -409,20 +409,17 @@ Java_com_osfans_trime_core_Rime_getCurrentRimeSchema(JNIEnv *env, jclass /* thiz
409409

410410
extern "C"
411411
JNIEXPORT jboolean JNICALL
412-
Java_com_osfans_trime_core_Rime_select_1schema(JNIEnv *env, jclass /* thiz */, jstring schema_id) {
413-
const char* s = schema_id == nullptr ? nullptr : env->GetStringUTFChars(schema_id, nullptr);
414-
RimeConfig config = {nullptr};
415-
Bool b = RimeUserConfigOpen("user", &config);
416-
if (b) {
417-
b = RimeConfigSetString(&config, "var/previously_selected_schema", s);
418-
std::string str(s);
419-
str = "var/schema_access_time/" + str;
420-
b = RimeConfigSetInt(&config, str.c_str(), time(nullptr));
412+
Java_com_osfans_trime_core_Rime_selectRimeSchema(JNIEnv *env, jclass /* thiz */, jstring schema_id) {
413+
auto rime = rime_get_api();
414+
RimeConfig user = {nullptr};
415+
auto schema = CString(env, schema_id);
416+
if (rime->user_config_open("user", &user)) {
417+
rime->config_set_string(&user, "var/previously_selected_schema", schema);
418+
std::string key = "var/schema_access_time/" + std::string (schema);
419+
rime->config_set_int(&user, key.c_str(), time(nullptr));
420+
rime->config_close(&user);
421421
}
422-
RimeConfigClose(&config);
423-
bool value = RimeSelectSchema(activated_session_id, s);
424-
env->ReleaseStringUTFChars(schema_id, s);
425-
return value;
422+
return rime->select_schema(activated_session_id, schema);
426423
}
427424

428425
// configuration

0 commit comments

Comments
 (0)