Skip to content

Commit

Permalink
feat(jni): add selectRimeCanidate and forgetRimeCandidate APIs
Browse files Browse the repository at this point in the history
Useful when get bulk candidates
  • Loading branch information
WhiredPlanck committed Aug 27, 2024
1 parent 74430fa commit e5f7309
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class Rime : RimeApi, RimeLifecycleOwner {
getCurrentRimeSchema() == ".default" // 無方案
}

override suspend fun selectCandidate(idx: Int): Boolean =
withRimeContext {
selectRimeCandidate(idx).also { if (it) updateContext() }
}

override suspend fun forgetCandidate(idx: Int): Boolean =
withRimeContext {
forgetRimeCandidate(idx).also { if (it) updateContext() }
}

override suspend fun availableSchemata(): Array<SchemaItem> = withRimeContext { getAvailableRimeSchemaList() }

override suspend fun enabledSchemata(): Array<SchemaItem> = withRimeContext { getSelectedRimeSchemaList() }
Expand Down Expand Up @@ -421,6 +431,12 @@ class Rime : RimeApi, RimeLifecycleOwner {
@JvmStatic
external fun deleteRimeCandidateOnCurrentPage(index: Int): Boolean

@JvmStatic
external fun selectRimeCandidate(index: Int): Boolean

@JvmStatic
external fun forgetRimeCandidate(index: Int): Boolean

@JvmStatic
external fun getLibrimeVersion(): String

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ interface RimeApi {

suspend fun isEmpty(): Boolean

suspend fun selectCandidate(idx: Int): Boolean

suspend fun forgetCandidate(idx: Int): Boolean

suspend fun availableSchemata(): Array<SchemaItem>

suspend fun enabledSchemata(): Array<SchemaItem>
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/jni/librime_jni/rime_jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ class Rime {
return rime->delete_candidate_on_current_page(session, index);
}

bool selectCandidate(size_t index) {
return rime->select_candidate(session, index);
}

bool forgetCandidate(size_t index) {
return rime->delete_candidate(session, index);
}

std::string stateLabel(const std::string &optionName, bool state) {
return rime->get_state_label(session, optionName.c_str(), state);
}
Expand Down Expand Up @@ -443,6 +451,26 @@ Java_com_osfans_trime_core_Rime_deleteRimeCandidateOnCurrentPage(
return Rime::Instance().deleteCandidateOnCurrentPage(index);
}

extern "C" JNIEXPORT jboolean JNICALL
Java_com_osfans_trime_core_Rime_selectRimeCandidate(JNIEnv *env,
jclass /* thiz */,
jint index) {
if (!is_rime_running()) {
return false;
}
return Rime::Instance().selectCandidate(index);
}

extern "C" JNIEXPORT jboolean JNICALL
Java_com_osfans_trime_core_Rime_forgetRimeCandidate(JNIEnv *env,
jclass /* thiz */,
jint index) {
if (!is_rime_running()) {
return false;
}
return Rime::Instance().forgetCandidate(index);
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_osfans_trime_core_Rime_getLibrimeVersion(JNIEnv *env,
jclass /* thiz */) {
Expand Down

0 comments on commit e5f7309

Please sign in to comment.