Skip to content

Commit 2707bf1

Browse files
committed
refactor: simplify rime proto conversion
1 parent 1044be2 commit 2707bf1

File tree

22 files changed

+364
-395
lines changed

22 files changed

+364
-395
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ class Rime :
4040
override var schemaCached = RimeSchema(".default")
4141
private set
4242

43-
override var statusCached = RimeProto.Status()
43+
override var statusCached = StatusProto()
4444
private set
4545

46-
override var compositionCached = RimeProto.Context.Composition()
46+
override var compositionCached = CompositionProto()
4747
private set
4848

4949
override var hasMenu: Boolean = false
@@ -119,7 +119,7 @@ class Rime :
119119
emitResponse { commit }
120120
true
121121
} else {
122-
emitResponse { RimeProto.Commit(sequence) }
122+
emitResponse { CommitProto(sequence) }
123123
false
124124
}
125125
} else {
@@ -228,7 +228,7 @@ class Rime :
228228
}
229229

230230
private fun emitResponse(
231-
commit: (() -> RimeProto.Commit) = { getRimeCommit() },
231+
commit: (() -> CommitProto) = { getRimeCommit() },
232232
) {
233233
handleRimeMessage(4, arrayOf(commit.invoke()))
234234
val context = getRimeContext()
@@ -245,7 +245,7 @@ class Rime :
245245
handleRimeMessage(8, arrayOf(getRimeStatus()))
246246
}
247247

248-
private fun handlePreedit(composition: RimeProto.Context.Composition) {
248+
private fun handlePreedit(composition: CompositionProto) {
249249
val mode = if (getRimeOption("no_inline_preedit")) {
250250
InlinePreeditMode.DISABLE
251251
} else {
@@ -257,7 +257,7 @@ class Rime :
257257
InlinePreeditMode.COMMIT_TEXT_PREVIEW -> composition.commitTextPreview ?: ""
258258
}
259259
val composition = if (mode == InlinePreeditMode.COMPOSING_TEXT) {
260-
RimeProto.Context.Composition()
260+
CompositionProto()
261261
} else {
262262
composition
263263
}
@@ -305,7 +305,7 @@ class Rime :
305305
}
306306
}
307307

308-
private fun updateSchemaCached(status: RimeProto.Status) {
308+
private fun updateSchemaCached(status: StatusProto) {
309309
val (schemaId, schemaName) = status
310310
// Engine response update won't send SchemaMessage, but usually update RimeStatus
311311
if (schemaId != schemaCached.schemaId) {
@@ -326,7 +326,7 @@ class Rime :
326326

327327
lastAsciiTipsText = tipsText
328328

329-
val tips = RimeProto.Context.Composition(tipsText)
329+
val tips = CompositionProto(tipsText)
330330
messageFlow_.tryEmit(RimeMessage.CompositionMessage(tips))
331331
compositionCached = tips
332332
asciiSwitchTipsJob?.cancel()
@@ -419,13 +419,13 @@ class Rime :
419419

420420
// output
421421
@JvmStatic
422-
external fun getRimeCommit(): RimeProto.Commit
422+
external fun getRimeCommit(): CommitProto
423423

424424
@JvmStatic
425-
external fun getRimeContext(): RimeProto.Context
425+
external fun getRimeContext(): ContextProto
426426

427427
@JvmStatic
428-
external fun getRimeStatus(): RimeProto.Status
428+
external fun getRimeStatus(): StatusProto
429429

430430
// runtime options
431431
@JvmStatic

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ interface RimeApi {
1414

1515
val schemaCached: RimeSchema
1616

17-
val statusCached: RimeProto.Status
17+
val statusCached: StatusProto
1818

19-
val compositionCached: RimeProto.Context.Composition
19+
val compositionCached: CompositionProto
2020

2121
val hasMenu: Boolean
2222

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ sealed class RimeMessage<T>(
6868
}
6969

7070
data class CommitTextMessage(
71-
override val data: RimeProto.Commit,
72-
) : RimeMessage<RimeProto.Commit>(data) {
71+
override val data: CommitProto,
72+
) : RimeMessage<CommitProto>(data) {
7373
override val messageType = MessageType.Commit
7474
}
7575

@@ -80,20 +80,20 @@ sealed class RimeMessage<T>(
8080
}
8181

8282
data class CompositionMessage(
83-
override val data: RimeProto.Context.Composition,
84-
) : RimeMessage<RimeProto.Context.Composition>(data) {
83+
override val data: CompositionProto,
84+
) : RimeMessage<CompositionProto>(data) {
8585
override val messageType = MessageType.Composition
8686
}
8787

8888
data class CandidateMenuMessage(
89-
override val data: RimeProto.Context.Menu,
90-
) : RimeMessage<RimeProto.Context.Menu>(data) {
89+
override val data: MenuProto,
90+
) : RimeMessage<MenuProto>(data) {
9191
override val messageType = MessageType.Menu
9292
}
9393

9494
data class StatusMessage(
95-
override val data: RimeProto.Status,
96-
) : RimeMessage<RimeProto.Status>(data) {
95+
override val data: StatusProto,
96+
) : RimeMessage<StatusProto>(data) {
9797
override val messageType = MessageType.Status
9898
}
9999

@@ -182,15 +182,15 @@ sealed class RimeMessage<T>(
182182
DeployMessage.State.valueOf((params[0] as String).replaceFirstChar { it.titlecase() }),
183183
)
184184
MessageType.Commit ->
185-
CommitTextMessage(params[0] as RimeProto.Commit)
185+
CommitTextMessage(params[0] as CommitProto)
186186
MessageType.InlinePreedit ->
187187
InlinePreeditMessage(params[0] as String)
188188
MessageType.Composition ->
189-
CompositionMessage(params[0] as RimeProto.Context.Composition)
189+
CompositionMessage(params[0] as CompositionProto)
190190
MessageType.Menu ->
191-
CandidateMenuMessage(params[0] as RimeProto.Context.Menu)
191+
CandidateMenuMessage(params[0] as MenuProto)
192192
MessageType.Status ->
193-
StatusMessage(params[0] as RimeProto.Status)
193+
StatusMessage(params[0] as StatusProto)
194194
MessageType.Candidate ->
195195
CandidateListMessage(
196196
CandidateListMessage.Data(
Lines changed: 82 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,94 @@
1-
// SPDX-FileCopyrightText: 2024 Rime community
2-
//
3-
// SPDX-License-Identifier: GPL-3.0-or-later
1+
/*
2+
* SPDX-FileCopyrightText: 2015 - 2026 Rime community
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
45

56
package com.osfans.trime.core
67

7-
class RimeProto {
8-
data class Commit(
9-
val text: String?,
10-
)
11-
12-
data class Candidate(
13-
val text: String,
14-
val comment: String?,
15-
val label: String,
16-
)
8+
data class CommitProto(
9+
val text: String?,
10+
)
1711

18-
data class Context(
19-
val composition: Composition = Composition(),
20-
val menu: Menu = Menu(),
21-
val input: String = "",
22-
val caretPos: Int = 0,
23-
) {
24-
data class Composition(
25-
/**
26-
* Actually we can directly use [String.length] on [preedit], but
27-
* we add it here for the sake of completeness as it is semantically correct
28-
*/
29-
val length: Int = 0,
30-
val cursorPos: Int = 0,
31-
val selStart: Int = 0,
32-
val selEnd: Int = 0,
33-
val preedit: String? = null,
34-
val commitTextPreview: String? = null,
35-
) {
36-
constructor(text: String) : this(
37-
text.length,
38-
text.length,
39-
text.length,
40-
text.length,
41-
text,
42-
)
43-
}
12+
data class CandidateProto(
13+
val text: String,
14+
val comment: String?,
15+
val label: String,
16+
)
4417

45-
data class Menu(
46-
val pageSize: Int = 0,
47-
val pageNumber: Int = 0,
48-
val isLastPage: Boolean = false,
49-
val highlightedCandidateIndex: Int = 0,
50-
val candidates: Array<Candidate> = arrayOf(),
51-
val selectKeys: String? = null,
52-
val selectLabels: Array<String> = arrayOf(),
53-
) {
54-
override fun equals(other: Any?): Boolean {
55-
if (this === other) return true
56-
if (javaClass != other?.javaClass) return false
18+
data class CompositionProto(
19+
/**
20+
* Actually we can directly use [String.length] on [preedit], but
21+
* we add it here for the sake of completeness as it is semantically correct
22+
*/
23+
val length: Int = 0,
24+
val cursorPos: Int = 0,
25+
val selStart: Int = 0,
26+
val selEnd: Int = 0,
27+
val preedit: String? = null,
28+
val commitTextPreview: String? = null,
29+
) {
30+
internal constructor(text: String) : this(
31+
text.length,
32+
text.length,
33+
text.length,
34+
text.length,
35+
text,
36+
)
37+
}
5738

58-
other as Menu
39+
data class MenuProto(
40+
val pageSize: Int = 0,
41+
val pageNumber: Int = 0,
42+
val isLastPage: Boolean = false,
43+
val highlightedCandidateIndex: Int = 0,
44+
val candidates: Array<CandidateProto> = arrayOf(),
45+
val selectKeys: String? = null,
46+
val selectLabels: Array<String> = arrayOf(),
47+
) {
48+
override fun equals(other: Any?): Boolean {
49+
if (this === other) return true
50+
if (javaClass != other?.javaClass) return false
5951

60-
if (pageSize != other.pageSize) return false
61-
if (pageNumber != other.pageNumber) return false
62-
if (isLastPage != other.isLastPage) return false
63-
if (highlightedCandidateIndex != other.highlightedCandidateIndex) return false
64-
if (!candidates.contentEquals(other.candidates)) return false
65-
if (selectKeys != other.selectKeys) return false
66-
if (!selectLabels.contentEquals(other.selectLabels)) return false
52+
other as MenuProto
6753

68-
return true
69-
}
54+
if (pageSize != other.pageSize) return false
55+
if (pageNumber != other.pageNumber) return false
56+
if (isLastPage != other.isLastPage) return false
57+
if (highlightedCandidateIndex != other.highlightedCandidateIndex) return false
58+
if (!candidates.contentEquals(other.candidates)) return false
59+
if (selectKeys != other.selectKeys) return false
60+
if (!selectLabels.contentEquals(other.selectLabels)) return false
7061

71-
override fun hashCode(): Int {
72-
var result = pageSize
73-
result = 31 * result + pageNumber
74-
result = 31 * result + isLastPage.hashCode()
75-
result = 31 * result + highlightedCandidateIndex
76-
result = 31 * result + candidates.contentHashCode()
77-
result = 31 * result + (selectKeys?.hashCode() ?: 0)
78-
result = 31 * result + selectLabels.contentHashCode()
79-
return result
80-
}
81-
}
62+
return true
8263
}
8364

84-
data class Status(
85-
val schemaId: String = "",
86-
val schemaName: String = "",
87-
val isDisabled: Boolean = true,
88-
val isComposing: Boolean = false,
89-
val isAsciiMode: Boolean = true,
90-
val isFullShape: Boolean = false,
91-
val isSimplified: Boolean = false,
92-
val isTraditional: Boolean = false,
93-
val isAsciiPunct: Boolean = true,
94-
)
65+
override fun hashCode(): Int {
66+
var result = pageSize
67+
result = 31 * result + pageNumber
68+
result = 31 * result + isLastPage.hashCode()
69+
result = 31 * result + highlightedCandidateIndex
70+
result = 31 * result + candidates.contentHashCode()
71+
result = 31 * result + (selectKeys?.hashCode() ?: 0)
72+
result = 31 * result + selectLabels.contentHashCode()
73+
return result
74+
}
9575
}
76+
77+
data class ContextProto(
78+
val composition: CompositionProto = CompositionProto(),
79+
val menu: MenuProto = MenuProto(),
80+
val input: String = "",
81+
val caretPos: Int = 0,
82+
)
83+
84+
data class StatusProto(
85+
val schemaId: String = "",
86+
val schemaName: String = "",
87+
val isDisabled: Boolean = true,
88+
val isComposing: Boolean = false,
89+
val isAsciiMode: Boolean = true,
90+
val isFullShape: Boolean = false,
91+
val isSimplified: Boolean = false,
92+
val isTraditional: Boolean = false,
93+
val isAsciiPunct: Boolean = true,
94+
)

app/src/main/java/com/osfans/trime/ime/broadcast/InputBroadcastReceiver.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
package com.osfans.trime.ime.broadcast
77

88
import android.view.inputmethod.EditorInfo
9+
import com.osfans.trime.core.CompositionProto
10+
import com.osfans.trime.core.MenuProto
911
import com.osfans.trime.core.RimeMessage
10-
import com.osfans.trime.core.RimeProto
1112
import com.osfans.trime.core.SchemaItem
13+
import com.osfans.trime.core.StatusProto
1214
import com.osfans.trime.ime.window.BoardWindow
1315

1416
interface InputBroadcastReceiver {
@@ -25,11 +27,11 @@ interface InputBroadcastReceiver {
2527

2628
fun onCandidateListUpdate(data: RimeMessage.CandidateListMessage.Data) {}
2729

28-
fun onCompositionUpdate(data: RimeProto.Context.Composition) {}
30+
fun onCompositionUpdate(data: CompositionProto) {}
2931

30-
fun onCandidateMenuUpdate(data: RimeProto.Context.Menu) {}
32+
fun onCandidateMenuUpdate(data: MenuProto) {}
3133

32-
fun onInputStatusUpdate(value: RimeProto.Status) {}
34+
fun onInputStatusUpdate(value: StatusProto) {}
3335

3436
fun onWindowAttached(window: BoardWindow) {}
3537

app/src/main/java/com/osfans/trime/ime/broadcast/InputBroadcaster.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
package com.osfans.trime.ime.broadcast
77

88
import android.view.inputmethod.EditorInfo
9+
import com.osfans.trime.core.CompositionProto
10+
import com.osfans.trime.core.MenuProto
911
import com.osfans.trime.core.RimeMessage
10-
import com.osfans.trime.core.RimeProto
1112
import com.osfans.trime.core.SchemaItem
13+
import com.osfans.trime.core.StatusProto
1214
import com.osfans.trime.ime.window.BoardWindow
1315
import java.util.concurrent.ConcurrentLinkedQueue
1416

@@ -56,15 +58,15 @@ class InputBroadcaster : InputBroadcastReceiver {
5658
receivers.forEach { it.onCandidateListUpdate(data) }
5759
}
5860

59-
override fun onCompositionUpdate(data: RimeProto.Context.Composition) {
61+
override fun onCompositionUpdate(data: CompositionProto) {
6062
receivers.forEach { it.onCompositionUpdate(data) }
6163
}
6264

63-
override fun onCandidateMenuUpdate(data: RimeProto.Context.Menu) {
65+
override fun onCandidateMenuUpdate(data: MenuProto) {
6466
receivers.forEach { it.onCandidateMenuUpdate(data) }
6567
}
6668

67-
override fun onInputStatusUpdate(value: RimeProto.Status) {
69+
override fun onInputStatusUpdate(value: StatusProto) {
6870
receivers.forEach { it.onInputStatusUpdate(value) }
6971
}
7072

0 commit comments

Comments
 (0)