scoop:scoop.surface:0.1.0
src/shared/input/area.scoop
Selected production source · src/shared/input/area.scoop
1
package scoop.surface3
import scoop.graphics.Rect4
import scoop.graphics.Point6
/** Stable input identity; geometry is supplied by each successfully submitted frame. */7
public class InputArea internal constructor(8
internal val owner: InputRouter,9
focusable: Boolean,10
textInput: Boolean,11
parent: InputScope,12
handler: (RoutedEventContext, InputEvent) -> Unit13
): Closeable {14
internal var caret: Rect? = None15
internal var caretRevision: Long? = None16
internal var textInputRevision = 0UL17
private var acceptsFocus = focusable18
private var acceptsText = textInput19
internal var lastFocusRequest: ULong? = None20
internal var lastFocusOrigin = FocusOrigin.Programmatic21
private var visibility = FocusVisibility.Automatic22
public val scope: InputScope = InputScope(owner, Some(this), Some(parent), handler)23
internal val closed: Boolean get() = scope.closed24
private var ignored = false25
public var focusable: Boolean26
get() {27
owner.checkThread()28
return acceptsFocus29
}30
set(value) {31
checkOpen()32
requireInput(value || !acceptsText, "text input requires focus capability")33
if (acceptsFocus != value) {34
acceptsFocus = value35
owner.changed()36
}37
}38
public var textInput: Boolean39
get() {40
owner.checkThread();41
return acceptsText42
}43
set(value) {44
checkOpen()45
if (acceptsText != value) {46
acceptsText = value47
if (value)48
acceptsFocus = true49
owner.changed()50
}51
}52
public val isClosed: Boolean get() {53
owner.checkThread();54
return closed55
}56
public val hasFocus: Boolean get() {57
owner.checkThread();58
return sameArea(owner.logicalFocus, Some(this))59
}60
public val isFocusActive: Boolean get() {61
owner.checkThread();62
return sameArea(owner.activeFocus, Some(this))63
}64
public val focusOrigin: FocusOrigin get() {65
owner.checkThread()66
return lastFocusOrigin67
}68
public var focusVisibility: FocusVisibility69
get() {70
owner.checkThread();71
return visibility72
}73
set(value) {74
checkOpen()75
if (visibility != value) {76
visibility = value77
owner.changed()78
}79
}80
public val isFocusVisible: Boolean get() {81
owner.checkThread()82
return !closed &&83
isFocusActive &&84
when (visibility) {85
case Automatic -> textInput || lastFocusOrigin != FocusOrigin.Pointer86
case Always -> true87
case Never -> false88
}89
}90
/** Window sequence of the last successful focus request, including pointer focus. */91
public val focusRevision: ULong? get() {92
owner.checkThread()93
return if (closed)94
None95
else96
lastFocusRequest97
}98
public val windowBounds: Rect? get() {99
owner.checkThread()100
if (closed)101
return None102
val placement = owner.scene.find(this) ?: return None103
return Some(placement.geometry.bounds)104
}105
/** Latest observed window pointer mapped through the currently committed geometry. */106
public val pointerPosition: Point? get() {107
owner.checkThread()108
if (closed || !owner.inside)109
return None110
val pointer = owner.pointer ?: return None111
val placement = owner.scene.find(this) ?: return None112
return placement.geometry.toLocal(113
Point(pointer.logicalPosition.x, pointer.logicalPosition.y)114
)115
}116
public val visibleBounds: Rect? get() {117
owner.checkThread()118
if (closed)119
return None120
val placement = owner.scene.find(this) ?: return None121
return placement.geometry.visibleBounds122
}123
/** The clipped caret from a successful frame that still matches the current document. */124
public val visibleCaretBounds: Rect? get() {125
owner.checkThread()126
return if (closed)127
None128
else129
owner.currentCaret(this).rect130
}131
/** Tests the current accepted hit path, including clipping and foreground areas. */132
public fun hitTest(windowPoint: Point): Boolean {133
owner.checkThread()134
return !closed && containsScope(owner.hit(windowPoint), scope)135
}136
/** Maps a window point through the current successful placement. */137
public fun toLocal(windowPoint: Point): Point? {138
owner.checkThread()139
if (closed)140
return None141
val placement = owner.scene.find(this) ?: return None142
return placement.geometry.toLocal(windowPoint)143
}144
/** Maps a local subregion without exposing a borrowed geometry object. */145
public fun toWindow(localRect: Rect): Rect? {146
owner.checkThread()147
if (closed)148
return None149
val placement = owner.scene.find(this) ?: return None150
if (placement.geometry.toLocal(Point(0.0, 0.0)) == None)151
return None152
return Some(placement.geometry.toWindow(localRect))153
}154
public val canFocus: Boolean get() {155
owner.checkThread()156
return !closed &&157
focusable &&158
owner.allowsFocus(scope) &&159
owner.path(scope).size >160
0L &&161
visibleBounds !=162
None163
}164
public var ignoreSelf: Boolean165
get() {166
owner.checkThread();167
return ignored168
}169
set(value) {170
checkOpen();171
if (ignored != value) {172
ignored = value;173
owner.changed()174
}175
}176
/** Returns false when this area has no active placement or does not accept focus. */177
public fun requestFocus(origin: FocusOrigin = FocusOrigin.Programmatic): Boolean {178
checkOpen();179
return owner.focus(this, origin)180
}181
public fun clearFocus() {182
checkOpen()183
if (hasFocus)184
owner.clearFocus()185
else186
owner.reserveFocusRequest()187
}188
/** Returns whether this area already owns the native cycle being retired. */189
public fun restartTextInput(): Boolean {190
checkOpen()191
return owner.restartTextInput(this)192
}193
/** Continues the source's current native document when ordinary focus reaches this area. */194
public fun continueTextInputFrom(source: InputArea): Boolean {195
checkOpen()196
return owner.continueTextInput(source, this)197
}198
/** Stages a value for the next placement; None withdraws unavailable geometry. */199
public fun setCaret(rect: Rect?, revision: Long? = None) {200
checkOpen()201
when (revision) {202
case Some(val value) -> requireInput(value >= 0L, "negative text revision");203
case None -> {}204
}205
when (rect) {206
case Some(val value) -> requireInput(207
value.x.isFinite() &&208
value.y.isFinite() &&209
value.width.isFinite() &&210
value.height.isFinite() &&211
value.width >=212
0.0 &&213
value.height >=214
0.0 &&215
(value.x + value.width).isFinite() &&216
(value.y + value.height).isFinite(),217
"invalid area caret rectangle"218
)219
case None -> {}220
}221
if (caret != rect || caretRevision != revision) {222
caret = rect223
caretRevision = revision224
owner.changed()225
}226
}227
internal fun checkOpen() {228
scope.checkOpen()229
}230
public override fun close() {231
scope.close()232
}233
}235
internal fun sameArea(left: InputArea?, right: InputArea?): Boolean =236
when (left) {237
case Some(val a) -> when (right) {238
case Some(val b) -> a === b;239
case None -> false240
}241
case None -> when (right) {242
case Some(_) -> false;243
case None -> true244
}245
}247
internal fun requireInput(condition: Boolean, message: String) {248
if (!condition)249
throw IllegalArgumentException(Some(message))250
}