From 22c73172768191a6e20156ae8c666bbd28370f9c Mon Sep 17 00:00:00 2001 From: th2tran Date: Tue, 3 Nov 2026 19:02:43 -0500 Subject: [PATCH] Android: Fixed keyboard switcher immediately closing after it opens on the Google Pixel 10. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause: the bug is in the Android System WebView (156.0.8078.47), not in Vector's own code (Chromium issue crbug.com/560321831). This WebView version hides the keyboard whenever the app's window loses focus. Gboard's keyboard switcher takes that focus when it opens, so the keyboard was hidden and the switcher closed straight away. A debugger stack trace showed the hide coming from WebView's own input code. Chromium has since changed this to stop hiding the keyboard. Fix (MainActivity.kt): the WebView now sits inside a small wrapper. While the keyboard is showing, the wrapper doesn't pass a focus-loss signal on to the WebView. If the app is actually being left, the held signal is passed on in onPause. Expected to be unaffected: • Other keyboards (Samsung, SwiftKey, older Gboard): they just get the same fix if their switcher steals focus. If they don't, nothing changes. • Older WebView versions, or future ones with Chromium's fix: holding back the focus loss only skips a keyboard reset that isn't needed there. • Older Android versions: if the keyboard-visible check misreads on an older device, the code falls back to the old behaviour. • Read receipts and the window-active state: Tauri gets focus changes from the activity directly, not through the view tree, so these still work. • Layout, side-edge gestures and Mini App overlays: the wrapper fills the screen, and no Vector code or bundled Tauri plugin assumes the WebView is the top-level view. Small behaviour changes: • Notification shade or a system dialog opened while typing: the keyboard now stays up behind it, as it does in native apps. The page also no longer gets a blur event at that moment, so a tooltip or context menu may stay open until you return. Leaving the app still hands the WebView the focus loss. • Split-screen or freeform windows (foldables, tablets): if focus moves to the other app while Vector's keyboard is up, Vector isn't paused, so the WebView keeps acting focused (for example, a blinking cursor) until you return. This is cosmetic. Known gap: Mini App WebViews aren't wrapped, so the keyboard switcher may still close inside Mini Apps on affected WebView versions. I can wrap them the same way if you want. --- .../main/java/io/vectorapp/MainActivity.kt | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src-tauri/gen/android/app/src/main/java/io/vectorapp/MainActivity.kt b/src-tauri/gen/android/app/src/main/java/io/vectorapp/MainActivity.kt index 45b27f50d..95d649e01 100644 --- a/src-tauri/gen/android/app/src/main/java/io/vectorapp/MainActivity.kt +++ b/src-tauri/gen/android/app/src/main/java/io/vectorapp/MainActivity.kt @@ -1,5 +1,6 @@ package io.vectorapp +import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.graphics.Rect @@ -14,14 +15,47 @@ import android.webkit.WebSettings import android.webkit.WebView import android.os.Bundle import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat import io.vectorapp.miniapp.MiniAppManager /** Chromium the shared frontend needs (Vector Web's preflight floor). */ private const val MIN_WEBVIEW = 98 +/** + * Some WebView versions hide the keyboard whenever their window loses focus (crbug.com/560321831), + * and the keyboard's own input-method switcher takes that focus, so the switcher closed as it + * opened. While the keyboard is up, a focus loss is held back from the WebView. + */ +private class ImeFocusGuard(context: Context) : FrameLayout(context) { + private var heldLoss = false + + override fun dispatchWindowFocusChanged(hasFocus: Boolean) { + if (!hasFocus && imeVisible()) { + heldLoss = true + return + } + heldLoss = false + super.dispatchWindowFocusChanged(hasFocus) + } + + /** Hands the WebView a held focus loss once the app is really leaving the foreground. */ + fun releaseHeldLoss() { + if (heldLoss && !hasWindowFocus()) { + heldLoss = false + super.dispatchWindowFocusChanged(false) + } + } + + private fun imeVisible() = + ViewCompat.getRootWindowInsets(this)?.isVisible(WindowInsetsCompat.Type.ime()) == true +} + class MainActivity : TauriActivity() { companion object { @@ -47,6 +81,19 @@ class MainActivity : TauriActivity() { } private var managedWebView: WebView? = null + private var focusGuard: ImeFocusGuard? = null + + // wry hands the WebView straight to setContentView; it goes in behind the focus guard. + override fun setContentView(view: View) { + if (view !is WebView || view.parent != null) { + super.setContentView(view) + return + } + val guard = ImeFocusGuard(this) + guard.addView(view, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + focusGuard = guard + super.setContentView(guard) + } // ===== Device-tilt for the badge card ===== // Started/stopped from JS (window.__vectorGyroBridge) only while the card is open, so the sensor @@ -200,6 +247,7 @@ class MainActivity : TauriActivity() { } override fun onPause() { + focusGuard?.releaseHeldLoss() super.onPause() stopGyro() // never leave the rotation sensor running while backgrounded try { nativeOnPause() } catch (_: Exception) {}