Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/claim.scoop

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

1package scoop.surface
3import scoop.window.MouseButton
5/** Claims the active button sequence without acquiring an OS pointer lock. */
6public fun InputScope.claimPointer(button: MouseButton = MouseButton.Left): Boolean {
7 checkOpen()
8 val acquired =
9 try {
10 owner.claimPointer(this, button)
11 } finally {
12 owner.dispatch {}
13 }
14 return acquired && !closed && sameScope(owner.pointerClaim, Some(this))
17/** Cancels a sequence only when this scope is its confirmed owner. */
18public fun InputScope.cancelPointerClaim(): Unit {
19 owner.checkThread()
20 if (!closed && sameScope(owner.pointerClaim, Some(this)))
21 owner.dispatch {
22 owner.cancelPress(InputCancelReason.ClaimCancelled, inputObservation(), false)
23 }
26internal fun InputRouter.claimPointer(scope: InputScope, button: MouseButton): Boolean {
27 if (!hasButton(buttons, button) || path(scope).size == 0L)
28 return false
29 when (pointerClaim) {
30 case Some(val current) -> return current === scope
31 case None -> {}
32 }
33 val original = pressed ?: return false
34 var position = 0L
35 while (position < original.size && original[position] !== scope)
36 position += 1L
37 if (position == original.size)
38 return false
39 val start = position
40 val next = Array<InputScope>(original.size - start) { index -> original[start + index] }
41 pointerClaim = Some(scope)
42 pressed = Some(next)
43 val time = inputObservation()
44 var failure: Throwable? = None
45 // Publish the new owner before cancellation callbacks can mutate the hierarchy.
46 for (index in 0L ..< start)
47 failure =
48 cleanupFailure(failure) {
49 notify(
50 original[index],
51 InputEvent.Pointer(
52 InputPointerEvent.Cancelled(InputCancelReason.Claimed, time)
53 ),
54 time,
55 Some(original[0L]),
56 terminal = true
57 )
58 }
59 throwFailure(failure)
60 return !closed && sameScope(pointerClaim, Some(scope)) && samePath(next, path(scope))