scoop:scoop.ui.component:0.1.0
src/slider/component.scoop
Selected production source · src/slider/component.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*8
internal class SliderSlot {9
var area: InputArea? = None10
var focus: FocusTarget? = None11
}13
internal class SliderInput(14
val props: SliderProps,15
val values: Array<Double>,16
val theme: Theme,17
val slots: Array<SliderSlot>,18
val active: State<Long?>19
)21
public class Slider public constructor(22
private val props: Props<SliderProps>,23
scope: ComponentScope24
): Component(scope) {25
private val themes = ThemeAccess(scope)26
private val active = state<Long?>(None)27
private var slots: Array<SliderSlot> = []28
private val control = SliderControl(scope, { props.value }, active, { currentInput() })29
private fun currentInput(): SliderInput {30
val input = props.value31
return SliderInput(input, input.values.value, themes.read(input.theme), slots, active)32
}33
public override fun onUnmount() {34
control.retire()35
}36
public override fun onMount() {37
themes.mount(props.value.theme)38
}39
public override fun render(): View {40
val input = props.value41
val values = input.values.value42
val theme = themes.read(input.theme)43
if (values.size != slots.size) {44
val previous = slots45
slots =46
Array<SliderSlot>(values.size) {47
index -> if (index < previous.size)48
previous[index]49
else50
SliderSlot()51
}52
}53
val config = SliderInput(input, values, theme, slots, active)54
val control = this.control55
val children =56
Array<View>(values.size) {57
index -> element(58
SliderThumbProps(config, index, active.value == Some(index), control),59
{ current, mount -> SliderThumb(current, scope = mount) },60
key = ViewKey.Number(index)61
)62
}63
return element(64
config,65
{ current, mount -> SliderLayout(current, control, scope = mount) },66
children67
)68
}69
}71
private class SliderLayout(72
private val props: Props<SliderInput>,73
private val control: SliderControl,74
scope: ComponentScope75
): Element<SliderGeometry>(scope) {76
private val track = scope.resource { StylePainter() }77
private val fill = scope.resource { StylePainter() }78
public override fun update(): UiInvalidation {79
control.update()80
return UiInvalidation.Layout81
}82
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<83
SliderGeometry84
> {85
val input = props.value86
val style = input.props.style87
val density = input.theme.tokens.typography.body.fontSize / 14.088
val horizontal = input.props.orientation == Orientation.Horizontal89
val maximum =90
if (horizontal)91
constraints.maxWidth92
else93
constraints.maxHeight94
val length = maximum ?: style.length ?: 160.0 * density95
val side = style.thumbSize ?: 12.0 * density96
val size =97
constraints.constrain(98
if (horizontal)99
Size(length, side)100
else101
Size(side, length)102
)103
val shape =104
sliderGeometry(input.props, input.values, input.theme, size, context.layoutDirection)105
for (index in 0L ..< context.childCount) {106
val rect = shape.thumbs[index]107
context.measureChild(index, Constraints.tight(Size(rect.width, rect.height)))108
context.placeChild(index, Point(rect.x, rect.y))109
}110
return LayoutResult(LayoutMetrics(size), shape)111
}112
public override fun scene(context: SceneContext, shape: SliderGeometry) {113
val input = props.value114
if (!input.props.enabled)115
context.saveLayer(input.theme.tokens.disabledOpacity)116
track.scene(117
context,118
shape.track,119
sliderPartStyle(input.theme.tokens.palette.muted.background, input.props.style.track)120
) { _ -> }121
if (shape.fill.width > 0.0 && shape.fill.height > 0.0)122
fill.scene(123
context,124
shape.fill,125
sliderPartStyle(126
input.theme.tokens.palette.primary.background,127
input.props.style.range128
)129
) { _ -> }130
val order = ArrayList<Long>()131
var top: Long? = None132
for (index in 0L ..< input.slots.size) {133
if (input.slots[index].area?.hasFocus ?: false)134
top = Some(index)135
}136
when (input.active.value) {137
case Some(val index) -> top = Some(index);138
case None -> {}139
}140
for (index in 0L ..< input.slots.size)141
if (top != Some(index))142
order.add(index)143
when (top) {144
case Some(val index) -> order.add(index);145
case None -> {}146
}147
control.place(context, shape)148
context.children(order.toArray())149
if (!input.props.enabled)150
context.restore()151
context.onFrameComplete {152
result -> if (result.submission == FrameSubmission.Accepted)153
control.accept(shape)154
}155
}156
}