ui: make scaffold own the IME inset in every context
- Commit
d3c186f3e6a1ae8a2057a813c19f9ba94bf4f0af- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 17:13 UTC (16 days ago)
- Parents
750dd9c
Material3 1.5 ScaffoldDefaults carries no IME inset, and this repo applied IME by context: under MainNavigation the shared Scaffold wrapper unioned WindowInsets.ime into contentWindowInsets, but standalone activities, previews and screenshot tests got plain M3 behaviour and had to remember imePadding() themselves - so the same screen was correct in one context and keyboard-covered or double-padded in the other. Docs and the lint detector still described an older single-owner model, too. The library/ui Scaffold funnel now unions IME unconditionally, so hosted == standalone == preview (the CompositionLocal now only gates the hosted global-snackbar injection), and it consumes navigationBars + ime for its content: a descendant imePadding() or navigationBarsPadding() becomes a no-op instead of double padding. Only the bottom insets are consumed, deliberately, so nested scaffolds keep their top-bar inset. The hosted snackbar now applies navigationBars union IME instead of summing them (the repo's own rule for BottomAppBar), which floated it a nav-bar height above the keyboard. files' TextEditor/DocumentPicker activities drop their scaffold- root imePadding() (the shell owns it now). Non-Scaffold surfaces (securemail compose, maps sheets/side panel) keep their direct imePadding() - that is the correct pattern there. Contracts in library Navigation, library/ui AGENTS.md and the WindowInsets lint detector now describe the actual model. Device verification still needed for keyboard animation on hosted, standalone and tabbed screens; previews without a keyboard are pixel-identical (IME inset 0). Verified: :library:ui:compileDebugKotlin, :library and :lint-rules compile, :files compile, :library:ui:testDebugUnitTest (1055 tests, 0 failures).
Diff
files/src/main/java/com/vayunmathur/files/data/saf/DocumentPickerActivity.kt
diff --git a/files/src/main/java/com/vayunmathur/files/data/saf/DocumentPickerActivity.kt b/files/src/main/java/com/vayunmathur/files/data/saf/DocumentPickerActivity.ktindex ffbc83e3be..983d08adab 100644--- a/files/src/main/java/com/vayunmathur/files/data/saf/DocumentPickerActivity.kt+++ b/files/src/main/java/com/vayunmathur/files/data/saf/DocumentPickerActivity.kt@@ -20,7 +20,6 @@ import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth-import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items@@ -154,7 +153,6 @@ class DocumentPickerActivity : ComponentActivity() { AppScaffold( title = titleFor(mode),- modifier = Modifier.imePadding(), onNavigateBack = { navUp() }, ) { padding -> Column(Modifier.fillMaxSize().padding(padding)) {
files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.kt
diff --git a/files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.kt b/files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.ktindex 3a7a532b8d..b07da42422 100644--- a/files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.kt+++ b/files/src/main/java/com/vayunmathur/files/ui/TextEditorActivity.kt@@ -8,7 +8,6 @@ import androidx.activity.viewModels import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize-import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.padding import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.text.BasicTextField@@ -68,7 +67,6 @@ private fun TextEditorScreen(uri: Uri, viewModel: TextEditorViewModel) { // here would overwrite the unread original with whatever is typed. AppScaffold( title = uri.lastPathSegment ?: stringResource(R.string.type_file),- modifier = Modifier.imePadding(), ) { paddingValues -> Text( stringResource(R.string.editor_load_failed),@@ -79,7 +77,6 @@ private fun TextEditorScreen(uri: Uri, viewModel: TextEditorViewModel) { // Loading: render the bar only so the screen isn't blank during async read. AppScaffold( title = uri.lastPathSegment ?: stringResource(R.string.type_file),- modifier = Modifier.imePadding(), ) { } } else { TextEditorLoaded(uri, content, viewModel)@@ -95,7 +92,6 @@ private fun TextEditorLoaded(uri: Uri, initialContent: String, viewModel: TextEd AppScaffold( title = uri.lastPathSegment ?: stringResource(R.string.type_file),- modifier = Modifier.imePadding(), actions = { IconButton(onClick = { if (isEditing && state.text.toString() != initialContent) {
library/src/main/java/com/vayunmathur/library/util/Navigation.kt
diff --git a/library/src/main/java/com/vayunmathur/library/util/Navigation.kt b/library/src/main/java/com/vayunmathur/library/util/Navigation.ktindex 354e4b0e8f..b1c8b5a128 100644--- a/library/src/main/java/com/vayunmathur/library/util/Navigation.kt+++ b/library/src/main/java/com/vayunmathur/library/util/Navigation.kt@@ -113,14 +113,16 @@ class NavBackStack<T: NavKey>(initial: Array<out T>) { val LocalSnackbarHostState = compositionLocalOf<SnackbarHostState?> { null } /**- * Whether the screen is hosted inside [MainNavigation], so inner scaffolds apply- * window insets exactly once.- *- * False (the default) means the screen is NOT hosted: previews, screenshot tests,- * and pages hosted directly by an Activity. Those keep current M3 behavior.- * True means hosted: the host owns the outer chrome, and the shared `Scaffold`- * wrapper in `:library:ui` unions IME into the insets M3 itself consumes — never- * padding outside a Scaffold.+ * Whether the screen is hosted inside [MainNavigation].+ *+ * IME ownership no longer depends on this: the shared `Scaffold` wrapper in+ * `:library:ui` always unions IME into the insets M3 itself consumes and+ * consumes them for its content, so hosted, standalone (previews, screenshot+ * tests, pages hosted directly by an Activity) and preview rendering all+ * behave identically — never padding outside a Scaffold.+ * True now gates only the hosted-only extra: the shared wrapper injects the+ * global snackbar (see [LocalSnackbarHostState]) when a hosted screen+ * supplied no explicit snackbar host. */ val LocalHostScaffoldInfo = staticCompositionLocalOf { false } @@ -387,9 +389,13 @@ fun Modifier.sharedContent(key: Any): Modifier { } /**- * Single owner of the IME (keyboard) inset for every screen it hosts: it applies- * [imePadding] once to all hosted content. Screens and reusable components rendered- * inside must NOT call [imePadding] themselves, or the inset is applied twice.+ * Host for every screen it hosts. It does NOT apply the IME (keyboard) inset+ * itself: the shared `Scaffold` wrapper in `:library:ui` is the single owner+ * of that inset for every AppScaffold-family screen (hosted or standalone —+ * it unions IME into the scaffold insets and consumes them for its content).+ * Screens and reusable components rendered inside must NOT call `imePadding`+ * themselves; under a scaffold it is now a consumed no-op, and outside one it+ * would double against the scaffold padding. * * [containerColor] is [Color.Unspecified] by default, which leaves the scaffold's own * opaque `colorScheme.background`. Pass [Color.Transparent] when something behind the@@ -432,10 +438,11 @@ fun <T : NavKey> MainNavigation( // screen's inner Scaffold consumed insets again, double-lifting bottom bars by // a keyboard height (live probe: bottomBar laid out at H-2K). Instead the host // is a plain Box that marks screens hosted via LocalHostScaffoldInfo, and the- // shared Scaffold wrapper in :library:ui unions IME into the insets M3 itself- // consumes — exactly once, never padded outside a Scaffold.+ // shared Scaffold wrapper in :library:ui always unions IME into the insets M3+ // itself consumes and consumes them for its content — exactly once, hosted+ // or standalone, never padded outside a Scaffold. // The snackbar renderer lives there too (default host when the screen supplies- // none); this function keeps only the producer (state + AppMessages drain).+ // none, hosted only); this function keeps only the producer (state + drain). NavHostChrome( resultRegistry = resultRegistry, snackbarHostState = snackbarHostState,
library/ui/AGENTS.md
diff --git a/library/ui/AGENTS.md b/library/ui/AGENTS.mdindex 5eced9c5ed..e11a26ce29 100644--- a/library/ui/AGENTS.md+++ b/library/ui/AGENTS.md@@ -21,7 +21,7 @@ **Entry points + reading order:** `MaActivity.kt` → `AppScaffold.kt` → `Icons.kt` → `Messages.kt` → `DynamicTheme.kt`, then the area you need. **Gotchas:** - One public `@Composable` per file, named as the file; closed package roots still apply inside this library-- Never `imePadding()`/`navigationBarsPadding()`/`systemBarsPadding()` in this library+- Never `imePadding()`/`navigationBarsPadding()`/`systemBarsPadding()` in this library's reusable components: the shared `Scaffold` wrapper owns IME (unconditional `contentWindowInsets ∪ ime`, consumed as `navigationBars ∪ ime` for content, so a stray inset modifier under an AppScaffold-family screen is a no-op). Exceptions that still own their insets: `bottomBar`-slot bars (`EditorBottomBar`, `BottomAppBar` — outside consumed content) and non-Scaffold overlays (`HistoryScrubberCard` — nav-bars only, suppressed with justification) - Connectivity UI (`OfflineAware`/`rememberIsOnline`) lives in `:library:network`, not here (#884) - `MaFragmentActivity(autoSetContent=false)` + `showContent()` is the biometric-gate pattern (passwords, photos)
library/ui/src/main/java/com/vayunmathur/library/ui/EditorFormatter.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/EditorFormatter.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/EditorFormatter.ktindex 2a33c46c5b..e7b8fea933 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/EditorFormatter.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/EditorFormatter.kt@@ -208,6 +208,12 @@ fun EditorBaseButtons(formatter: EditorFormatter) { * already covers the navigation bar (same fix as `BottomNavBar` in `:library`). * Not a [BottomAppBar]: the grid needs wrap-content height, and M3 fixes that * bar to a single container height.+ *+ * Deliberately NOT removed by the Scaffold-owns-IME fix: this is used in the+ * `bottomBar` slot (notes, email), which sits outside the Scaffold content+ * wrapper that consumes insets — a bottom bar must still claim the union+ * itself, exactly like [BottomAppBar]. Using this inside Scaffold *content*+ * instead would now be a no-op (content consumes IME), not double-padding. */ @OptIn(ExperimentalLayoutApi::class) @Composable
library/ui/src/main/java/com/vayunmathur/library/ui/HistoryScrubber.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/HistoryScrubber.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/HistoryScrubber.ktindex 020d8fff0b..9f5cf0f2e6 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/HistoryScrubber.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/HistoryScrubber.kt@@ -134,7 +134,13 @@ fun RowScope.HistoryStepButton(label: String, onClick: () -> Unit) { @OptIn(ExperimentalTime::class) // Floating overlay card with no host bar to defer to: it owns its own nav-bar-// inset. Exempt from WindowInsetsInReusableComponent (not a keyboard-tracking bar).+// inset. Exempt from WindowInsetsInReusableComponent (not a keyboard-tracking+// bar). Deliberately NOT removed by the Scaffold-owns-IME fix: callers host+// this over a full-screen map (Astronomy SkyMap, FindFamily MainPage), not as+// Scaffold content applying the scaffold PaddingValues, and it claims only+// navigationBars — never IME. When it ever is placed under Scaffold content,+// the shared Scaffold wrapper has consumed that inset, so this becomes a+// harmless no-op instead of double-padding. @Suppress("WindowInsetsInReusableComponent") @Composable fun BoxScope.HistoryScrubberCard(
library/ui/src/main/java/com/vayunmathur/library/ui/MaterialComponents.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/MaterialComponents.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/MaterialComponents.ktindex 9366a7162c..084a7c43fb 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/MaterialComponents.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/MaterialComponents.kt@@ -14,8 +14,10 @@ import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.asPaddingValues+import androidx.compose.foundation.layout.consumeWindowInsets+import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.ime-import androidx.compose.foundation.layout.navigationBarsPadding+import androidx.compose.foundation.layout.navigationBars import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.union import androidx.compose.foundation.text.KeyboardActions@@ -285,14 +287,31 @@ fun Surface( // --- Scaffold --- //-// Hosted-mode IME ownership: screens under MainNavigation must apply the keyboard-// inset exactly once. MainNavigation no longer lifts content itself; instead it-// publishes LocalHostScaffoldInfo, and this funnel — the single path every-// AppScaffold screen goes through — unions IME into the insets M3 itself consumes,-// so bars, FAB, snackbar and content padding are all positioned consistently by-// M3, exactly once. Nothing is ever padded outside a Scaffold. When the local is-// absent (previews, screenshot tests, directly-hosted pages) behavior is-// byte-identical to before.+// IME ownership is unconditional: this funnel — the single path every+// AppScaffold-family screen goes through — always unions IME into the insets+// M3 itself consumes, so bars, FAB, snackbar and content padding are all+// positioned consistently by M3, exactly once, whether the screen is hosted+// by MainNavigation, standalone in its own Activity, or in a preview /+// screenshot test. Hosted == standalone == preview; nothing is ever padded+// outside a Scaffold for the keyboard.+//+// The content is additionally wrapped so the bottom insets M3 has already+// folded into the content PaddingValues are consumed: a descendant+// `imePadding()` (or `navigationBarsPadding()`) becomes a no-op instead of+// double-padding. Only `navigationBars ∪ ime` is consumed — deliberately not+// the full PaddingValues — so a nested scaffold's top bar can still claim+// the status-bar inset the outer scaffold forwarded past (TabbedPagerScaffold+// relies on inner pages owning their top bar). Callers still apply the+// PaddingValues themselves — as `Modifier.padding(pad)` or as a LazyList+// `contentPadding` — which is unaffected: consumption only stops live inset+// modifiers in descendants from re-reading insets the scaffold already+// accounted for. Slots (topBar / bottomBar / FAB / snackbarHost) sit outside+// that wrapper: components there (e.g. a bottom bar claiming+// `navigationBars ∪ ime` itself) keep owning their own insets.+//+// LocalHostScaffoldInfo now gates only the hosted-only extra: injecting the+// global snackbar (from LocalSnackbarHostState, published by MainNavigation)+// when a hosted screen supplied no explicit host. @Composable fun Scaffold( modifier: Modifier = Modifier,@@ -307,26 +326,22 @@ fun Scaffold( content: @Composable (PaddingValues) -> Unit, ) { val hosted = LocalHostScaffoldInfo.current- if (!hosted) {- androidx.compose.material3.Scaffold(- modifier = modifier, topBar = topBar, bottomBar = bottomBar,- snackbarHost = snackbarHost ?: {},- floatingActionButton = floatingActionButton, floatingActionButtonPosition = floatingActionButtonPosition,- containerColor = containerColor, contentColor = contentColor, contentWindowInsets = contentWindowInsets,- content = content,- )- return- }- // Hosted: render the global snackbar when the screen supplied no explicit host.- val globalSnackbarState: SnackbarHostState? = LocalSnackbarHostState.current+ val effectiveInsets = contentWindowInsets.union(WindowInsets.ime)+ // Hosted only: render the global snackbar when the screen supplied none.+ val globalSnackbarState: SnackbarHostState? =+ if (hosted) LocalSnackbarHostState.current else null val resolvedSnackbarHost: @Composable () -> Unit = snackbarHost ?: globalSnackbarState?.let { state -> { SnackbarHost( state,- modifier = Modifier- .navigationBarsPadding()- .padding(WindowInsets.ime.asPaddingValues()),+ // Union, not sum: a visible keyboard already covers the+ // navigation bar (same rule as BottomAppBar in+ // MaterialComponentsExtra.kt). Summing would float the+ // snackbar a nav-bar's height above the keys.+ modifier = Modifier.padding(+ WindowInsets.navigationBars.union(WindowInsets.ime).asPaddingValues(),+ ), ) } } ?: {}@@ -335,9 +350,16 @@ fun Scaffold( snackbarHost = resolvedSnackbarHost, floatingActionButton = floatingActionButton, floatingActionButtonPosition = floatingActionButtonPosition, containerColor = containerColor, contentColor = contentColor,- contentWindowInsets = contentWindowInsets.union(WindowInsets.ime),- content = content,- )+ contentWindowInsets = effectiveInsets,+ ) { padding ->+ Box(+ Modifier+ .fillMaxSize()+ .consumeWindowInsets(WindowInsets.navigationBars.union(WindowInsets.ime)),+ ) {+ content(padding)+ }+ } } // --- Top app bars ---
lint-rules/src/main/java/com/vayunmathur/lint/WindowInsetsInReusableComponentDetector.kt
diff --git a/lint-rules/src/main/java/com/vayunmathur/lint/WindowInsetsInReusableComponentDetector.kt b/lint-rules/src/main/java/com/vayunmathur/lint/WindowInsetsInReusableComponentDetector.ktindex 8ce9c47abe..6de8afb02d 100644--- a/lint-rules/src/main/java/com/vayunmathur/lint/WindowInsetsInReusableComponentDetector.kt+++ b/lint-rules/src/main/java/com/vayunmathur/lint/WindowInsetsInReusableComponentDetector.kt@@ -17,14 +17,17 @@ import org.jetbrains.uast.UCallExpression * `com.vayunmathur.library.ui`. * * These components are hosted in many different screens and do not know their- * host's inset ownership. `MainNavigation` is the single owner of the IME inset- * for every screen it hosts (it applies `imePadding()` once), and standalone- * roots apply it exactly once at their own Scaffold. A reusable component that- * also applies an inset makes it count twice - e.g. an editor toolbar floating a- * keyboard's height above the keyboard.+ * host's inset ownership. The shared `Scaffold` wrapper in `:library:ui` is+ * the single owner of the IME inset for every AppScaffold-family screen,+ * hosted or standalone: it unions IME into the scaffold insets and consumes+ * `navigationBars` union `ime` for its content, so an inset modifier in content+ * is a no-op there and would count twice outside it - e.g. an editor toolbar+ * floating a keyboard's height above the keyboard. Bottom-bar-slot and+ * non-Scaffold overlay components still own their insets themselves. *- * Let the host own the inset. If a genuinely host-agnostic overlay must own its- * own inset, suppress this on the specific declaration with a justification.+ * Let the scaffold own the inset. If a genuinely host-agnostic overlay must+ * own its own inset, suppress this on the specific declaration with a+ * justification. */ class WindowInsetsInReusableComponentDetector : Detector(), SourceCodeScanner { @@ -41,7 +44,8 @@ class WindowInsetsInReusableComponentDetector : Detector(), SourceCodeScanner { node, context.getLocation(node), "`$name()` must not be called in a reusable `$REUSABLE_PACKAGE` component. " +- "The host (MainNavigation, or a standalone root Scaffold) owns window insets.",+ "The shared Scaffold wrapper owns window insets for its content " ++ "(bottom-bar and non-Scaffold overlay components own theirs).", ) } @@ -61,14 +65,17 @@ class WindowInsetsInReusableComponentDetector : Detector(), SourceCodeScanner { not apply window insets (`imePadding()`, `navigationBarsPadding()`, \ `systemBarsPadding()`) themselves. - `MainNavigation` is the single owner of the IME inset for every screen it \- hosts - it applies `imePadding()` once - and standalone Activities apply it \- exactly once at their own root Scaffold. A reusable component that also applies \- an inset makes it count twice (for example, an editor toolbar floating a \- keyboard's height above the keyboard).+ The shared `Scaffold` wrapper in `:library:ui` is the single owner of \+ the IME inset for every AppScaffold-family screen, hosted or standalone: \+ it unions IME into the scaffold insets and consumes \+ `navigationBars` union `ime` for its content, so an inset modifier in \+ content is a no-op there and would count twice outside it (for example, \+ an editor toolbar floating a keyboard's height above the keyboard). \+ Bottom-bar-slot and non-Scaffold overlay components still own theirs. - Let the host own the inset. If a genuinely host-agnostic overlay must own its \- own inset, suppress this issue on the specific declaration with a justification.+ Let the scaffold own the inset. If a genuinely host-agnostic overlay must \+ own its own inset, suppress this issue on the specific declaration with a \+ justification. """.trimIndent(), category = Category.CORRECTNESS, priority = 6,