From b4e99adfb12e8c988024494108946e3ebd761b4c Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Fri, 4 Dec 2026 14:58:22 +0530 Subject: [PATCH] fix(insets): no bottom padding when the navigation bar is at a side (#1029) With 3 button navigation in landscape the keyboard window can still get a bottom inset, which showed as extra space below the keys. --- .../helium314/keyboard/latin/InputView.kt | 19 +++++++++++++++++-- .../keyboard/latin/NavBarOverlapTest.kt | 7 +++++++ 2 files changed, 24 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 5b4ab46c6..efb1a0e1a 100644 --- a/app/src/main/java/helium314/keyboard/latin/InputView.kt +++ b/app/src/main/java/helium314/keyboard/latin/InputView.kt @@ -8,6 +8,7 @@ package helium314.keyboard.latin import android.annotation.SuppressLint import android.content.Context +import android.graphics.Insets import android.graphics.Rect import android.util.AttributeSet import android.util.DisplayMetrics @@ -96,7 +97,15 @@ class InputView @JvmOverloads constructor( /** The navigation bar inset as reported, or the height of the IME's own navigation bar if that is taller */ private fun reportedNavInset(): Int = - navigationBarFrameHeight().takeIf { it > 0 } ?: if (mNavBarBottomInsets > 0) mNavBarBottomInsets else getNavInsets() + navigationBarFrameHeight().takeIf { it > 0 } + ?: if (screenNavInsets()?.let { isSideNavBar(it.left, it.right, it.bottom) } == true) 0 + else if (mNavBarBottomInsets > 0) mNavBarBottomInsets else getNavInsets() + + /** the navigation bar insets of the screen, independent of what is dispatched to this window */ + private fun screenNavInsets(): Insets? = + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) null + else context.getSystemService(WindowManager::class.java) + ?.maximumWindowMetrics?.windowInsets?.getInsets(WindowInsets.Type.navigationBars()) /** what [navPadding] was computed from, for the log when the padding changes */ private var mLastNavMeasure = "" @@ -130,7 +139,7 @@ class InputView @JvmOverloads constructor( } /** 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 insetSources() = "(imeNavFrame=${navigationBarFrameHeight()} systemInset=$mNavBarBottomInsets screenNav=${screenNavInsets()} sdk=${Build.VERSION.SDK_INT})" private fun screenHeight(): Int { val windowManager = context.getSystemService(WindowManager::class.java) ?: return 0 @@ -444,3 +453,9 @@ private const val TAG = "InputView" */ internal fun navBarOverlap(reportedInset: Int, viewBottom: Int, screenHeight: Int): Int = (viewBottom - (screenHeight - reportedInset)).coerceIn(0, maxOf(reportedInset, 0)) + +/** + * Whether the screen has its navigation bar at a side. Some devices (3 button navigation in landscape, #1029) still + * give the keyboard window a bottom inset then, which must not be padded for. + */ +internal fun isSideNavBar(left: Int, right: Int, bottom: Int): Boolean = bottom == 0 && (left > 0 || right > 0) diff --git a/app/src/test/java/helium314/keyboard/latin/NavBarOverlapTest.kt b/app/src/test/java/helium314/keyboard/latin/NavBarOverlapTest.kt index d0b0ea605..668dbd316 100644 --- a/app/src/test/java/helium314/keyboard/latin/NavBarOverlapTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/NavBarOverlapTest.kt @@ -29,4 +29,11 @@ class NavBarOverlapTest { assertEquals(48, navBarOverlap(reportedInset = 48, viewBottom = 2500, screenHeight = 2400)) assertEquals(0, navBarOverlap(reportedInset = 0, viewBottom = 2400, screenHeight = 2400)) } + + @Test fun `only a bar at the side makes a bottom inset bogus`() { + assertEquals(true, isSideNavBar(left = 0, right = 135, bottom = 0)) + assertEquals(true, isSideNavBar(left = 135, right = 0, bottom = 0)) + assertEquals(false, isSideNavBar(left = 0, right = 0, bottom = 135)) + assertEquals(false, isSideNavBar(left = 0, right = 0, bottom = 0)) // nothing known: keep the inset + } }