Scoop API

scoop:scoop.ui.style:0.1.0

src/theme/preferences.scoop

Selected production source · src/theme/preferences.scoop

1package scoop.ui.style
3import scoop.graphics.*
4import scoop.graphics.text.TextStyle
5import scoop.ui.ComponentScope
7/** Effective display inputs, independent of an OS query, window or painter lifetime. */
8public struct ThemePreferences(
9 val reduceMotion: Boolean = false,
10 val highContrast: Boolean = false,
11 val reduceTransparency: Boolean = false,
12 val textScaleFactor: Double = 1.0
13) {
14 internal fun check() {
15 requireStyle(
16 textScaleFactor.isFinite() && textScaleFactor > 0.0,
17 "theme text scale must be finite and positive"
18 )
19 }
22/** Reads both the base theme and root preferences through their ordinary UI dependencies. */
23public fun ThemeSource.read(scope: ComponentScope): Theme {
24 val value = scope.interactionPreferences
25 return resolve(
26 ThemePreferences(
27 value.reduceMotion ?: false,
28 value.highContrast ?: false,
29 value.reduceTransparency ?: false,
30 value.textScaleFactor ?: 1.0
31 )
32 )
35internal fun resolveShapePreferences(base: ShapeTheme, preferences: ThemePreferences): ShapeTheme {
36 if (preferences == ThemePreferences()) return base
37 val colors = preferences.highContrast || preferences.reduceTransparency
38 val canvas = if (base.tokens.mode == ThemeMode.Dark) Color.Black else Color.White
39 val palette = if (colors)
40 preferencePalette(base.tokens.palette, canvas, preferences.highContrast)
41 else
42 base.tokens.palette
43 val background = opaqueThemeColor(palette.background.background, canvas)
44 val disabledOpacity = themeUnit(base.tokens.disabledOpacity)
45 val type = base.tokens.typography
46 val typography = if (preferences.textScaleFactor != 1.0 || preferences.highContrast)
47 ThemeTypography(
48 preferenceText(
49 type.body,
50 preferences.textScaleFactor,
51 palette.background.foreground,
52 preferences.highContrast
53 ),
54 preferenceText(
55 type.caption,
56 preferences.textScaleFactor,
57 palette.muted.foreground,
58 preferences.highContrast
59 ),
60 preferenceText(
61 type.action,
62 preferences.textScaleFactor,
63 palette.background.foreground,
64 preferences.highContrast
65 ),
66 preferenceText(
67 type.input,
68 preferences.textScaleFactor,
69 palette.background.foreground,
70 preferences.highContrast
71 )
72 )
73 else
74 type
75 val motion = if (preferences.reduceMotion)
76 base.tokens.motion.copy { this.preference = MotionPreference.Reduced }
77 else
78 base.tokens.motion
79 val tokens = base
80 .tokens
81 .copy {
82 this.palette = palette
83 this.typography = typography
84 this.motion = motion
85 if (colors) this.effects = themeEffects()
86 if (preferences.highContrast) {
87 this.disabledOpacity = 1.0
88 this.validation = preferenceOutline(base.tokens.validation, background, true)
89 }
90 }
91 return ShapeTheme(
92 tokens,
93 if (colors)
94 preferenceSurfaces(base.surfaces, background, preferences.highContrast, disabledOpacity)
95 else
96 base.surfaces,
97 if (colors)
98 preferenceCheckbox(base.checkbox, background, preferences.highContrast, disabledOpacity)
99 else
100 base.checkbox,
101 if (colors)
102 preferenceRadio(base.radio, background, preferences.highContrast, disabledOpacity)
103 else
104 base.radio,
105 if (colors)
106 preferenceScrollbar(base.scrollbar, background, preferences.highContrast)
107 else
108 base.scrollbar,
109 Some(base),
110 preferences
111 )
114private fun preferenceText(
115 style: TextStyle,
116 scale: Double,
117 color: Color,
118 contrast: Boolean
119): TextStyle = style
120 .copy {
121 this.fontSize = style.fontSize * scale
122 if (contrast) this.color = color
123 }