Skip to content

Commit

Permalink
refactor: move the composing logic to the view into InputView
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jun 26, 2024
1 parent 2c1bcb3 commit c873442
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.view.WindowManager
import android.view.inputmethod.CursorAnchorInfo
import android.widget.PopupWindow
import androidx.core.math.MathUtils
import com.osfans.trime.core.Rime
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.Theme
Expand Down Expand Up @@ -175,7 +176,7 @@ class CompositionPopupWindow(
popupWindowPos !== PopupPosition.LEFT_UP && popupWindowPos !== PopupPosition.RIGHT_UP
}

fun updatePopupWindow(
private fun updatePopupWindow(
offsetX: Int,
offsetY: Int,
) {
Expand All @@ -186,12 +187,20 @@ class CompositionPopupWindow(
mPopupWindow.update(popupWindowX, popupWindowY, -1, -1, true)
}

fun hideCompositionView() {
fun updateView() {
if (Rime.isComposing) {
updateCompositionView()
} else {
hideCompositionView()
}
}

private fun hideCompositionView() {
mPopupWindow.dismiss()
mPopupHandler.removeCallbacks(mPopupTimer)
}

fun updateCompositionView() {
private fun updateCompositionView() {
if (isPopupWindowMovable == "once") {
popupWindowPos = PopupPosition.fromString(ThemeManager.activeTheme.generalStyle.layout.position)
}
Expand Down Expand Up @@ -234,5 +243,6 @@ class CompositionPopupWindow(
}
cursorAnchorInfo.matrix.mapRect(mPopupRectF)
}
updateView()
}
}
31 changes: 31 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 @@ -12,14 +12,17 @@ import android.os.Build
import android.view.View
import android.view.View.OnClickListener
import android.view.WindowManager
import android.view.inputmethod.CursorAnchorInfo
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputConnection
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
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.daemon.RimeSession
import com.osfans.trime.data.prefs.AppPrefs
Expand Down Expand Up @@ -329,6 +332,34 @@ class InputView(
}
}

fun updateCursorAnchorInfo(info: CursorAnchorInfo) {
composition.updateCursorAnchorInfo(info)
}

fun updateComposing(ic: InputConnection?) {
val candidateView = quickBar.oldCandidateBar.candidates
val compositionView = composition.composition.compositionView
val mainKeyboardView = keyboardWindow.oldMainInputView.mainKeyboardView
if (composition.isPopupWindowEnabled) {
val offset = Rime.inputContext?.let { compositionView.update(it) } ?: 0
candidateView.setText(offset)
val isCursorUpdated =
if (ic != null && !composition.isWinFixed()) {
ic.requestCursorUpdates(InputConnection.CURSOR_UPDATE_IMMEDIATE)
} else {
false
}.also { composition.isCursorUpdated = it }
// if isCursorUpdated, updateView will be called in onUpdateCursorAnchorInfo
// otherwise we need to call it here
if (!isCursorUpdated) {
composition.updateView()
}
} else {
candidateView.setText(0)
}
mainKeyboardView.invalidateComposingKeys()
}

private var showingDialog: Dialog? = null

fun showDialog(dialog: Dialog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.broadcast.IntentReceiver
import com.osfans.trime.ime.composition.CompositionPopupWindow
import com.osfans.trime.ime.enums.FullscreenMode
import com.osfans.trime.ime.enums.InlinePreeditMode
import com.osfans.trime.ime.enums.Keycode
Expand All @@ -55,7 +54,6 @@ import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.ime.keyboard.KeyboardView
import com.osfans.trime.ime.symbol.SymbolBoardType
import com.osfans.trime.ime.symbol.TabManager
import com.osfans.trime.ime.text.Candidate
import com.osfans.trime.ime.text.TextInputManager
import com.osfans.trime.util.ShortcutUtils
import com.osfans.trime.util.ShortcutUtils.openCategory
Expand Down Expand Up @@ -88,15 +86,13 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
private val prefs: AppPrefs
get() = AppPrefs.defaultInstance()
private var mainKeyboardView: KeyboardView? = null // 主軟鍵盤
private var mCandidate: Candidate? = null // 候選
var inputView: InputView? = null
private var initializationUi: InitializationUi? = null
private var eventListeners = WeakHashSet<EventListener>()
private var mIntentReceiver: IntentReceiver? = null
private var isWindowShown = false // 键盘窗口是否已显示
private var isAutoCaps = false // 句首自動大寫
var textInputManager: TextInputManager? = null // 文字输入管理器
private var mCompositionPopupWindow: CompositionPopupWindow? = null
var candidateExPage = false

var shouldUpdateRimeOption = false
Expand Down Expand Up @@ -318,24 +314,12 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
commitTextByChar(checkNotNull(ShortcutUtils.pasteFromClipboard(this)).toString())
}

private fun showCompositionView() {
if (Rime.isComposing) {
mCompositionPopupWindow?.updateCompositionView()
} else {
mCompositionPopupWindow?.hideCompositionView()
}
}

/** Must be called on the UI thread
*
* 重置鍵盤、候選條、狀態欄等 !!注意,如果其中調用Rime.setOption,切換方案會卡住 */
fun recreateInputView() {
inputView = InputView(this, rime)
mainKeyboardView = inputView!!.keyboardWindow.oldMainInputView.mainKeyboardView
// 初始化候选栏
mCandidate = inputView!!.quickBar.oldCandidateBar.candidates

mCompositionPopupWindow = inputView!!.composition

loadConfig()
KeyboardSwitcher.newOrReset()
Expand All @@ -351,7 +335,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
mIntentReceiver = null
InputFeedbackManager.destroy()
inputView = null
mCompositionPopupWindow = null
for (listener in eventListeners) {
listener.onDestroy()
}
Expand Down Expand Up @@ -392,8 +375,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}

override fun onUpdateCursorAnchorInfo(cursorAnchorInfo: CursorAnchorInfo) {
mCompositionPopupWindow?.updateCursorAnchorInfo(cursorAnchorInfo)
showCompositionView()
inputView?.updateCursorAnchorInfo(cursorAnchorInfo)
}

override fun onUpdateSelection(
Expand Down Expand Up @@ -1110,27 +1092,10 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}

/** 更新Rime的中西文狀態、編輯區文本 */
fun updateComposing(): Int {
val ic = currentInputConnection
fun updateComposing() {
updateComposingText()
if (ic != null && mCompositionPopupWindow?.isWinFixed() == false) {
mCompositionPopupWindow?.isCursorUpdated = ic.requestCursorUpdates(1)
}
var startNum = 0
if (mCompositionPopupWindow?.isPopupWindowEnabled == true) {
val composition = mCompositionPopupWindow!!.composition
composition.compositionView.visibility = View.VISIBLE
startNum = Rime.inputContext?.let { composition.compositionView.update(it) } ?: 0
mCandidate?.setText(startNum)
// if isCursorUpdated, showCompositionView will be called in onUpdateCursorAnchorInfo
// otherwise we need to call it here
if (mCompositionPopupWindow?.isCursorUpdated == false) showCompositionView()
} else {
mCandidate?.setText(0)
}
mainKeyboardView?.invalidateComposingKeys()
inputView?.updateComposing(currentInputConnection)
if (!onEvaluateInputViewShown()) setCandidatesViewShown(textInputManager!!.isComposable) // 實體鍵盤打字時顯示候選欄
return startNum
}

/**
Expand Down

0 comments on commit c873442

Please sign in to comment.