Scoop API

scoop:scoop.ui.component:0.1.0

src/slider/control.scoop

Selected production source · src/slider/control.scoop

1package scoop.ui.component
3import scoop.window.accessibility.SemanticActionResult
5import scoop.graphics.*
6import scoop.math.abs
7import scoop.surface.*
8import scoop.ui.*
9import scoop.ui.style.*
10import scoop.window.*
12private class SliderGesture(
13 val index: Long,
14 val initial: Array<Double>,
15 val range: RangeSpec,
16 val orientation: Orientation,
17 val direction: LayoutDirection,
18 val grab: Double
19) {
20 var expected = initial
23internal class SliderControl(
24 private val scope: ComponentScope,
25 private val model: () -> SliderProps,
26 private val activeThumb: State<Long?>,
27 private val input: () -> SliderInput
28) {
29 private val area = scope.createArea()
30 private var drag: Drag? = None
31 private var geometry: SliderGeometry? = None
32 private var gesture: SliderGesture? = None
33 private var checkedValues: Array<Double>? = None
34 private var checkedRange: RangeSpec? = None
35 private var configured = input().props
36 private var direction = scope.layoutDirection
37 init {
38 scope.routeChildren(area)
39 drag =
40 Some(
41 scope.drag(
42 area,
43 options(),
44 { point -> candidate(point) != None },
45 { event -> handle(event) }
46 )
47 )
48 }
49 private fun options(): DragOptions =
50 DragOptions(
51 if (input().props.orientation == Orientation.Horizontal)
52 DragAxis.Horizontal
53 else
54 DragAxis.Vertical,
55 threshold = 0.0,
56 enabled = input().props.enabled
57 )
58 private fun values(): Array<Double> {
59 val props = input().props
60 val result = props.values.value
61 val same =
62 when (checkedValues) {
63 case Some(val previous) -> previous ===
64 result &&
65 sameSliderRange(checkedRange!!, props.range)
66 case None -> false
67 }
68 if (!same) {
69 props.range.validate(result)
70 checkedRange = Some(props.range)
71 checkedValues = Some(result)
72 }
73 return result
74 }
75 fun update() {
76 val props = input().props
77 val current = values()
78 requireComponent(
79 props.thumbs.size == 0L || props.thumbs.size == current.size,
80 "slider thumb options must match its values"
81 )
82 when (gesture) {
83 case Some(val active) -> if (!props.enabled || !compatible(active, current))
84 cancel(
85 if (!props.enabled)
86 DragCancelReason.Disabled
87 else
88 DragCancelReason.ConfigurationChanged
89 )
90 case None -> {}
91 }
92 if (
93 !sameSliderRange(configured.range, props.range) ||
94 configured.orientation !=
95 props.orientation ||
96 direction !=
97 scope.layoutDirection
98 )
99 geometry = None
100 configured = props
101 direction = scope.layoutDirection
102 area.scope.enabled = props.enabled
103 drag!!.update(options(), { point -> candidate(point) != None }, { event -> handle(event) })
104 }
105 fun place(context: SceneContext, shape: SliderGeometry) {
106 if (shape.size.width > 0.0 && shape.size.height > 0.0 && input().values.size > 0L)
107 context.place(area, shape.hit)
108 }
109 fun accept(shape: SliderGeometry) {
110 if (!scope.isMounted)
111 return
112 val props = input().props
113 if (
114 shape.orientation !=
115 props.orientation ||
116 shape.direction !=
117 scope.layoutDirection ||
118 !sameSliderRange(shape.range, props.range)
119 )
120 return
121 geometry = Some(shape)
122 if (shape.travel == 0.0)
123 cancel(DragCancelReason.GeometryUnavailable)
124 }
125 private fun candidate(point: Point): Long? {
126 if (!input().props.enabled)
127 return None
128 val shape = geometry ?: return None
129 val current = values()
130 if (shape.travel == 0.0 || shape.thumbs.size != current.size)
131 return None
132 var focused: Long? = None
133 for (index in 0L ..< input().slots.size)
134 if (input().slots[index].area?.hasFocus ?: false)
135 focused = Some(index)
136 return nearestSliderThumb(shape, input().props.range, current, point, focused)
137 }
138 private fun compatible(active: SliderGesture, current: Array<Double>): Boolean =
139 sameSliderRange(active.range, input().props.range) &&
140 active.orientation ==
141 input().props.orientation &&
142 active.direction ==
143 scope.layoutDirection &&
144 sameSliderValues(current, active.expected)
145 private fun handle(event: DragEvent) {
146 when (event) {
147 case Started(val sample) -> start(sample.position)
148 case Moved(val sample) -> move(sample.position)
149 case Ended(val sample) -> {
150 move(sample.position)
151 val active = gesture ?: return
152 val current = values()
153 gesture = None
154 input().active.value = None
155 if (!sameSliderValues(active.initial, current) && scope.isMounted)
156 input().props.onValueCommit(current)
157 }
158 case Cancelled(_, val reason) -> cancel(reason)
159 }
160 }
161 private fun start(point: Point) {
162 val index = candidate(point) ?: return
163 val shape = geometry!!
164 val slot = input().slots[index]
165 val focus = slot.focus ?: return
166 if (!focus.handle.requestFocus(FocusOrigin.Pointer)) {
167 drag!!.cancel()
168 return
169 }
170 val offset = shape.coordinate(point) - shape.center(index)
171 val hit =
172 input().props.style.hitInsets ?:
173 Insets.all(8.0 * input().theme.tokens.typography.body.fontSize / 14.0)
174 val inset =
175 if (shape.orientation == Orientation.Horizontal)
176 lowerBound(hit.left, hit.right)
177 else
178 lowerBound(hit.top, hit.bottom)
179 val grabbing = abs(offset) <= shape.thumbSize / 2.0 + inset
180 val grab =
181 if (grabbing && shape.thumbSize > 0.0)
182 offset / shape.thumbSize
183 else
184 0.0
185 val current = values()
186 gesture =
187 Some(
188 SliderGesture(
189 index,
190 current,
191 input().props.range,
192 input().props.orientation,
193 scope.layoutDirection,
194 grab
195 )
196 )
197 input().active.value = Some(index)
198 if (!grabbing)
199 move(point)
200 }
201 private fun move(point: Point) {
202 val active = gesture ?: return
203 val current = values()
204 if (!input().props.enabled || !compatible(active, current)) {
205 cancel(
206 if (!input().props.enabled)
207 DragCancelReason.Disabled
208 else
209 DragCancelReason.ConfigurationChanged
210 )
211 return
212 }
213 val shape = geometry ?: return
214 if (shape.travel == 0.0 || shape.thumbs.size != current.size) {
215 cancel(DragCancelReason.GeometryUnavailable)
216 return
217 }
218 val next =
219 input().props.range.moveValidated(
220 current,
221 active.index,
222 input().props.range.valueAt(shape.position(point, active.grab))
223 )
224 if (!sameSliderValues(current, next))
225 input().props.values.value = next
226 active.expected = values()
227 }
228 fun cancel(reason: DragCancelReason) {
229 val active = gesture ?: return
230 gesture = None
231 activeThumb.value = None
232 drag?.cancel()
233 // Retirement keeps the business model, but no longer permits theme queries.
234 val props = model()
235 val current = props.values.value
236 if (
237 sameSliderRange(active.range, props.range) &&
238 sameSliderValues(current, active.expected) &&
239 !sameSliderValues(current, active.initial)
240 )
241 props.values.value = active.initial
242 props.onValueCancel(props.values.value, reason)
243 }
244 fun lostFocus(index: Long) {
245 if (activeThumb.value == Some(index))
246 cancel(DragCancelReason.Explicit)
247 }
248 fun key(index: Long, context: RoutedEventContext, key: KeyEvent) {
249 if (!input().props.enabled || !sliderKeyAllowed(key))
250 return
251 if (key.logicalKey == LogicalKey.Named(NamedKey.Escape) && input().active.value != None) {
252 context.consume()
253 cancel(DragCancelReason.Explicit)
254 return
255 }
256 val command =
257 sliderKeyCommand(input().props, scope.layoutDirection, key.logicalKey) ?: return
258 context.consume()
259 adjust(index, command)
260 }
261 fun adjust(index: Long, command: SliderKey): SemanticActionResult {
262 if (!scope.isMounted)
263 return SemanticActionResult.Unavailable
264 if (!input().props.enabled)
265 return SemanticActionResult.Rejected
266 cancel(DragCancelReason.Explicit)
267 val current = values()
268 if (index < 0L || index >= current.size)
269 return SemanticActionResult.Unavailable
270 val value = sliderKeyValue(input().props, current[index], command)
271 return setNumber(index, value)
272 }
273 fun setNumber(index: Long, value: Double): SemanticActionResult {
274 if (!scope.isMounted)
275 return SemanticActionResult.Unavailable
276 if (!input().props.enabled)
277 return SemanticActionResult.Rejected
278 cancel(DragCancelReason.Explicit)
279 if (!scope.isMounted)
280 return SemanticActionResult.Unavailable
281 val current = values()
282 if (index < 0L || index >= current.size)
283 return SemanticActionResult.Unavailable
284 if (!value.isFinite())
285 return SemanticActionResult.InvalidArgument
286 val next = input().props.range.moveValidated(current, index, value)
287 if (sameSliderValues(current, next))
288 return SemanticActionResult.Completed
289 input().props.values.value = next
290 if (!scope.isMounted)
291 return SemanticActionResult.Unavailable
292 val accepted = values()
293 if (scope.isMounted && !sameSliderValues(current, accepted))
294 input().props.onValueCommit(accepted)
295 return if (sameSliderValues(next, accepted))
296 SemanticActionResult.Completed
297 else
298 SemanticActionResult.Rejected
299 }
300 fun retire() {
301 cancel(DragCancelReason.Closed)
302 geometry = None
303 }