Scoop API

scoop:scoop.ui:0.1.0

src/accessibility/handle.scoop

Selected production source · src/accessibility/handle.scoop

1package scoop.ui
3import scoop.window.accessibility.*
4import scoop.collections.HashMap
6/** One scope-owned semantic identity, independent of drawing and input areas. */
7public class SemanticsHandle internal constructor(
8 internal val owner: MountedNode,
9 internal val controller: RootSemantics,
10 internal val resource: AccessibilityNode,
11 internal var explicitParent: SemanticsHandle?,
12 internal var parent: SemanticsHandle?
13) : Closeable {
14 private var closed = false
15 private var references: Array<SemanticsReference> = []
16 internal var properties = SemanticProperties()
17 internal var actions = SemanticActions()
18 private var declaredRelations = SemanticRelations()
19 internal var focus: FocusTarget? = None
20 internal var merged = false
21 internal var contentExposure = 0
22 internal var revealPlacement: SemanticRevealPlacement? = None
23 internal var textBinding: SemanticTextBinding? = None
24 internal var textChild: SemanticTextChildBinding? = None
25 private var merging = false
26 internal var collectionSource: ItemSource? = None
27 internal var collectionItems: ItemSemantics? = None
28 internal var collectionViewport: ScrollViewport? = None
29 internal val gridColumns = HashMap<ViewKey, SemanticGridColumns>()
30 internal val generation: ULong get() = resource.contentRevision
32 public val id: SemanticNodeId get() = resource.id
33 public val role: SemanticRole get() = resource.role
34 /** Merges plain text leaves in this semantic container, retaining interactive children. */
35 public var mergeLabels: Boolean
36 get() {
37 checkOpen()
38 return merging
39 }
40 set(value) {
41 checkOpen()
42 checkSemanticsMutation()
43 if (merging == value) return
44 merging = value
45 controller.contentChanged()
46 }
48 internal val mergeCandidate: Boolean
49 get() {
50 if (role !=
51 SemanticRole.Text ||
52 merging ||
53 properties.protectedText ||
54 properties.selection !=
55 None
56 )
57 return false
58 if (!actions.isEmpty) return false
59 return when (focus) {
60 case None -> true;
61 case Some(_) -> false
62 }
63 }
64 /** Current same-root references, with retired targets removed. */
65 public val relations: SemanticRelations
66 get() {
67 checkOpen()
68 return resource.relations
69 }
70 public val isClosed: Boolean
71 get() {
72 owner.root.checkThread()
73 return !active
74 }
75 internal val active: Boolean
76 get() = !closed && owner.active && owner.root.isOpen && !resource.isClosed
78 /** The last accepted geometry matching the current semantic content. */
79 public val geometry: SemanticGeometry?
80 get() {
81 checkOpen()
82 return resource.geometry
83 }
85 public fun announce(text: String, priority: SemanticLive = SemanticLive.Polite) {
86 checkOpen()
87 checkSemanticsMutation()
88 if (!owner.scope.input.isInputAllowed) return
89 resource.announce(text, priority)
90 controller.changed()
91 }
93 /** Retaining the same reference does not invalidate its render readers. */
94 public fun bindReference(next: SemanticsReference?) {
95 bindReferences(
96 when (next) {
97 case Some(val value) -> [value];
98 case None -> []
99 }
100 )
101 }
103 /** Allows a component's public reference and its internal relationships to share one node. */
104 public fun bindReferences(next: Array<SemanticsReference>) {
105 checkOpen()
106 checkSemanticsMutation()
107 for (value in next)
108 value.checkTarget(this)
109 for (old in references) {
110 var retained = false
111 for (value in next)
112 if (value === old) retained = true
113 if (!retained) old.detach(this)
114 }
115 for (value in next)
116 value.attach(this)
117 references = next
118 }
120 /** Replaces facts, callbacks and relations without changing identity or requesting a frame. */
121 public fun update(
122 properties: SemanticProperties,
123 actions: SemanticActions = SemanticActions(),
124 relations: SemanticRelations = SemanticRelations()
125 ) {
126 updateFacts(properties, actions, relations)
127 textBinding?.close()
128 }
130 internal fun updateText(
131 host: SemanticTextHost?,
132 properties: SemanticProperties,
133 actions: SemanticActions,
134 relations: SemanticRelations
135 ) {
136 updateFacts(properties, actions, relations)
137 when ((textBinding, host)) {
138 case(Some(val previous), Some(val current)) -> {
139 if (previous.host === current) return
140 }
142 else -> {}
143 }
144 textBinding?.close()
145 when (host) {
146 case Some(val current) -> {
147 current.binding?.close()
148 val binding = SemanticTextBinding(this, current)
149 textBinding = Some(binding)
150 current.binding = Some(binding)
151 owner.invalidateScene()
152 }
154 case None -> {}
155 }
156 }
158 private fun updateFacts(
159 properties: SemanticProperties,
160 actions: SemanticActions,
161 relations: SemanticRelations
162 ) {
163 checkOpen()
164 checkSemanticsMutation()
165 val candidate = mergeCandidate
166 val exposure = this.properties.ignored !=
167 properties.ignored ||
168 this.properties.hidden !=
169 properties.hidden
170 publishFacts(
171 properties,
172 actions,
173 if (declaredRelations == relations) resource.relations else relations
174 )
175 this.properties = properties
176 this.actions = actions
177 declaredRelations = relations
178 if (exposure || candidate != mergeCandidate)
179 controller.contentChanged()
180 else
181 controller.changed()
182 }
184 /** Borrows the real focus target; binding never requests keyboard focus. */
185 public fun bindFocus(target: FocusTarget?) {
186 checkOpen()
187 checkSemanticsMutation()
188 when (target) {
189 case Some(val value) -> {
190 requireUi(
191 value.active && value.node === owner,
192 "semantic focus belongs to another mount"
193 )
194 controller.checkFocus(this, value)
195 }
197 case None -> {}
198 }
199 val candidate = mergeCandidate
200 focus = target
201 publishFacts(properties, actions, resource.relations)
202 if (candidate != mergeCandidate) controller.contentChanged() else controller.changed()
203 }
205 internal fun publishFacts(
206 properties: SemanticProperties,
207 actions: SemanticActions,
208 relations: SemanticRelations
209 ) {
210 val exposure = semanticInputExposure(this)
211 resource
212 .update(
213 semanticProperties(this, properties, merged, exposure),
214 semanticActions(this, actions),
215 relations
216 )
217 contentExposure = exposure
218 }
220 internal fun checkOpen() {
221 owner.root.checkThread()
222 if (!active) throw IllegalStateException(Some("semantic node is no longer mounted"))
223 }
225 internal fun finishClose() {
226 if (closed) return
227 closed = true
228 for (reference in references)
229 reference.detach(this)
230 references = []
231 collectionSource = None
232 collectionItems = None
233 collectionViewport = None
234 when (owner.lazySemantics) {
235 case Some(val current) ->
236 if (current === this) {
237 owner.lazySemantics = None
238 owner.lazyItems?.unbindSemantics()
239 }
240 case None -> {}
241 }
242 when (owner.semanticItem) {
243 case Some(val current) -> if (current === this) owner.semanticItem = None
244 case None -> {}
245 }
246 textBinding?.close()
247 textChild = None
248 actions = SemanticActions()
249 revealPlacement = None
250 declaredRelations = SemanticRelations()
251 focus = None
252 explicitParent = None
253 parent = None
254 when (val route = owner.semanticRoute) {
255 case Some(val value) -> if (value === this) owner.semanticRoute = None
256 case None -> {}
257 }
258 for (index in 0L..<owner.semanticHandles.size)
259 if (owner.semanticHandles[index] === this) {
260 owner.semanticHandles.removeAt(index)
261 break
262 }
263 owner.scope.forgetSemantics(this)
264 }
266 /** Retires this identity while preserving independently owned semantic children. */
267 public override fun close() {
268 owner.root.checkThread()
269 checkSemanticsMutation()
270 if (!closed) controller.retire(this)
271 }
274internal fun checkSemanticsMutation() {
275 when (currentEvaluation()) {
276 case Some(_) ->
277 throw IllegalStateException(
278 Some("semantics cannot change during render, measure, scene or draw")
279 )
280 case None -> {}
281 }
284internal fun sameSemantics(a: SemanticsHandle?, b: SemanticsHandle?): Boolean = when ((a, b)) {
285 case(Some(val first), Some(val second)) -> first === second
286 case(None, None) -> true
287 else -> false