Scoop API

scoop:scoop.ui:0.1.0

src/accessibility/root.scoop

Selected production source · src/accessibility/root.scoop

1package scoop.ui
3import scoop.graphics.Canvas
4import scoop.graphics.Rect
5import scoop.graphics.Path
6import scoop.surface.FrameCompletion
7import scoop.surface.FrameSubmission
8import scoop.surface.InputArea
9import scoop.surface.InputBoundary
10import scoop.window.accessibility.*
11import scoop.ui.text.NativeTextSource
13private class SemanticCompletion {
14 var result = SemanticActionResult.Unavailable
17/** The single semantic owner attached lazily to one UI root. */
18internal class RootSemantics(
19 private val root: UiRoot,
20 private val provider: AccessibilityProvider
21) : Closeable {
22 private val handles = ArrayList<SemanticsHandle>()
23 private var ordered: Array<SemanticsHandle> = []
24 private var structure = true
25 private var content = true
26 private var dirty = true
27 private var closed = false
28 private var drawing: SemanticScene? = None
29 private var focusedArea: InputArea? = None
30 private var focusActive = false
31 private var boundary: InputBoundary? = None
32 private val reveals = SemanticRevealController(root)
33 private val observer = root.surface.input.observeActivity { focusChanged() }
35 fun create(owner: MountedNode, role: SemanticRole, parent: SemanticsHandle?): SemanticsHandle {
36 val inherited = semanticParent(parent, inheritedSemantics(owner))
37 val handle = SemanticsHandle(
38 owner,
39 this,
40 provider.createNode(role, inherited?.resource),
41 parent,
42 inherited
43 )
44 handle.resource.update(handle.properties, semanticActions(handle, handle.actions))
45 handles.add(handle)
46 owner.semanticHandles.add(handle)
47 structureChanged()
48 return handle
49 }
51 fun changed() {
52 dirty = true
53 if (!closed && root.isOpen) root.scheduleUpdate()
54 }
55 fun structureChanged() {
56 structure = true
57 contentChanged()
58 }
59 fun contentChanged() {
60 content = true
61 changed()
62 }
64 fun publish() {
65 if (closed || !dirty || !root.isOpen || provider.isClosed) return
66 if (structure) {
67 when (val node = root.node) {
68 case Some(val value) -> {
69 val order = SemanticOrder()
70 order.apply(value, None)
71 ordered = order.handles.toArray()
72 }
74 case None -> return
75 }
76 structure = false
77 }
78 if (content) {
79 SemanticContent(ordered).apply()
80 content = false
81 }
82 refreshSemanticTextChildren(ordered)
83 reveals.check()
84 // Resolve content revisions before capture; publish the frame's final input geometry once.
85 when (drawing) {
86 case Some(_) -> return
87 case None -> {}
88 }
89 var focused: AccessibilityNode? = None
90 for (handle in handles) {
91 if (handle.active && (handle.focus?.handle?.isFocusActive ?: false)) {
92 focused = Some(handle.resource)
93 break
94 }
95 }
96 for (handle in handles)
97 if (handle.active) handle.refreshActivation()
98 provider.focused = focused
99 provider.publish()
100 dirty = false
101 }
103 private fun focusChanged() {
104 val nextBoundary = root.surface.inputBoundary
105 val sameBoundary = when ((boundary, nextBoundary)) {
106 case(Some(val before), Some(val after)) -> before === after
107 case(None, None) -> true
108 else -> false
109 }
110 if (!sameBoundary) {
111 boundary = nextBoundary
112 contentChanged()
113 }
114 if (closed || !root.isOpen) return
115 reveals.check()
116 val current = root.surface.focusedArea
117 val active = current?.isFocusActive ?: false
118 val same = when ((focusedArea, current)) {
119 case(Some(val before), Some(val after)) -> before === after
120 case(None, None) -> true
121 else -> false
122 }
123 if (same && focusActive == active) return
124 focusedArea = current
125 focusActive = active
126 changed()
127 }
129 fun checkFocus(handle: SemanticsHandle, target: FocusTarget) {
130 for (other in handles)
131 when (val bound = other.focus) {
132 case Some(val value) ->
133 requireUi(
134 other === handle || !other.active || value !== target,
135 "focus target already has a semantic node"
136 )
137 case None -> {}
138 }
139 }
141 fun perform(handle: SemanticsHandle, action: () -> SemanticActionResult): SemanticActionResult {
142 if (closed || !handle.active) return SemanticActionResult.Unavailable
143 if (root.host.isDispatching) return SemanticActionResult.Busy
144 when (currentEvaluation()) {
145 case Some(_) -> return SemanticActionResult.Busy
146 case None -> {}
147 }
148 if (!handle.owner.scope.input.isInputAllowed) return SemanticActionResult.Rejected
149 val completed = SemanticCompletion()
150 root
151 .surface
152 .dispatch {
153 completed.result = action()
154 if (root.isOpen) root.flush()
155 }
156 return completed.result
157 }
159 fun beginFrame() {
160 drawing?.close()
161 drawing = Some(SemanticScene())
162 }
163 private fun frame(): SemanticScene {
164 when (drawing) {
165 case None -> drawing = Some(SemanticScene())
166 case Some(_) -> {}
167 }
168 return drawing!!
169 }
170 fun revealText(
171 handle: SemanticsHandle,
172 region: () -> Rect?,
173 horizontal: ScrollAlignment,
174 vertical: ScrollAlignment
175 ): SemanticActionResult = reveals.requestRegion(handle, region, horizontal, vertical)
176 fun reveal(handle: SemanticsHandle): SemanticActionResult = reveals.request(handle)
177 fun revealItem(parent: SemanticsHandle, entry: LazyItemEntry): SemanticActionResult = reveals
178 .requestItem(parent, entry)
179 fun revealCell(
180 parent: SemanticsHandle,
181 entry: LazyItemEntry,
182 cell: SemanticsHandle,
183 columnEntry: LazyItemEntry?,
184 column: SemanticItemKey
185 ): SemanticActionResult = reveals.requestCell(parent, entry, cell, columnEntry, column)
186 fun cancelCollectionReveal(parent: SemanticsHandle) {
187 reveals.cancelCollection(parent)
188 }
189 fun cancelReveal() {
190 reveals.cancel()
191 }
193 fun capture(handle: SemanticsHandle, canvas: Canvas, rect: Rect, path: Array<ScrollReveal>) {
194 frame().capture(handle, canvas, rect, path)
195 }
196 fun capture(handle: SemanticsHandle, canvas: Canvas, shape: Path, path: Array<ScrollReveal>) {
197 frame().capture(handle, canvas, shape, path)
198 }
199 fun captureText(
200 binding: SemanticTextBinding,
201 canvas: Canvas,
202 bounds: Rect,
203 source: NativeTextSource,
204 path: Array<ScrollReveal>
205 ) {
206 frame().captureText(binding, canvas, bounds, source, path)
207 }
208 fun complete(frame: FrameCompletion) {
209 val scene = drawing
210 drawing = None
211 try {
212 if (closed || provider.isClosed) return
213 if (frame.submission != FrameSubmission.Accepted) {
214 reveals.discarded()
215 publish()
216 return
217 }
218 for (handle in handles)
219 if (handle.active)
220 when (scene) {
221 case Some(val value) -> value.apply(handle)
223 case None -> {
224 handle.resource.geometry = None
225 when (handle.textBinding) {
226 case Some(val binding) -> binding.path = None;
227 case None -> {}
228 }
229 handle.revealPlacement = None
230 }
231 }
232 reveals.complete()
233 dirty = true
234 publish()
235 } finally {
236 scene?.close()
237 }
238 }
240 fun retire(handle: SemanticsHandle) {
241 reveals.retire(handle)
242 if (!closed && !provider.isClosed) {
243 val parent = handle.parent
244 for (child in handles) {
245 if (child !== handle && !child.resource.isClosed) {
246 if (sameSemantics(child.parent, Some(handle))) {
247 child.resource.reparent(parent?.resource)
248 child.parent = parent
249 }
250 if (sameSemantics(child.explicitParent, Some(handle)))
251 child.explicitParent = parent
252 }
253 }
254 if (!handle.resource.isClosed) handle.resource.close()
255 }
256 handle.finishClose()
257 for (index in 0L..<handles.size)
258 if (handles[index] === handle) {
259 handles.removeAt(index)
260 break
261 }
262 if (handle.owner.active && root.isOpen) handle.owner.invalidateScene()
263 structureChanged()
264 }
266 public override fun close() {
267 if (closed) return
268 closed = true
269 reveals.close()
270 val scene = drawing
271 drawing = None
272 focusedArea = None
273 var failure = cleanupUi(None) { scene?.close() }
274 failure = cleanupUi(failure) { observer.close() }
275 failure = cleanupUi(failure) { provider.close() }
276 for (handle in handles)
277 handle.finishClose()
278 handles.clear()
279 ordered = []
280 throwUi(failure)
281 }