Scoop API

scoop:scoop.ui.component:0.1.0

src/drawer/events.scoop

Selected production source · src/drawer/events.scoop

1package scoop.ui.component
3import scoop.math.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.window.*
8internal fun drawerSwipe(control: DrawerControl, sample: SwipeGestureSample) {
9 control.cancel(DragCancelReason.Explicit)
10 if (!control.available() || !control.read().props.nativeSwipe)
11 return
12 val shape = control.geometry!!
13 val positive =
14 sample.direction == SwipeDirection.Right || sample.direction == SwipeDirection.Down
15 val expand = positive != (shape.outward > 0.0)
16 when (drawerAdjacent(shape.snaps, control.current(), expand)) {
17 case Some(val index) -> control.select(index, true)
18 case None -> if (!expand && control.read().props.swipeToClose)
19 control.close()
20 }
23internal fun drawerDrag(control: DrawerControl, event: DragEvent) {
24 when (event) {
25 case Started(val sample) -> {
26 if (!control.available()) {
27 control.cancel(DragCancelReason.GeometryUnavailable);
28 return
29 }
30 control.motion.cancel()
31 control.gesture = Some(DrawerGesture(control.position, control.geometry!!))
32 control.focus.handle.requestFocus(FocusOrigin.Pointer)
33 if (!control.active)
34 return
35 drawerDragged(control, sample);
36 control.publish()
37 }
38 case Moved(val sample) -> {
39 drawerDragged(control, sample);
40 control.publish()
41 }
42 case Ended(val sample) -> {
43 val gesture = control.gesture ?: return
44 drawerDragged(control, sample)
45 if (!control.active)
46 return
47 control.gesture = None
48 val input = control.read()
49 val target =
50 drawerReleased(
51 gesture.geometry,
52 control.position,
53 -sample.velocity * gesture.geometry.outward,
54 input.props.projectionSeconds,
55 input.props.swipeToClose
56 )
57 when (target) {
58 case Some(val value) -> if (value.index < 0L)
59 control.close()
60 else
61 control.select(value.index, true)
62 case None -> {}
63 }
64 control.publish()
65 }
66 case Cancelled(_, val reason) -> control.cancel(reason)
67 }
70private fun drawerDragged(control: DrawerControl, sample: DragSample) {
71 val gesture = control.gesture ?: return
72 val extent = gesture.geometry.extent
73 var next = gesture.origin - sample.displacement * gesture.geometry.outward
74 if (!next.isFinite())
75 return
76 next = max(0.0, next)
77 if (next > extent)
78 next = extent + min(extent / 3.0, (next - extent) / 3.0)
79 if (control.motion.value != next)
80 control.motion.snapTo(next)
83internal fun drawerKeyboard(
84 control: DrawerControl,
85 context: RoutedEventContext,
86 event: KeyEvent,
87 handle: Boolean
88) {
89 if (!control.active || !sliderKeyAllowed(event))
90 return
91 if (event.logicalKey == LogicalKey.Named(NamedKey.Escape)) {
92 when (control.gesture) {
93 case Some(_) -> {
94 context.consume();
95 control.cancel(DragCancelReason.Explicit)
96 }
97 case None -> {}
98 }
99 return
100 }
101 if (!handle || !control.available())
102 return
103 val shape = control.geometry!!
104 val key = event.logicalKey
105 val expand: Boolean? =
106 when (key) {
107 case Named(NamedKey.ArrowUp) -> if (shape.vertical)
108 Some(shape.outward > 0.0)
109 else
110 None
111 case Named(NamedKey.ArrowDown) -> if (shape.vertical)
112 Some(shape.outward < 0.0)
113 else
114 None
115 case Named(NamedKey.ArrowLeft) -> if (!shape.vertical)
116 Some(shape.outward > 0.0)
117 else
118 None
119 case Named(NamedKey.ArrowRight) -> if (!shape.vertical)
120 Some(shape.outward < 0.0)
121 else
122 None
123 else -> None
124 }
125 val endpoint = key == LogicalKey.Named(NamedKey.Home) || key == LogicalKey.Named(NamedKey.End)
126 if (expand == None && !endpoint)
127 return
128 context.consume()
129 var selected = control.current()
130 if (endpoint) {
131 for (index in 0L ..< shape.snaps.size) {
132 if (
133 if (key == LogicalKey.Named(NamedKey.End))
134 shape.snaps[index] > shape.snaps[selected]
135 else
136 shape.snaps[index] < shape.snaps[selected]
137 )
138 selected = index
139 }
140 } else
141 selected = drawerAdjacent(shape.snaps, selected, expand!!) ?: selected
142 control.select(selected, true)