Fix doubled imePadding: single-owner IME rule + lint
- Commit
e95ed8bbe71d720e8e53718f826fc4ef5bf8eb2e- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-16 01:58 UTC (3 months ago)
- Parents
0d9a7ef
MainNavigation is the single owner of the IME inset for hosted screens. Remove imePadding() from the shared EditorBottomBar (library/ui) so MainNavigation-hosted editors (contacts note toolbar, etc.) no longer get it twice. Drop now-unused imePadding imports. Add WindowInsetsInReusableComponent lint (WARNING) flagging imePadding/navigationBarsPadding/systemBarsPadding in com.vayunmathur.library.ui; register in ModernAppsIssueRegistry. Suppress on HistoryScrubberCard (a host-agnostic floating overlay that owns its own nav-bar inset). Document the single-owner rule on MainNavigation.
Diff
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 0902269d5d..8b9cdabd97 100644--- a/library/src/main/java/com/vayunmathur/library/util/Navigation.kt+++ b/library/src/main/java/com/vayunmathur/library/util/Navigation.kt@@ -112,6 +112,11 @@ class EntryProviderScope<T: NavKey>(val obj: T) { } } +/**+ * 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.+ */ @OptIn(ExperimentalMaterial3AdaptiveApi::class) @Composable fun <T: NavKey> MainNavigation(backStack: NavBackStack<T>, bottomBar: @Composable () -> Unit = {}, entryProvider: EntryProviderScope<T>.() -> Unit) {
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 eaf7553753..3f6d04b79c 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@@ -5,7 +5,6 @@ import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.RowScope import androidx.compose.foundation.layout.fillMaxWidth-import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.rememberScrollState@@ -183,9 +182,13 @@ fun EditorBaseButtons(formatter: EditorFormatter) { } /**- * Shared bottom-bar container for editor toolbars: a tonal [Surface] with an- * [imePadding]-ed [Row]. Place [EditorBaseButtons] plus any editor-specific- * extras inside [content]. Set [scrollable] for wide toolbars (e.g. office).+ * Shared bottom-bar container for editor toolbars: a tonal [Surface] wrapping a+ * [Row]. Place [EditorBaseButtons] plus any editor-specific extras inside+ * [content]. Set [scrollable] for wide toolbars (e.g. office).+ *+ * Does NOT apply window insets: as a reusable component it does not know its+ * host's inset ownership. The host (MainNavigation, or a standalone root+ * Scaffold) owns imePadding and keeps this bar above the keyboard. */ @Composable fun EditorBottomBar(@@ -198,7 +201,6 @@ fun EditorBottomBar( modifier = Modifier .fillMaxWidth() .then(if (scrollable) Modifier.horizontalScroll(rememberScrollState()) else Modifier)- .imePadding() .padding(horizontal = 4.dp, vertical = 4.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = if (scrollable) Arrangement.Start else Arrangement.SpaceEvenly,
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 dfd6b02d0e..f09863bcb5 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@@ -133,6 +133,9 @@ fun RowScope.HistoryStepButton(label: String, onClick: () -> Unit) { } @OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class, 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).+@Suppress("WindowInsetsInReusableComponent") @Composable fun BoxScope.HistoryScrubberCard( state: HistoryScrubberState,
library/ui/src/main/java/com/vayunmathur/library/ui/MarkdownEditor.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/MarkdownEditor.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/MarkdownEditor.ktindex 90c3f69a55..647743c282 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/MarkdownEditor.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/MarkdownEditor.kt@@ -7,7 +7,6 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.ExperimentalLayoutApi import androidx.compose.foundation.layout.FlowRow import androidx.compose.foundation.layout.fillMaxWidth-import androidx.compose.foundation.layout.imePadding import androidx.compose.foundation.layout.padding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicTextField
lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.kt
diff --git a/lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.kt b/lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.ktindex 5e773cd706..3de6b8b06b 100644--- a/lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.kt+++ b/lint-rules/src/main/java/com/vayunmathur/lint/ModernAppsIssueRegistry.kt@@ -13,6 +13,7 @@ class ModernAppsIssueRegistry : IssueRegistry() { DirectBuildDatabaseDetector.ISSUE, OneComposablePerFileDetector.ISSUE, PackageStructureDetector.ISSUE,+ WindowInsetsInReusableComponentDetector.ISSUE, ) override val api: Int = CURRENT_API
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.ktnew file mode 100644index 0000000000..524a0784d2--- /dev/null+++ b/lint-rules/src/main/java/com/vayunmathur/lint/WindowInsetsInReusableComponentDetector.kt@@ -0,0 +1,82 @@+package com.vayunmathur.lint++import com.android.tools.lint.detector.api.Category+import com.android.tools.lint.detector.api.Detector+import com.android.tools.lint.detector.api.Implementation+import com.android.tools.lint.detector.api.Issue+import com.android.tools.lint.detector.api.JavaContext+import com.android.tools.lint.detector.api.Scope+import com.android.tools.lint.detector.api.Severity+import com.android.tools.lint.detector.api.SourceCodeScanner+import com.intellij.psi.PsiMethod+import org.jetbrains.uast.UCallExpression++/**+ * Flags window-inset modifiers (`imePadding()`, `navigationBarsPadding()`,+ * `systemBarsPadding()`) used in reusable components under the shared UI package+ * `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.+ *+ * 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.+ */+class WindowInsetsInReusableComponentDetector : Detector(), SourceCodeScanner {++ override fun getApplicableMethodNames(): List<String> =+ listOf("imePadding", "navigationBarsPadding", "systemBarsPadding")++ override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {+ if (!context.evaluator.isMemberInClass(method, WINDOW_INSETS_CLASS)) return+ val packageName = context.uastFile?.packageName ?: return+ if (!packageName.startsWith(REUSABLE_PACKAGE)) return+ val name = node.methodName ?: return+ context.report(+ ISSUE,+ 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.",+ )+ }++ companion object {+ private const val REUSABLE_PACKAGE = "com.vayunmathur.library.ui"++ // All three are top-level extension functions on Modifier in this class.+ private const val WINDOW_INSETS_CLASS =+ "androidx.compose.foundation.layout.WindowInsetsPaddingKt"++ val ISSUE: Issue = Issue.create(+ id = "WindowInsetsInReusableComponent",+ briefDescription = "Window inset modifier in a reusable component",+ explanation = """+ Reusable components under `com.vayunmathur.library.ui` are hosted in many \+ different screens and do not know their host's inset ownership, so they must \+ 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).++ 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.+ """.trimIndent(),+ category = Category.CORRECTNESS,+ priority = 6,+ severity = Severity.WARNING,+ implementation = Implementation(+ WindowInsetsInReusableComponentDetector::class.java,+ Scope.JAVA_FILE_SCOPE,+ ),+ )+ }+}