Scoop API

scoop:scoop.ui.style:0.1.0

src/theme/shape_theme.scoop

Selected production source · src/theme/shape_theme.scoop

1package scoop.ui.style
3import scoop.graphics.*
4import scoop.ui.LayoutDirection
6/** Shared algorithms for the four public parts; concrete themes supply all appearance choices. */
7public class ShapeTheme internal constructor(
8 public override val tokens: ThemeTokens,
9 public val surfaces: SurfaceParts,
10 public val checkbox: CheckboxPart,
11 public val radio: RadioPart,
12 public val scrollbar: ScrollbarPart,
13 internal val preferenceBase: ShapeTheme? = None,
14 public override val preferences: ThemePreferences = ThemePreferences()
15) : Theme {
16 public fun copy(configure: ShapeThemeBuilder.() -> Unit = {}): ShapeTheme = configureShapeTheme(
17 this,
18 configure
19 )
21 public override fun withPreferences(preferences: ThemePreferences): Theme {
22 preferences.check()
23 if (preferences == this.preferences) return this
24 return resolveShapePreferences(preferenceBase ?: this, preferences)
25 }
27 private fun surfaceStyle(
28 visual: SurfaceVisual,
29 options: SurfaceOptions,
30 direction: LayoutDirection
31 ): Style {
32 val part = surfaces.get(visual.kind)
33 return physicalStyle(
34 options
35 .style
36 .resolve(
37 Style(
38 background = Some(Paint(part.fill.resolve(visual.interaction))),
39 border = Some(
40 themeBorder(
41 part.metrics.borderInsets,
42 outlineBorder(
43 part.border.resolve(visual.interaction),
44 visual.interaction,
45 options.focus ?: part.focus,
46 options.validation ?: tokens.validation
47 )
48 )
49 ),
50 corners = Corners.Physical(part.metrics.corners),
51 boxShadows = surfaceShadows(part, visual.interaction)
52 )
53 ),
54 direction
55 )
56 }
57 public override fun surfaceTextColor(visual: SurfaceVisual): Color = surfaces
58 .get(visual.kind)
59 .foreground
60 .resolve(visual.interaction)
61 public override fun surfaceMetrics(
62 visual: SurfaceVisual,
63 options: SurfaceOptions,
64 direction: LayoutDirection
65 ): SurfaceMetrics {
66 val part = surfaces.get(visual.kind)
67 val style = surfaceStyle(visual, options, direction)
68 val focus = options.focus ?: part.focus
69 val result = SurfaceMetrics(
70 options.suggestedSize ?: part.metrics.suggestedSize,
71 options.minimumSize ?: part.metrics.minimumSize,
72 options.padding ?: part.metrics.padding,
73 physicalEdges(style).insets,
74 style.corners.resolve(direction),
75 outlineOutset(focus, options.validation ?: tokens.validation)
76 )
77 themeSize(result.suggestedSize);
78 themeSize(result.minimumSize);
79 result.contentInsets.totals()
80 result.corners.normalized(result.suggestedSize);
81 themeLength(result.focusOutset);
82 themeLength(focus.width)
83 return result
84 }
85 public override fun surfaceGeometry(
86 bounds: Rect,
87 visual: SurfaceVisual,
88 options: SurfaceOptions,
89 direction: LayoutDirection
90 ): SurfaceGeometry {
91 themeBounds(bounds)
92 val metrics = surfaceMetrics(visual, options, direction)
93 val outline = RoundedOutline(bounds, metrics.corners).normalized()
94 val inner = insetOutline(outline, metrics.borderInsets)
95 return SurfaceGeometry(
96 bounds,
97 outline,
98 inner,
99 insetTheme(bounds, metrics.contentInsets),
100 outlineTheme(
101 outline,
102 visual.interaction,
103 options.focus ?: surfaces.get(visual.kind).focus,
104 options.validation ?: tokens.validation
105 ),
106 surfaceStyle(visual, options, direction),
107 controlOpacity(visual.interaction.enabled)
108 )
109 }
110 public override fun checkboxMetrics(
111 options: CheckboxOptions,
112 direction: LayoutDirection
113 ): CheckboxMetrics {
114 val base = checkbox.metrics
115 val size = options.size ?: base.suggestedSize.width
116 val min = options.minimumSize ?: base.minimumSize.width
117 val corners = options
118 .style
119 .corners
120 .resolve(Corners.Physical(base.corners))
121 .resolve(direction)
122 val edge = options.style.border.resolve(Some(themeBorder(base.borderInsets, Color.Black)))
123 val insets = when (edge) {
124 case Some(val value) -> value.resolve(direction).insets
125 case None -> Insets()
126 }
127 val focus = options.focus ?: checkbox.appearance.focus
128 val mixedFocus = options.focus ?: (checkbox.mixedAppearance ?: checkbox.appearance).focus
129 val result = CheckboxMetrics(
130 Size(size, size),
131 Size(min, min),
132 insets,
133 corners,
134 options.markInset ?: base.markInset,
135 base.markWidth,
136 themeMax(
137 markerFocusOutset(
138 size,
139 checkbox.appearance,
140 focus,
141 options.validation ?: tokens.validation,
142 options.style.corners
143 ),
144 markerFocusOutset(
145 size, checkbox.mixedAppearance ?: checkbox.appearance,
146 mixedFocus, options.validation ?: tokens.validation,
147 options.style.corners
148 )
149 )
150 )
151 for (value in [size, min, result.markInset, result.markWidth, focus.outset, focus.width])
152 themeLength(value)
153 corners.normalized(result.suggestedSize)
154 return result
155 }
156 public override fun checkboxGeometry(
157 bounds: Rect,
158 visual: CheckboxVisual,
159 options: CheckboxOptions,
160 direction: LayoutDirection
161 ): CheckboxGeometry {
162 val metrics = checkboxMetrics(options, direction)
163 val square = squareTheme(bounds)
164 val outline = RoundedOutline(square, metrics.corners).normalized()
165 val interior = insetOutline(outline, metrics.borderInsets)
166 val mark = insetTheme(square, Insets.all(metrics.markInset))
167 val width = themeMin(metrics.markWidth, themeMin(mark.width, mark.height))
168 val tick = StrokeSegment(
169 Point(mark.x, mark.y + mark.height * 0.52),
170 Point(mark.x + mark.width * 0.38, mark.y + mark.height),
171 Point(mark.x + mark.width, mark.y),
172 width
173 )
174 val mixed = when (checkbox.mixedShape) {
175 case Bar ->
176 roundTheme(
177 Rect(mark.x, square.y + (square.height - width) / 2.0, mark.width, width),
178 width / 2.0
179 )
180 case Square -> RoundedOutline(mark)
181 }
182 val amount = themeProgress(visual.selectionProgress, visual.mark != CheckMark.Off)
183 val appearance = if (visual.mark == CheckMark.Mixed)
184 checkbox.mixedAppearance ?: checkbox.appearance
185 else
186 checkbox.appearance
187 val style = physicalStyle(
188 options
189 .style
190 .resolve(
191 markerStyle(
192 appearance,
193 visual.interaction,
194 amount,
195 metrics.borderInsets,
196 outline.radii,
197 options.focus ?: appearance.focus,
198 options.validation ?: tokens.validation
199 )
200 ),
201 direction
202 )
203 return CheckboxGeometry(
204 bounds,
205 outline,
206 interior,
207 mark,
208 tick,
209 mixed,
210 outlineTheme(
211 markerFocusOutline(outline, appearance, visual.interaction, options.style.corners),
212 visual.interaction,
213 options.focus ?: appearance.focus,
214 options.validation ?: tokens.validation
215 ),
216 style,
217 options.markColor ?: appearance.mark.resolve(visual.interaction),
218 amount,
219 controlOpacity(visual.interaction.enabled),
220 markerLayerGeometry(
221 square, appearance, visual.interaction, amount,
222 options.validation ?: tokens.validation
223 )
224 )
225 }
226 public override fun radioMetrics(options: RadioOptions): RadioMetrics {
227 val base = radio.metrics
228 val size = options.size ?: base.suggestedSize.width
229 val min = options.minimumSize ?: base.minimumSize.width
230 val edge = options.border.resolve(Some(BorderEdge(base.ringWidth, Color.Black)))
231 val width = when (edge) {
232 case Some(val value) -> value.width;
233 case None -> 0.0
234 }
235 val focus = options.focus ?: radio.appearance.focus
236 val result = RadioMetrics(
237 Size(size, size),
238 Size(min, min),
239 width,
240 options.dotInset ?: base.dotInset,
241 markerFocusOutset(
242 size,
243 radio.appearance,
244 focus,
245 options.validation ?: tokens.validation
246 )
247 )
248 for (value in [size, min, width, result.dotInset, focus.outset, focus.width])
249 themeLength(value)
250 return result
251 }
252 public override fun radioGeometry(
253 bounds: Rect,
254 visual: RadioVisual,
255 options: RadioOptions
256 ): RadioGeometry {
257 val metrics = radioMetrics(options)
258 val square = squareTheme(bounds)
259 val outer = Circle(
260 Point(square.x + square.width / 2.0, square.y + square.height / 2.0),
261 square.width / 2.0
262 )
263 val interior = Circle(outer.center, themeMax(0.0, outer.radius - metrics.ringWidth))
264 val amount = themeProgress(visual.selectionProgress, visual.selected)
265 val dot = Circle(outer.center, themeMax(0.0, outer.radius - metrics.dotInset) * amount)
266 val appearance = radio.appearance
267 val style = markerStyle(
268 appearance,
269 visual.interaction,
270 amount,
271 Insets.all(metrics.ringWidth),
272 CornerRadii.all(outer.radius),
273 options.focus ?: appearance.focus,
274 options.validation ?: tokens.validation
275 )
276 return RadioGeometry(
277 bounds,
278 outer,
279 interior,
280 dot,
281 outlineTheme(
282 markerFocusOutline(
283 roundTheme(square, outer.radius),
284 appearance,
285 visual.interaction
286 ),
287 visual.interaction,
288 options.focus ?: appearance.focus,
289 options.validation ?: tokens.validation
290 ),
291 style
292 .copy {
293 this.background = options.background.resolve(this.background)
294 this.border = when (options.border) {
295 case Inherit -> this.border
296 case Set(val value) ->
297 when (value) {
298 case Some(val edge) -> Some(Border.all(edge.width, edge.color))
299 case None -> None
300 }
301 }
302 },
303 options.dotColor ?: appearance.mark.resolve(visual.interaction),
304 amount,
305 controlOpacity(visual.interaction.enabled),
306 markerLayerGeometry(
307 square, appearance, visual.interaction, amount,
308 options.validation ?: tokens.validation
309 )
310 )
311 }
312 public override fun scrollbarMetrics(options: ScrollbarOptions): ScrollbarMetrics {
313 val base = scrollbar.metrics
314 val result = ScrollbarMetrics(
315 options.thickness ?: base.thickness,
316 options.minimumThumbLength ?: base.minimumThumbLength,
317 options.trackInset ?: base.trackInset,
318 options.thumbInset ?: base.thumbInset,
319 base.cornerRadius,
320 options.buttonLength.resolve(base.buttonLength)
321 )
322 for (
323 value in [
324 result.thickness,
325 result.minimumThumbLength,
326 result.trackInset,
327 result.thumbInset,
328 result.cornerRadius,
329 result.buttonLength ?: 0.0,
330 scrollbar.arrowWidth
331 ]
332 )
333 themeLength(value)
334 return result
335 }
336 public override fun scrollbarGeometry(
337 bounds: Rect,
338 visual: ScrollbarVisual,
339 options: ScrollbarOptions
340 ): ScrollbarGeometry = makeScrollbarGeometry(
341 bounds,
342 visual,
343 scrollbarMetrics(options),
344 scrollbar.arrowWidth
345 )
346 private fun controlOpacity(enabled: Boolean): Double = if (enabled)
347 1.0
348 else
349 themeUnit(tokens.disabledOpacity)
351 public override fun createPainter(): ThemePainter = ShapeThemePainter(
352 scrollbar,
353 controlOpacity(false)
354 )
357private fun markerStyle(
358 appearance: MarkerAppearance,
359 visual: InteractionVisual,
360 selection: Double,
361 insets: Insets,
362 radii: CornerRadii,
363 focus: OutlineStyle,
364 validation: OutlineStyle
365): Style = Style(
366 background = Some(
367 Paint(
368 mixThemeColor(
369 appearance.empty.resolve(visual),
370 appearance.selected.resolve(visual),
371 selection
372 )
373 )
374 ),
375 border = Some(
376 themeBorder(
377 insets,
378 outlineBorder(
379 mixThemeColor(
380 appearance.border.resolve(visual),
381 appearance.selectedBorder.resolve(visual),
382 selection
383 ),
384 visual,
385 focus,
386 validation
387 )
388 )
389 ),
390 corners = Corners.Physical(radii),
391 boxShadows = appearance.shadows