scoop:scoop.ui.component:0.1.0
src/content/empty_outline.scoop
Selected production source · src/content/empty_outline.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.ui.style.*7
internal class EmptyFrameProps(val style: Style, val outlined: Boolean, val color: Color)9
internal class EmptyFrame(10
private val props: Props<EmptyFrameProps>,11
scope: ComponentScope12
): Element<Unit>(scope) {13
private var painter: StylePainter? = None14
private var border: EmptyBorderCache? = None15
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
Unit21
> {22
val metrics =23
context.measureChild(24
0L,25
Constraints(26
constraints.maxWidth ?: constraints.minWidth,27
constraints.maxWidth,28
constraints.minHeight,29
constraints.maxHeight30
)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.value37
val size = context.size38
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.547
)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
}54
}56
/** Geometry is rebuilt only when the final rounded border changes. */57
internal class EmptyBorderCache: Closeable {58
private var outline: RoundedOutline? = None59
private var value: Path? = None60
fun get(next: RoundedOutline): Path {61
if (outline == Some(next))62
return value!!63
val replacement = emptyBorderPath(next)64
val previous = value65
value = Some(replacement)66
outline = Some(next)67
previous?.close()68
return replacement69
}70
public override fun close() {71
val previous = value72
value = None73
outline = None74
previous?.close()75
}76
}78
private 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.length83
var start = 0.084
while (start < length) {85
val end = upperBound(start + 3.0, length)86
this.moveTo(measure.sample(start)!!.position)87
var distance = start88
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 = next96
}97
val nextStart = start + 6.098
requireComponent(99
nextStart > start,100
"empty border geometry is not representable"101
)102
start = nextStart103
}104
}105
}106
}