Skip to content

Commit 77996e2

Browse files
WhiredPlanckBambooin
authored andcommitted
refactor: convert Rime.java to Rime.kt
1 parent b2dcd7e commit 77996e2

File tree

14 files changed

+683
-673
lines changed

14 files changed

+683
-673
lines changed

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

-477
This file was deleted.

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

+440
Large diffs are not rendered by default.

app/src/main/java/com/osfans/trime/core/Structs.kt

+118
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.osfans.trime.core
22

3+
import timber.log.Timber
4+
35
data class SchemaListItem(
46
val schemaId: String?,
57
val name: String?,
@@ -9,3 +11,119 @@ data class CandidateListItem(
911
val comment: String,
1012
val text: String,
1113
)
14+
15+
/** Rime編碼區 */
16+
data class RimeComposition(
17+
val length: Int = 0,
18+
val cursorPos: Int = 0,
19+
val selStart: Int = 0,
20+
val selEnd: Int = 0,
21+
val preedit: String? = "",
22+
) {
23+
val selStartPos: Int
24+
get() {
25+
if (length == 0) return 0
26+
return preedit?.let { String(it.toByteArray(), 0, selStart).length } ?: 0
27+
}
28+
29+
val selEndPos: Int
30+
get() {
31+
if (length == 0) return 0
32+
return preedit?.let { String(it.toByteArray(), 0, selEnd).length } ?: 0
33+
}
34+
}
35+
36+
/** Rime候選區,包含多個[候選項][CandidateListItem] */
37+
data class RimeMenu(
38+
val pageSize: Int = 0,
39+
val pageNo: Int = 0,
40+
val isLastPage: Boolean = false,
41+
val highlightedCandidateIndex: Int = 0,
42+
val numCandidates: Int = 0,
43+
val candidates: Array<CandidateListItem> = arrayOf(),
44+
) {
45+
// generated by Android Studio
46+
override fun equals(other: Any?): Boolean {
47+
if (this === other) return true
48+
if (javaClass != other?.javaClass) return false
49+
50+
other as RimeMenu
51+
52+
if (pageSize != other.pageSize) return false
53+
if (pageNo != other.pageNo) return false
54+
if (isLastPage != other.isLastPage) return false
55+
if (highlightedCandidateIndex != other.highlightedCandidateIndex) return false
56+
if (numCandidates != other.numCandidates) return false
57+
if (!candidates.contentEquals(other.candidates)) return false
58+
59+
return true
60+
}
61+
62+
// generated by Android Studio
63+
override fun hashCode(): Int {
64+
var result = pageSize
65+
result = 31 * result + pageNo
66+
result = 31 * result + isLastPage.hashCode()
67+
result = 31 * result + highlightedCandidateIndex
68+
result = 31 * result + numCandidates
69+
result = 31 * result + candidates.contentHashCode()
70+
return result
71+
}
72+
}
73+
74+
/** Rime上屏的字符串 */
75+
data class RimeCommit(
76+
val commitText: String = "",
77+
)
78+
79+
/** Rime環境,包括 [編碼區][RimeComposition] 、[候選區][RimeMenu] */
80+
data class RimeContext(
81+
val composition: RimeComposition? = null,
82+
val menu: RimeMenu? = null,
83+
val commitTextPreview: String? = "",
84+
val selectLabels: Array<String> = arrayOf(),
85+
) {
86+
val candidates: Array<CandidateListItem>
87+
get() {
88+
val numCandidates = menu?.numCandidates ?: 0
89+
Timber.d("RimeContext: getCandidate: numCandidates=$numCandidates")
90+
return if (numCandidates != 0) menu!!.candidates else arrayOf()
91+
}
92+
93+
// generated by Android Studio
94+
override fun equals(other: Any?): Boolean {
95+
if (this === other) return true
96+
if (javaClass != other?.javaClass) return false
97+
98+
other as RimeContext
99+
100+
if (composition != other.composition) return false
101+
if (menu != other.menu) return false
102+
if (commitTextPreview != other.commitTextPreview) return false
103+
if (!selectLabels.contentEquals(other.selectLabels)) return false
104+
105+
return true
106+
}
107+
108+
// generated by Android Studio
109+
override fun hashCode(): Int {
110+
var result = composition?.hashCode() ?: 0
111+
result = 31 * result + (menu?.hashCode() ?: 0)
112+
result = 31 * result + commitTextPreview.hashCode()
113+
result = 31 * result + selectLabels.contentHashCode()
114+
return result
115+
}
116+
}
117+
118+
/** Rime狀態 */
119+
data class RimeStatus(
120+
val schemaId: String = "",
121+
val schemaName: String = "",
122+
val isDisable: Boolean = true,
123+
val isComposing: Boolean = false,
124+
val isAsciiMode: Boolean = true,
125+
val isFullShape: Boolean = false,
126+
val isSimplified: Boolean = false,
127+
val isTraditional: Boolean = false,
128+
val isAsciiPunch: Boolean = true,
129+
)

app/src/main/java/com/osfans/trime/data/theme/Theme.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Theme {
7777
init {
7878
self = this
7979
ThemeManager.init()
80-
Rime.get(!sharedDataDir.exists())
80+
Rime.getInstance(!sharedDataDir.exists())
8181
init()
8282
Timber.d("Setting sound from color ...")
8383
SoundThemeManager.switchSound(colors.getString("sound"))

app/src/main/java/com/osfans/trime/ime/core/EditorInstance.kt

+8-10
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,22 @@ class EditorInstance(private val ims: InputMethodService) {
5555
* Commits the text got from Rime.
5656
*/
5757
fun commitRimeText(): Boolean {
58-
val ret = Rime.getCommit()
59-
if (ret) {
60-
commitText(Rime.getCommitText())
58+
return Rime.updateCommit().also {
59+
if (it) commitText(Rime.commitText)
60+
Timber.i("\t<TrimeInput>\tcommitRimeText()\tupdateComposing")
61+
(ims as Trime).updateComposing()
6162
}
62-
Timber.i("\t<TrimeInput>\tcommitRimeText()\tupdateComposing")
63-
(ims as Trime).updateComposing()
64-
return ret
6563
}
6664

6765
fun updateComposingText() {
6866
val ic = inputConnection ?: return
6967
val composingText = when (prefs.keyboard.inlinePreedit) {
70-
InlineModeType.INLINE_PREVIEW -> Rime.getComposingText()
71-
InlineModeType.INLINE_COMPOSITION -> Rime.getCompositionText()
68+
InlineModeType.INLINE_PREVIEW -> Rime.composingText
69+
InlineModeType.INLINE_COMPOSITION -> Rime.compositionText
7270
InlineModeType.INLINE_INPUT -> Rime.getRimeRawInput() ?: ""
7371
else -> ""
7472
}
75-
if (ic.getSelectedText(0).isNullOrEmpty() || !composingText.isNullOrEmpty()) {
73+
if (ic.getSelectedText(0).isNullOrEmpty() || composingText.isNotEmpty()) {
7674
ic.setComposingText(composingText, 1)
7775
}
7876
}
@@ -117,7 +115,7 @@ class EditorInstance(private val ims: InputMethodService) {
117115
* */
118116
fun getActiveText(type: Int): String {
119117
if (type == 2) return Rime.getRimeRawInput() ?: "" // 當前編碼
120-
var s = Rime.getComposingText() // 當前候選
118+
var s = Rime.composingText // 當前候選
121119
if (TextUtils.isEmpty(s)) {
122120
val ic = inputConnection
123121
var cs = if (ic != null) ic.getSelectedText(0) else null // 選中字

app/src/main/java/com/osfans/trime/ime/core/Trime.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public void pasteByChar() {
418418
}
419419

420420
public void invalidate() {
421-
Rime.get();
421+
Rime.getInstance(false);
422422
Theme.get().destroy();
423423
reset();
424424
textInputManager.setShouldUpdateRimeOption(true);
@@ -671,7 +671,7 @@ public void onUpdateSelection(
671671
// 移動光標時,更新候選區
672672
if ((newSelEnd < candidatesEnd) && (newSelEnd >= candidatesStart)) {
673673
final int n = newSelEnd - candidatesStart;
674-
Rime.RimeSetCaretPos(n);
674+
Rime.setCaretPos(n);
675675
updateComposing();
676676
}
677677
}

app/src/main/java/com/osfans/trime/ime/keyboard/Event.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void parseLabel() {
242242
if (!TextUtils.isEmpty(label)) return;
243243
int c = code;
244244
if (c == KeyEvent.KEYCODE_SPACE) {
245-
label = Rime.getSchemaName();
245+
label = Rime.getCurrentSchemaName();
246246
} else {
247247
if (c > 0) label = Keycode.Companion.getDisplayLabel(c, mask);
248248
}

app/src/main/java/com/osfans/trime/ime/keyboard/Key.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public boolean sendBindings(int type) {
531531
if (e != null) return true;
532532
if (events[KeyEventType.ASCII.ordinal()] != null && Rime.isAsciiMode()) return false;
533533
if (send_bindings) {
534-
if (events[KeyEventType.PAGING.ordinal()] != null && Rime.isPaging()) return true;
534+
if (events[KeyEventType.PAGING.ordinal()] != null && Rime.hasLeft()) return true;
535535
if (events[KeyEventType.HAS_MENU.ordinal()] != null && Rime.hasMenu()) return true;
536536
if (events[KeyEventType.COMPOSING.ordinal()] != null && Rime.isComposing()) return true;
537537
}
@@ -541,7 +541,7 @@ public boolean sendBindings(int type) {
541541
private Event getEvent() {
542542
if (events[KeyEventType.ASCII.ordinal()] != null && Rime.isAsciiMode())
543543
return events[KeyEventType.ASCII.ordinal()];
544-
if (events[KeyEventType.PAGING.ordinal()] != null && Rime.isPaging())
544+
if (events[KeyEventType.PAGING.ordinal()] != null && Rime.hasLeft())
545545
return events[KeyEventType.PAGING.ordinal()];
546546
if (events[KeyEventType.HAS_MENU.ordinal()] != null && Rime.hasMenu())
547547
return events[KeyEventType.HAS_MENU.ordinal()];
@@ -569,7 +569,7 @@ public Event getEvent(int i) {
569569
if (events[KeyEventType.ASCII.ordinal()] != null && Rime.isAsciiMode())
570570
return events[KeyEventType.ASCII.ordinal()];
571571
if (send_bindings) {
572-
if (events[KeyEventType.PAGING.ordinal()] != null && Rime.isPaging())
572+
if (events[KeyEventType.PAGING.ordinal()] != null && Rime.hasLeft())
573573
return events[KeyEventType.PAGING.ordinal()];
574574
if (events[KeyEventType.HAS_MENU.ordinal()] != null && Rime.hasMenu())
575575
return events[KeyEventType.HAS_MENU.ordinal()];

app/src/main/java/com/osfans/trime/ime/symbol/LiquidKeyboard.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ class LiquidKeyboard(private val context: Context) {
244244
val candidateAdapter = CandidateAdapter(theme).apply {
245245
setListener { position ->
246246
TextInputManager.getInstance().onCandidatePressed(position)
247-
if (Rime.isComposing()) {
248-
updateCandidates(Rime.getCandidatesWithoutSwitch().asList())
247+
if (Rime.isComposing) {
248+
updateCandidates(Rime.candidatesWithoutSwitch.toList())
249249
keyboardView.scrollToPosition(0)
250250
} else {
251251
service.selectLiquidKeyboard(-1)
@@ -259,7 +259,7 @@ class LiquidKeyboard(private val context: Context) {
259259
isSelected = true
260260
}
261261

262-
candidateAdapter.updateCandidates(Rime.getCandidatesWithoutSwitch().asList())
262+
candidateAdapter.updateCandidates(Rime.candidatesWithoutSwitch.toList())
263263
keyboardView.scrollToPosition(0)
264264
}
265265

app/src/main/java/com/osfans/trime/ime/text/Composition.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import androidx.appcompat.widget.AppCompatTextView;
4242
import com.osfans.trime.core.CandidateListItem;
4343
import com.osfans.trime.core.Rime;
44+
import com.osfans.trime.core.RimeComposition;
4445
import com.osfans.trime.data.theme.FontManager;
4546
import com.osfans.trime.data.theme.Theme;
4647
import com.osfans.trime.ime.core.Trime;
@@ -180,7 +181,7 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
180181
String s =
181182
getText().toString().substring(n, composition_pos[1]).replace(" ", "").replace("‸", "");
182183
n = Rime.getRimeRawInput().length() - s.length(); // 從右側定位
183-
Rime.RimeSetCaretPos(n);
184+
Rime.setCaretPos(n);
184185
Trime.getService().updateComposing();
185186
return true;
186187
}
@@ -303,9 +304,9 @@ private Object getAlign(Map<String, Object> m) {
303304
}
304305

305306
private void appendComposition(Map<String, Object> m) {
306-
final Rime.RimeComposition r = Rime.getComposition();
307+
final RimeComposition r = Rime.getComposition();
307308
assert r != null;
308-
final String s = r.getText();
309+
final String s = r.getPreedit();
309310
int start, end;
310311
String sep = CollectionUtils.obtainString(m, "start", "");
311312
if (!TextUtils.isEmpty(sep)) {
@@ -326,8 +327,8 @@ private void appendComposition(Map<String, Object> m) {
326327
final float size = CollectionUtils.obtainFloat(m, "letter_spacing", 0);
327328
if (size != 0f) ss.setSpan(new LetterSpacingSpan(size), start, end, span);
328329
}
329-
start = composition_pos[0] + r.getStart();
330-
end = composition_pos[0] + r.getEnd();
330+
start = composition_pos[0] + r.getSelStartPos();
331+
end = composition_pos[0] + r.getSelEndPos();
331332
ss.setSpan(new ForegroundColorSpan(hilited_text_color), start, end, span);
332333
ss.setSpan(new BackgroundColorSpan(hilited_back_color), start, end, span);
333334
sep = CollectionUtils.obtainString(m, "end", "");
@@ -475,7 +476,7 @@ private void appendCandidates(Map<String, Object> m, int length, int end_num) {
475476
private void appendButton(@NonNull Map<String, Object> m) {
476477
if (m.containsKey("when")) {
477478
final String when = CollectionUtils.obtainString(m, "when", "");
478-
if (when.contentEquals("paging") && !Rime.isPaging()) return;
479+
if (when.contentEquals("paging") && !Rime.hasLeft()) return;
479480
if (when.contentEquals("has_menu") && !Rime.hasMenu()) return;
480481
}
481482
final String label;
@@ -553,9 +554,9 @@ public int setWindow(int stringMinLength, int candidateMinCheck) {
553554
+ stacks[2].toString()
554555
+ ", [3]"
555556
+ stacks[3].toString());
556-
Rime.RimeComposition r = Rime.getComposition();
557+
RimeComposition r = Rime.getComposition();
557558
if (r == null) return 0;
558-
String s = r.getText();
559+
String s = r.getPreedit();
559560
if (TextUtils.isEmpty(s)) return 0;
560561
setSingleLine(true); // 設置單行
561562
ss = new SpannableStringBuilder();

app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class TextInputManager private constructor() :
107107
intentReceiver = IntentReceiver().also {
108108
it.registerReceiver(trime)
109109
}
110-
rimeNotiHandlerJob = Rime.get().rimeNotiFlow
110+
rimeNotiHandlerJob = Rime.getInstance().notificationFlow
111111
.onEach(::handleRimeNotification)
112112
.launchIn(trime.lifecycleScope)
113113

@@ -232,15 +232,15 @@ class TextInputManager private constructor() :
232232
// Select a keyboard based on the input type of the editing field.
233233
switchKeyboard(keyboardType)
234234
}
235-
Rime.get()
235+
Rime.getInstance()
236236

237237
// style/reset_ascii_mode指定了弹出键盘时是否重置ASCII状态。
238238
// 键盘的reset_ascii_mode指定了重置时是否重置到keyboard的ascii_mode描述的状态。
239239
if (shouldResetAsciiMode && KeyboardSwitcher.currentKeyboard.isResetAsciiMode) {
240240
tempAsciiMode = KeyboardSwitcher.currentKeyboard.asciiMode
241241
}
242242
tempAsciiMode?.let { Rime.setOption("ascii_mode", it) }
243-
isComposable = isComposable && !Rime.isEmpty()
243+
isComposable = isComposable && !Rime.isEmpty
244244
if (!trime.onEvaluateInputViewShown()) {
245245
// Show candidate view when using physical keyboard
246246
trime.setCandidatesViewShown(isComposable)
@@ -252,7 +252,7 @@ class TextInputManager private constructor() :
252252
Rime.initSchema()
253253
trime.initKeyboard()
254254
} else if (event is RimeEvent.OptionEvent) {
255-
Rime.getContexts() // 切換中英文、簡繁體時更新候選
255+
Rime.updateContext() // 切換中英文、簡繁體時更新候選
256256
val value = event.value
257257
when (val option = event.option) {
258258
"ascii_mode" -> {
@@ -475,7 +475,7 @@ class TextInputManager private constructor() :
475475

476476
override fun onText(text: CharSequence?) {
477477
text ?: return
478-
if (!text.startsWithAsciiChar() && Rime.isComposing()) {
478+
if (!text.startsWithAsciiChar() && Rime.isComposing) {
479479
Rime.commitComposition()
480480
activeEditorInstance.commitRimeText()
481481
}
@@ -487,8 +487,8 @@ class TextInputManager private constructor() :
487487
when {
488488
escapeTagMatcher.matches() -> {
489489
target = escapeTagMatcher.group(1) ?: ""
490-
Rime.onText(target)
491-
if (!activeEditorInstance.commitRimeText() && !Rime.isComposing()) {
490+
Rime.simulateKeySequence(target)
491+
if (!activeEditorInstance.commitRimeText() && !Rime.isComposing) {
492492
activeEditorInstance.commitText(target)
493493
}
494494
trime.updateComposing()
@@ -512,7 +512,7 @@ class TextInputManager private constructor() :
512512
*/
513513
override fun onCandidatePressed(index: Int) {
514514
onPress(0)
515-
if (!Rime.isComposing()) {
515+
if (!Rime.isComposing) {
516516
if (index >= 0) {
517517
SchemaManager.toggleSwitchOption(index)
518518
trime.updateComposing()
@@ -580,7 +580,7 @@ class TextInputManager private constructor() :
580580
) { dialog: DialogInterface, id: Int ->
581581
dialog.dismiss()
582582
trime.lifecycleScope.launch {
583-
Rime.selectSchema(schemaIdList[id])
583+
Rime.selectSchema(schemaIdList[id] ?: return@launch)
584584
}
585585
shouldUpdateRimeOption = true
586586
}

0 commit comments

Comments
 (0)