|
| 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 |
0 commit comments