scoop:scoop.surface:0.1.0
src/shared/input/claim.scoop
Selected production source · src/shared/input/claim.scoop
1
package scoop.surface3
import scoop.window.MouseButton5
/** Claims the active button sequence without acquiring an OS pointer lock. */6
public 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))15
}17
/** Cancels a sequence only when this scope is its confirmed owner. */18
public 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
}24
}26
internal fun InputRouter.claimPointer(scope: InputScope, button: MouseButton): Boolean {27
if (!hasButton(buttons, button) || path(scope).size == 0L)28
return false29
when (pointerClaim) {30
case Some(val current) -> return current === scope31
case None -> {}32
}33
val original = pressed ?: return false34
var position = 0L35
while (position < original.size && original[position] !== scope)36
position += 1L37
if (position == original.size)38
return false39
val start = position40
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? = None45
// 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 = true57
)58
}59
throwFailure(failure)60
return !closed && sameScope(pointerClaim, Some(scope)) && samePath(next, path(scope))61
}