@@ -48,18 +48,14 @@ import com.osfans.trime.daemon.RimeSession
4848import com.osfans.trime.data.db.DraftHelper
4949import com.osfans.trime.data.prefs.AppPrefs
5050import com.osfans.trime.data.theme.ColorManager
51- import com.osfans.trime.data.theme.KeyActionManager
5251import com.osfans.trime.data.theme.Theme
5352import com.osfans.trime.data.theme.ThemeManager
5453import com.osfans.trime.ime.broadcast.IntentReceiver
5554import com.osfans.trime.ime.enums.FullscreenMode
5655import com.osfans.trime.ime.enums.InlinePreeditMode
57- import com.osfans.trime.ime.enums.Keycode
58- import com.osfans.trime.ime.keyboard.CommonKeyboardActionListener
5956import com.osfans.trime.ime.keyboard.InitializationUi
6057import com.osfans.trime.ime.keyboard.InputFeedbackManager
6158import com.osfans.trime.util.ShortcutUtils
62- import com.osfans.trime.util.ShortcutUtils.openCategory
6359import com.osfans.trime.util.findSectionFrom
6460import com.osfans.trime.util.isLandscape
6561import com.osfans.trime.util.isNightMode
@@ -87,16 +83,12 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
8783 private val prefs: AppPrefs
8884 get() = AppPrefs .defaultInstance()
8985 var inputView: InputView ? = null
90- private val commonKeyboardActionListener: CommonKeyboardActionListener ?
91- get() = inputView?.commonKeyboardActionListener
9286 private var initializationUi: InitializationUi ? = null
9387 private var mIntentReceiver: IntentReceiver ? = null
9488 private var isWindowShown = false // 键盘窗口是否已显示
9589 private var isComposable: Boolean = false
9690 private val locales = Array (2 ) { Locale .getDefault() }
9791
98- var shouldUpdateRimeOption = false
99-
10092 var lastCommittedText: CharSequence = " "
10193 private set
10294
@@ -161,18 +153,13 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
161153 isWindowShown = false
162154 }
163155
164- private fun updateRimeOption (): Boolean {
156+ private suspend fun updateRimeOption (api : RimeApi ) {
165157 try {
166- if (shouldUpdateRimeOption) {
167- Rime .setOption(" soft_cursor" , prefs.keyboard.softCursorEnabled) // 軟光標
168- Rime .setOption(" _horizontal" , ThemeManager .activeTheme.generalStyle.horizontal) // 水平模式
169- shouldUpdateRimeOption = false
170- }
158+ api.setRuntimeOption(" soft_cursor" , prefs.keyboard.softCursorEnabled) // 軟光標
159+ api.setRuntimeOption(" _horizontal" , ThemeManager .activeTheme.generalStyle.horizontal) // 水平模式
171160 } catch (e: Exception ) {
172161 Timber .e(e)
173- return false
174162 }
175- return true
176163 }
177164
178165 /* * 防止重启系统 强行停止应用时alarm任务丢失 */
@@ -204,6 +191,9 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
204191
205192 override fun onCreate () {
206193 rime = RimeDaemon .createSession(javaClass.name)
194+ postRimeJob {
195+ updateRimeOption(this )
196+ }
207197 lifecycleScope.launch {
208198 jobs.consumeEach { it.join() }
209199 }
@@ -231,7 +221,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
231221 ThemeManager .init ()
232222 InputFeedbackManager .init ()
233223 restartSystemStartTimingSync()
234- shouldUpdateRimeOption = true
235224 val theme = ThemeManager .activeTheme
236225 val defaultLocale = theme.generalStyle.locale.split(DELIMITER_SPLITTER )
237226 locales[0 ] =
@@ -259,7 +248,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
259248 when (it) {
260249 is RimeNotification .OptionNotification -> {
261250 val value = it.value.value
262- when (val option = it.value.option) {
251+ when (it.value.option) {
263252 " ascii_mode" -> {
264253 InputFeedbackManager .ttsLanguage =
265254 locales[if (value) 1 else 0 ]
@@ -269,16 +258,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
269258 -> {
270259 setCandidatesViewShown(isComposable && ! value)
271260 }
272- else ->
273- if (option.startsWith(" _key_" ) && option.length > 5 && value) {
274- shouldUpdateRimeOption = false // 防止在 handleRimeNotification 中 setOption
275- val key = option.substring(5 )
276- inputView
277- ?.commonKeyboardActionListener
278- ?.listener
279- ?.onAction(KeyActionManager .getAction(key))
280- shouldUpdateRimeOption = true
281- }
282261 }
283262 }
284263 is RimeEvent .IpcResponseEvent ->
@@ -322,7 +301,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
322301 *
323302 * 重置鍵盤、候選條、狀態欄等 !!注意,如果其中調用Rime.setOption,切換方案會卡住 */
324303 fun recreateInputView (theme : Theme ) {
325- shouldUpdateRimeOption = true // 不能在Rime.onMessage中調用set_option,會卡死
326304 updateComposing() // 切換主題時刷新候選
327305 setInputView(InputView (this , rime, theme).also { inputView = it })
328306 initializationUi = null
@@ -822,40 +800,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
822800 }
823801 }
824802
825- // 处理键盘事件(Android keycode)
826- fun handleKey (
827- keyEventCode : Int ,
828- metaState : Int ,
829- ): Boolean { // 軟鍵盤
830- commonKeyboardActionListener?.shouldReleaseKey = false
831- val value =
832- RimeKeyMapping
833- .keyCodeToVal(keyEventCode)
834- .takeIf { it != RimeKeyMapping .RimeKey_VoidSymbol }
835- ? : Rime .getRimeKeycodeByName(Keycode .keyNameOf(keyEventCode))
836- val modifiers = KeyModifiers .fromMetaState(metaState).modifiers
837- val code = ScancodeMapping .keyCodeToScancode(keyEventCode)
838- var result = false
839- postRimeJob {
840- if (! processKey(value, modifiers, code)) {
841- if (! hookKeyboard(keyEventCode, metaState)) {
842- if (! openCategory(keyEventCode)) {
843- result = false
844- } else {
845- Timber .d(" handleKey: openCategory" )
846- }
847- } else {
848- Timber .d(" handleKey: hook" )
849- }
850- } else {
851- commonKeyboardActionListener?.shouldReleaseKey = true
852- Timber .d(" handleKey: processKey" )
853- }
854- }
855- if (! result) commonKeyboardActionListener?.shouldReleaseKey = true
856- return result
857- }
858-
859803 fun shareText (): Boolean {
860804 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
861805 val ic = currentInputConnection ? : return false
@@ -867,7 +811,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
867811 }
868812
869813 /* * 編輯操作 */
870- private fun hookKeyboard (
814+ fun hookKeyboard (
871815 code : Int ,
872816 mask : Int ,
873817 ): Boolean {
0 commit comments