Scoop API

scoop:scoop.surface:0.1.0

src/shared/input/area.scoop

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

1package scoop.surface
3import scoop.graphics.Rect
4import scoop.graphics.Point
6/** Stable input identity; geometry is supplied by each successfully submitted frame. */
7public class InputArea internal constructor(
8 internal val owner: InputRouter,
9 focusable: Boolean,
10 textInput: Boolean,
11 parent: InputScope,
12 handler: (RoutedEventContext, InputEvent) -> Unit
13): Closeable {
14 internal var caret: Rect? = None
15 internal var caretRevision: Long? = None
16 internal var textInputRevision = 0UL
17 private var acceptsFocus = focusable
18 private var acceptsText = textInput
19 internal var lastFocusRequest: ULong? = None
20 internal var lastFocusOrigin = FocusOrigin.Programmatic
21 private var visibility = FocusVisibility.Automatic
22 public val scope: InputScope = InputScope(owner, Some(this), Some(parent), handler)
23 internal val closed: Boolean get() = scope.closed
24 private var ignored = false
25 public var focusable: Boolean
26 get() {
27 owner.checkThread()
28 return acceptsFocus
29 }
30 set(value) {
31 checkOpen()
32 requireInput(value || !acceptsText, "text input requires focus capability")
33 if (acceptsFocus != value) {
34 acceptsFocus = value
35 owner.changed()
36 }
37 }
38 public var textInput: Boolean
39 get() {
40 owner.checkThread();
41 return acceptsText
42 }
43 set(value) {
44 checkOpen()
45 if (acceptsText != value) {
46 acceptsText = value
47 if (value)
48 acceptsFocus = true
49 owner.changed()
50 }
51 }
52 public val isClosed: Boolean get() {
53 owner.checkThread();
54 return closed
55 }
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 lastFocusOrigin
67 }
68 public var focusVisibility: FocusVisibility
69 get() {
70 owner.checkThread();
71 return visibility
72 }
73 set(value) {
74 checkOpen()
75 if (visibility != value) {
76 visibility = value
77 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.Pointer
86 case Always -> true
87 case Never -> false
88 }
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 None
95 else
96 lastFocusRequest
97 }
98 public val windowBounds: Rect? get() {
99 owner.checkThread()
100 if (closed)
101 return None
102 val placement = owner.scene.find(this) ?: return None
103 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 None
110 val pointer = owner.pointer ?: return None
111 val placement = owner.scene.find(this) ?: return None
112 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 None
120 val placement = owner.scene.find(this) ?: return None
121 return placement.geometry.visibleBounds
122 }
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 None
128 else
129 owner.currentCaret(this).rect
130 }
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 None
141 val placement = owner.scene.find(this) ?: return None
142 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 None
149 val placement = owner.scene.find(this) ?: return None
150 if (placement.geometry.toLocal(Point(0.0, 0.0)) == None)
151 return None
152 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 None
163 }
164 public var ignoreSelf: Boolean
165 get() {
166 owner.checkThread();
167 return ignored
168 }
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 else
186 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 = rect
223 caretRevision = revision
224 owner.changed()
225 }
226 }
227 internal fun checkOpen() {
228 scope.checkOpen()
229 }
230 public override fun close() {
231 scope.close()
232 }
235internal 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 -> false
240 }
241 case None -> when (right) {
242 case Some(_) -> false;
243 case None -> true
244 }
245 }
247internal fun requireInput(condition: Boolean, message: String) {
248 if (!condition)
249 throw IllegalArgumentException(Some(message))