Scoop API

scoop:scoop.ui.component:0.1.0

src/scroll/feedback.scoop

Selected production source · src/scroll/feedback.scoop

1package scoop.ui.component
3import scoop.platform.time.MonotonicInstant
4import scoop.graphics.*
5import scoop.platform.time.MonotonicTimeSource
6import scoop.surface.*
7import scoop.time.*
8import scoop.ui.*
9import scoop.ui.style.*
10import scoop.window.*
12/** Both axes share one visibility animation, one native deadline and one passive pointer observer. */
13internal class ScrollbarFeedback(
14 private val scope: ComponentScope,
15 private val scrollState: () -> ScrollState,
16 private val props: () -> ViewportScrollbarOptions,
17 private val parent: InputArea,
18 private val changed: () -> Unit
19): Closeable {
20 var options = resolve()
21 val opacity =
22 scope.animation(
23 if (options.autoHide)
24 0.0
25 else
26 1.0
27 )
28 private val idle = ScrollbarIdle()
29 private val timer = scope.createDeadline { tick() }
30 private val probe = scope.createArea()
31 private var state = scrollState()
32 private var intent: Closeable? = None
33 private var closed = false
34 private var ready = false
35 private var bounds = Rect(0.0, 0.0, 0.0, 0.0)
36 private var horizontal: Rect? = None
37 private var vertical: Rect? = None
38 private var within = false
39 private var edge = false
40 private var horizontalHover = false
41 private var verticalHover = false
42 private var focused = false
43 private val keys = ArrayList<NamedKey>()
44 private var repeating: ScrollbarRepetition? = None
45 private var scheduled: MonotonicInstant? = None
46 private var windowActivity = scope.nativeWindow.activity
48 init {
49 probe.ignoreSelf = true
50 probe.scope.parent = Some(parent.scope)
51 observeState()
52 scope.resource { parent.scope.observePointer { event -> pointerEvent(event) } }
53 scope.resource { parent.scope.observeActivity { activityChanged() } }
54 scope.resource { parent.scope.addEventFinished { event -> finished(event) } }
55 scope.observeInteractionPreferences { _ -> update() }
56 scope.observeWindowActivity {
57 value ->
58 val previous = windowActivity
59 windowActivity = value
60 if (
61 previous.focused !=
62 value.focused ||
63 previous.visible !=
64 value.visible ||
65 previous.occluded !=
66 value.occluded
67 )
68 activityChanged()
69 }
70 }
72 private fun resolve(): ResolvedScrollbarBehavior {
73 val value = props()
74 return ResolvedScrollbarBehavior(
75 value.scrollbarBehavior,
76 scope.interactionPreferences,
77 value.theme.scrollbarMetrics(value.scrollbar).thickness,
78 value.theme.tokens.motion.preference == MotionPreference.Reduced
79 )
80 }
81 private fun observeState() {
82 intent =
83 Some(
84 state.observeIntent {
85 event -> if (!closed && options.input.revealOnScroll) {
86 when (event) {
87 case User(_) -> wake()
88 case Programmatic(_) -> wake()
89 case LayoutCorrection(_) -> {}
90 }
91 }
92 }
93 )
94 }
95 fun update() {
96 val previous = options
97 options = resolve()
98 // Newly enabled triggers also apply to the current accepted pointer.
99 if (!previous.input.revealOnViewportHover && options.input.revealOnViewportHover)
100 within = false
101 if (!previous.input.revealOnEdgeHover && options.input.revealOnEdgeHover)
102 edge = false
103 if (state !== scrollState()) {
104 intent?.close()
105 state = scrollState()
106 ready = false
107 horizontal = None
108 vertical = None
109 repeating = None
110 keys.clear()
111 observeState()
112 }
113 if (!allowed())
114 keys.clear()
115 if (!allowed() || options.input.repeatDelay == None)
116 repeating = None
117 pointer(probe.pointerPosition)
118 changed()
119 }
120 private fun allowed(): Boolean =
121 !closed &&
122 scope.isMounted &&
123 props().enabled &&
124 parent.scope.isInputAllowed &&
125 scope.nativeWindow.state ==
126 WindowState.Open &&
127 scope.nativeWindow.isFocused &&
128 scope.nativeWindow.isVisible &&
129 !scope.nativeWindow.isOccluded
131 private fun presented(): Boolean =
132 !closed &&
133 scope.isMounted &&
134 scope.nativeWindow.state ==
135 WindowState.Open &&
136 scope.nativeWindow.isVisible &&
137 !scope.nativeWindow.isOccluded &&
138 probe.visibleBounds !=
139 None
141 fun hover(orientation: Orientation, value: Boolean) {
142 if (orientation == Orientation.Horizontal) {
143 if (horizontalHover == value)
144 return
145 horizontalHover = value
146 } else {
147 if (verticalHover == value)
148 return
149 verticalHover = value
150 }
151 refresh()
152 }
154 private fun activityChanged() {
155 if (closed)
156 return
157 val next = parent.scope.activity.hasFocus && allowed()
158 if (
159 next &&
160 !focused &&
161 parent.isFocusActive &&
162 parent.focusOrigin !=
163 FocusOrigin.Pointer &&
164 options.input.revealOnKeyboard
165 )
166 wake()
167 focused = next
168 if (!next)
169 keys.clear()
170 if (!allowed())
171 repeating = None
172 refresh()
173 }
175 private fun finished(event: InputEvent) {
176 if (closed)
177 return
178 when (event) {
179 case Keyboard(val key) -> {
180 if (key.synthetic)
181 return
182 when (key.logicalKey) {
183 case Named(val name) -> {
184 var index = 0L
185 while (index < keys.size) {
186 if (keys[index] == name)
187 keys.removeAt(index)
188 else
189 index += 1L
190 }
191 if (
192 key.state == ElementState.Pressed && scrollbarHeldKey(name) && allowed()
193 )
194 keys.add(name)
195 }
196 else -> {}
197 }
198 if (
199 key.state == ElementState.Pressed && allowed() && options.input.revealOnKeyboard
200 )
201 wake()
202 else
203 refresh()
204 }
205 else -> {}
206 }
207 }
209 private fun pointerEvent(event: PointerObservation) {
210 if (closed)
211 return
212 when (event) {
213 case Pointer(PointerEvent.Left(_)) -> pointer(None)
214 case Pointer(PointerEvent.Cancelled(_, _)) -> {
215 keys.clear()
216 repeating = None
217 pointer(None)
218 }
219 case FocusLost -> {
220 keys.clear()
221 repeating = None
222 pointer(None)
223 }
224 case Closed -> {
225 ready = false
226 repeating = None
227 pointer(None)
228 }
229 case Pointer(_) -> pointer(probe.pointerPosition)
230 }
231 }
232 private fun pointer(point: Point?) {
233 val inside =
234 allowed() &&
235 ready &&
236 parent.scope.isHovered &&
237 when (point) {
238 case Some(val value) -> bounds.contains(value)
239 case None -> false
240 }
241 val near =
242 inside &&
243 scrollbarNearEdge(bounds, horizontal, vertical, point!!, options.edgeThickness)
244 val reveal =
245 (inside && !within && options.input.revealOnViewportHover) ||
246 (near && !edge && options.input.revealOnEdgeHover)
247 within = inside
248 edge = near
249 if (reveal)
250 wake()
251 else
252 refresh()
253 }
254 private fun busy(): Boolean {
255 if (!allowed())
256 return false
257 val activity = parent.scope.activity
258 return horizontalHover ||
259 verticalHover ||
260 activity.hasPress ||
261 activity.hasContact ||
262 activity.hasScrollRoute ||
263 state.isAnimating ||
264 keys.size >
265 0L ||
266 (within && options.input.revealOnViewportHover) ||
267 (edge && options.input.revealOnEdgeHover)
268 }
269 fun wake() {
270 if (closed || !allowed())
271 return
272 configure()
273 idle.wake(MonotonicTimeSource.now())
274 refresh()
275 }
276 private fun configure() {
277 idle.configure(
278 ready && (horizontal != None || vertical != None) && (!options.autoHide || presented()),
279 options.autoHide,
280 options.input.idleDelay,
281 busy(),
282 MonotonicTimeSource.now()
283 )
284 }
285 private fun refresh() {
286 if (closed || !scope.isMounted)
287 return
288 configure()
289 idle.expire(MonotonicTimeSource.now())
290 val target =
291 if (idle.visible)
292 1.0
293 else
294 0.0
295 val before = opacity.target
296 changeProgress(
297 opacity,
298 target,
299 if (ready && presented())
300 options.transition(
301 if (idle.visible)
302 options.input.fadeIn
303 else
304 options.input.fadeOut,
305 true
306 )
307 else
308 None
309 )
310 schedule()
311 if (before != target)
312 changed()
313 }
314 fun repeat(orientation: Orientation, resume: Boolean, step: () -> Boolean) {
315 if (!allowed() || options.input.repeatDelay == None || panelPresent(repeating))
316 return
317 repeating =
318 Some(
319 ScrollbarRepetition(
320 orientation,
321 MonotonicTimeSource.now() +
322 if (resume)
323 options.input.repeatInterval
324 else
325 options.input.repeatDelay!!,
326 step
327 )
328 )
329 schedule()
330 }
331 fun stopRepeat(orientation: Orientation) {
332 if (repeating?.orientation == Some(orientation)) {
333 repeating = None
334 schedule()
335 }
336 }
337 private fun schedule() {
338 var next = idle.deadline
339 when (repeating) {
340 case Some(val value) -> if (next == None || value.next < next!!)
341 next = Some(value.next)
342 case None -> {}
343 }
344 if (next == scheduled)
345 return
346 scheduled = next
347 when (next) {
348 case Some(val time) -> timer.schedule(time)
349 case None -> timer.cancel()
350 }
351 }
352 private fun tick() {
353 scheduled = None
354 val now = MonotonicTimeSource.now()
355 val repeat = repeating
356 when (repeat) {
357 case Some(val value) -> if (value.next <= now) {
358 value.next = now + options.input.repeatInterval
359 if (!allowed() || !value.step()) {
360 if (panelPresent(repeating) && repeating!! === value)
361 repeating = None
362 }
363 }
364 case None -> {}
365 }
366 refresh()
367 }
368 fun scene(context: SceneContext, bounds: Rect, horizontal: Rect?, vertical: Rect?) {
369 if (horizontal != None || vertical != None)
370 context.place(probe, bounds)
371 val owner = state
372 context.onFrameComplete {
373 frame -> if (
374 frame.submission == FrameSubmission.Accepted && !closed && state === owner
375 ) {
376 this.bounds = bounds
377 this.horizontal = horizontal
378 this.vertical = vertical
379 ready = probe.visibleBounds != None
380 if (horizontal == None && vertical == None)
381 repeating = None
382 pointer(probe.pointerPosition)
383 }
384 }
385 }
386 public override fun close() {
387 if (closed)
388 return
389 closed = true
390 try {
391 intent?.close()
392 } finally {
393 intent = None
394 repeating = None
395 timer.close()
396 }
397 }
400private class ScrollbarRepetition(
401 val orientation: Orientation,
402 var next: MonotonicInstant,
403 val step: () -> Boolean
406private fun scrollbarHeldKey(key: NamedKey): Boolean =
407 key ==
408 NamedKey.ArrowUp ||
409 key ==
410 NamedKey.ArrowDown ||
411 key ==
412 NamedKey.ArrowLeft ||
413 key ==
414 NamedKey.ArrowRight ||
415 key ==
416 NamedKey.PageUp ||
417 key ==
418 NamedKey.PageDown ||
419 key ==
420 NamedKey.Home ||
421 key ==
422 NamedKey.End ||
423 key ==
424 NamedKey.Space
426internal fun scrollbarNearEdge(
427 bounds: Rect,
428 horizontal: Rect?,
429 vertical: Rect?,
430 point: Point,
431 thickness: Double
432): Boolean {
433 if (thickness <= 0.0 || !bounds.contains(point))
434 return false
435 when (horizontal) {
436 case Some(val rect) -> if (
437 point.x >=
438 rect.x &&
439 point.x <
440 rect.x +
441 rect.width &&
442 point.y >=
443 rect.y +
444 rect.height -
445 thickness
446 )
447 return true
448 case None -> {}
449 }
450 when (vertical) {
451 case Some(val rect) -> {
452 val inside =
453 if (rect.x + rect.width / 2.0 < bounds.x + bounds.width / 2.0)
454 point.x < rect.x + thickness
455 else
456 point.x >= rect.x + rect.width - thickness
457 if (inside && point.y >= rect.y && point.y < rect.y + rect.height)
458 return true
459 }
460 case None -> {}
461 }
462 return false