scoop:scoop.surface:0.1.0
src/shared/input/routing.scoop
Selected production source · src/shared/input/routing.scoop
1
package scoop.surface3
import scoop.graphics.Point4
import scoop.graphics.native.CanvasGeometry5
import scoop.window.*7
internal struct RouteDelivery(8
val scope: InputScope,9
val position: Point?,10
val geometry: CanvasGeometry? = None11
)13
internal class RouteRemainder(val delivery: RouteDelivery, val action: () -> Unit)15
internal fun InputRouter.deliveries(scopes: Array<InputScope>, point: Point? = None): Array<16
RouteDelivery17
> {18
val result = ArrayList<RouteDelivery>()19
for (scope in scopes) {20
var local: Point? = None21
var geometry: CanvasGeometry? = None22
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 = true29
return []30
}31
}32
else -> {}33
}34
result.add(RouteDelivery(scope, local, geometry))35
}36
return result.toArray()37
}39
private 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)? = None49
) {50
val scope = delivery.scope51
if (!terminal && (closed || scope.closed || (!inactive && path(scope).size == 0L)))52
return53
when (event) {54
case Keyboard(_) -> {55
if (!allowsFocus(scope))56
return57
}58
else -> {}59
}60
if (!terminal && !scrollDeliveryAllowed(flags.scroll))61
return62
if (!terminal && !gestureDeliveryAllowed(flags.gesture))63
return64
if (!terminal && !contactDeliveryAllowed(flags.contact, scope))65
return66
val context =67
RoutedEventContext(68
this,69
target,70
scope,71
phase,72
time,73
delivery.position,74
delivery.geometry,75
flags76
)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
}89
}91
internal fun InputRouter.route(92
chain: Array<RouteDelivery>,93
event: InputEvent,94
time: InputTime,95
scroll: InputScrollRoute? = None,96
gesture: InputGestureRoute? = None,97
contact: InputContactRoute? = None98
) {99
var failure = cleanupFailure(None) { routeStages(chain, event, time, scroll, gesture, contact) }100
for (delivery in chain) {101
val scope = delivery.scope102
if (!closed && !scope.closed && path(scope).size > 0L)103
failure = cleanupFailure(failure) { scope.finishEvent(event) }104
}105
throwFailure(failure)106
}108
private 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
return118
val target = chain[0L].scope119
val flags =120
RouteFlags(121
true,122
scrollConsumption(event, scroll),123
gesture,124
contact,125
contactConsumption(event, contact)126
)127
var index = chain.size128
while (index > 1L && !flags.stopped) {129
index -= 1L130
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 = 1L135
while (index < chain.size && !flags.stopped) {136
invoke(chain[index], target, event, RoutedPhase.Bubble, time, flags)137
index += 1L138
}139
var focused = false140
for (delivery in chain) {141
if (flags.prevented || closed)142
break143
if (path(delivery.scope).size == 0L || !contactDeliveryAllowed(contact, delivery.scope))144
continue145
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 = true150
}151
case None -> {}152
}153
}154
val actions = delivery.scope.defaultActions155
invoke(delivery, target, event, RoutedPhase.Default, time, flags)156
var pass = 0157
while (pass < 3) {158
val kind =159
when (pass) {160
case 0 -> InputDefaultKind.Ordinary161
case 1 -> InputDefaultKind.Shortcut162
else -> InputDefaultKind.HostShortcut163
}164
for (action in actions) {165
if (flags.prevented || closed)166
break167
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 += 1179
}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
}193
}195
private 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(_)) -> true201
else -> false202
}204
internal 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 = false212
) {213
invoke(214
RouteDelivery(scope, position),215
target ?: scope,216
event,217
RoutedPhase.Notification,218
time,219
RouteFlags(false),220
terminal,221
inactive222
)223
}