scoop:scoop.surface:0.1.0
src/shared/input/context.scoop
Selected production source · src/shared/input/context.scoop
1
package scoop.surface3
import scoop.graphics.Point4
import scoop.graphics.native.CanvasGeometry5
import scoop.window.InputTime7
internal class RouteFlags(8
val cancelable: Boolean,9
val scroll: ScrollConsumption? = None,10
val gesture: InputGestureRoute? = None,11
val contact: InputContactRoute? = None,12
val movement: ContactConsumption? = None13
) {14
var stopped = false15
var prevented = false16
var remainder: RouteRemainder? = None17
}19
/** A synchronous delivery borrow; propagation and default cancellation are independent. */20
public class RoutedEventContext internal constructor(21
private val owner: InputRouter,22
private val destination: InputScope,23
private val receiver: InputScope,24
private val stage: RoutedPhase,25
private val stamp: InputTime,26
private val position: Point?,27
private val geometry: CanvasGeometry?,28
private val flags: RouteFlags29
) {30
private var active = true31
public val target: InputScope get() {32
checkActive()33
return destination34
}35
public val currentTarget: InputScope get() {36
checkActive()37
return receiver38
}39
public val phase: RoutedPhase get() {40
checkActive()41
return stage42
}43
public val time: InputTime get() {44
checkActive()45
return stamp46
}47
public val localPosition: Point? get() {48
checkActive()49
return position50
}51
public val cancelable: Boolean get() {52
checkActive()53
return flags.cancelable54
}55
public val propagationStopped: Boolean get() {56
checkActive()57
return flags.stopped58
}59
public val defaultPrevented: Boolean get() {60
checkActive()61
return flags.prevented62
}63
/** Remaining source-unit displacement; the event itself keeps its original delta. */64
public val scrollRemaining: InputScrollDelta? get() {65
checkActive()66
return when (flags.scroll) {67
case Some(val scroll) -> Some(scroll.remaining)68
case None -> None69
}70
}71
/** Applies a local portion, then returns the unconsumed displacement to source units. */72
public fun consumeScroll(73
lineStep: Double,74
onRemainder: ((Point) -> Point)? = None,75
apply: (Point) -> Point76
) {77
checkActive()78
if (stage != RoutedPhase.Default)79
throw IllegalStateException(Some("scroll consumption requires the default phase"))80
val scroll = flags.scroll ?: throw IllegalStateException(Some("event is not a wheel"))81
if (!flags.prevented) {82
scroll.consume(lineStep, geometry, position, apply)83
when (onRemainder) {84
case Some(val action) -> {85
val delivery = RouteDelivery(receiver, position, geometry)86
flags.remainder =87
Some(88
RouteRemainder(delivery) {89
scroll.consume(90
lineStep,91
delivery.geometry,92
delivery.position,93
action,94
true95
)96
}97
)98
}99
case None -> {}100
}101
}102
}103
/** Remaining window-logical movement after the contact owner opens this delivery. */104
public val contactRemaining: Point? get() {105
checkActive()106
return if (owner.contactDeliveryAllowed(flags.contact, receiver))107
when (flags.movement) {108
case Some(val movement) -> movement.remaining109
case None -> None110
}111
else112
None113
}114
/** The contact owner opens movement sharing; ancestors consume only its remainder. */115
public fun consumeContactMovement(116
onRemainder: ((Point) -> Point)? = None,117
apply: (Point) -> Point118
) {119
checkActive()120
if (stage != RoutedPhase.Default)121
throw IllegalStateException(Some("contact consumption requires the default phase"))122
val movement =123
flags.movement ?: throw IllegalStateException(Some("event is not a contact movement"))124
if (!flags.prevented && owner.contactDeliveryAllowed(flags.contact, receiver)) {125
movement.consume(receiver, geometry, apply)126
when (onRemainder) {127
case Some(val action) -> {128
val delivery = RouteDelivery(receiver, position, geometry)129
flags.remainder =130
Some(131
RouteRemainder(delivery) {132
movement.consume(delivery.scope, delivery.geometry, action, true)133
}134
)135
}136
case None -> {}137
}138
}139
}140
public fun stopPropagation() {141
checkActive()142
if (flags.cancelable)143
flags.stopped = true144
}145
public fun preventDefault() {146
checkActive()147
if (flags.cancelable)148
flags.prevented = true149
}150
public fun consume() {151
checkActive()152
if (flags.cancelable) {153
flags.stopped = true154
flags.prevented = true155
}156
}157
private fun checkActive() {158
owner.checkThread()159
if (!active)160
throw IllegalStateException(Some("routed event context has ended"))161
}162
internal fun end() {163
active = false164
}165
}