Scoop API

scoop:scoop.ui.component:0.1.0

src/content/separator.scoop

Selected production source · src/content/separator.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.window.accessibility.*
6import scoop.ui.style.*
8public class SeparatorProps public constructor(
9 public val axis: FlexAxis = FlexAxis.Horizontal,
10 public val length: Double? = None,
11 public val thickness: Double = 1.0,
12 public val color: Color? = None,
13 public val theme: ThemeSource? = None,
14 public val semantics: ComponentSemantics? = None
15) {
16 init {
17 nonnegative(thickness, "invalid separator thickness")
18 when (length) {
19 case Some(val value) -> nonnegative(value, "invalid separator length");
20 case None -> {}
21 }
22 }
25public class Separator public constructor(
26 private val props: Props<SeparatorProps>,
27 scope: ComponentScope
28): Component(scope) {
29 private val themes = ThemeAccess(scope)
30 public override fun onMount() {
31 themes.mount(props.value.theme)
32 }
33 public override fun render(): View {
34 val input = props.value
35 val theme = themes.read(input.theme)
36 val content =
37 element(
38 SeparatorInkProps(input, input.color ?: theme.tokens.palette.border),
39 { current, mount -> SeparatorInk(current, scope = mount) }
40 )
41 return when (input.semantics) {
42 case Some(val options) -> semanticContent(
43 content,
44 SemanticRole.Separator,
45 options.properties().copy {
46 orientation =
47 Some(
48 if (input.axis == FlexAxis.Horizontal)
49 SemanticOrientation.Horizontal
50 else
51 SemanticOrientation.Vertical
52 )
53 },
54 relations = options.relations,
55 reference = options.reference
56 )
57 case None -> content
58 }
59 }
62private class SeparatorInkProps(val input: SeparatorProps, val color: Color)
64internal fun separatorSize(input: SeparatorProps, constraints: Constraints): Size {
65 val horizontal = input.axis == FlexAxis.Horizontal
66 val length =
67 input.length ?:
68 (
69 if (horizontal)
70 constraints.maxWidth ?: constraints.minWidth
71 else
72 constraints.maxHeight ?: constraints.minHeight
73 )
74 return constraints.constrain(
75 if (horizontal)
76 Size(length, input.thickness)
77 else
78 Size(input.thickness, length)
79 )
82private class SeparatorInk(
83 private val props: Props<SeparatorInkProps>,
84 scope: ComponentScope
85): Element<Unit>(scope) {
86 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
87 Unit
88 > = LayoutResult(LayoutMetrics(separatorSize(props.value.input, constraints)), Unit)
89 public override fun scene(context: SceneContext, measured: Unit) {
90 val input = props.value
91 val bounds = separatorLine(input.input, context.size)
92 if (bounds.width > 0.0 && bounds.height > 0.0)
93 context.draw { paint -> paint.canvas.drawRect(bounds, Paint(input.color)) }
94 }
97public class SeparatorBuilder internal constructor() {
98 public var axis: FlexAxis = FlexAxis.Horizontal
99 public var length: Double? = None
100 public var thickness: Double = 1.0
101 public var color: Color? = None
102 public var theme: ThemeSource? = None
103 public var key: ViewKey = ViewKey.Position
104 public var semantics: ComponentSemantics? = None
107public fun separator(configure: SeparatorBuilder.() -> Unit = {}): View = makeSeparator(configure)
109internal fun makeSeparator(configure: SeparatorBuilder.() -> Unit): View {
110 val builder = SeparatorBuilder()
111 builder.configure()
112 return component(
113 SeparatorProps(
114 builder.axis,
115 builder.length,
116 builder.thickness,
117 builder.color,
118 builder.theme,
119 builder.semantics
120 ),
121 { current, mount -> Separator(current, scope = mount) },
122 builder.key
123 )
126internal fun separatorLine(input: SeparatorProps, size: Size): Rect {
127 if (input.length == Some(0.0))
128 return Rect(0.0, 0.0, 0.0, 0.0)
129 val horizontal = input.axis == FlexAxis.Horizontal
130 val thickness =
131 upperBound(
132 input.thickness,
133 if (horizontal)
134 size.height
135 else
136 size.width
137 )
138 return if (horizontal)
139 Rect(0.0, (size.height - thickness) / 2.0, size.width, thickness)
140 else
141 Rect((size.width - thickness) / 2.0, 0.0, thickness, size.height)