Scoop API

scoop:scoop.ui.component:0.1.0

src/input/motion.scoop

Selected production source · src/input/motion.scoop

1package scoop.ui.component
3import scoop.surface.*
4import scoop.ui.ComponentScope
5import scoop.ui.style.InteractionVisual
7/** The only clocks are the two animations already owned by the Surface scope. */
8internal class InteractionMotion(scope: ComponentScope, initial: InteractionVisual) {
9 private val hover =
10 scope.animation(
11 if (initial.enabled && initial.hovered)
12 1.0
13 else
14 0.0
15 )
16 private val press =
17 scope.animation(
18 if (initial.enabled && initial.pressed)
19 1.0
20 else
21 0.0
22 )
23 fun update(visual: InteractionVisual, hoverSpec: AnimationSpec?, pressSpec: AnimationSpec?) {
24 changeProgress(
25 hover,
26 if (visual.enabled && visual.hovered)
27 1.0
28 else
29 0.0,
30 if (visual.enabled)
31 hoverSpec
32 else
33 None
34 )
35 changeProgress(
36 press,
37 if (visual.enabled && visual.pressed)
38 1.0
39 else
40 0.0,
41 if (visual.enabled)
42 pressSpec
43 else
44 None
45 )
46 }
47 fun sample(visual: InteractionVisual): InteractionVisual =
48 InteractionVisual(
49 visual.enabled,
50 visual.hovered,
51 visual.pressed,
52 visual.focusVisible,
53 Some(hover.value),
54 Some(press.value),
55 visual.invalid
56 )
59internal fun changeProgress(value: Animatable<Double>, target: Double, transition: AnimationSpec?) {
60 when (transition) {
61 case Some(val spec) -> {
62 if (value.target != target)
63 value.animateTo(target, spec)
64 }
65 case None -> {
66 if (
67 value.value !=
68 target ||
69 value.state ==
70 AnimationState.Running ||
71 value.state ==
72 AnimationState.Paused
73 )
74 value.snapTo(target)
75 }
76 }