scoop:scoop.ui:0.1.0
src/accessibility/handle.scoop
Selected production source · src/accessibility/handle.scoop
1
package scoop.ui3
import scoop.window.accessibility.*4
import scoop.collections.HashMap6
/** One scope-owned semantic identity, independent of drawing and input areas. */7
public 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 = false15
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? = None20
internal var merged = false21
internal var contentExposure = 022
internal var revealPlacement: SemanticRevealPlacement? = None23
internal var textBinding: SemanticTextBinding? = None24
internal var textChild: SemanticTextChildBinding? = None25
private var merging = false26
internal var collectionSource: ItemSource? = None27
internal var collectionItems: ItemSemantics? = None28
internal var collectionViewport: ScrollViewport? = None29
internal val gridColumns = HashMap<ViewKey, SemanticGridColumns>()30
internal val generation: ULong get() = resource.contentRevision32
public val id: SemanticNodeId get() = resource.id33
public val role: SemanticRole get() = resource.role34
/** Merges plain text leaves in this semantic container, retaining interactive children. */35
public var mergeLabels: Boolean36
get() {37
checkOpen()38
return merging39
}40
set(value) {41
checkOpen()42
checkSemanticsMutation()43
if (merging == value) return44
merging = value45
controller.contentChanged()46
}48
internal val mergeCandidate: Boolean49
get() {50
if (role !=51
SemanticRole.Text ||52
merging ||53
properties.protectedText ||54
properties.selection !=55
None56
)57
return false58
if (!actions.isEmpty) return false59
return when (focus) {60
case None -> true;61
case Some(_) -> false62
}63
}64
/** Current same-root references, with retired targets removed. */65
public val relations: SemanticRelations66
get() {67
checkOpen()68
return resource.relations69
}70
public val isClosed: Boolean71
get() {72
owner.root.checkThread()73
return !active74
}75
internal val active: Boolean76
get() = !closed && owner.active && owner.root.isOpen && !resource.isClosed78
/** The last accepted geometry matching the current semantic content. */79
public val geometry: SemanticGeometry?80
get() {81
checkOpen()82
return resource.geometry83
}85
public fun announce(text: String, priority: SemanticLive = SemanticLive.Polite) {86
checkOpen()87
checkSemanticsMutation()88
if (!owner.scope.input.isInputAllowed) return89
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 = false111
for (value in next)112
if (value === old) retained = true113
if (!retained) old.detach(this)114
}115
for (value in next)116
value.attach(this)117
references = next118
}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: SemanticRelations135
) {136
updateFacts(properties, actions, relations)137
when ((textBinding, host)) {138
case(Some(val previous), Some(val current)) -> {139
if (previous.host === current) return140
}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: SemanticRelations162
) {163
checkOpen()164
checkSemanticsMutation()165
val candidate = mergeCandidate166
val exposure = this.properties.ignored !=167
properties.ignored ||168
this.properties.hidden !=169
properties.hidden170
publishFacts(171
properties,172
actions,173
if (declaredRelations == relations) resource.relations else relations174
)175
this.properties = properties176
this.actions = actions177
declaredRelations = relations178
if (exposure || candidate != mergeCandidate)179
controller.contentChanged()180
else181
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 = mergeCandidate200
focus = target201
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: SemanticRelations209
) {210
val exposure = semanticInputExposure(this)211
resource212
.update(213
semanticProperties(this, properties, merged, exposure),214
semanticActions(this, actions),215
relations216
)217
contentExposure = exposure218
}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) return227
closed = true228
for (reference in references)229
reference.detach(this)230
references = []231
collectionSource = None232
collectionItems = None233
collectionViewport = None234
when (owner.lazySemantics) {235
case Some(val current) ->236
if (current === this) {237
owner.lazySemantics = None238
owner.lazyItems?.unbindSemantics()239
}240
case None -> {}241
}242
when (owner.semanticItem) {243
case Some(val current) -> if (current === this) owner.semanticItem = None244
case None -> {}245
}246
textBinding?.close()247
textChild = None248
actions = SemanticActions()249
revealPlacement = None250
declaredRelations = SemanticRelations()251
focus = None252
explicitParent = None253
parent = None254
when (val route = owner.semanticRoute) {255
case Some(val value) -> if (value === this) owner.semanticRoute = None256
case None -> {}257
}258
for (index in 0L..<owner.semanticHandles.size)259
if (owner.semanticHandles[index] === this) {260
owner.semanticHandles.removeAt(index)261
break262
}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
}272
}274
internal 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
}282
}284
internal fun sameSemantics(a: SemanticsHandle?, b: SemanticsHandle?): Boolean = when ((a, b)) {285
case(Some(val first), Some(val second)) -> first === second286
case(None, None) -> true287
else -> false288
}