-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTouchTargetRule.kt
More file actions
41 lines (35 loc) · 1.58 KB
/
Copy pathTouchTargetRule.kt
File metadata and controls
41 lines (35 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.composea11yscanner.rules
import com.composea11yscanner.core.model.A11yIssue
import com.composea11yscanner.core.model.A11yNode
import com.composea11yscanner.core.model.A11ySeverity
import com.composea11yscanner.core.rule.BaseA11yRule
class TouchTargetRule(
private val minTouchTargetDp: Int = 48,
) : BaseA11yRule() {
override val ruleId = "touch-target-size"
override val ruleName = "Touch Target Size"
override val severity = A11ySeverity.Error
override val wcagReference = "WCAG 2.5.5 Target Size (Level AA)"
override fun check(node: A11yNode): A11yIssue? {
if (node.isMergedDescendant) return null
if (!node.isTouchTarget) return null
val w = node.touchTargetSize.width
val h = node.touchTargetSize.height
// Pixel bounds converted back to dp can land infinitesimally below an exact dp value
// (for example, a 48 dp target may be reported as 47.999996 dp).
if (
w + MeasurementToleranceDp >= minTouchTargetDp &&
h + MeasurementToleranceDp >= minTouchTargetDp
) return null
return issue(
node = node,
message = "Touch target is ${"%.0f".format(w)}x${"%.0f".format(h)}dp. " +
"Minimum required is ${minTouchTargetDp}x${minTouchTargetDp}dp.",
howToFix = "Apply Modifier.minimumInteractiveComponentSize() or add padding so the " +
"composable reaches at least ${minTouchTargetDp}dp in both dimensions.",
)
}
private companion object {
const val MeasurementToleranceDp = 0.01f
}
}