Skip to content

Commit

Permalink
fix: crash on showing composition popup window somehow
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed May 2, 2024
1 parent a1bb068 commit 2bc4f2f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,27 +292,25 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}

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

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

mCompositionPopupWindow =
CompositionPopupWindow(this, ThemeManager.activeTheme).apply {
anchorView = inputView?.quickBar?.view
}.apply { hideCompositionView() }
CompositionPopupWindow(this, ThemeManager.activeTheme, inputView!!.quickBar.view)

loadConfig()
KeyboardSwitcher.newOrReset()
Expand All @@ -328,11 +326,11 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
mIntentReceiver = null
InputFeedbackManager.destroy()
inputView = null
mCompositionPopupWindow = null
for (listener in eventListeners) {
listener.onDestroy()
}
eventListeners.clear()
mCompositionPopupWindow?.finishInput()
ColorManager.removeOnChangedListener(onColorChangeListener)
super.onDestroy()
RimeDaemon.destroySession(javaClass.name)
Expand Down Expand Up @@ -546,21 +544,20 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}

override fun onFinishInputView(finishingInput: Boolean) {
super.onFinishInputView(finishingInput)
Timber.d("onFinishInputView: finishingInput=$finishingInput")
rime.runIfReady {
if (normalTextEditor) {
DraftHelper.onInputEventChanged()
}
try {
performEscape()
mCompositionPopupWindow!!.hideCompositionView()
} catch (e: Exception) {
Timber.e(e, "Failed to show the PopupWindow.")
}
}
InputFeedbackManager.finishInput()
inputView?.finishInput()
Timber.d("OnFinishInputView")
mCompositionPopupWindow?.hideCompositionView()
}

fun bindKeyboardToInputView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ import timber.log.Timber
class CompositionPopupWindow(
private val ctx: Context,
private val theme: Theme,
private val anchorView: View,
) {
// 顯示懸浮窗口
val isPopupWindowEnabled =
AppPrefs.defaultInstance().keyboard.popupWindowEnabled &&
theme.generalStyle.window != null
theme.generalStyle.window.isNotEmpty()

val composition =
CompositionRootBinding.inflate(LayoutInflater.from(ctx)).apply {
Expand Down Expand Up @@ -82,14 +83,11 @@ class CompositionPopupWindow(

private val mPopupRectF = RectF()
private val mPopupHandler = Handler(Looper.getMainLooper())
var anchorView: View? = null

private val mPopupTimer =
Runnable {
if (!isPopupWindowEnabled) return@Runnable
anchorView?.let { anchor ->
if (anchor.windowToken == null) return@Runnable

if (!isPopupWindowEnabled || anchorView.windowToken == null) return@Runnable
anchorView.let { anchor ->
var x = 0
var y = 0
val candidateLocation = IntArray(2)
Expand Down Expand Up @@ -146,7 +144,7 @@ class CompositionPopupWindow(
}
y -= BarUtils.getStatusBarHeight()
if (!mPopupWindow.isShowing) {
mPopupWindow.showAtLocation(anchorView, Gravity.START or Gravity.TOP, x, y)
mPopupWindow.showAtLocation(anchor, Gravity.START or Gravity.TOP, x, y)
} else {
/* must use the width and height of popup window itself here directly,
* otherwise the width and height cannot be updated! */
Expand Down Expand Up @@ -221,9 +219,4 @@ class CompositionPopupWindow(
cursorAnchorInfo.matrix.mapRect(mPopupRectF)
}
}

fun finishInput() {
hideCompositionView()
anchorView = null
}
}

0 comments on commit 2bc4f2f

Please sign in to comment.