scoop:scoop.ui.style:0.1.0
src/theme/geometry.scoop
Selected production source · src/theme/geometry.scoop
1
package scoop.ui.style3
import scoop.graphics.*4
import scoop.ui.LayoutDirection6
internal fun themeLength(value: Double) {7
requireStyle(value.isFinite() && value >= 0.0, "theme lengths must be finite and nonnegative")8
}10
internal fun themeSize(size: Size) {11
themeLength(size.width);12
themeLength(size.height)13
}15
internal 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
)24
}26
internal fun themeMin(a: Double, b: Double): Double =27
if (a < b)28
a29
else30
b32
internal fun themeMax(a: Double, b: Double): Double =33
if (a > b)34
a35
else36
b38
internal fun themeUnit(value: Double): Double {39
requireStyle(value.isFinite(), "theme progress must be finite")40
return themeMin(1.0, themeMax(0.0, value))41
}43
internal fun themeProgress(value: Double?, selected: Boolean): Double =44
themeUnit(45
value ?:46
if (selected)47
1.048
else49
0.050
)52
internal fun insetTheme(bounds: Rect, insets: Insets): Rect =53
insetOutline(RoundedOutline(bounds), insets).bounds55
internal fun insetOutline(outline: RoundedOutline, insets: Insets): RoundedOutline =56
outline.inset(insets.left, insets.top, insets.right, insets.bottom)58
internal fun roundTheme(bounds: Rect, radius: Double): RoundedOutline =59
RoundedOutline(bounds, CornerRadii.all(radius)).normalized()61
internal fun physicalEdges(62
style: Style,63
direction: LayoutDirection = LayoutDirection.Ltr64
): BorderEdges =65
when (style.border) {66
case Some(val value) -> value.resolve(direction)67
case None -> BorderEdges()68
}70
internal 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
)80
internal 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 -> None86
},87
Corners.Physical(style.corners.resolve(direction)),88
style.boxShadows,89
style.contentEffects,90
style.clipContent91
)93
internal 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
side101
)102
}104
private fun stateOutline(105
visual: InteractionVisual,106
focus: OutlineStyle,107
validation: OutlineStyle108
): OutlineStyle? =109
if (visual.invalid)110
Some(validation)111
else112
if (visual.enabled && visual.focusVisible)113
Some(focus)114
else115
None117
internal fun outlineBorder(118
base: Color,119
visual: InteractionVisual,120
focus: OutlineStyle,121
validation: OutlineStyle122
): Color {123
val style = stateOutline(visual, focus, validation) ?: return base124
val color = style.borderColor ?: return base125
return color126
}128
internal 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)132
}134
internal fun outlineTheme(135
outline: RoundedOutline,136
visual: InteractionVisual,137
focus: OutlineStyle,138
validation: OutlineStyle139
): OutlineRing? {140
val style = stateOutline(visual, focus, validation) ?: return None141
themeLength(style.outset);142
themeLength(style.width)143
if (style.width == 0.0)144
return None145
return Some(OutlineRing(outline.expanded(style.outset), BorderEdge(style.width, style.color)))146
}