Skip to content

Commit 11f9a79

Browse files
committed
refactor: improve touch event receiving of stock PreeditUi
1 parent b5ee296 commit 11f9a79

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

app/src/main/java/com/osfans/trime/ime/composition/CandidatesView.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.osfans.trime.ime.composition
77

88
import android.annotation.SuppressLint
9+
import android.view.View
910
import android.view.ViewGroup
1011
import androidx.constraintlayout.widget.ConstraintLayout
1112
import com.osfans.trime.core.RimeProto
@@ -77,6 +78,7 @@ class CandidatesView(
7778
private fun updateUi() {
7879
if (evaluateVisibility()) {
7980
preeditUi.update(inputComposition)
81+
preeditUi.root.visibility = if (preeditUi.visible) View.VISIBLE else View.INVISIBLE
8082
// if CandidatesView can be shown, rime engine is ready most of the time,
8183
// so it should be safety to get option immediately
8284
val isHorizontalLayout = rime.run { getRuntimeOption("_horizontal") }

app/src/main/java/com/osfans/trime/ime/composition/PreeditModule.kt

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@
55

66
package com.osfans.trime.ime.composition
77

8+
import android.annotation.SuppressLint
89
import android.content.Context
910
import android.graphics.Outline
1011
import android.graphics.Rect
1112
import android.view.Gravity
13+
import android.view.MotionEvent
1214
import android.view.View
1315
import android.view.ViewOutlineProvider
14-
import android.view.WindowManager
1516
import android.widget.PopupWindow
1617
import com.osfans.trime.core.RimeProto
1718
import com.osfans.trime.daemon.RimeSession
1819
import com.osfans.trime.daemon.launchOnReady
1920
import com.osfans.trime.data.prefs.AppPrefs
2021
import com.osfans.trime.data.theme.ColorManager
2122
import com.osfans.trime.data.theme.Theme
22-
import com.osfans.trime.ime.bar.QuickBar
2323
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
2424
import com.osfans.trime.ime.candidates.popup.PopupCandidatesMode
2525
import com.osfans.trime.ime.dependency.InputScope
2626
import me.tatarka.inject.annotations.Inject
27+
import splitties.dimensions.dp
2728
import splitties.views.backgroundColor
2829

2930
@InputScope
@@ -32,7 +33,6 @@ class PreeditModule(
3233
context: Context,
3334
theme: Theme,
3435
rime: RimeSession,
35-
private val bar: QuickBar,
3636
) : InputBroadcastReceiver {
3737
private val textBackColor = ColorManager.getColor("text_back_color")
3838

@@ -42,7 +42,7 @@ class PreeditModule(
4242
view: View,
4343
outline: Outline,
4444
) {
45-
val radius = theme.generalStyle.layout.roundCorner
45+
val radius = context.dp(theme.generalStyle.layout.roundCorner)
4646
val width = view.width
4747
val height = view.height
4848
val rect = Rect(-radius.toInt(), 0, width, (height + radius).toInt())
@@ -57,17 +57,42 @@ class PreeditModule(
5757
root.alpha = theme.generalStyle.layout.alpha / 255f
5858
root.outlineProvider = topLeftCornerRadiusOutlineProvider
5959
root.clipToOutline = true
60+
root.visibility = View.INVISIBLE
6061
preedit.setOnCursorMoveListener { position ->
6162
rime.launchOnReady { it.moveCursorPos(position) }
6263
}
6364
}
6465

65-
private val window =
66-
PopupWindow(ui.root).apply {
67-
width = WindowManager.LayoutParams.WRAP_CONTENT
68-
height = WindowManager.LayoutParams.WRAP_CONTENT
69-
animationStyle = 0
66+
private val touchEventReceiverWindow =
67+
PopupWindow(
68+
object : View(context) {
69+
@SuppressLint("ClickableViewAccessibility")
70+
override fun onTouchEvent(event: MotionEvent): Boolean = ui.preedit.onTouchEvent(event)
71+
},
72+
)
73+
74+
private var isWindowShowing = false
75+
76+
private fun showWindow() {
77+
isWindowShowing = true
78+
val (left, top) = intArrayOf(0, 0).also { ui.preedit.getLocationInWindow(it) }
79+
val width = ui.preedit.width
80+
val height = ui.preedit.height
81+
if (touchEventReceiverWindow.isShowing) {
82+
touchEventReceiverWindow.update(left, top, width, height)
83+
} else {
84+
touchEventReceiverWindow.width = width
85+
touchEventReceiverWindow.height = height
86+
touchEventReceiverWindow.showAtLocation(ui.root, Gravity.NO_GRAVITY, left, top)
87+
}
88+
}
89+
90+
private fun dismissWindow() {
91+
if (isWindowShowing) {
92+
isWindowShowing = false
93+
touchEventReceiverWindow.dismiss()
7094
}
95+
}
7196

7297
private val candidatesMode by AppPrefs.defaultInstance().candidates.mode
7398

@@ -76,18 +101,11 @@ class PreeditModule(
76101
if (candidatesMode == PopupCandidatesMode.ALWAYS_SHOW) return
77102

78103
ui.update(ctx.composition)
104+
ui.root.visibility = if (ui.visible) View.VISIBLE else View.INVISIBLE
79105
if (ctx.composition.length > 0) {
80-
val (x, y) = intArrayOf(0, 0).also { bar.view.getLocationInWindow(it) }
81-
window.showAtLocation(bar.view, Gravity.START or Gravity.TOP, x, y)
82-
ui.root.post {
83-
window.update(x, y - ui.root.height, -1, -1)
84-
}
106+
showWindow()
85107
} else {
86-
window.dismiss()
108+
dismissWindow()
87109
}
88110
}
89-
90-
fun onDetached() {
91-
window.dismiss()
92-
}
93111
}

app/src/main/java/com/osfans/trime/ime/composition/PreeditUi.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ open class PreeditUi(
6363
}
6464
}
6565

66+
var visible = false
67+
private set
68+
6669
private fun updateTextView(
6770
str: CharSequence,
6871
visible: Boolean,
@@ -79,6 +82,8 @@ open class PreeditUi(
7982
val string = inputComposition.toSpannedString()
8083
val cursorPos = inputComposition.cursorPos
8184
val hasPreedit = inputComposition.length > 0
85+
visible = hasPreedit
86+
if (!visible) return
8287
val stringWithCursor =
8388
if (cursorPos == 0 || cursorPos == string.length) {
8489
string

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ class InputView(
246246

247247
updateKeyboardSize()
248248

249+
add(
250+
preedit.ui.root,
251+
lParams(matchParent, wrapContent) {
252+
above(keyboardView)
253+
centerHorizontally()
254+
},
255+
)
256+
249257
add(
250258
keyboardView,
251259
lParams(matchParent, wrapContent) {
@@ -377,7 +385,6 @@ class InputView(
377385
// implies that InputView should not be attached again after detached.
378386
baseMessageHandler.cancelJob()
379387
updateWindowViewHeightJob.cancel()
380-
preedit.onDetached()
381388
preview.root.removeAllViews()
382389
broadcaster.clear()
383390
super.onDetachedFromWindow()

0 commit comments

Comments
 (0)