Scoop API

scoop:scoop.ui.component:0.1.0

src/slider/component.scoop

Selected production source · src/slider/component.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
8internal class SliderSlot {
9 var area: InputArea? = None
10 var focus: FocusTarget? = None
13internal 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?>
21public class Slider public constructor(
22 private val props: Props<SliderProps>,
23 scope: ComponentScope
24): 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.value
31 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.value
41 val values = input.values.value
42 val theme = themes.read(input.theme)
43 if (values.size != slots.size) {
44 val previous = slots
45 slots =
46 Array<SliderSlot>(values.size) {
47 index -> if (index < previous.size)
48 previous[index]
49 else
50 SliderSlot()
51 }
52 }
53 val config = SliderInput(input, values, theme, slots, active)
54 val control = this.control
55 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 children
67 )
68 }
71private class SliderLayout(
72 private val props: Props<SliderInput>,
73 private val control: SliderControl,
74 scope: ComponentScope
75): 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.Layout
81 }
82 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
83 SliderGeometry
84 > {
85 val input = props.value
86 val style = input.props.style
87 val density = input.theme.tokens.typography.body.fontSize / 14.0
88 val horizontal = input.props.orientation == Orientation.Horizontal
89 val maximum =
90 if (horizontal)
91 constraints.maxWidth
92 else
93 constraints.maxHeight
94 val length = maximum ?: style.length ?: 160.0 * density
95 val side = style.thumbSize ?: 12.0 * density
96 val size =
97 constraints.constrain(
98 if (horizontal)
99 Size(length, side)
100 else
101 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.value
114 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.range
128 )
129 ) { _ -> }
130 val order = ArrayList<Long>()
131 var top: Long? = None
132 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 }