scoop:scoop.ui.component:0.1.0
src/content/separator.scoop
Selected production source · src/content/separator.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.window.accessibility.*6
import scoop.ui.style.*8
public 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? = None15
) {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
}23
}25
public class Separator public constructor(26
private val props: Props<SeparatorProps>,27
scope: ComponentScope28
): 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.value35
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.Horizontal50
else51
SemanticOrientation.Vertical52
)53
},54
relations = options.relations,55
reference = options.reference56
)57
case None -> content58
}59
}60
}62
private class SeparatorInkProps(val input: SeparatorProps, val color: Color)64
internal fun separatorSize(input: SeparatorProps, constraints: Constraints): Size {65
val horizontal = input.axis == FlexAxis.Horizontal66
val length =67
input.length ?:68
(69
if (horizontal)70
constraints.maxWidth ?: constraints.minWidth71
else72
constraints.maxHeight ?: constraints.minHeight73
)74
return constraints.constrain(75
if (horizontal)76
Size(length, input.thickness)77
else78
Size(input.thickness, length)79
)80
}82
private class SeparatorInk(83
private val props: Props<SeparatorInkProps>,84
scope: ComponentScope85
): Element<Unit>(scope) {86
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<87
Unit88
> = LayoutResult(LayoutMetrics(separatorSize(props.value.input, constraints)), Unit)89
public override fun scene(context: SceneContext, measured: Unit) {90
val input = props.value91
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
}95
}97
public class SeparatorBuilder internal constructor() {98
public var axis: FlexAxis = FlexAxis.Horizontal99
public var length: Double? = None100
public var thickness: Double = 1.0101
public var color: Color? = None102
public var theme: ThemeSource? = None103
public var key: ViewKey = ViewKey.Position104
public var semantics: ComponentSemantics? = None105
}107
public fun separator(configure: SeparatorBuilder.() -> Unit = {}): View = makeSeparator(configure)109
internal 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.semantics120
),121
{ current, mount -> Separator(current, scope = mount) },122
builder.key123
)124
}126
internal 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.Horizontal130
val thickness =131
upperBound(132
input.thickness,133
if (horizontal)134
size.height135
else136
size.width137
)138
return if (horizontal)139
Rect(0.0, (size.height - thickness) / 2.0, size.width, thickness)140
else141
Rect((size.width - thickness) / 2.0, 0.0, thickness, size.height)142
}