scoop:scoop.ui:0.1.0
src/accessibility/root.scoop
Selected production source · src/accessibility/root.scoop
1
package scoop.ui3
import scoop.graphics.Canvas4
import scoop.graphics.Rect5
import scoop.graphics.Path6
import scoop.surface.FrameCompletion7
import scoop.surface.FrameSubmission8
import scoop.surface.InputArea9
import scoop.surface.InputBoundary10
import scoop.window.accessibility.*11
import scoop.ui.text.NativeTextSource13
private class SemanticCompletion {14
var result = SemanticActionResult.Unavailable15
}17
/** The single semantic owner attached lazily to one UI root. */18
internal class RootSemantics(19
private val root: UiRoot,20
private val provider: AccessibilityProvider21
) : Closeable {22
private val handles = ArrayList<SemanticsHandle>()23
private var ordered: Array<SemanticsHandle> = []24
private var structure = true25
private var content = true26
private var dirty = true27
private var closed = false28
private var drawing: SemanticScene? = None29
private var focusedArea: InputArea? = None30
private var focusActive = false31
private var boundary: InputBoundary? = None32
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
inherited43
)44
handle.resource.update(handle.properties, semanticActions(handle, handle.actions))45
handles.add(handle)46
owner.semanticHandles.add(handle)47
structureChanged()48
return handle49
}51
fun changed() {52
dirty = true53
if (!closed && root.isOpen) root.scheduleUpdate()54
}55
fun structureChanged() {56
structure = true57
contentChanged()58
}59
fun contentChanged() {60
content = true61
changed()62
}64
fun publish() {65
if (closed || !dirty || !root.isOpen || provider.isClosed) return66
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 -> return75
}76
structure = false77
}78
if (content) {79
SemanticContent(ordered).apply()80
content = false81
}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(_) -> return87
case None -> {}88
}89
var focused: AccessibilityNode? = None90
for (handle in handles) {91
if (handle.active && (handle.focus?.handle?.isFocusActive ?: false)) {92
focused = Some(handle.resource)93
break94
}95
}96
for (handle in handles)97
if (handle.active) handle.refreshActivation()98
provider.focused = focused99
provider.publish()100
dirty = false101
}103
private fun focusChanged() {104
val nextBoundary = root.surface.inputBoundary105
val sameBoundary = when ((boundary, nextBoundary)) {106
case(Some(val before), Some(val after)) -> before === after107
case(None, None) -> true108
else -> false109
}110
if (!sameBoundary) {111
boundary = nextBoundary112
contentChanged()113
}114
if (closed || !root.isOpen) return115
reveals.check()116
val current = root.surface.focusedArea117
val active = current?.isFocusActive ?: false118
val same = when ((focusedArea, current)) {119
case(Some(val before), Some(val after)) -> before === after120
case(None, None) -> true121
else -> false122
}123
if (same && focusActive == active) return124
focusedArea = current125
focusActive = active126
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.Unavailable143
if (root.host.isDispatching) return SemanticActionResult.Busy144
when (currentEvaluation()) {145
case Some(_) -> return SemanticActionResult.Busy146
case None -> {}147
}148
if (!handle.owner.scope.input.isInputAllowed) return SemanticActionResult.Rejected149
val completed = SemanticCompletion()150
root151
.surface152
.dispatch {153
completed.result = action()154
if (root.isOpen) root.flush()155
}156
return completed.result157
}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: ScrollAlignment175
): SemanticActionResult = reveals.requestRegion(handle, region, horizontal, vertical)176
fun reveal(handle: SemanticsHandle): SemanticActionResult = reveals.request(handle)177
fun revealItem(parent: SemanticsHandle, entry: LazyItemEntry): SemanticActionResult = reveals178
.requestItem(parent, entry)179
fun revealCell(180
parent: SemanticsHandle,181
entry: LazyItemEntry,182
cell: SemanticsHandle,183
columnEntry: LazyItemEntry?,184
column: SemanticItemKey185
): 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 = drawing210
drawing = None211
try {212
if (closed || provider.isClosed) return213
if (frame.submission != FrameSubmission.Accepted) {214
reveals.discarded()215
publish()216
return217
}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 = None225
when (handle.textBinding) {226
case Some(val binding) -> binding.path = None;227
case None -> {}228
}229
handle.revealPlacement = None230
}231
}232
reveals.complete()233
dirty = true234
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.parent244
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 = parent249
}250
if (sameSemantics(child.explicitParent, Some(handle)))251
child.explicitParent = parent252
}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
break261
}262
if (handle.owner.active && root.isOpen) handle.owner.invalidateScene()263
structureChanged()264
}266
public override fun close() {267
if (closed) return268
closed = true269
reveals.close()270
val scene = drawing271
drawing = None272
focusedArea = None273
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
}282
}