Skip to content

Commit

Permalink
feat: make new PreeditUi support moving cursor on touch
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Nov 21, 2024
1 parent 9abbf9a commit b3406b5
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 12 deletions.
6 changes: 6 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 @@ -126,6 +126,12 @@ class Rime :
deleteRimeCandidateOnCurrentPage(idx).also { if (it) ipcResponseCallback() }
}

override suspend fun moveCursorPos(position: Int) =
withRimeContext {
setRimeCaretPos(position)
ipcResponseCallback()
}

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

override suspend fun enabledSchemata(): Array<SchemaItem> = withRimeContext { getSelectedRimeSchemaList() }
Expand Down
2 changes: 2 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 @@ -41,6 +41,8 @@ interface RimeApi {

suspend fun deletedPagedCandidate(idx: Int): Boolean

suspend fun moveCursorPos(position: Int)

suspend fun availableSchemata(): Array<SchemaItem>

suspend fun enabledSchemata(): Array<SchemaItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ class CandidatesView(
private var menu = RimeProto.Context.Menu()
private var inputComposition = RimeProto.Context.Composition()

private val preeditUi = PreeditUi(ctx, theme)
private val preeditUi =
PreeditUi(ctx, theme).apply {
preedit.setOnCursorMoveListener { position ->
rime.launchOnReady { it.moveCursorPos(position) }
}
}

private val candidatesUi =
PagedCandidatesUi(ctx, theme).apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* SPDX-FileCopyrightText: 2015 - 2024 Rime community
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package com.osfans.trime.ime.composition

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.TextView

@SuppressLint("AppCompatCustomView")
class PreeditTextView
@JvmOverloads
constructor(
context: Context,
attributeSet: AttributeSet? = null,
) : TextView(context, attributeSet) {
private var onCursorMove: ((Int) -> Unit)? = null

fun setOnCursorMoveListener(listener: ((Int) -> Unit)) {
onCursorMove = listener
}

private var touchX: Int = 0
private var newSel: CharSequence = ""

@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
touchX = getOffsetForPosition(event.x, event.y)
newSel = text.subSequence(0, touchX).replace("\\s".toRegex(), "")
return true
}
MotionEvent.ACTION_UP -> {
onCursorMove?.invoke(newSel.length)
touchX = 0
newSel = ""
return true
}
MotionEvent.ACTION_CANCEL -> {
touchX = 0
newSel = ""
return true
}
}
return super.onTouchEvent(event)
}
}
27 changes: 16 additions & 11 deletions app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import android.content.Context
import android.text.Spanned
import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import android.view.MotionEvent
import android.view.View
import android.widget.LinearLayout
import androidx.core.text.buildSpannedString
import com.osfans.trime.core.RimeProto
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.FontManager
import com.osfans.trime.data.theme.Theme
import splitties.dimensions.dp
import splitties.views.dsl.core.Ui
import splitties.views.dsl.core.add
import splitties.views.dsl.core.horizontalLayout
import splitties.views.dsl.core.lParams
import splitties.views.dsl.core.textView
import splitties.views.horizontalPadding
import splitties.views.dsl.core.view
import splitties.views.setPaddingDp

open class PreeditUi(
override val ctx: Context,
final override val ctx: Context,
private val theme: Theme,
) : Ui {
private val textColor = ColorManager.getColor("text_color")
private val highlightTextColor = ColorManager.getColor("hilited_text_color")
private val highlightBackColor = ColorManager.getColor("hilited_back_color")

private val preedit =
textView {
horizontalPadding = dp(8)
val preedit =
view(::PreeditTextView) {
setPaddingDp(3, 1, 3, 1)
textColor?.let { setTextColor(it) }
textSize = theme.generalStyle.textSize.toFloat()
typeface = FontManager.getTypeface("text_font")
Expand All @@ -42,9 +42,14 @@ open class PreeditUi(
var visible = false
private set

override val root: View =
horizontalLayout {
add(preedit, lParams())
override val root =
object : LinearLayout(ctx) {
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean = false

init {
orientation = HORIZONTAL
add(preedit, lParams())
}
}

private fun updatePreeditView(
Expand Down

0 comments on commit b3406b5

Please sign in to comment.