Resync keyboard insets after a back-gesture dismissal (#177)

This commit is contained in:
makiolaj committed 2026-11-25 12:09:15 +01:00
1 parent 8cdf3ad709
commit 61324a0072
4 files changed
+87 -1

No files matched your search

+5
View File
@@ -59,6 +59,11 @@ All notable changes to this project are documented here. The format follows
all-day reminder time. With a tasks app as the store, both ends gain or
drop their time together (#101).
### Fixed
- Closing the keyboard with the back gesture no longer leaves the task lists
squeezed into the top half of the screen, as if the keyboard were still open,
until it was opened again (#177).
## [1.3.2] - 2026-11-13
### Added
@@ -0,0 +1,79 @@
package de.jeanlucmakiola.agendula.ui
import android.graphics.Insets
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsAnimation
import androidx.compose.foundation.layout.WindowInsets as ComposeInsets
import androidx.compose.foundation.layout.ime
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SdkSuppress
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.floret.components.ImeInsetsResync
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
/**
* A keyboard hidden by a predictive back gesture runs an insets animation without a
* duration; Compose then keeps the last keyboard height it saw (#177).
*/
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 30)
class ImeInsetsResyncTest {
@get:Rule
val rule = createComposeRule()
private var imeBottom = -1
private lateinit var view: View
@Test
fun keyboardInsetsSettleAfterUserDrivenAnimation() {
setContent(resync = true)
runUserDrivenKeyboardAnimation()
rule.waitUntil(TIMEOUT_MS) { imeBottom == 0 }
assertThat(imeBottom).isEqualTo(0)
}
/** Shows the Compose behaviour [ImeInsetsResync] works around; drop both once this fails. */
@Test
fun withoutResyncKeyboardInsetsStayStale() {
setContent(resync = false)
runUserDrivenKeyboardAnimation()
rule.waitForIdle()
assertThat(imeBottom).isEqualTo(KEYBOARD_PX)
}
private fun setContent(resync: Boolean) {
rule.setContent {
if (resync) ImeInsetsResync()
view = LocalView.current
imeBottom = ComposeInsets.ime.getBottom(LocalDensity.current)
}
rule.waitForIdle()
}
private fun runUserDrivenKeyboardAnimation() {
rule.runOnUiThread {
val root = view.rootView
val ime = WindowInsets.Type.ime()
val animation = WindowInsetsAnimation(ime, null, -1)
val keyboard = Insets.of(0, 0, 0, KEYBOARD_PX)
val midway = WindowInsets.Builder().setInsets(ime, keyboard).setVisible(ime, true).build()
root.dispatchWindowInsetsAnimationPrepare(animation)
root.dispatchWindowInsetsAnimationStart(animation, WindowInsetsAnimation.Bounds(Insets.NONE, keyboard))
root.dispatchWindowInsetsAnimationProgress(midway, listOf(animation))
root.dispatchWindowInsetsAnimationEnd(animation)
}
rule.waitForIdle()
}
private companion object {
const val KEYBOARD_PX = 500
const val TIMEOUT_MS = 2_000L
}
}
@@ -36,6 +36,7 @@ import de.jeanlucmakiola.agendula.ui.navigation.NavRequest
import de.jeanlucmakiola.agendula.ui.settings.SettingsViewModel
import de.jeanlucmakiola.agendula.ui.theme.AgendulaTheme
import de.jeanlucmakiola.agendula.ui.theme.SystemTheme
import de.jeanlucmakiola.floret.components.ImeInsetsResync
import de.jeanlucmakiola.floret.crash.CrashReportDialog
import de.jeanlucmakiola.floret.crash.CrashReporter
import de.jeanlucmakiola.floret.crash.submitCrashReport
@@ -99,6 +100,7 @@ class MainActivity : ComponentActivity() {
launchHooks.forEach { hook -> lifecycleScope.launch { runCatching { hook.onLaunch(intent) } } }
}
setContent {
ImeInsetsResync()
val settingsViewModel: SettingsViewModel = hiltViewModel()
val ui by settingsViewModel.state.collectAsStateWithLifecycle()
val darkTheme = ui.settings.themeMode.isDark(isSystemInDarkTheme())