Skip to content

Commit a56a329

Browse files
committed
feat(compose): add MaterialTheme.dimensions spacing tokens
1 parent 9027fe8 commit a56a329

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
// SPDX-FileCopyrightText: 2026 Ashish Yadav <mailtoashish693@gmail.com>
3+
4+
package com.ichi2.compose.theme
5+
6+
import androidx.compose.material3.MaterialTheme
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.runtime.Immutable
9+
import androidx.compose.runtime.ReadOnlyComposable
10+
import androidx.compose.runtime.staticCompositionLocalOf
11+
import androidx.compose.ui.unit.Dp
12+
import androidx.compose.ui.unit.dp
13+
14+
/**
15+
* Semantic spacing tokens for Compose screens, defined by intent rather than exact pixels.
16+
* * Access these via [MaterialTheme.dimensions] to keep spacing consistent with
17+
* Material 3 colors and typography (e.g., `MaterialTheme.dimensions.screenEdge`).
18+
*
19+
* @property screenEdge Outer padding between content and the device edge.
20+
* @property sectionGap Gap between independent UI sections.
21+
* @property itemGap Gap between related items in a row or column.
22+
* @property tightGap Fine-grained spacing inside compound elements.
23+
* @property microGap Sub-element spacing (e.g., an eyebrow text above a title).
24+
*/
25+
@Immutable
26+
data class Dimensions(
27+
val screenEdge: Dp = 24.dp,
28+
val sectionGap: Dp = 16.dp,
29+
val itemGap: Dp = 12.dp,
30+
val tightGap: Dp = 8.dp,
31+
val microGap: Dp = 4.dp,
32+
)
33+
34+
/**
35+
* Provides the active [Dimensions] down the Compose tree.
36+
* * For cleaner code, read from [MaterialTheme.dimensions] instead of using this directly.
37+
* You only need to use this directly when overriding tokens via `CompositionLocalProvider`.
38+
*/
39+
val LocalDimensions = staticCompositionLocalOf { Dimensions() }
40+
41+
/**
42+
* A handy extension to access spacing tokens the exact same way you access
43+
* [MaterialTheme.colorScheme] or [MaterialTheme.typography].
44+
*/
45+
val MaterialTheme.dimensions: Dimensions
46+
@Composable
47+
@ReadOnlyComposable
48+
get() = LocalDimensions.current

AnkiDroid/src/main/java/com/ichi2/compose/theme/Theme.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import androidx.compose.material3.MaterialTheme
1212
import androidx.compose.material3.darkColorScheme
1313
import androidx.compose.material3.lightColorScheme
1414
import androidx.compose.runtime.Composable
15+
import androidx.compose.runtime.CompositionLocalProvider
1516
import androidx.compose.runtime.remember
1617
import androidx.compose.ui.graphics.Color
1718
import androidx.compose.ui.graphics.toArgb
@@ -36,7 +37,9 @@ import com.ichi2.anki.R
3637
fun Theme(content: @Composable () -> Unit) {
3738
val context = LocalContext.current
3839
val colorScheme = remember(context) { context.toMaterial3ColorScheme() }
39-
MaterialTheme(colorScheme = colorScheme, content = content)
40+
CompositionLocalProvider(LocalDimensions provides Dimensions()) {
41+
MaterialTheme(colorScheme = colorScheme, content = content)
42+
}
4043
}
4144

4245
@VisibleForTesting

0 commit comments

Comments
 (0)