Skip to content

Commit

Permalink
refactor: migrate rime out data class into RimeProto
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Aug 30, 2024
1 parent e7e9543 commit 5e1e9bc
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 230 deletions.
26 changes: 13 additions & 13 deletions app/src/main/java/com/osfans/trime/core/Rime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ class Rime : RimeApi, RimeLifecycleOwner {
}

companion object {
var inputContext: RimeContext? = null
private var mStatus: RimeStatus? = null
var inputContext: RimeProto.Context? = null
private var mStatus: RimeProto.Status? = null
private val notificationFlow_ =
MutableSharedFlow<RimeNotification<*>>(
extraBufferCapacity = 15,
Expand All @@ -180,14 +180,14 @@ class Rime : RimeApi, RimeLifecycleOwner {
fun updateStatus() {
SchemaManager.updateSwitchOptions()
measureTimeMillis {
mStatus = getRimeStatus() ?: RimeStatus()
mStatus = getRimeStatus()
}.also { Timber.d("Took $it ms to get status") }
}

fun updateContext() {
Timber.d("Update Rime context ...")
measureTimeMillis {
inputContext = getRimeContext() ?: RimeContext()
inputContext = getRimeContext()
}.also { Timber.d("Took $it ms to get context") }
updateStatus()
}
Expand Down Expand Up @@ -229,12 +229,12 @@ class Rime : RimeApi, RimeLifecycleOwner {

@JvmStatic
fun hasMenu(): Boolean {
return isComposing && inputContext?.menu?.numCandidates != 0
return !inputContext?.menu?.candidates.isNullOrEmpty()
}

@JvmStatic
fun hasLeft(): Boolean {
return hasMenu() && inputContext?.menu?.pageNo != 0
return hasMenu() && inputContext?.menu?.pageNumber != 0
}

@JvmStatic
Expand All @@ -248,7 +248,7 @@ class Rime : RimeApi, RimeLifecycleOwner {
}

@JvmStatic
val composition: RimeComposition?
val composition: RimeProto.Context.Composition?
get() = inputContext?.composition

@JvmStatic
Expand Down Expand Up @@ -291,12 +291,12 @@ class Rime : RimeApi, RimeLifecycleOwner {
}
}

val candidatesWithoutSwitch: Array<CandidateListItem>
get() = if (isComposing) inputContext?.candidates ?: arrayOf() else arrayOf()
val candidatesWithoutSwitch: Array<RimeProto.Candidate>
get() = inputContext?.menu?.candidates ?: arrayOf()

@JvmStatic
val candHighlightIndex: Int
get() = if (isComposing) inputContext?.menu?.highlightedCandidateIndex ?: -1 else -1
get() = inputContext?.menu?.highlightedCandidateIndex ?: -1

fun selectCandidate(index: Int): Boolean {
return selectRimeCandidateOnCurrentPage(index).also {
Expand Down Expand Up @@ -373,13 +373,13 @@ class Rime : RimeApi, RimeLifecycleOwner {

// output
@JvmStatic
external fun getRimeCommit(): RimeCommit?
external fun getRimeCommit(): RimeProto.Commit?

@JvmStatic
external fun getRimeContext(): RimeContext?
external fun getRimeContext(): RimeProto.Context?

@JvmStatic
external fun getRimeStatus(): RimeStatus?
external fun getRimeStatus(): RimeProto.Status?

// runtime options
@JvmStatic
Expand Down
94 changes: 94 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeProto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.osfans.trime.core

class RimeProto {
data class Commit(val text: String?)

data class Candidate(val text: String, val comment: String?)

data class Context(
val composition: Composition,
val menu: Menu,
val commitTextPreview: String?,
val selectLabels: Array<String>,
) {
data class Composition(
val length: Int = 0,
val cursorPos: Int = 0,
private val _selStart: Int = 0,
private val _selEnd: Int = 0,
val preedit: String? = null,
) {
val selStart: Int =
if (preedit.isNullOrEmpty()) 0 else String(preedit.toByteArray(), 0, _selStart).length

val selEnd: Int =
if (preedit.isNullOrEmpty()) 0 else String(preedit.toByteArray(), 0, _selEnd).length
}

data class Menu(
val pageSize: Int = 0,
val pageNumber: Int = 0,
val isLastPage: Boolean = false,
val highlightedCandidateIndex: Int = 0,
val candidates: Array<Candidate> = arrayOf(),
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Menu

if (pageSize != other.pageSize) return false
if (pageNumber != other.pageNumber) return false
if (isLastPage != other.isLastPage) return false
if (highlightedCandidateIndex != other.highlightedCandidateIndex) return false
if (!candidates.contentEquals(other.candidates)) return false

return true
}

override fun hashCode(): Int {
var result = pageSize
result = 31 * result + pageNumber
result = 31 * result + isLastPage.hashCode()
result = 31 * result + highlightedCandidateIndex
result = 31 * result + candidates.contentHashCode()
return result
}
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Context

if (composition != other.composition) return false
if (menu != other.menu) return false
if (commitTextPreview != other.commitTextPreview) return false
if (!selectLabels.contentEquals(other.selectLabels)) return false

return true
}

override fun hashCode(): Int {
var result = composition.hashCode()
result = 31 * result + menu.hashCode()
result = 31 * result + (commitTextPreview?.hashCode() ?: 0)
result = 31 * result + selectLabels.contentHashCode()
return result
}
}

data class Status(
val schemaId: String,
val schemaName: String,
val isDisabled: Boolean,
val isComposing: Boolean,
val isAsciiMode: Boolean,
val isFullShape: Boolean,
val isSimplified: Boolean,
val isTraditional: Boolean,
val isAsciiPunch: Boolean,
)
}
112 changes: 0 additions & 112 deletions app/src/main/java/com/osfans/trime/core/Structs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package com.osfans.trime.core

import timber.log.Timber

data class SchemaItem(
val id: String,
val name: String = "",
Expand All @@ -15,113 +13,3 @@ data class CandidateListItem(
val comment: String,
val text: String,
)

/** Rime編碼區 */
data class RimeComposition(
val length: Int = 0,
val cursorPos: Int = 0,
private val _selStart: Int = 0,
private val _selEnd: Int = 0,
val preedit: String? = "",
) {
val selStart: Int =
if (preedit.isNullOrEmpty()) 0 else String(preedit.toByteArray(), 0, _selStart).length

val selEnd: Int =
if (preedit.isNullOrEmpty()) 0 else String(preedit.toByteArray(), 0, _selEnd).length
}

/** Rime候選區,包含多個[候選項][CandidateListItem] */
data class RimeMenu(
val pageSize: Int = 0,
val pageNo: Int = 0,
val isLastPage: Boolean = false,
val highlightedCandidateIndex: Int = 0,
val numCandidates: Int = 0,
val candidates: Array<CandidateListItem> = arrayOf(),
) {
// generated by Android Studio
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as RimeMenu

if (pageSize != other.pageSize) return false
if (pageNo != other.pageNo) return false
if (isLastPage != other.isLastPage) return false
if (highlightedCandidateIndex != other.highlightedCandidateIndex) return false
if (numCandidates != other.numCandidates) return false
if (!candidates.contentEquals(other.candidates)) return false

return true
}

// generated by Android Studio
override fun hashCode(): Int {
var result = pageSize
result = 31 * result + pageNo
result = 31 * result + isLastPage.hashCode()
result = 31 * result + highlightedCandidateIndex
result = 31 * result + numCandidates
result = 31 * result + candidates.contentHashCode()
return result
}
}

/** Rime上屏的字符串 */
data class RimeCommit(
val commitText: String = "",
)

/** Rime環境,包括 [編碼區][RimeComposition] 、[候選區][RimeMenu] */
data class RimeContext(
val composition: RimeComposition? = null,
val menu: RimeMenu? = null,
val commitTextPreview: String? = "",
val selectLabels: Array<String> = arrayOf(),
) {
val candidates: Array<CandidateListItem>
get() {
val numCandidates = menu?.numCandidates ?: 0
Timber.d("RimeContext: getCandidate: numCandidates=$numCandidates")
return if (numCandidates != 0) menu!!.candidates else arrayOf()
}

// generated by Android Studio
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as RimeContext

if (composition != other.composition) return false
if (menu != other.menu) return false
if (commitTextPreview != other.commitTextPreview) return false
if (!selectLabels.contentEquals(other.selectLabels)) return false

return true
}

// generated by Android Studio
override fun hashCode(): Int {
var result = composition?.hashCode() ?: 0
result = 31 * result + (menu?.hashCode() ?: 0)
result = 31 * result + commitTextPreview.hashCode()
result = 31 * result + selectLabels.contentHashCode()
return result
}
}

/** Rime狀態 */
data class RimeStatus(
val schemaId: String = "",
val schemaName: String = "",
val isDisable: Boolean = true,
val isComposing: Boolean = false,
val isAsciiMode: Boolean = true,
val isFullShape: Boolean = false,
val isSimplified: Boolean = false,
val isTraditional: Boolean = false,
val isAsciiPunch: Boolean = true,
)
20 changes: 9 additions & 11 deletions app/src/main/java/com/osfans/trime/ime/composition/Composition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import android.view.ViewConfiguration
import android.widget.TextView
import androidx.core.text.buildSpannedString
import androidx.core.text.inSpans
import com.osfans.trime.core.CandidateListItem
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeComposition
import com.osfans.trime.core.RimeContext
import com.osfans.trime.core.RimeProto
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.EventManager
import com.osfans.trime.data.theme.FontManager
Expand Down Expand Up @@ -264,7 +262,7 @@ class Composition(context: Context, attrs: AttributeSet?) : TextView(context, at

private fun SpannableStringBuilder.buildSpannedComposition(
m: CompositionComponent,
composition: RimeComposition,
composition: RimeProto.Context.Composition,
) {
val alignmentSpan = alignmentSpan(m.align)
val preeditSpans =
Expand All @@ -290,7 +288,7 @@ class Composition(context: Context, attrs: AttributeSet?) : TextView(context, at
/** 生成悬浮窗内的文本 */
private fun SpannableStringBuilder.buildSpannedCandidates(
m: CompositionComponent,
candidates: Array<CandidateListItem>,
candidates: Array<RimeProto.Candidate>,
selectLabels: Array<String>,
offset: Int,
) {
Expand All @@ -299,7 +297,6 @@ class Composition(context: Context, attrs: AttributeSet?) : TextView(context, at
val alignmentSpan = alignmentSpan(m.align)
for ((i, candidate) in candidates.withIndex()) {
val text = String.format(m.candidate, candidate.text)
val comment = String.format(m.comment, candidate.comment)
val label = String.format(m.label, selectLabels[i])
if (i >= maxCount) break
if (!allPhrases && i >= offset) break
Expand Down Expand Up @@ -340,7 +337,8 @@ class Composition(context: Context, attrs: AttributeSet?) : TextView(context, at
inSpans(alignmentSpan, candidateSpan, candidateTextSizeSpan) { append(text) }
currentLineLength += text.length

if (showComment) {
if (showComment && !candidate.comment.isNullOrEmpty()) {
val comment = String.format(m.comment, candidate.comment)
val commentSpan =
CandidateSpan(
i,
Expand Down Expand Up @@ -389,7 +387,7 @@ class Composition(context: Context, attrs: AttributeSet?) : TextView(context, at
/**
* 计算悬浮窗显示候选词后,候选栏从第几个候选词开始展示 注意当 all_phrases==true 时,悬浮窗显示的候选词数量和候选栏从第几个开始,是不一致的
*/
private fun calculateOffset(candidates: Array<CandidateListItem>): Int {
private fun calculateOffset(candidates: Array<RimeProto.Candidate>): Int {
if (candidates.isEmpty()) return 0
var j = (minOf(minCheckCount, candidates.size, maxCount) - 1).coerceAtLeast(0)
while (j > 0) {
Expand All @@ -412,10 +410,10 @@ class Composition(context: Context, attrs: AttributeSet?) : TextView(context, at
*
* @return 悬浮窗显示的候选词数量
*/
fun update(inputContext: RimeContext): Int {
fun update(inputContext: RimeProto.Context): Int {
if (visibility != VISIBLE) return 0
inputContext.composition?.preedit?.takeIf { it.isNotBlank() } ?: return 0
val candidates = inputContext.candidates
inputContext.composition.preedit.takeIf { !it.isNullOrEmpty() } ?: return 0
val candidates = inputContext.menu.candidates
val startNum = calculateOffset(candidates)
val content =
buildSpannedString {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
*/
fun commitRimeText(): Boolean {
val commit = Rime.getRimeCommit()
commit?.let { commitCharSequence(it.commitText) }
commit?.text?.let { commitCharSequence(it) }
Timber.d("commitRimeText: updateComposing")
updateComposing()
return commit != null
Expand Down
Loading

0 comments on commit 5e1e9bc

Please sign in to comment.