Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / lint-rules / src / main / java / com / vayunmathur / lint / WindowInsetsInReusableComponentDetector.kt

WindowInsetsInReusableComponentDetector.kt82 lines · 3.9 KiBRaw
1package com.vayunmathur.lint
2
3import com.android.tools.lint.detector.api.Category
4import com.android.tools.lint.detector.api.Detector
5import com.android.tools.lint.detector.api.Implementation
6import com.android.tools.lint.detector.api.Issue
7import com.android.tools.lint.detector.api.JavaContext
8import com.android.tools.lint.detector.api.Scope
9import com.android.tools.lint.detector.api.Severity
10import com.android.tools.lint.detector.api.SourceCodeScanner
11import com.intellij.psi.PsiMethod
12import 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. `MainNavigation` is the single owner of the IME inset
21 * for every screen it hosts (it applies `imePadding()` once), and standalone
22 * roots apply it exactly once at their own Scaffold. A reusable component that
23 * also applies an inset makes it count twice - e.g. an editor toolbar floating a
24 * keyboard's height above the keyboard.
25 *
26 * Let the host own the inset. If a genuinely host-agnostic overlay must own its
27 * own inset, suppress this on the specific declaration with a justification.
28 */
29class WindowInsetsInReusableComponentDetector : Detector(), SourceCodeScanner {
30
31 override fun getApplicableMethodNames(): List<String> =
32 listOf("imePadding", "navigationBarsPadding", "systemBarsPadding")
33
34 override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
35 if (!context.evaluator.isMemberInClass(method, WINDOW_INSETS_CLASS)) return
36 val packageName = context.uastFile?.packageName ?: return
37 if (!packageName.startsWith(REUSABLE_PACKAGE)) return
38 val name = node.methodName ?: return
39 context.report(
40 ISSUE,
41 node,
42 context.getLocation(node),
43 "`$name()` must not be called in a reusable `$REUSABLE_PACKAGE` component. " +
44 "The host (MainNavigation, or a standalone root Scaffold) owns window insets.",
45 )
46 }
47
48 companion object {
49 private const val REUSABLE_PACKAGE = "com.vayunmathur.library.ui"
50
51 // All three are top-level extension functions on Modifier in this class.
52 private const val WINDOW_INSETS_CLASS =
53 "androidx.compose.foundation.layout.WindowInsetsPaddingKt"
54
55 val ISSUE: Issue = Issue.create(
56 id = "WindowInsetsInReusableComponent",
57 briefDescription = "Window inset modifier in a reusable component",
58 explanation = """
59 Reusable components under `com.vayunmathur.library.ui` are hosted in many \
60 different screens and do not know their host's inset ownership, so they must \
61 not apply window insets (`imePadding()`, `navigationBarsPadding()`, \
62 `systemBarsPadding()`) themselves.
63
64 `MainNavigation` is the single owner of the IME inset for every screen it \
65 hosts - it applies `imePadding()` once - and standalone Activities apply it \
66 exactly once at their own root Scaffold. A reusable component that also applies \
67 an inset makes it count twice (for example, an editor toolbar floating a \
68 keyboard's height above the keyboard).
69
70 Let the host own the inset. If a genuinely host-agnostic overlay must own its \
71 own inset, suppress this issue on the specific declaration with a justification.
72 """.trimIndent(),
73 category = Category.CORRECTNESS,
74 priority = 6,
75 severity = Severity.WARNING,
76 implementation = Implementation(
77 WindowInsetsInReusableComponentDetector::class.java,
78 Scope.JAVA_FILE_SCOPE,
79 ),
80 )
81 }
82}