5
5
6
6
package com.osfans.trime.ime.composition
7
7
8
+ import android.annotation.SuppressLint
8
9
import android.content.Context
9
10
import android.graphics.Outline
10
11
import android.graphics.Rect
11
12
import android.view.Gravity
13
+ import android.view.MotionEvent
12
14
import android.view.View
13
15
import android.view.ViewOutlineProvider
14
- import android.view.WindowManager
15
16
import android.widget.PopupWindow
16
17
import com.osfans.trime.core.RimeProto
17
18
import com.osfans.trime.daemon.RimeSession
18
19
import com.osfans.trime.daemon.launchOnReady
19
20
import com.osfans.trime.data.prefs.AppPrefs
20
21
import com.osfans.trime.data.theme.ColorManager
21
22
import com.osfans.trime.data.theme.Theme
22
- import com.osfans.trime.ime.bar.QuickBar
23
23
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
24
24
import com.osfans.trime.ime.candidates.popup.PopupCandidatesMode
25
25
import com.osfans.trime.ime.dependency.InputScope
26
26
import me.tatarka.inject.annotations.Inject
27
+ import splitties.dimensions.dp
27
28
import splitties.views.backgroundColor
28
29
29
30
@InputScope
@@ -32,7 +33,6 @@ class PreeditModule(
32
33
context : Context ,
33
34
theme : Theme ,
34
35
rime : RimeSession ,
35
- private val bar : QuickBar ,
36
36
) : InputBroadcastReceiver {
37
37
private val textBackColor = ColorManager .getColor(" text_back_color" )
38
38
@@ -42,7 +42,7 @@ class PreeditModule(
42
42
view : View ,
43
43
outline : Outline ,
44
44
) {
45
- val radius = theme.generalStyle.layout.roundCorner
45
+ val radius = context.dp( theme.generalStyle.layout.roundCorner)
46
46
val width = view.width
47
47
val height = view.height
48
48
val rect = Rect (- radius.toInt(), 0 , width, (height + radius).toInt())
@@ -57,17 +57,42 @@ class PreeditModule(
57
57
root.alpha = theme.generalStyle.layout.alpha / 255f
58
58
root.outlineProvider = topLeftCornerRadiusOutlineProvider
59
59
root.clipToOutline = true
60
+ root.visibility = View .INVISIBLE
60
61
preedit.setOnCursorMoveListener { position ->
61
62
rime.launchOnReady { it.moveCursorPos(position) }
62
63
}
63
64
}
64
65
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()
70
94
}
95
+ }
71
96
72
97
private val candidatesMode by AppPrefs .defaultInstance().candidates.mode
73
98
@@ -76,18 +101,11 @@ class PreeditModule(
76
101
if (candidatesMode == PopupCandidatesMode .ALWAYS_SHOW ) return
77
102
78
103
ui.update(ctx.composition)
104
+ ui.root.visibility = if (ui.visible) View .VISIBLE else View .INVISIBLE
79
105
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()
85
107
} else {
86
- window.dismiss ()
108
+ dismissWindow ()
87
109
}
88
110
}
89
-
90
- fun onDetached () {
91
- window.dismiss()
92
- }
93
111
}
0 commit comments