Skip to content

Commit

Permalink
Remove listeners that could have stayed waiting for focus if getting …
Browse files Browse the repository at this point in the history
…focus is too slow
  • Loading branch information
LunarX committed Jun 19, 2024
1 parent dfa3d30 commit f3abbb2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class RichHtmlEditorWebView @JvmOverloads constructor(

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
keyboardOpener.removePendingListener()
jsBridgeJob.cancel()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import android.view.ViewTreeObserver
import android.view.inputmethod.InputMethodManager

internal class KeyboardOpener(private val view: View) : JsLifecycleAwareExecutor<Unit>() {

private var listener: ViewTreeObserver.OnWindowFocusChangeListener? = null

override fun executeImmediately(value: Unit) {
if (view.requestFocus()) {
if (view.hasWindowFocus()) {
Expand All @@ -14,7 +17,7 @@ internal class KeyboardOpener(private val view: View) : JsLifecycleAwareExecutor
// The window won't have the focus most of the time when the configuration changes and we want to reopen the
// keyboard right away. When this happen, we need to wait for the window to get the focus before opening the
// keyboard.
val listener = object : ViewTreeObserver.OnWindowFocusChangeListener {
listener = object : ViewTreeObserver.OnWindowFocusChangeListener {
override fun onWindowFocusChanged(hasFocus: Boolean) {
if (hasFocus) {
openKeyboard()
Expand All @@ -28,6 +31,11 @@ internal class KeyboardOpener(private val view: View) : JsLifecycleAwareExecutor
}
}

fun removePendingListener() {
view.viewTreeObserver.removeOnWindowFocusChangeListener(listener)
listener = null
}

private fun openKeyboard() {
val inputMethodManager = view.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
Expand Down

0 comments on commit f3abbb2

Please sign in to comment.