scoop:scoop.ui.component:0.1.0
src/field/separator.scoop
Selected production source · src/field/separator.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*8
public class FieldSeparatorProps public constructor(9
public val content: PanelContent? = None,10
public val fonts: FontCollection? = None,11
public val theme: ThemeSource? = None,12
public val color: Color? = None,13
public val style: StyleOverride = styleOverride()14
)16
public class FieldSeparator public constructor(17
private val props: Props<FieldSeparatorProps>,18
scope: ComponentScope19
): Component(scope) {20
private val themes = ThemeAccess(scope)21
public override fun onMount() {22
themes.mount(props.value.theme)23
}24
public override fun render(): View {25
val input = props.value26
val theme = themes.read(input.theme)27
val line = SeparatorProps(color = Some(input.color ?: theme.tokens.palette.border))28
val children = ArrayList<View>()29
children.add(30
component(31
line,32
{ current, mount -> Separator(current, scope = mount) },33
ViewKey.Number(0L)34
)35
)36
when (input.content) {37
case Some(val value) -> {38
children.add(39
component(40
line,41
{ current, mount -> Separator(current, scope = mount) },42
ViewKey.Number(1L)43
)44
)45
children.add(46
panelSlot(47
makeRow {48
this.add(49
panelCaption(50
value,51
fieldTextStyle(FieldTextRole.Description, theme),52
input.fonts53
)54
)55
},56
2L57
)58
)59
}60
case None -> {}61
}62
return panelFrame(63
element(64
theme.tokens.typography.body.fontSize / 14.0,65
{ current, mount -> FieldSeparatorLayout(current, scope = mount) },66
children.toArray()67
),68
visual = input.style69
)70
}71
}73
private class FieldSeparatorLayout(74
private val props: Props<Double>,75
scope: ComponentScope76
): Element<Unit>(scope) {77
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<78
Unit79
> {80
val hasContent = context.childCount == 3L81
val gap = upperBound(8.0 * props.value, (constraints.maxWidth ?: 16.0 * props.value) / 2.0)82
val caption =83
if (hasContent)84
context.measureChild(85
2L,86
Constraints(87
maxWidth = insetMaximum(constraints.maxWidth, gap * 2.0),88
maxHeight = constraints.maxHeight89
)90
)91
else92
LayoutMetrics(Size(0.0, 0.0))93
val size =94
constraints.constrain(95
Size(96
constraints.maxWidth ?:97
(98
caption.size.width +99
if (hasContent)100
gap * 2.0101
else102
0.0103
),104
lowerBound(20.0 * props.value, caption.size.height)105
)106
)107
val lineWidth =108
if (hasContent)109
lowerBound((size.width - caption.size.width - gap * 2.0) / 2.0, 0.0)110
else111
size.width112
val lineConstraints =113
Constraints(114
minWidth = lineWidth,115
maxWidth = Some(lineWidth),116
maxHeight = Some(size.height)117
)118
val first = context.measureChild(0L, lineConstraints)119
val second =120
if (hasContent)121
context.measureChild(1L, lineConstraints)122
else123
first124
if (size.width > 0.0 && size.height > 0.0) {125
context.placeChild(0L, Point(0.0, (size.height - first.size.height) / 2.0))126
if (hasContent) {127
context.placeChild(128
1L,129
Point(size.width - lineWidth, (size.height - second.size.height) / 2.0)130
)131
context.placeChild(132
2L,133
Point(134
(size.width - caption.size.width) / 2.0,135
(size.height - caption.size.height) / 2.0136
)137
)138
}139
}140
return LayoutResult(141
offsetMetrics(caption, size, (size.height - caption.size.height) / 2.0),142
Unit143
)144
}145
public override fun scene(context: SceneContext, measured: Unit) {146
containerScene(context, true)147
}148
}150
public class FieldSeparatorBuilder internal constructor() {151
public var fonts: FontCollection? = None152
public var theme: ThemeSource? = None153
public var color: Color? = None154
public var style: StyleOverride = styleOverride()155
public var key: ViewKey = ViewKey.Position156
public fun style(configure: StyleOverrideBuilder.() -> Unit) {157
style = style.copy(configure)158
}159
}161
public fun fieldSeparator(configure: FieldSeparatorBuilder.() -> Unit = {}): View =162
makeFieldSeparator(None, configure)164
public fun fieldSeparator(text: String, configure: FieldSeparatorBuilder.() -> Unit = {}): View =165
makeFieldSeparator(Some(fieldPlain(text)), configure)167
public fun fieldSeparator(168
text: StyledText,169
configure: FieldSeparatorBuilder.() -> Unit = {}170
): View = makeFieldSeparator(Some(fieldStyled(text)), configure)172
public fun fieldSeparator(content: View, configure: FieldSeparatorBuilder.() -> Unit = {}): View =173
makeFieldSeparator(Some(PanelContent.Custom(content)), configure)175
internal fun makeFieldSeparator(176
content: PanelContent?,177
configure: FieldSeparatorBuilder.() -> Unit178
): View {179
val builder = FieldSeparatorBuilder()180
builder.configure()181
return component(182
FieldSeparatorProps(content, builder.fonts, builder.theme, builder.color, builder.style),183
{ current, mount -> FieldSeparator(current, scope = mount) },184
builder.key185
)186
}