Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / lint-rules / src / main / java / com / vayunmathur / lint / WindowInsetsInReusableComponentDetector.kt
WindowInsetsInReusableComponentDetector.kt89 lines · 4.3 KiBRaw
| 1 | package com.vayunmathur.lint |
| 2 | |
| 3 | import com.android.tools.lint.detector.api.Category |
| 4 | import com.android.tools.lint.detector.api.Detector |
| 5 | import com.android.tools.lint.detector.api.Implementation |
| 6 | import com.android.tools.lint.detector.api.Issue |
| 7 | import com.android.tools.lint.detector.api.JavaContext |
| 8 | import com.android.tools.lint.detector.api.Scope |
| 9 | import com.android.tools.lint.detector.api.Severity |
| 10 | import com.android.tools.lint.detector.api.SourceCodeScanner |
| 11 | import com.intellij.psi.PsiMethod |
| 12 | import org.jetbrains.uast.UCallExpression |
| 13 | |
| 14 | /** |
| 15 | * Flags window-inset modifiers (`imePadding()`, `navigationBarsPadding()`, |
| 16 | * `systemBarsPadding()`) used in reusable components under the shared UI package |
| 17 | * `com.vayunmathur.library.ui`. |
| 18 | * |
| 19 | * These components are hosted in many different screens and do not know their |
| 20 | * host's inset ownership. The shared `Scaffold` wrapper in `:library:ui` is |
| 21 | * the single owner of the IME inset for every AppScaffold-family screen, |
| 22 | * hosted or standalone: it unions IME into the scaffold insets and consumes |
| 23 | * `navigationBars` union `ime` for its content, so an inset modifier in content |
| 24 | * is a no-op there and would count twice outside it - e.g. an editor toolbar |
| 25 | * floating a keyboard's height above the keyboard. Bottom-bar-slot and |
| 26 | * non-Scaffold overlay components still own their insets themselves. |
| 27 | * |
| 28 | * Let the scaffold own the inset. If a genuinely host-agnostic overlay must |
| 29 | * own its own inset, suppress this on the specific declaration with a |
| 30 | * justification. |
| 31 | */ |
| 32 | class WindowInsetsInReusableComponentDetector : Detector(), SourceCodeScanner { |
| 33 | |
| 34 | override fun getApplicableMethodNames(): List<String> = |
| 35 | listOf("imePadding", "navigationBarsPadding", "systemBarsPadding") |
| 36 | |
| 37 | override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { |
| 38 | if (!context.evaluator.isMemberInClass(method, WINDOW_INSETS_CLASS)) return |
| 39 | val packageName = context.uastFile?.packageName ?: return |
| 40 | if (!packageName.startsWith(REUSABLE_PACKAGE)) return |
| 41 | val name = node.methodName ?: return |
| 42 | context.report( |
| 43 | ISSUE, |
| 44 | node, |
| 45 | context.getLocation(node), |
| 46 | "`$name()` must not be called in a reusable `$REUSABLE_PACKAGE` component. " + |
| 47 | "The shared Scaffold wrapper owns window insets for its content " + |
| 48 | "(bottom-bar and non-Scaffold overlay components own theirs).", |
| 49 | ) |
| 50 | } |
| 51 | |
| 52 | companion object { |
| 53 | private const val REUSABLE_PACKAGE = "com.vayunmathur.library.ui" |
| 54 | |
| 55 | // All three are top-level extension functions on Modifier in this class. |
| 56 | private const val WINDOW_INSETS_CLASS = |
| 57 | "androidx.compose.foundation.layout.WindowInsetsPaddingKt" |
| 58 | |
| 59 | val ISSUE: Issue = Issue.create( |
| 60 | id = "WindowInsetsInReusableComponent", |
| 61 | briefDescription = "Window inset modifier in a reusable component", |
| 62 | explanation = """ |
| 63 | Reusable components under `com.vayunmathur.library.ui` are hosted in many \ |
| 64 | different screens and do not know their host's inset ownership, so they must \ |
| 65 | not apply window insets (`imePadding()`, `navigationBarsPadding()`, \ |
| 66 | `systemBarsPadding()`) themselves. |
| 67 | |
| 68 | The shared `Scaffold` wrapper in `:library:ui` is the single owner of \ |
| 69 | the IME inset for every AppScaffold-family screen, hosted or standalone: \ |
| 70 | it unions IME into the scaffold insets and consumes \ |
| 71 | `navigationBars` union `ime` for its content, so an inset modifier in \ |
| 72 | content is a no-op there and would count twice outside it (for example, \ |
| 73 | an editor toolbar floating a keyboard's height above the keyboard). \ |
| 74 | Bottom-bar-slot and non-Scaffold overlay components still own theirs. |
| 75 | |
| 76 | Let the scaffold own the inset. If a genuinely host-agnostic overlay must \ |
| 77 | own its own inset, suppress this issue on the specific declaration with a \ |
| 78 | justification. |
| 79 | """.trimIndent(), |
| 80 | category = Category.CORRECTNESS, |
| 81 | priority = 6, |
| 82 | severity = Severity.ERROR, |
| 83 | implementation = Implementation( |
| 84 | WindowInsetsInReusableComponentDetector::class.java, |
| 85 | Scope.JAVA_FILE_SCOPE, |
| 86 | ), |
| 87 | ) |
| 88 | } |
| 89 | } |