Scoop API

scoop:scoop.ui.component:0.1.0

src/content/empty_outline.scoop

Selected production source · src/content/empty_outline.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
7internal class EmptyFrameProps(val style: Style, val outlined: Boolean, val color: Color)
9internal class EmptyFrame(
10 private val props: Props<EmptyFrameProps>,
11 scope: ComponentScope
12): Element<Unit>(scope) {
13 private var painter: StylePainter? = None
14 private var border: EmptyBorderCache? = None
15 public override fun onMount() {
16 painter = Some(scope.resource { StylePainter() })
17 border = Some(scope.resource { EmptyBorderCache() })
18 }
19 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
20 Unit
21 > {
22 val metrics =
23 context.measureChild(
24 0L,
25 Constraints(
26 constraints.maxWidth ?: constraints.minWidth,
27 constraints.maxWidth,
28 constraints.minHeight,
29 constraints.maxHeight
30 )
31 )
32 context.placeChild(0L, Point(0.0, 0.0))
33 return LayoutResult(metrics, Unit)
34 }
35 public override fun scene(context: SceneContext, measured: Unit) {
36 val input = props.value
37 val size = context.size
38 painter!!.scene(context, input.style) { content -> content.children() }
39 if (input.outlined && size.width > 1.0 && size.height > 1.0) {
40 val radii = input.style.corners.resolve(context.layoutDirection)
41 val outline =
42 RoundedOutline(Rect(0.0, 0.0, size.width, size.height), radii).inset(
43 0.5,
44 0.5,
45 0.5,
46 0.5
47 )
48 val path = border!!.get(outline)
49 context.draw {
50 paint -> paint.canvas.drawPath(path, Paint(input.color, PaintingStyle.Stroke, 1.0))
51 }
52 }
53 }
56/** Geometry is rebuilt only when the final rounded border changes. */
57internal class EmptyBorderCache: Closeable {
58 private var outline: RoundedOutline? = None
59 private var value: Path? = None
60 fun get(next: RoundedOutline): Path {
61 if (outline == Some(next))
62 return value!!
63 val replacement = emptyBorderPath(next)
64 val previous = value
65 value = Some(replacement)
66 outline = Some(next)
67 previous?.close()
68 return replacement
69 }
70 public override fun close() {
71 val previous = value
72 value = None
73 outline = None
74 previous?.close()
75 }
78private fun emptyBorderPath(outline: RoundedOutline): Path =
79 path { this.addRoundRect(outline.bounds, outline.radii) }.use {
80 source -> source.measure().use {
81 measure -> path {
82 val length = measure.length
83 var start = 0.0
84 while (start < length) {
85 val end = upperBound(start + 3.0, length)
86 this.moveTo(measure.sample(start)!!.position)
87 var distance = start
88 while (distance < end) {
89 val next = upperBound(distance + 1.0, end)
90 requireComponent(
91 next > distance,
92 "empty border geometry is not representable"
93 )
94 this.lineTo(measure.sample(next)!!.position)
95 distance = next
96 }
97 val nextStart = start + 6.0
98 requireComponent(
99 nextStart > start,
100 "empty border geometry is not representable"
101 )
102 start = nextStart
103 }
104 }
105 }
106 }