@momo-kits/native-kits 0.169.13-beta.3 → 0.169.13-beta.3-debug
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build.gradle.kts +11 -0
- package/compose/build.gradle.kts +202 -0
- package/compose/compose.podspec +47 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/const/HostFont.android.kt +19 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/navigation/ScrollToTop.android.kt +6 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/BackGesture.android.kt +12 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/OsFontScale.android.kt +7 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/Platform.android.kt +154 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReduceMotion.android.kt +17 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReservedRegions.android.kt +6 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiActionsTest.kt +647 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiExprTest.kt +1411 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiHostTest.kt +130 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiKitArgsTest.kt +524 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiKitNodesTest.kt +559 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiRenderPlanTest.kt +741 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiRenderReuseTest.kt +221 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiRendererPropsTest.kt +270 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiStylePlanTest.kt +849 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiStyleTest.kt +212 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiTrackingTest.kt +239 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiViewTest.kt +252 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/Context.kt +110 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/DesignSystemKits.kt +61 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/LiteScreen.kt +1067 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/Localize.kt +268 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/WidgetContainer.kt +56 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Avatar.kt +157 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Badge.kt +81 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeDot.kt +32 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeRibbon.kt +338 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/BaselineView.kt +175 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Button.kt +354 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Carousel.kt +115 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/CheckBox.kt +89 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Chip.kt +130 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Collapse.kt +209 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/CupertinoOverscroll.kt +482 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Divider.kt +49 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Icon.kt +86 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/IconButton.kt +125 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Image.kt +224 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Information.kt +137 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Input.kt +473 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputDropDown.kt +113 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputMoney.kt +262 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputOTP.kt +237 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputPhoneNumber.kt +228 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputSearch.kt +280 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputTextArea.kt +183 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/LazyColumnWithBouncing.kt +342 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Loader.kt +108 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationDot.kt +56 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationNumber.kt +40 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationScroll.kt +82 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationWhiteDot.kt +39 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupNotify.kt +362 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupPromotion.kt +108 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/ProgressInfo.kt +304 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Radio.kt +59 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Rating.kt +87 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/ScaleSizeScope.kt +21 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Skeleton.kt +88 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Slider.kt +308 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Stepper.kt +218 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Steps.kt +456 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/SuggestAction.kt +122 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Swipe.kt +205 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Switch.kt +89 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/TabView.kt +542 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tag.kt +86 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Text.kt +175 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Title.kt +201 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tooltip.kt +498 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/TrustBanner.kt +176 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Uploader.kt +182 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePicker.kt +193 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerTypes.kt +29 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerUtils.kt +208 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/WheelPicker.kt +180 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Colors.kt +325 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/HostFont.kt +12 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Radius.kt +12 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Spacing.kt +16 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Theme.kt +194 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Typography.kt +291 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Card.kt +54 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/HeaderLayout.kt +71 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Item.kt +50 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ModalLayout.kt +184 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ScreenLayout.kt +315 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Section.kt +41 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/AutomationId.kt +54 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Clickable.kt +66 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Conditional.kt +11 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/DeprecatedModifier.kt +104 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Shadow.kt +48 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Size.kt +51 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/BottomSheet.kt +397 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/HostBackGestureBridge.kt +58 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ModalScreen.kt +153 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigation.kt +118 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/NavigationContainer.kt +206 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigator.kt +421 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ScrollToTop.kt +8 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackScreen.kt +750 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackTransition.kt +152 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/SwipeBackHaptic.kt +35 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTab.kt +242 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTabBar.kt +398 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/CurvedContainer.kt +103 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoBarCompression.kt +93 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoTabBar.kt +208 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/TabBarMetrics.kt +133 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/FloatingButton.kt +167 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/Header.kt +446 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderBackground.kt +105 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderJourney.kt +106 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderLocation.kt +74 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderRight.kt +400 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderTitle.kt +39 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderUser.kt +363 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SideBar.kt +197 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SnackBar.kt +133 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/tracking/ScreenTracker.kt +127 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/BackGesture.kt +12 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ComposeLottieAnimation.kt +114 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/OsFontScale.kt +7 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/Platform.kt +76 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReduceMotion.kt +7 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReservedRegions.kt +21 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiActions.kt +154 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiExpr.kt +1038 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiKitNodes.kt +848 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiLog.kt +47 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiModel.kt +39 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiPropsRenderer.kt +463 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiRenderer.kt +1146 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiStyle.kt +667 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiTracking.kt +240 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiValidate.kt +185 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiView.kt +200 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Icons.kt +1329 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Resources.kt +55 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Tracking.kt +15 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Utils.kt +117 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/const/HostFont.ios.kt +42 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/HostBackGesture.ios.kt +45 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/ScrollToTop.ios.kt +32 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/BackGesture.ios.kt +38 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/OsFontScale.ios.kt +65 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/Platform.ios.kt +217 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReduceMotion.ios.kt +48 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReservedRegions.ios.kt +143 -0
- package/gradle/libs.versions.toml +69 -0
- package/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/gradle/wrapper/gradle-wrapper.properties +8 -0
- package/gradle.properties +24 -0
- package/gradlew +252 -0
- package/gradlew.bat +94 -0
- package/ios-demo/README.md +120 -0
- package/package.json +1 -1
- package/scripts/sdui-visual/README.md +64 -0
- package/settings.gradle.kts +64 -0
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
// File: compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiStyle.kt
|
|
2
|
+
// Created At: 2026-10-27 12:00:00 +07:00
|
|
3
|
+
// Created By: AI
|
|
4
|
+
// AI Agent: Claude Code
|
|
5
|
+
// Model: claude-sonnet-5-5
|
|
6
|
+
|
|
7
|
+
package vn.momo.kits.sdui
|
|
8
|
+
|
|
9
|
+
import androidx.compose.foundation.background
|
|
10
|
+
import androidx.compose.foundation.border
|
|
11
|
+
import androidx.compose.foundation.layout.aspectRatio
|
|
12
|
+
import androidx.compose.foundation.layout.fillMaxHeight
|
|
13
|
+
import androidx.compose.foundation.layout.fillMaxWidth
|
|
14
|
+
import androidx.compose.foundation.layout.height
|
|
15
|
+
import androidx.compose.foundation.layout.heightIn
|
|
16
|
+
import androidx.compose.foundation.layout.padding
|
|
17
|
+
import androidx.compose.foundation.layout.width
|
|
18
|
+
import androidx.compose.foundation.layout.widthIn
|
|
19
|
+
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
20
|
+
import androidx.compose.ui.Modifier
|
|
21
|
+
import androidx.compose.ui.draw.alpha
|
|
22
|
+
import androidx.compose.ui.draw.clip
|
|
23
|
+
import androidx.compose.ui.zIndex
|
|
24
|
+
import androidx.compose.ui.graphics.Color
|
|
25
|
+
import androidx.compose.ui.graphics.RectangleShape
|
|
26
|
+
import androidx.compose.ui.graphics.Shape
|
|
27
|
+
import androidx.compose.ui.draw.drawBehind
|
|
28
|
+
import androidx.compose.ui.geometry.Offset
|
|
29
|
+
import androidx.compose.ui.geometry.Size
|
|
30
|
+
import androidx.compose.ui.layout.IntrinsicMeasurable
|
|
31
|
+
import androidx.compose.ui.layout.IntrinsicMeasureScope
|
|
32
|
+
import androidx.compose.ui.layout.LayoutModifier
|
|
33
|
+
import androidx.compose.ui.layout.Measurable
|
|
34
|
+
import androidx.compose.ui.layout.MeasureResult
|
|
35
|
+
import androidx.compose.ui.layout.MeasureScope
|
|
36
|
+
import androidx.compose.ui.layout.layout
|
|
37
|
+
import androidx.compose.runtime.Composable
|
|
38
|
+
import androidx.compose.runtime.Stable
|
|
39
|
+
import androidx.compose.runtime.getValue
|
|
40
|
+
import androidx.compose.runtime.mutableIntStateOf
|
|
41
|
+
import androidx.compose.runtime.remember
|
|
42
|
+
import androidx.compose.runtime.setValue
|
|
43
|
+
import androidx.compose.ui.unit.Constraints
|
|
44
|
+
import kotlin.math.roundToInt
|
|
45
|
+
import androidx.compose.ui.unit.coerceAtLeast
|
|
46
|
+
import androidx.compose.ui.unit.constrainHeight
|
|
47
|
+
import androidx.compose.ui.unit.constrainWidth
|
|
48
|
+
import androidx.compose.ui.unit.offset
|
|
49
|
+
import androidx.compose.ui.text.TextStyle
|
|
50
|
+
import androidx.compose.ui.unit.Dp
|
|
51
|
+
import androidx.compose.ui.unit.dp
|
|
52
|
+
import vn.momo.kits.const.Background
|
|
53
|
+
import vn.momo.kits.const.BorderColors
|
|
54
|
+
import vn.momo.kits.const.ColorSet
|
|
55
|
+
import vn.momo.kits.const.Colors
|
|
56
|
+
import vn.momo.kits.const.Radius
|
|
57
|
+
import vn.momo.kits.const.Spacing
|
|
58
|
+
import vn.momo.kits.const.TextColors
|
|
59
|
+
import vn.momo.kits.const.Theme
|
|
60
|
+
import vn.momo.kits.const.Typography
|
|
61
|
+
|
|
62
|
+
// AI-GENERATED START: token mapper (Spacing/Radius/Colors/theme.colors -> kit values) + style -> Modifier mapper
|
|
63
|
+
private val TOKEN_RE = Regex("^(Colors|Spacing|Radius|theme)\\.[A-Za-z0-9_.]+$")
|
|
64
|
+
|
|
65
|
+
private fun spacing(n: String): Dp? = when (n) {
|
|
66
|
+
"XXS" -> Spacing.XXS; "XS" -> Spacing.XS; "S" -> Spacing.S; "M" -> Spacing.M; "L" -> Spacing.L
|
|
67
|
+
"XL" -> Spacing.XL; "XXL" -> Spacing.XXL; "Size3XL" -> Spacing.Size3XL; "Size4XL" -> Spacing.Size4XL
|
|
68
|
+
"Size5XL" -> Spacing.Size5XL
|
|
69
|
+
else -> null
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private fun radius(n: String): Dp? = when (n) {
|
|
73
|
+
"XXS" -> Radius.XXS; "XS" -> Radius.XS; "S" -> Radius.S; "M" -> Radius.M; "L" -> Radius.L; "XL" -> Radius.XL
|
|
74
|
+
else -> null
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private fun kitColor(n: String): Color? = Colors.colorList[n]
|
|
78
|
+
|
|
79
|
+
private fun colorSet(set: ColorSet, field: String): Color? = when (field) {
|
|
80
|
+
"primary" -> set.primary; "secondary" -> set.secondary; "container" -> set.container
|
|
81
|
+
else -> null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private fun backgroundColor(b: Background, field: String): Color? = when (field) {
|
|
85
|
+
"default" -> b.default; "surface" -> b.surface; "tonal" -> b.tonal
|
|
86
|
+
"pressed" -> b.pressed; "selected" -> b.selected; "disable" -> b.disable
|
|
87
|
+
else -> null
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private fun textColor(t: TextColors, field: String): Color? = when (field) {
|
|
91
|
+
"default" -> t.default; "secondary" -> t.secondary; "hint" -> t.hint; "disable" -> t.disable
|
|
92
|
+
else -> null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private fun borderColor(b: BorderColors, field: String): Color? = when (field) {
|
|
96
|
+
"default" -> b.default; "disable" -> b.disable
|
|
97
|
+
else -> null
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** `theme.colors.<group>.<field>` (or `theme.colors.primary` / `secondary` / `gradient`) -> the theme's color. */
|
|
101
|
+
private fun themeColor(path: List<String>, t: Theme): Color? {
|
|
102
|
+
if (path.firstOrNull() != "colors") return null
|
|
103
|
+
val c = t.colors
|
|
104
|
+
val group = path.getOrNull(1) ?: return null
|
|
105
|
+
val field = path.getOrNull(2)
|
|
106
|
+
if (path.size > 3) return null
|
|
107
|
+
if (field == null) return when (group) {
|
|
108
|
+
"primary" -> c.primary; "secondary" -> c.secondary; "gradient" -> c.gradient
|
|
109
|
+
else -> null
|
|
110
|
+
}
|
|
111
|
+
return when (group) {
|
|
112
|
+
"background" -> backgroundColor(c.background, field)
|
|
113
|
+
"text" -> textColor(c.text, field)
|
|
114
|
+
"border" -> borderColor(c.border, field)
|
|
115
|
+
"success" -> colorSet(c.success, field)
|
|
116
|
+
"warning" -> colorSet(c.warning, field)
|
|
117
|
+
"error" -> colorSet(c.error, field)
|
|
118
|
+
"highlight" -> colorSet(c.highlight, field)
|
|
119
|
+
"interactive" -> colorSet(c.interactive, field)
|
|
120
|
+
else -> null
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* `Spacing.M` -> Dp, `Colors.x` / `theme.colors.x` -> Color. Unknown token -> input string. [fromBinding]: [s] was produced
|
|
126
|
+
* by a binding (it may carry data), so the log line does not print it.
|
|
127
|
+
*/
|
|
128
|
+
internal fun resolveToken(s: String, theme: Theme, log: SduiLog = SduiLog.NONE, nodeErrors: NodeErrorSink? = null, fromBinding: Boolean = false): Any? {
|
|
129
|
+
if (!TOKEN_RE.matches(s)) return s
|
|
130
|
+
val parts = s.split('.')
|
|
131
|
+
val r: Any? = when (parts[0]) {
|
|
132
|
+
// `Colors.x.y` is not a token: these objects are flat, so exactly one name follows
|
|
133
|
+
"Spacing" -> parts.takeIf { it.size == 2 }?.let { spacing(it[1]) }
|
|
134
|
+
"Radius" -> parts.takeIf { it.size == 2 }?.let { radius(it[1]) }
|
|
135
|
+
"Colors" -> parts.takeIf { it.size == 2 }?.let { kitColor(it[1]) }
|
|
136
|
+
"theme" -> themeColor(parts.drop(1), theme)
|
|
137
|
+
else -> null
|
|
138
|
+
}
|
|
139
|
+
if (r == null) {
|
|
140
|
+
log.w(if (fromBinding) "unknown token (from binding)" else "unknown token $s")
|
|
141
|
+
nodeErrors?.report(NODE_ERR_UNKNOWN_TOKEN)
|
|
142
|
+
}
|
|
143
|
+
return r ?: s
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Resolves bindings + tokens in any prop/style value (maps/lists recursed). */
|
|
147
|
+
internal fun resolveValue(v: Any?, scope: SduiScope, theme: Theme): Any? = when (v) {
|
|
148
|
+
is String -> when (val r = resolveString(v, scope)) {
|
|
149
|
+
is String -> resolveToken(r, theme, scope.log, scope.nodeErrors, fromBinding = r !== v)
|
|
150
|
+
else -> r
|
|
151
|
+
}
|
|
152
|
+
is Map<*, *> -> LinkedHashMap<String, Any?>().also { m -> v.forEach { (k, x) -> m[k.toString()] = resolveValue(x, scope, theme) } }
|
|
153
|
+
is List<*> -> v.map { resolveValue(it, scope, theme) }
|
|
154
|
+
else -> v
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** CSS hex colors: `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa` (alpha LAST, as on the web). */
|
|
158
|
+
internal fun parseHexColor(hex: String): Color? {
|
|
159
|
+
val h = hex.removePrefix("#")
|
|
160
|
+
if (h.any { it !in '0'..'9' && it !in 'a'..'f' && it !in 'A'..'F' }) return null
|
|
161
|
+
val full = when (h.length) {
|
|
162
|
+
3, 4 -> h.map { "$it$it" }.joinToString("")
|
|
163
|
+
6, 8 -> h
|
|
164
|
+
else -> return null
|
|
165
|
+
}
|
|
166
|
+
val rgb = full.substring(0, 6).toLong(16)
|
|
167
|
+
val alpha = if (full.length == 8) full.substring(6, 8).toLong(16) else 0xFFL
|
|
168
|
+
return Color(((alpha shl 24) or rgb).toInt())
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
internal fun parseColor(v: Any?): Color? = when (v) {
|
|
172
|
+
is Color -> v
|
|
173
|
+
is String -> when {
|
|
174
|
+
v == "transparent" -> Color.Transparent
|
|
175
|
+
v == "white" -> Color.White
|
|
176
|
+
v == "black" -> Color.Black
|
|
177
|
+
v.startsWith("#") -> parseHexColor(v)
|
|
178
|
+
else -> null
|
|
179
|
+
}
|
|
180
|
+
else -> null
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Largest size a spec may give, in dp (either sign). Beyond it the value is ignored like any unreadable one: an
|
|
185
|
+
* Infinity / NaN / huge size overflows Compose's pixel constraints and throws during layout (a crash, not a bad look).
|
|
186
|
+
*/
|
|
187
|
+
internal const val MAX_SPEC_DP = 10_000f
|
|
188
|
+
|
|
189
|
+
internal fun parseDp(v: Any?): Dp? {
|
|
190
|
+
val f = when (v) {
|
|
191
|
+
is Dp -> v.value
|
|
192
|
+
is Number -> v.toFloat()
|
|
193
|
+
is String -> v.toFloatOrNull()
|
|
194
|
+
else -> null
|
|
195
|
+
} ?: return null
|
|
196
|
+
return if (f.isFinite() && kotlin.math.abs(f) <= MAX_SPEC_DP) f.dp else null
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Accepted `aspectRatio` range: outside it (or 0 / Infinity / NaN) the measured size overflows and layout throws. */
|
|
200
|
+
internal const val MIN_ASPECT_RATIO = 0.01f
|
|
201
|
+
internal const val MAX_ASPECT_RATIO = 100f
|
|
202
|
+
|
|
203
|
+
internal fun parseRatio(v: Any?): Float? {
|
|
204
|
+
val r = when (v) {
|
|
205
|
+
is Number -> v.toFloat()
|
|
206
|
+
is String -> v.split('/').let { p ->
|
|
207
|
+
if (p.size == 2) (p[0].trim().toFloatOrNull() ?: 0f) / (p[1].trim().toFloatOrNull() ?: 1f) else v.toFloatOrNull()
|
|
208
|
+
}
|
|
209
|
+
else -> null
|
|
210
|
+
} ?: return null
|
|
211
|
+
// 0 and negatives keep their old meaning (no aspect ratio); only the values that would crash layout are dropped
|
|
212
|
+
return if (r.isFinite() && r <= MAX_ASPECT_RATIO && (r <= 0f || r >= MIN_ASPECT_RATIO)) r else null
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Resolved style map with typed accessors. */
|
|
216
|
+
internal class Sty(val m: Map<String, Any?>) {
|
|
217
|
+
// value equality: a child handed the same resolved styles again can skip recomposition
|
|
218
|
+
override fun equals(other: Any?) = other is Sty && other.m == m
|
|
219
|
+
override fun hashCode() = m.hashCode()
|
|
220
|
+
|
|
221
|
+
fun dp(k: String): Dp? = parseDp(m[k])
|
|
222
|
+
fun str(k: String): String? = m[k] as? String
|
|
223
|
+
fun color(k: String): Color? = parseColor(m[k])
|
|
224
|
+
val isFlexOne get() = (m["flex"] as? Number)?.toInt() == 1
|
|
225
|
+
val isRow get() = m["flexDirection"] == "row"
|
|
226
|
+
val isAbsolute get() = m["position"] == "absolute"
|
|
227
|
+
val hasWidth get() = m["width"] != null
|
|
228
|
+
val hasHeight get() = m["height"] != null
|
|
229
|
+
val aspect get() = parseRatio(m["aspectRatio"])
|
|
230
|
+
|
|
231
|
+
private fun side(vararg keys: String): Dp? = keys.firstNotNullOfOrNull { dp(it) }
|
|
232
|
+
// left-to-right only: Start is the left side, End the right side; Start/End beat Left/Right
|
|
233
|
+
fun padTop() = side("paddingTop", "paddingVertical", "padding") ?: 0.dp
|
|
234
|
+
fun padBottom() = side("paddingBottom", "paddingVertical", "padding") ?: 0.dp
|
|
235
|
+
fun padStart() = side("paddingStart", "paddingLeft", "paddingHorizontal", "padding") ?: 0.dp
|
|
236
|
+
fun padEnd() = side("paddingEnd", "paddingRight", "paddingHorizontal", "padding") ?: 0.dp
|
|
237
|
+
fun marTop() = side("marginTop", "marginVertical", "margin") ?: 0.dp
|
|
238
|
+
fun marBottom() = side("marginBottom", "marginVertical", "margin") ?: 0.dp
|
|
239
|
+
fun marStart() = side("marginStart", "marginLeft", "marginHorizontal", "margin") ?: 0.dp
|
|
240
|
+
fun marEnd() = side("marginEnd", "marginRight", "marginHorizontal", "margin") ?: 0.dp
|
|
241
|
+
|
|
242
|
+
fun shape(): Shape? {
|
|
243
|
+
val all = dp("borderRadius")
|
|
244
|
+
val tl = dp("borderTopLeftRadius") ?: all
|
|
245
|
+
val tr = dp("borderTopRightRadius") ?: all
|
|
246
|
+
val bl = dp("borderBottomLeftRadius") ?: all
|
|
247
|
+
val br = dp("borderBottomRightRadius") ?: all
|
|
248
|
+
if (tl == null && tr == null && bl == null && br == null) return null
|
|
249
|
+
return RoundedCornerShape(tl ?: 0.dp, tr ?: 0.dp, br ?: 0.dp, bl ?: 0.dp)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Margin semantics: the child is measured in the parent's constraints shrunk (or, for a negative margin, GROWN) by the
|
|
255
|
+
* margins; the node reports `size + margins` to its parent. So a negative margin pulls the node toward its neighbour AND
|
|
256
|
+
* every following sibling moves with it. The child is never clipped by this modifier.
|
|
257
|
+
*/
|
|
258
|
+
internal fun Modifier.specMargin(start: Dp, top: Dp, end: Dp, bottom: Dp): Modifier {
|
|
259
|
+
if (start == 0.dp && top == 0.dp && end == 0.dp && bottom == 0.dp) return this
|
|
260
|
+
return layout { measurable, constraints ->
|
|
261
|
+
val l = start.roundToPx(); val t = top.roundToPx(); val r = end.roundToPx(); val b = bottom.roundToPx()
|
|
262
|
+
val p = measurable.measure(constraints.offset(horizontal = SpecMargin.constraintDelta(l, r), vertical = SpecMargin.constraintDelta(t, b)))
|
|
263
|
+
layout(
|
|
264
|
+
constraints.constrainWidth(SpecMargin.outerSize(p.width, l, r)),
|
|
265
|
+
constraints.constrainHeight(SpecMargin.outerSize(p.height, t, b)),
|
|
266
|
+
) { p.place(l, t) }
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Pure margin arithmetic behind [specMargin] (unit-testable without a Compose runtime). All values in px, may be negative. */
|
|
271
|
+
internal object SpecMargin {
|
|
272
|
+
/** How much the available size shrinks (positive margins) or GROWS (negative margins) for the child. */
|
|
273
|
+
fun constraintDelta(before: Int, after: Int): Int = -(before + after)
|
|
274
|
+
|
|
275
|
+
/** Size the node reports to its parent: child + margins, never below 0 (a negative margin pulls siblings closer). */
|
|
276
|
+
fun outerSize(child: Int, before: Int, after: Int): Int = (child + before + after).coerceAtLeast(0)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* A container does not clip its children to borderRadius unless `overflow: hidden` (its own background/border still follow
|
|
281
|
+
* the radius); an Image always draws its bitmap rounded when a radius is set.
|
|
282
|
+
*/
|
|
283
|
+
internal fun Sty.shouldClip(isImage: Boolean): Boolean = shape() != null && (isImage || m["overflow"] == "hidden")
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* How a node's width is decided: nothing, `fillMaxWidth(fraction)` (explicit "N%" or stretched, see
|
|
287
|
+
* [StylePlan.widthFraction]), or a fixed non-negative [Dp].
|
|
288
|
+
*/
|
|
289
|
+
internal enum class WidthMode { NONE, FILL, FIXED }
|
|
290
|
+
|
|
291
|
+
private val PERCENT_RE = Regex("^\\s*([0-9]+(?:\\.[0-9]+)?|\\.[0-9]+)\\s*%\\s*$")
|
|
292
|
+
|
|
293
|
+
/** `"N%"` with 0 < N <= 100 -> N / 100; anything else (a number, "0%", "101%", "abc") -> null. */
|
|
294
|
+
internal fun parsePercent(v: Any?): Float? {
|
|
295
|
+
val n = (v as? String)?.let { PERCENT_RE.matchEntire(it) }?.groupValues?.get(1)?.toFloatOrNull() ?: return null
|
|
296
|
+
return if (n.isFinite() && n > 0f && n <= 100f) n / 100f else null
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** `opacity` clamped to 0..1, or null when it is not a finite number or is >= 1 (fully opaque: no modifier). */
|
|
300
|
+
internal fun Sty.opacityOrNull(): Float? {
|
|
301
|
+
val f = (m["opacity"] as? Number)?.toFloat()?.takeIf { it.isFinite() } ?: return null
|
|
302
|
+
return f.coerceIn(0f, 1f).takeIf { it < 1f }
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* `zIndex` as a finite number, or null when absent / not a number / 0 (the default order). It only orders the node among
|
|
307
|
+
* the children of its own parent layout, so whatever layout node is that parent's direct child must carry it outermost:
|
|
308
|
+
* [toModifier] puts it first, and every wrapper a parent puts around a child ([specZIndex]) carries it too.
|
|
309
|
+
*/
|
|
310
|
+
internal fun Sty.zIndexOrNull(): Float? = (m["zIndex"] as? Number)?.toFloat()?.takeIf { it.isFinite() && it != 0f }
|
|
311
|
+
|
|
312
|
+
/** `Modifier.zIndex` from [sty] (unchanged modifier when it has none): for a wrapper that sits between a parent layout and a node. */
|
|
313
|
+
internal fun Modifier.specZIndex(sty: Sty): Modifier = sty.zIndexOrNull()?.let { this.zIndex(it) } ?: this
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* min / max bounds of one axis (null = unbounded), each either a [Dp] or a fraction of the percent base (`"N%"`,
|
|
317
|
+
* [minFraction] / [maxFraction], only when the Dp form is absent). When min > max the min wins: for two Dp values the max
|
|
318
|
+
* is raised to it here, otherwise when they are resolved ([resolveAxisPx]).
|
|
319
|
+
*/
|
|
320
|
+
internal data class SizeBounds(val min: Dp?, val max: Dp?, val minFraction: Float? = null, val maxFraction: Float? = null) {
|
|
321
|
+
val hasFraction: Boolean get() = minFraction != null || maxFraction != null
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private fun boundsOf(sty: Sty, minKey: String, maxKey: String): SizeBounds? {
|
|
325
|
+
val min = sty.dp(minKey)?.takeIf { it >= 0.dp }
|
|
326
|
+
var max = sty.dp(maxKey)?.takeIf { it >= 0.dp }
|
|
327
|
+
val minF = if (min == null) parsePercent(sty.m[minKey]) else null
|
|
328
|
+
val maxF = if (max == null) parsePercent(sty.m[maxKey]) else null
|
|
329
|
+
if (min == null && max == null && minF == null && maxF == null) return null
|
|
330
|
+
if (min != null && max != null && min > max) max = min
|
|
331
|
+
return SizeBounds(min, max, minF, maxF)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* One axis of a node's size when it is laid out by [specSize] (it involves a percent): a [fixed] size, a [fraction] of
|
|
336
|
+
* the percent base, [fill] (the incoming max: stretch / "100%"), or none of them (auto), then clamped by [bounds].
|
|
337
|
+
*/
|
|
338
|
+
internal data class AxisPlan(val fixed: Dp? = null, val fraction: Float? = null, val fill: Boolean = false, val bounds: SizeBounds? = null)
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* CSS sizing of one axis, all in px (pure; [specSize] calls it while measuring). Percents resolve against [basePx] (null =
|
|
342
|
+
* unknown: a percent is then dropped). A definite size is clamped by min / max (min wins over max) and then fitted into
|
|
343
|
+
* the incoming [inMin]..[inMax] like `Modifier.width` does; an auto size only gets the bounds, like `Modifier.widthIn`.
|
|
344
|
+
* Returns the (min, max) the content is measured with.
|
|
345
|
+
*/
|
|
346
|
+
internal fun resolveAxisPx(a: AxisPlan, basePx: Int?, inMin: Int, inMax: Int, density: Float): Pair<Int, Int> {
|
|
347
|
+
fun px(d: Dp) = (d.value * density).roundToInt().coerceAtLeast(0)
|
|
348
|
+
fun pct(f: Float?) = if (f != null && basePx != null) (f * basePx).roundToInt() else null
|
|
349
|
+
val b = a.bounds
|
|
350
|
+
val lo = b?.min?.let(::px) ?: pct(b?.minFraction)
|
|
351
|
+
var hi = b?.max?.let(::px) ?: pct(b?.maxFraction)
|
|
352
|
+
if (lo != null && hi != null && lo > hi) hi = lo
|
|
353
|
+
val target = a.fixed?.let(::px) ?: pct(a.fraction) ?: if (a.fill && inMax != Constraints.Infinity) inMax else null
|
|
354
|
+
if (target != null) {
|
|
355
|
+
var t = target
|
|
356
|
+
if (hi != null) t = minOf(t, hi)
|
|
357
|
+
if (lo != null) t = maxOf(t, lo)
|
|
358
|
+
val c = t.coerceIn(inMin, inMax)
|
|
359
|
+
return c to c
|
|
360
|
+
}
|
|
361
|
+
val mn = (lo ?: inMin).coerceIn(inMin, inMax)
|
|
362
|
+
val mx = (hi ?: inMax).coerceIn(inMin, inMax)
|
|
363
|
+
return mn to maxOf(mn, mx)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The size a parent hands its children for their percents: its content box (px), written while the parent measures and
|
|
368
|
+
* read by each child's [specSize]. [Constraints.Infinity] = unknown (the child then uses its own incoming max).
|
|
369
|
+
* Snapshot state, so a child is measured again when the base changes even though its own constraints did not.
|
|
370
|
+
*/
|
|
371
|
+
@Stable
|
|
372
|
+
internal class PercentBase {
|
|
373
|
+
var width by mutableIntStateOf(Constraints.Infinity)
|
|
374
|
+
var height by mutableIntStateOf(Constraints.Infinity)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** True when a child with these styles sizes against a percent base (a percent width / height below 100% or a percent min / max). */
|
|
378
|
+
internal fun Sty.needsPercentBase(): Boolean {
|
|
379
|
+
fun below100(k: String) = parsePercent(m[k])?.let { it < 1f } == true
|
|
380
|
+
return below100("width") || below100("height") ||
|
|
381
|
+
PERCENT_BOUND_KEYS.any { parseDp(m[it]) == null && parsePercent(m[it]) != null }
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private val PERCENT_BOUND_KEYS = listOf("minWidth", "maxWidth", "minHeight", "maxHeight")
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Records the incoming constraints as [base] (for the children's percents). Intrinsic queries pass straight through and
|
|
388
|
+
* write nothing, so only real measurement moves the base.
|
|
389
|
+
*/
|
|
390
|
+
private class PercentBaseModifier(val base: PercentBase) : LayoutModifier {
|
|
391
|
+
override fun MeasureScope.measure(measurable: Measurable, constraints: Constraints): MeasureResult {
|
|
392
|
+
base.width = if (constraints.hasBoundedWidth) constraints.maxWidth else Constraints.Infinity
|
|
393
|
+
base.height = if (constraints.hasBoundedHeight) constraints.maxHeight else Constraints.Infinity
|
|
394
|
+
val p = measurable.measure(constraints)
|
|
395
|
+
return layout(p.width, p.height) { p.place(0, 0) }
|
|
396
|
+
}
|
|
397
|
+
override fun IntrinsicMeasureScope.minIntrinsicWidth(measurable: IntrinsicMeasurable, height: Int) = measurable.minIntrinsicWidth(height)
|
|
398
|
+
override fun IntrinsicMeasureScope.maxIntrinsicWidth(measurable: IntrinsicMeasurable, height: Int) = measurable.maxIntrinsicWidth(height)
|
|
399
|
+
override fun IntrinsicMeasureScope.minIntrinsicHeight(measurable: IntrinsicMeasurable, width: Int) = measurable.minIntrinsicHeight(width)
|
|
400
|
+
override fun IntrinsicMeasureScope.maxIntrinsicHeight(measurable: IntrinsicMeasurable, width: Int) = measurable.maxIntrinsicHeight(width)
|
|
401
|
+
override fun equals(other: Any?) = other is PercentBaseModifier && other.base === base
|
|
402
|
+
override fun hashCode() = base.hashCode()
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** Makes this layout's incoming constraints the percent base of its children ([base] null: unchanged modifier). */
|
|
406
|
+
internal fun Modifier.percentBase(base: PercentBase?): Modifier = if (base == null) this else this.then(PercentBaseModifier(base))
|
|
407
|
+
|
|
408
|
+
/** A parent's [PercentBase] when at least one of [childStyles] needs it, else null (the parent then installs nothing). */
|
|
409
|
+
@Composable
|
|
410
|
+
internal fun rememberPercentBase(childStyles: List<Sty>): PercentBase? {
|
|
411
|
+
val base = remember { PercentBase() }
|
|
412
|
+
return if (childStyles.any { it.needsPercentBase() }) base else null
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Intrinsic size (px) of an axis sized by [specSize] (pure; [SpecSizeModifier] calls it), like Compose's own
|
|
417
|
+
* `Modifier.width`: an axis with a fixed size or a resolvable percent ([basePx] known) reports that size clamped by its
|
|
418
|
+
* min / max (min wins); otherwise (auto, fill, or a percent without a base) the [content]'s own intrinsic is reported,
|
|
419
|
+
* clamped by whatever min / max resolves. [a] null = the axis is not touched: the content's intrinsic as is.
|
|
420
|
+
*/
|
|
421
|
+
internal fun intrinsicAxisPx(a: AxisPlan?, basePx: Int?, density: Float, content: () -> Int): Int {
|
|
422
|
+
if (a == null) return content()
|
|
423
|
+
val (mn, mx) = resolveAxisPx(a, basePx, 0, Constraints.Infinity, density)
|
|
424
|
+
return if (mn == mx) mn else content().coerceIn(mn, mx)
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* The size (px) of axis [a] that the content's intrinsic on the OTHER axis is queried at, given the incoming [inPx]
|
|
429
|
+
* ([Constraints.Infinity] = unbounded), resolved like [SpecSizeModifier.measure] does (percents against [basePx], else
|
|
430
|
+
* against [inPx]): a two-50%-with-aspectRatio row then asks each child's height at its own 50% width, not at the row's.
|
|
431
|
+
*/
|
|
432
|
+
internal fun intrinsicCrossPx(a: AxisPlan?, basePx: Int?, inPx: Int, density: Float): Int {
|
|
433
|
+
if (a == null) return inPx
|
|
434
|
+
val base = basePx ?: inPx.takeIf { it != Constraints.Infinity }
|
|
435
|
+
return resolveAxisPx(a, base, 0, inPx, density).second
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Sizes the node per [w] / [h] (null = that axis is not touched), percents against [base] or else the incoming max.
|
|
440
|
+
* Intrinsics are overridden too (else Compose's fallback would report the content's own size, e.g. 0 for an empty
|
|
441
|
+
* `width: "50%"` box, and a Row with `height(IntrinsicSize.Max)` would collapse): see [intrinsicAxisPx] / [intrinsicCrossPx].
|
|
442
|
+
*/
|
|
443
|
+
internal data class SpecSizeModifier(val w: AxisPlan?, val h: AxisPlan?, val base: PercentBase?) : LayoutModifier {
|
|
444
|
+
private fun baseW() = base?.width?.takeIf { it != Constraints.Infinity }
|
|
445
|
+
private fun baseH() = base?.height?.takeIf { it != Constraints.Infinity }
|
|
446
|
+
|
|
447
|
+
override fun MeasureScope.measure(measurable: Measurable, constraints: Constraints): MeasureResult {
|
|
448
|
+
val c = constraints
|
|
449
|
+
val bw = baseW() ?: c.maxWidth.takeIf { c.hasBoundedWidth }
|
|
450
|
+
val bh = baseH() ?: c.maxHeight.takeIf { c.hasBoundedHeight }
|
|
451
|
+
val (wMin, wMax) = w?.let { resolveAxisPx(it, bw, c.minWidth, c.maxWidth, density) } ?: (c.minWidth to c.maxWidth)
|
|
452
|
+
val (hMin, hMax) = h?.let { resolveAxisPx(it, bh, c.minHeight, c.maxHeight, density) } ?: (c.minHeight to c.maxHeight)
|
|
453
|
+
val p = measurable.measure(Constraints(wMin, wMax, hMin, hMax))
|
|
454
|
+
return layout(p.width, p.height) { p.place(0, 0) }
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
override fun IntrinsicMeasureScope.minIntrinsicWidth(measurable: IntrinsicMeasurable, height: Int) =
|
|
458
|
+
intrinsicAxisPx(w, baseW(), density) { measurable.minIntrinsicWidth(intrinsicCrossPx(h, baseH(), height, density)) }
|
|
459
|
+
override fun IntrinsicMeasureScope.maxIntrinsicWidth(measurable: IntrinsicMeasurable, height: Int) =
|
|
460
|
+
intrinsicAxisPx(w, baseW(), density) { measurable.maxIntrinsicWidth(intrinsicCrossPx(h, baseH(), height, density)) }
|
|
461
|
+
override fun IntrinsicMeasureScope.minIntrinsicHeight(measurable: IntrinsicMeasurable, width: Int) =
|
|
462
|
+
intrinsicAxisPx(h, baseH(), density) { measurable.minIntrinsicHeight(intrinsicCrossPx(w, baseW(), width, density)) }
|
|
463
|
+
override fun IntrinsicMeasureScope.maxIntrinsicHeight(measurable: IntrinsicMeasurable, width: Int) =
|
|
464
|
+
intrinsicAxisPx(h, baseH(), density) { measurable.maxIntrinsicHeight(intrinsicCrossPx(w, baseW(), width, density)) }
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
internal fun Modifier.specSize(w: AxisPlan?, h: AxisPlan?, base: PercentBase?): Modifier = this.then(SpecSizeModifier(w, h, base))
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Border drawn on one edge only (`borderTopWidth` / `borderBottomWidth` / `borderLeftWidth` / `borderRightWidth`, each
|
|
471
|
+
* with its own colour, else `borderColor`, else transparent): a straight line inside the node's bounds, used only when
|
|
472
|
+
* there is no all-sides border (`borderWidth` absent or <= 0) and that edge's width is > 0. Left / right are
|
|
473
|
+
* left-to-right only, like padding.
|
|
474
|
+
*/
|
|
475
|
+
internal data class EdgeBorderPlan(val width: Dp, val color: Color)
|
|
476
|
+
|
|
477
|
+
/** Full outline border following [StylePlan.shape] (or a square corner when there is none). */
|
|
478
|
+
internal data class BorderPlan(val width: Dp, val color: Color)
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Every decision [toModifier] makes. Modifiers are applied in this order: zIndex -> margin -> widthIn (min/max width) ->
|
|
482
|
+
* heightIn (min/max height) -> width (fixed / fill fraction) -> height (fixed / fill fraction) -> aspect -> opacity (alpha)
|
|
483
|
+
* -> clip -> background -> border -> top / bottom / left / right edge borders -> padding (all paddings already clamped to >= 0).
|
|
484
|
+
* min/max sit BEFORE width/height so a fixed or fill size is coerced into them (min/max win over width / height).
|
|
485
|
+
* An axis that involves a percent the plain chain cannot express ([widthAxis] / [heightAxis] set: a percent min / max, or
|
|
486
|
+
* a percent size below 100% that has a percent base or min / max) is instead sized by one [specSize] at the min/max
|
|
487
|
+
* position, and its widthIn / width (heightIn / height) are skipped.
|
|
488
|
+
* The fields added later ([zIndex] ... [rightBorder]) default to "nothing", so the plan of a style without them is unchanged.
|
|
489
|
+
*/
|
|
490
|
+
internal data class StylePlan(
|
|
491
|
+
val marginStart: Dp, val marginTop: Dp, val marginEnd: Dp, val marginBottom: Dp,
|
|
492
|
+
val widthMode: WidthMode, val width: Dp?,
|
|
493
|
+
val height: Dp?,
|
|
494
|
+
val aspectRatio: Float?,
|
|
495
|
+
val shape: Shape?,
|
|
496
|
+
val clip: Boolean,
|
|
497
|
+
val background: Color?,
|
|
498
|
+
val border: BorderPlan?,
|
|
499
|
+
val topBorder: EdgeBorderPlan?,
|
|
500
|
+
val paddingStart: Dp, val paddingTop: Dp, val paddingEnd: Dp, val paddingBottom: Dp,
|
|
501
|
+
val zIndex: Float? = null,
|
|
502
|
+
val widthBounds: SizeBounds? = null,
|
|
503
|
+
val heightBounds: SizeBounds? = null,
|
|
504
|
+
/** Fraction for [WidthMode.FILL]: 1 for "100%" / stretch, N/100 for "N%". */
|
|
505
|
+
val widthFraction: Float = 1f,
|
|
506
|
+
/** `height: "N%"` -> `fillMaxHeight(N/100)` (needs a parent with a bounded height); null when not a percent. */
|
|
507
|
+
val heightFraction: Float? = null,
|
|
508
|
+
/** `Modifier.alpha`, only when < 1; fades the whole node, children included. */
|
|
509
|
+
val opacity: Float? = null,
|
|
510
|
+
/** Width laid out by [specSize] (see the class doc); null = the plain chain. */
|
|
511
|
+
val widthAxis: AxisPlan? = null,
|
|
512
|
+
/** Height laid out by [specSize]; null = the plain chain. */
|
|
513
|
+
val heightAxis: AxisPlan? = null,
|
|
514
|
+
/** Edge borders besides [topBorder] (see [EdgeBorderPlan]); drawn after it, in this order. */
|
|
515
|
+
val bottomBorder: EdgeBorderPlan? = null,
|
|
516
|
+
val leftBorder: EdgeBorderPlan? = null,
|
|
517
|
+
val rightBorder: EdgeBorderPlan? = null,
|
|
518
|
+
)
|
|
519
|
+
|
|
520
|
+
/** One edge's border per [EdgeBorderPlan] (null when there is an all-sides border or the edge's width is absent / <= 0). */
|
|
521
|
+
private fun edgeBorderOf(sty: Sty, allSides: Dp?, edge: String): EdgeBorderPlan? {
|
|
522
|
+
if (allSides != null && allSides > 0.dp) return null
|
|
523
|
+
val w = sty.dp("border${edge}Width")?.takeIf { it > 0.dp } ?: return null
|
|
524
|
+
return EdgeBorderPlan(w, sty.color("border${edge}Color") ?: sty.color("borderColor") ?: Color.Transparent)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** Pure decision step behind [toModifier]; see there for the parameters ([hasBase]: the parent gives a [PercentBase]). */
|
|
528
|
+
internal fun stylePlanOf(sty: Sty, stretch: Boolean, weight: Boolean = false, isImage: Boolean = false, hasBase: Boolean = false): StylePlan {
|
|
529
|
+
val m = sty.m
|
|
530
|
+
val w = m["width"]
|
|
531
|
+
var widthMode = WidthMode.NONE
|
|
532
|
+
var width: Dp? = null
|
|
533
|
+
var widthFraction = 1f
|
|
534
|
+
when {
|
|
535
|
+
parsePercent(w) != null -> { widthMode = WidthMode.FILL; widthFraction = parsePercent(w)!! }
|
|
536
|
+
// a negative size or padding from a spec makes Compose throw: ignore / clamp it (the renderer never throws)
|
|
537
|
+
parseDp(w) != null -> parseDp(w)!!.takeIf { it >= 0.dp }?.let { widthMode = WidthMode.FIXED; width = it }
|
|
538
|
+
stretch && !weight -> widthMode = WidthMode.FILL
|
|
539
|
+
}
|
|
540
|
+
val h = m["height"]
|
|
541
|
+
val height = parseDp(h)?.takeIf { it >= 0.dp }
|
|
542
|
+
val heightFraction = if (height == null) parsePercent(h) else null
|
|
543
|
+
val wb = boundsOf(sty, "minWidth", "maxWidth")
|
|
544
|
+
val hb = boundsOf(sty, "minHeight", "maxHeight")
|
|
545
|
+
// "100%" (fraction 1) is exactly fillMaxWidth() wherever it is resolved, so only a smaller percent needs a base
|
|
546
|
+
val wPct = widthMode == WidthMode.FILL && widthFraction < 1f
|
|
547
|
+
val hPct = heightFraction != null && heightFraction < 1f
|
|
548
|
+
val widthAxis = if (wb?.hasFraction == true || (wPct && (hasBase || wb != null))) {
|
|
549
|
+
AxisPlan(fixed = width, fraction = widthFraction.takeIf { wPct }, fill = widthMode == WidthMode.FILL && !wPct, bounds = wb)
|
|
550
|
+
} else null
|
|
551
|
+
val heightAxis = if (hb?.hasFraction == true || (hPct && (hasBase || hb != null))) {
|
|
552
|
+
AxisPlan(fixed = height, fraction = heightFraction, bounds = hb)
|
|
553
|
+
} else null
|
|
554
|
+
val shape = sty.shape()
|
|
555
|
+
val bw = sty.dp("borderWidth")
|
|
556
|
+
return StylePlan(
|
|
557
|
+
marginStart = sty.marStart(), marginTop = sty.marTop(), marginEnd = sty.marEnd(), marginBottom = sty.marBottom(),
|
|
558
|
+
widthMode = widthMode, width = width,
|
|
559
|
+
height = height,
|
|
560
|
+
aspectRatio = sty.aspect?.takeIf { it > 0f },
|
|
561
|
+
shape = shape,
|
|
562
|
+
clip = shape != null && sty.shouldClip(isImage),
|
|
563
|
+
background = sty.color("backgroundColor"),
|
|
564
|
+
border = if (bw != null && bw > 0.dp) BorderPlan(bw, sty.color("borderColor") ?: Color.Transparent) else null,
|
|
565
|
+
topBorder = edgeBorderOf(sty, bw, "Top"),
|
|
566
|
+
paddingStart = sty.padStart().coerceAtLeast(0.dp), paddingTop = sty.padTop().coerceAtLeast(0.dp),
|
|
567
|
+
paddingEnd = sty.padEnd().coerceAtLeast(0.dp), paddingBottom = sty.padBottom().coerceAtLeast(0.dp),
|
|
568
|
+
zIndex = sty.zIndexOrNull(),
|
|
569
|
+
widthBounds = wb,
|
|
570
|
+
heightBounds = hb,
|
|
571
|
+
widthFraction = widthFraction,
|
|
572
|
+
heightFraction = heightFraction,
|
|
573
|
+
opacity = sty.opacityOrNull(),
|
|
574
|
+
widthAxis = widthAxis,
|
|
575
|
+
heightAxis = heightAxis,
|
|
576
|
+
bottomBorder = edgeBorderOf(sty, bw, "Bottom"),
|
|
577
|
+
leftBorder = edgeBorderOf(sty, bw, "Left"),
|
|
578
|
+
rightBorder = edgeBorderOf(sty, bw, "Right"),
|
|
579
|
+
)
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Box-model modifier: zIndex (outermost) -> margin -> min/max -> size -> opacity -> clip/background/border -> padding (inner).
|
|
584
|
+
* [stretch] = parent column with alignItems stretch -> fill width unless the node has an explicit width.
|
|
585
|
+
* [base]: the parent's content size for percents (a Row parent hands one: there a child's incoming width is only what is
|
|
586
|
+
* left, not the parent's width); without one a percent resolves against the incoming max, which is right in a Column / Box.
|
|
587
|
+
*/
|
|
588
|
+
internal fun Sty.toModifier(
|
|
589
|
+
stretch: Boolean, weight: Boolean = false, isImage: Boolean = false, base: PercentBase? = null,
|
|
590
|
+
/** false: leave the padding out (the caller puts [paddingModifier] on an inner layer). */
|
|
591
|
+
padding: Boolean = true,
|
|
592
|
+
): Modifier {
|
|
593
|
+
val p = stylePlanOf(this, stretch, weight, isImage, hasBase = base != null)
|
|
594
|
+
var mod: Modifier = Modifier
|
|
595
|
+
p.zIndex?.let { mod = mod.zIndex(it) }
|
|
596
|
+
mod = mod.specMargin(p.marginStart, p.marginTop, p.marginEnd, p.marginBottom)
|
|
597
|
+
if (p.widthAxis != null || p.heightAxis != null) mod = mod.specSize(p.widthAxis, p.heightAxis, base)
|
|
598
|
+
if (p.widthAxis == null) p.widthBounds?.let { b -> mod = mod.widthIn(b.min ?: Dp.Unspecified, b.max ?: Dp.Unspecified) }
|
|
599
|
+
if (p.heightAxis == null) p.heightBounds?.let { b -> mod = mod.heightIn(b.min ?: Dp.Unspecified, b.max ?: Dp.Unspecified) }
|
|
600
|
+
if (p.widthAxis == null) when (p.widthMode) {
|
|
601
|
+
// fraction 1f is exactly the old fillMaxWidth()
|
|
602
|
+
WidthMode.FILL -> mod = mod.fillMaxWidth(p.widthFraction)
|
|
603
|
+
WidthMode.FIXED -> mod = mod.width(p.width!!)
|
|
604
|
+
WidthMode.NONE -> {}
|
|
605
|
+
}
|
|
606
|
+
if (p.heightAxis == null) {
|
|
607
|
+
p.height?.let { mod = mod.height(it) }
|
|
608
|
+
p.heightFraction?.let { mod = mod.fillMaxHeight(it) }
|
|
609
|
+
}
|
|
610
|
+
p.aspectRatio?.let { mod = mod.aspectRatio(it) }
|
|
611
|
+
p.opacity?.let { mod = mod.alpha(it) }
|
|
612
|
+
if (p.shape != null && p.clip) mod = mod.clip(p.shape)
|
|
613
|
+
p.background?.let { mod = mod.background(it, p.shape ?: RectangleShape) }
|
|
614
|
+
p.border?.let { mod = mod.border(it.width, it.color, p.shape ?: RoundedCornerShape(0.dp)) }
|
|
615
|
+
p.topBorder?.let { tb -> mod = mod.drawBehind { drawRect(tb.color, size = Size(size.width, tb.width.toPx())) } }
|
|
616
|
+
p.bottomBorder?.let { b ->
|
|
617
|
+
mod = mod.drawBehind { val w = b.width.toPx(); drawRect(b.color, topLeft = Offset(0f, size.height - w), size = Size(size.width, w)) }
|
|
618
|
+
}
|
|
619
|
+
p.leftBorder?.let { b -> mod = mod.drawBehind { drawRect(b.color, size = Size(b.width.toPx(), size.height)) } }
|
|
620
|
+
p.rightBorder?.let { b ->
|
|
621
|
+
mod = mod.drawBehind { val w = b.width.toPx(); drawRect(b.color, topLeft = Offset(size.width - w, 0f), size = Size(w, size.height)) }
|
|
622
|
+
}
|
|
623
|
+
if (!padding) return mod
|
|
624
|
+
return mod.padding(start = p.paddingStart, top = p.paddingTop, end = p.paddingEnd, bottom = p.paddingBottom)
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/** Just the node's padding (clamped to >= 0, as in [toModifier]). */
|
|
628
|
+
internal fun Sty.paddingModifier(): Modifier = Modifier.padding(
|
|
629
|
+
start = padStart().coerceAtLeast(0.dp), top = padTop().coerceAtLeast(0.dp),
|
|
630
|
+
end = padEnd().coerceAtLeast(0.dp), bottom = padBottom().coerceAtLeast(0.dp),
|
|
631
|
+
)
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* Line height for [name] at the given screen width (dp), or null for an unknown typography. The base value is the kit
|
|
635
|
+
* typography's own line height (designed at 375dp width); it is scaled by screenWidth/375 (clamped 0.8–1.2) because the
|
|
636
|
+
* kit's line heights are unscaled, which made multi-line text ~8% shorter on a 411dp screen.
|
|
637
|
+
*/
|
|
638
|
+
internal fun specLineHeight(name: String?, screenWidthDp: Float): Float? {
|
|
639
|
+
val base = TYPOGRAPHY[name ?: "body_default_regular"]?.lineHeight?.takeIf { it.isSp }?.value ?: return null
|
|
640
|
+
return base * (screenWidthDp / 375f).coerceIn(0.8f, 1.2f)
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/** All kit typographies by snake_case name (`headerMBold` -> `header_m_bold`). */
|
|
644
|
+
internal val TYPOGRAPHY: Map<String, TextStyle> = mapOf(
|
|
645
|
+
"headline_default_bold" to Typography.headlineDefaultBold,
|
|
646
|
+
"header_m_bold" to Typography.headerMBold,
|
|
647
|
+
"header_default_bold" to Typography.headerDefaultBold,
|
|
648
|
+
"header_s_semibold" to Typography.headerSSemibold,
|
|
649
|
+
"header_xs_semibold" to Typography.headerXsSemibold,
|
|
650
|
+
"body_default_regular" to Typography.bodyDefaultRegular,
|
|
651
|
+
"body_default_regular_strikethrough" to Typography.bodyDefaultRegularStrikethrough,
|
|
652
|
+
"description_default_regular" to Typography.descriptionDefaultRegular,
|
|
653
|
+
"description_default_regular_strikethrough" to Typography.descriptionDefaultRegularStrikethrough,
|
|
654
|
+
"description_xs_regular" to Typography.descriptionXsRegular,
|
|
655
|
+
"description_xs_regular_strikethrough" to Typography.descriptionXsRegularStrikethrough,
|
|
656
|
+
"label_default_medium" to Typography.labelDefaultMedium,
|
|
657
|
+
"label_s_medium" to Typography.labelSMedium,
|
|
658
|
+
"label_xs_medium" to Typography.labelXsMedium,
|
|
659
|
+
"action_default_bold" to Typography.actionDefaultBold,
|
|
660
|
+
"action_s_bold" to Typography.actionSBold,
|
|
661
|
+
"action_xs_bold" to Typography.actionXsBold,
|
|
662
|
+
"action_xxs_bold" to Typography.actionXxsBold,
|
|
663
|
+
)
|
|
664
|
+
|
|
665
|
+
/** typography token (snake_case) -> kit TextStyle; unknown -> body_default_regular. */
|
|
666
|
+
internal fun typographyOf(name: String?): TextStyle = TYPOGRAPHY[name] ?: Typography.bodyDefaultRegular
|
|
667
|
+
// AI-GENERATED END: token mapper + style mapper
|