From 816ddbf7eaafadb2709f4c6f83af973bdc3554c4 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 16 Nov 2026 23:35:17 +0530 Subject: [PATCH 1/2] fix(layout): measure navigation bar overlap on screen and recheck after reload The overlap from #828 compared the view bottom with its own window, so a window that itself ends above the navigation bar still reserved the full inset and doubled the space below the keys. Compare with the screen instead. A settings change (font, toolbar mode) replaces the input view and re-shows the window at once, so the padding can be computed while the window is still settling and stay wrong until a key is tapped. Recheck it once the window is visible, and log the padding inputs when the value changes so exported logs show them. --- .../helium314/keyboard/latin/InputView.kt | 63 ++++++++++++++++--- .../keyboard/latin/NavBarOverlapTest.kt | 18 ++++-- 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/InputView.kt b/app/src/main/java/helium314/keyboard/latin/InputView.kt index 15fa6b5a7..6da116004 100644 --- a/app/src/main/java/helium314/keyboard/latin/InputView.kt +++ b/app/src/main/java/helium314/keyboard/latin/InputView.kt @@ -10,16 +10,19 @@ import android.annotation.SuppressLint import android.content.Context import android.graphics.Rect import android.util.AttributeSet +import android.util.DisplayMetrics import android.view.MotionEvent import android.view.View import android.widget.FrameLayout import android.os.Build import android.view.WindowInsets +import android.view.WindowManager import helium314.keyboard.accessibility.AccessibilityUtils import helium314.keyboard.keyboard.MainKeyboardView import helium314.keyboard.latin.common.ColorType import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.suggestions.SuggestionStripView +import helium314.keyboard.latin.utils.Log import kotlin.math.min class InputView @JvmOverloads constructor( @@ -96,13 +99,39 @@ class InputView @JvmOverloads constructor( private fun reportedNavInset(): Int = navigationBarFrameHeight().takeIf { it > 0 } ?: if (mNavBarBottomInsets > 0) mNavBarBottomInsets else getNavInsets() - /** Bottom padding needed for the navigation bar; known only once this view is laid out, until then the reported inset is used */ + /** what [navPadding] was computed from, for the log when the padding changes */ + private var mLastNavMeasure = "" + + /** + * Bottom padding needed for the navigation bar; known only once this view is laid out, until then the reported + * inset is used. Measured on the screen, not in the window: a window that itself ends above the navigation bar + * (seen on Samsung with Android 12) has this view end at its bottom, which in window coordinates looks like the + * view is under the navigation bar. + */ private fun navPadding(): Int { val reported = reportedNavInset() - if (reported <= 0 || !isLaidOut || height == 0 || rootView.height == 0) return reported + val screenHeight = screenHeight() + if (reported <= 0 || !isLaidOut || height == 0 || screenHeight == 0) { + mLastNavMeasure = "reported=$reported laidOut=$isLaidOut height=$height screen=$screenHeight" + return reported + } val location = IntArray(2) - getLocationInWindow(location) - return navBarOverlap(reported, location[1] + height, rootView.height) + getLocationOnScreen(location) + val viewBottom = location[1] + height + mLastNavMeasure = "reported=$reported viewBottom=$viewBottom screen=$screenHeight" + return navBarOverlap(reported, viewBottom, screenHeight) + } + + private fun screenHeight(): Int { + val windowManager = context.getSystemService(WindowManager::class.java) ?: return 0 + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + windowManager.maximumWindowMetrics.bounds.height() + } else { + val metrics = DisplayMetrics() + @Suppress("DEPRECATION") + windowManager.defaultDisplay.getRealMetrics(metrics) + metrics.heightPixels + } } fun resetChildrenFloatingPadding() { @@ -159,6 +188,10 @@ class InputView @JvmOverloads constructor( R.id.touchpad_view ) val insets = navPadding() + if (insets != mAppliedNavPadding) { + // kept in exported logs, so reports of wrong space below the keys come with the numbers + Log.i(TAG, "navigation bar padding $mAppliedNavPadding -> $insets ($mLastNavMeasure)") + } mAppliedNavPadding = insets val wrapper = findViewById(R.id.keyboard_view_wrapper) val showToolbarOnly = wrapper?.visibility != View.VISIBLE @@ -208,6 +241,18 @@ class InputView @JvmOverloads constructor( post { requestApplyInsets() } } + override fun onWindowVisibilityChanged(visibility: Int) { + super.onWindowVisibilityChanged(visibility) + // A settings change (font, toolbar mode, theme) replaces this view and re-shows the window at once, so the + // first padding can be computed while the window is still settling. Check it again once the window is shown. + if (visibility == View.VISIBLE && !isFloatingMode()) post { + if (mAppliedNavPadding != navPadding()) { + updateBottomPadding() + requestApplyInsets() + } + } + } + override fun onDetachedFromWindow() { super.onDetachedFromWindow() LatinIME.getInstance()?.floatingKeyboardManager?.resetDragAndResizeState() @@ -380,10 +425,12 @@ class InputView @JvmOverloads constructor( } } +private const val TAG = "InputView" + /** - * The part of the navigation bar (the bottom [reportedInset] pixels of a window that is [windowHeight] high) that a - * view ending at [viewBottom] (in window coordinates) lies under. When the window already leaves room for the + * The part of the navigation bar (the bottom [reportedInset] pixels of a screen that is [screenHeight] high) that a + * view ending at [viewBottom] (in screen coordinates) lies under. When the window already leaves room for the * navigation bar, the view ends above it and nothing has to be reserved, whatever the reported inset is. */ -internal fun navBarOverlap(reportedInset: Int, viewBottom: Int, windowHeight: Int): Int = - (viewBottom - (windowHeight - reportedInset)).coerceIn(0, maxOf(reportedInset, 0)) +internal fun navBarOverlap(reportedInset: Int, viewBottom: Int, screenHeight: Int): Int = + (viewBottom - (screenHeight - reportedInset)).coerceIn(0, maxOf(reportedInset, 0)) diff --git a/app/src/test/java/helium314/keyboard/latin/NavBarOverlapTest.kt b/app/src/test/java/helium314/keyboard/latin/NavBarOverlapTest.kt index d6d3200df..d0b0ea605 100644 --- a/app/src/test/java/helium314/keyboard/latin/NavBarOverlapTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/NavBarOverlapTest.kt @@ -6,21 +6,27 @@ import kotlin.test.assertEquals class NavBarOverlapTest { @Test fun `view under the navigation bar reserves its full height`() { - // edge to edge: the view ends at the bottom of the window - assertEquals(144, navBarOverlap(reportedInset = 144, viewBottom = 2400, windowHeight = 2400)) + // edge to edge: the view ends at the bottom of the screen + assertEquals(144, navBarOverlap(reportedInset = 144, viewBottom = 2400, screenHeight = 2400)) } @Test fun `view above the navigation bar reserves nothing`() { // the window leaves room for the navigation bar itself, the reported inset must not be added again - assertEquals(0, navBarOverlap(reportedInset = 144, viewBottom = 2256, windowHeight = 2400)) + assertEquals(0, navBarOverlap(reportedInset = 144, viewBottom = 2256, screenHeight = 2400)) + } + + @Test fun `window that ends above the navigation bar reserves nothing`() { + // the keyboard window itself stops at the top of the navigation bar (Samsung, Android 12): the view ends at the + // bottom of its window, but on the screen it is above the navigation bar + assertEquals(0, navBarOverlap(reportedInset = 144, viewBottom = 2256, screenHeight = 2400)) } @Test fun `view partly under the navigation bar reserves the overlap`() { - assertEquals(44, navBarOverlap(reportedInset = 144, viewBottom = 2300, windowHeight = 2400)) + assertEquals(44, navBarOverlap(reportedInset = 144, viewBottom = 2300, screenHeight = 2400)) } @Test fun `never more than the reported inset or less than nothing`() { - assertEquals(48, navBarOverlap(reportedInset = 48, viewBottom = 2500, windowHeight = 2400)) - assertEquals(0, navBarOverlap(reportedInset = 0, viewBottom = 2400, windowHeight = 2400)) + assertEquals(48, navBarOverlap(reportedInset = 48, viewBottom = 2500, screenHeight = 2400)) + assertEquals(0, navBarOverlap(reportedInset = 0, viewBottom = 2400, screenHeight = 2400)) } } From 24bcf30745004bed98c91b6ee95a663aad1ee38d Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 16 Nov 2026 23:36:47 +0530 Subject: [PATCH 2/2] fix(layout): log which inset source the navigation bar padding came from The reported inset is either the IME's own navigation bar frame (Android 13+) or the system inset, and the two can differ (126px vs 63px on a test device). Log both and the SDK level so a report from a Samsung on Android 13 shows which one was used. --- app/src/main/java/helium314/keyboard/latin/InputView.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/InputView.kt b/app/src/main/java/helium314/keyboard/latin/InputView.kt index 6da116004..7f89990b5 100644 --- a/app/src/main/java/helium314/keyboard/latin/InputView.kt +++ b/app/src/main/java/helium314/keyboard/latin/InputView.kt @@ -112,16 +112,19 @@ class InputView @JvmOverloads constructor( val reported = reportedNavInset() val screenHeight = screenHeight() if (reported <= 0 || !isLaidOut || height == 0 || screenHeight == 0) { - mLastNavMeasure = "reported=$reported laidOut=$isLaidOut height=$height screen=$screenHeight" + mLastNavMeasure = "reported=$reported ${insetSources()} laidOut=$isLaidOut height=$height screen=$screenHeight" return reported } val location = IntArray(2) getLocationOnScreen(location) val viewBottom = location[1] + height - mLastNavMeasure = "reported=$reported viewBottom=$viewBottom screen=$screenHeight" + mLastNavMeasure = "reported=$reported ${insetSources()} viewBottom=$viewBottom screen=$screenHeight" return navBarOverlap(reported, viewBottom, screenHeight) } + /** where [reportedNavInset] can come from: the IME's own navigation bar frame (Android 13+) or the system inset */ + private fun insetSources() = "(imeNavFrame=${navigationBarFrameHeight()} systemInset=$mNavBarBottomInsets sdk=${Build.VERSION.SDK_INT})" + private fun screenHeight(): Int { val windowManager = context.getSystemService(WindowManager::class.java) ?: return 0 return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {