Skip to content

Commit

Permalink
refactor: handle option notifications about input view in InputView
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Feb 2, 2024
1 parent c3ffaea commit 76de434
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
35 changes: 35 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/core/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import android.view.WindowManager
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import com.osfans.trime.core.Rime
import com.osfans.trime.core.RimeNotification
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.ime.bar.QuickBar
import com.osfans.trime.ime.keyboard.KeyboardWindow
import com.osfans.trime.ime.symbol.LiquidKeyboard
import com.osfans.trime.util.styledFloat
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.koin.core.context.loadKoinModules
import org.koin.core.context.unloadKoinModules
import org.koin.dsl.module
Expand Down Expand Up @@ -40,6 +45,7 @@ import splitties.views.dsl.core.wrapContent
@SuppressLint("ViewConstructor")
class InputView(
val service: Trime,
val rime: Rime,
val theme: Theme,
) : ConstraintLayout(service) {
private val placeholderListener = OnClickListener { }
Expand All @@ -59,6 +65,8 @@ class InputView(
setOnClickListener { placeholderListener }
}

private val notificationHandlerJob: Job

private val themedContext = context.withTheme(android.R.style.Theme_DeviceDefault_Settings)
val quickBar = QuickBar()
val keyboardWindow = KeyboardWindow()
Expand Down Expand Up @@ -106,6 +114,13 @@ class InputView(
// MUST call before any other operations
loadKoinModules(module)

notificationHandlerJob =
service.lifecycleScope.launch {
rime.notificationFlow.collect {
handleRimeNotification(it)
}
}

liquidKeyboard.setKeyboardView(keyboardWindow.oldSymbolInputView.liquidKeyboardView)

keyboardView =
Expand Down Expand Up @@ -198,6 +213,26 @@ class InputView(
quickBar.view.setPadding(sidePadding, 0, sidePadding, 0)
}

private fun handleRimeNotification(it: RimeNotification) {
when (it) {
is RimeNotification.OptionNotification -> {
when (it.option) {
"_hide_comment" -> {
quickBar.oldCandidateBar.candidates.setShowComment(!it.value)
}
"_hide_candidate" -> {
quickBar.oldCandidateBar.root.visibility =
if (it.value) View.GONE else View.VISIBLE
}
"_hide_key_hint" -> keyboardWindow.oldMainInputView.mainKeyboardView.setShowHint(!it.value)
"_hide_key_symbol" -> keyboardWindow.oldMainInputView.mainKeyboardView.setShowSymbol(!it.value)
}
keyboardWindow.oldMainInputView.mainKeyboardView.invalidateAllKeys()
}
else -> {}
}
}

fun switchUiByState(state: KeyboardWindow.State) {
keyboardWindow.switchUiByState(state)
quickBar.switchUiByState(QuickBar.State.entries[state.ordinal])
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/osfans/trime/ime/core/Trime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ open class Trime : LifecycleInputMethodService() {
override fun onCreateInputView(): View {
Timber.d("onCreateInputView()")
RimeWrapper.runAfterStarted {
inputView = InputView(this, ThemeManager.activeTheme)
inputView = InputView(this, Rime.getInstance(false), ThemeManager.activeTheme)
mainKeyboardView = inputView!!.keyboardWindow.oldMainInputView.mainKeyboardView
// 初始化候选栏
mCandidateRoot = inputView!!.quickBar.oldCandidateBar.root
Expand Down
19 changes: 0 additions & 19 deletions app/src/main/java/com/osfans/trime/ime/text/TextInputManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.osfans.trime.ime.text
import android.content.DialogInterface
import android.text.InputType
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.appcompat.R.style.Theme_AppCompat_DayNight_Dialog_Alert
import androidx.appcompat.app.AlertDialog
Expand All @@ -17,7 +16,6 @@ import com.osfans.trime.data.schema.SchemaManager
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.broadcast.IntentReceiver
import com.osfans.trime.ime.core.EditorInstance
import com.osfans.trime.ime.core.InputView
import com.osfans.trime.ime.core.Speech
import com.osfans.trime.ime.core.Trime
import com.osfans.trime.ime.enums.Keycode
Expand Down Expand Up @@ -64,7 +62,6 @@ class TextInputManager private constructor() :
private var rimeNotiHandlerJob: Job? = null

private var mainKeyboardView: KeyboardView? = null
private var candidateRoot: ScrollView? = null

val locales = Array(2) { Locale.getDefault() }

Expand Down Expand Up @@ -132,24 +129,13 @@ class TextInputManager private constructor() :
trime.loadConfig()
}

override fun onInitializeInputUi(inputView: InputView) {
super.onInitializeInputUi(inputView)
// Initialize main keyboard view
mainKeyboardView = inputView.keyboardWindow.oldMainInputView.mainKeyboardView
}

/**
* Cancels all coroutines and cleans up.
*/
override fun onDestroy() {
intentReceiver?.unregisterReceiver(trime)
intentReceiver = null

candidateRoot = null

mainKeyboardView?.setOnKeyboardActionListener(null)
mainKeyboardView = null

rimeNotiHandlerJob?.cancel()
rimeNotiHandlerJob = null
instance = null
Expand Down Expand Up @@ -246,14 +232,10 @@ class TextInputManager private constructor() :
trime.inputFeedbackManager?.ttsLanguage =
locales[if (value) 1 else 0]
}
"_hide_comment" -> trime.setShowComment(!value)
"_hide_candidate" -> {
candidateRoot?.visibility = if (!value) View.VISIBLE else View.GONE
trime.setCandidatesViewShown(isComposable && !value)
}
"_liquid_keyboard" -> trime.selectLiquidKeyboard(0)
"_hide_key_hint" -> mainKeyboardView?.setShowHint(!value)
"_hide_key_symbol" -> mainKeyboardView?.setShowSymbol(!value)
else ->
if (option.startsWith("_keyboard_") &&
option.length > 10 && value
Expand All @@ -268,7 +250,6 @@ class TextInputManager private constructor() :
shouldUpdateRimeOption = true
}
}
mainKeyboardView?.invalidateAllKeys()
}
}

Expand Down

0 comments on commit 76de434

Please sign in to comment.