Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/routing.scoop

Selected production source · src/shared/input/routing.scoop

1package scoop.surface
3import scoop.graphics.Point
4import scoop.graphics.native.CanvasGeometry
5import scoop.window.*
7internal struct RouteDelivery(
8 val scope: InputScope,
9 val position: Point?,
10 val geometry: CanvasGeometry? = None
13internal class RouteRemainder(val delivery: RouteDelivery, val action: () -> Unit)
15internal fun InputRouter.deliveries(scopes: Array<InputScope>, point: Point? = None): Array<
16 RouteDelivery
17> {
18 val result = ArrayList<RouteDelivery>()
19 for (scope in scopes) {
20 var local: Point? = None
21 var geometry: CanvasGeometry? = None
22 when ((scope.area, point)) {
23 case(Some(val area), Some(val position)) -> {
24 val placement = scene.find(area) ?: return []
25 geometry = Some(placement.geometry)
26 local = placement.geometry.toLocal(position)
27 if (local == None) {
28 pending = true
29 return []
30 }
31 }
32 else -> {}
33 }
34 result.add(RouteDelivery(scope, local, geometry))
35 }
36 return result.toArray()
39private fun InputRouter.invoke(
40 delivery: RouteDelivery,
41 target: InputScope,
42 event: InputEvent,
43 phase: RoutedPhase,
44 time: InputTime,
45 flags: RouteFlags,
46 terminal: Boolean = false,
47 inactive: Boolean = false,
48 callback: ((RoutedEventContext, InputEvent) -> Unit)? = None
49) {
50 val scope = delivery.scope
51 if (!terminal && (closed || scope.closed || (!inactive && path(scope).size == 0L)))
52 return
53 when (event) {
54 case Keyboard(_) -> {
55 if (!allowsFocus(scope))
56 return
57 }
58 else -> {}
59 }
60 if (!terminal && !scrollDeliveryAllowed(flags.scroll))
61 return
62 if (!terminal && !gestureDeliveryAllowed(flags.gesture))
63 return
64 if (!terminal && !contactDeliveryAllowed(flags.contact, scope))
65 return
66 val context =
67 RoutedEventContext(
68 this,
69 target,
70 scope,
71 phase,
72 time,
73 delivery.position,
74 delivery.geometry,
75 flags
76 )
77 try {
78 when (callback) {
79 case Some(val action) -> action(context, event)
80 case None -> when (phase) {
81 case Capture -> scope.captureHandler(context, event)
82 case Default -> scope.defaultAction(context, event)
83 else -> scope.deliverEvent(context, event)
84 }
85 }
86 } finally {
87 context.end()
88 }
91internal fun InputRouter.route(
92 chain: Array<RouteDelivery>,
93 event: InputEvent,
94 time: InputTime,
95 scroll: InputScrollRoute? = None,
96 gesture: InputGestureRoute? = None,
97 contact: InputContactRoute? = None
98) {
99 var failure = cleanupFailure(None) { routeStages(chain, event, time, scroll, gesture, contact) }
100 for (delivery in chain) {
101 val scope = delivery.scope
102 if (!closed && !scope.closed && path(scope).size > 0L)
103 failure = cleanupFailure(failure) { scope.finishEvent(event) }
104 }
105 throwFailure(failure)
108private fun InputRouter.routeStages(
109 chain: Array<RouteDelivery>,
110 event: InputEvent,
111 time: InputTime,
112 scroll: InputScrollRoute?,
113 gesture: InputGestureRoute?,
114 contact: InputContactRoute?
115) {
116 if (chain.size == 0L)
117 return
118 val target = chain[0L].scope
119 val flags =
120 RouteFlags(
121 true,
122 scrollConsumption(event, scroll),
123 gesture,
124 contact,
125 contactConsumption(event, contact)
126 )
127 var index = chain.size
128 while (index > 1L && !flags.stopped) {
129 index -= 1L
130 invoke(chain[index], target, event, RoutedPhase.Capture, time, flags)
131 }
132 if (!flags.stopped)
133 invoke(chain[0L], target, event, RoutedPhase.Target, time, flags)
134 index = 1L
135 while (index < chain.size && !flags.stopped) {
136 invoke(chain[index], target, event, RoutedPhase.Bubble, time, flags)
137 index += 1L
138 }
139 var focused = false
140 for (delivery in chain) {
141 if (flags.prevented || closed)
142 break
143 if (path(delivery.scope).size == 0L || !contactDeliveryAllowed(contact, delivery.scope))
144 continue
145 if (!focused && focusesOnPress(event, delivery.scope)) {
146 when (delivery.scope.area) {
147 case Some(val area) -> if (area.focusable) {
148 focus(area, FocusOrigin.Pointer)
149 focused = true
150 }
151 case None -> {}
152 }
153 }
154 val actions = delivery.scope.defaultActions
155 invoke(delivery, target, event, RoutedPhase.Default, time, flags)
156 var pass = 0
157 while (pass < 3) {
158 val kind =
159 when (pass) {
160 case 0 -> InputDefaultKind.Ordinary
161 case 1 -> InputDefaultKind.Shortcut
162 else -> InputDefaultKind.HostShortcut
163 }
164 for (action in actions) {
165 if (flags.prevented || closed)
166 break
167 if (!action.closed && action.kind == kind)
168 invoke(
169 delivery,
170 target,
171 event,
172 RoutedPhase.Default,
173 time,
174 flags,
175 callback = Some(action.handler)
176 )
177 }
178 pass += 1
179 }
180 }
181 when (flags.remainder) {
182 case Some(val final) -> invoke(
183 final.delivery,
184 target,
185 event,
186 RoutedPhase.Default,
187 time,
188 flags,
189 callback = Some { _, _ -> final.action() }
190 )
191 case None -> {}
192 }
195private fun InputRouter.focusesOnPress(event: InputEvent, scope: InputScope): Boolean =
196 when (event) {
197 case Pointer(
198 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)
199 ) -> containsScope(hover, scope)
200 case Contact(InputContactEvent.Began(_)) -> true
201 else -> false
202 }
204internal fun InputRouter.notify(
205 scope: InputScope,
206 event: InputEvent,
207 time: InputTime = inputObservation(),
208 target: InputScope? = None,
209 position: Point? = None,
210 terminal: Boolean = false,
211 inactive: Boolean = false
212) {
213 invoke(
214 RouteDelivery(scope, position),
215 target ?: scope,
216 event,
217 RoutedPhase.Notification,
218 time,
219 RouteFlags(false),
220 terminal,
221 inactive
222 )