Scoop API

scoop:scoop.ui.style:0.1.0

src/theme/geometry.scoop

Selected production source · src/theme/geometry.scoop

1package scoop.ui.style
3import scoop.graphics.*
4import scoop.ui.LayoutDirection
6internal fun themeLength(value: Double) {
7 requireStyle(value.isFinite() && value >= 0.0, "theme lengths must be finite and nonnegative")
8}
10internal fun themeSize(size: Size) {
11 themeLength(size.width);
12 themeLength(size.height)
15internal fun themeBounds(bounds: Rect) {
16 themeSize(Size(bounds.width, bounds.height))
17 requireStyle(
18 bounds.x.isFinite() &&
19 bounds.y.isFinite() &&
20 (bounds.x + bounds.width).isFinite() &&
21 (bounds.y + bounds.height).isFinite(),
22 "invalid theme bounds"
23 )
26internal fun themeMin(a: Double, b: Double): Double =
27 if (a < b)
28 a
29 else
30 b
32internal fun themeMax(a: Double, b: Double): Double =
33 if (a > b)
34 a
35 else
36 b
38internal fun themeUnit(value: Double): Double {
39 requireStyle(value.isFinite(), "theme progress must be finite")
40 return themeMin(1.0, themeMax(0.0, value))
43internal fun themeProgress(value: Double?, selected: Boolean): Double =
44 themeUnit(
45 value ?:
46 if (selected)
47 1.0
48 else
49 0.0
50 )
52internal fun insetTheme(bounds: Rect, insets: Insets): Rect =
53 insetOutline(RoundedOutline(bounds), insets).bounds
55internal fun insetOutline(outline: RoundedOutline, insets: Insets): RoundedOutline =
56 outline.inset(insets.left, insets.top, insets.right, insets.bottom)
58internal fun roundTheme(bounds: Rect, radius: Double): RoundedOutline =
59 RoundedOutline(bounds, CornerRadii.all(radius)).normalized()
61internal fun physicalEdges(
62 style: Style,
63 direction: LayoutDirection = LayoutDirection.Ltr
64): BorderEdges =
65 when (style.border) {
66 case Some(val value) -> value.resolve(direction)
67 case None -> BorderEdges()
68 }
70internal fun themeBorder(insets: Insets, color: Color): Border =
71 Border.Physical(
72 BorderEdges(
73 BorderEdge(insets.left, color),
74 BorderEdge(insets.top, color),
75 BorderEdge(insets.right, color),
76 BorderEdge(insets.bottom, color)
77 )
78 )
80internal fun physicalStyle(style: Style, direction: LayoutDirection): Style =
81 Style(
82 style.background,
83 when (style.border) {
84 case Some(val value) -> Some(Border.Physical(value.resolve(direction)))
85 case None -> None
86 },
87 Corners.Physical(style.corners.resolve(direction)),
88 style.boxShadows,
89 style.contentEffects,
90 style.clipContent
91 )
93internal fun squareTheme(bounds: Rect): Rect {
94 themeBounds(bounds)
95 val side = themeMin(bounds.width, bounds.height)
96 return Rect(
97 bounds.x + (bounds.width - side) / 2.0,
98 bounds.y + (bounds.height - side) / 2.0,
99 side,
100 side
101 )
104private fun stateOutline(
105 visual: InteractionVisual,
106 focus: OutlineStyle,
107 validation: OutlineStyle
108): OutlineStyle? =
109 if (visual.invalid)
110 Some(validation)
111 else
112 if (visual.enabled && visual.focusVisible)
113 Some(focus)
114 else
115 None
117internal fun outlineBorder(
118 base: Color,
119 visual: InteractionVisual,
120 focus: OutlineStyle,
121 validation: OutlineStyle
122): Color {
123 val style = stateOutline(visual, focus, validation) ?: return base
124 val color = style.borderColor ?: return base
125 return color
128internal fun outlineOutset(focus: OutlineStyle, validation: OutlineStyle): Double {
129 for (value in [focus.width, focus.outset, validation.width, validation.outset])
130 themeLength(value)
131 return themeMax(focus.outset, validation.outset)
134internal fun outlineTheme(
135 outline: RoundedOutline,
136 visual: InteractionVisual,
137 focus: OutlineStyle,
138 validation: OutlineStyle
139): OutlineRing? {
140 val style = stateOutline(visual, focus, validation) ?: return None
141 themeLength(style.outset);
142 themeLength(style.width)
143 if (style.width == 0.0)
144 return None
145 return Some(OutlineRing(outline.expanded(style.outset), BorderEdge(style.width, style.color)))