Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions app/src/main/java/helium314/keyboard/latin/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class InputView @JvmOverloads constructor(
private var mMoreSuggestionsViewCanceler: MoreSuggestionsViewCanceler? = null
private var mActiveForwarder: MotionEventForwarder<*, *>? = null
private var mNavBarBottomInsets = 0
/** the bottom padding for the navigation bar that was applied last, see [navBarOverlap] */
private var mAppliedNavPadding = -1

private fun isFloatingMode(): Boolean = FloatingKeyboardManager.isFloatingMode(context)

Expand Down Expand Up @@ -78,7 +80,31 @@ class InputView @JvmOverloads constructor(
return insets
}
updateBottomPadding()
return super.onApplyWindowInsets(insets)
return withBottomInset(super.onApplyWindowInsets(insets), mAppliedNavPadding)
}

/**
* The children fit the system windows, so they pad themselves with the bottom inset they get. Some devices
* (e.g. Samsung on Android 12) report the navigation bar inset although the window already leaves room for it,
* which doubled the space below the keys. Give them what the keyboard really overlaps instead.
*/
private fun withBottomInset(insets: WindowInsets, bottom: Int): WindowInsets {
@Suppress("DEPRECATION")
return if (bottom < 0 || insets.systemWindowInsetBottom == bottom) insets
else insets.replaceSystemWindowInsets(insets.systemWindowInsetLeft, insets.systemWindowInsetTop, insets.systemWindowInsetRight, bottom)
}

/** 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()

/** Bottom padding needed for the navigation bar; known only once this view is laid out, until then the reported inset is used */
private fun navPadding(): Int {
val reported = reportedNavInset()
if (reported <= 0 || !isLaidOut || height == 0 || rootView.height == 0) return reported
val location = IntArray(2)
getLocationInWindow(location)
return navBarOverlap(reported, location[1] + height, rootView.height)
}

fun resetChildrenFloatingPadding() {
Expand Down Expand Up @@ -134,8 +160,8 @@ class InputView @JvmOverloads constructor(
R.id.clipboard_history_view,
R.id.touchpad_view
)
val insets = navigationBarFrameHeight().takeIf { it > 0 }
?: if (mNavBarBottomInsets > 0) mNavBarBottomInsets else getNavInsets()
val insets = navPadding()
mAppliedNavPadding = insets
val wrapper = findViewById<View>(R.id.keyboard_view_wrapper)
val showToolbarOnly = wrapper?.visibility != View.VISIBLE
val bottomPadding = if (showToolbarOnly) insets else 0
Expand Down Expand Up @@ -173,6 +199,13 @@ class InputView @JvmOverloads constructor(
Settings.getValues().mColors.setBackground(frame, ColorType.MAIN_BACKGROUND)
}
updateBottomPadding()
// where this view ends in the window is known after layout: correct the padding if it is not what is needed
addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
if (!isFloatingMode() && mAppliedNavPadding != navPadding()) post {
updateBottomPadding()
requestApplyInsets()
}
}
// Work around inset application being unreliable
post { requestApplyInsets() }
}
Expand Down Expand Up @@ -348,3 +381,11 @@ class InputView @JvmOverloads constructor(
}
}
}

/**
* 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
* 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))
26 changes: 26 additions & 0 deletions app/src/test/java/helium314/keyboard/latin/NavBarOverlapTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.latin

import kotlin.test.Test
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))
}

@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))
}

@Test fun `view partly under the navigation bar reserves the overlap`() {
assertEquals(44, navBarOverlap(reportedInset = 144, viewBottom = 2300, windowHeight = 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))
}
}
Loading