scoop:scoop.ui:0.1.0
src/accessibility/scene.scoop
Selected production source · src/accessibility/scene.scoop
1
package scoop.ui3
import scoop.collections.HashMap4
import scoop.graphics.*5
import scoop.graphics.native.*6
import scoop.window.LogicalRect7
import scoop.window.LogicalPosition8
import scoop.window.LogicalSize9
import scoop.window.accessibility.*10
import scoop.window.accessibility.native.NativeSemanticHitTest11
import scoop.window.accessibility.native.NativeSemanticTextGeometry12
import scoop.ui.text.NativeTextSource14
private class SemanticPlacement(15
val generation: ULong,16
private val geometry: SemanticGeometry,17
private val order: Long,18
private val hit: NativeGeometry,19
val reveal: SemanticRevealPlacement20
): Closeable {21
private var closed = false22
init {23
@Unsafe24
{25
hit.retain(hit.context)26
}27
}28
fun apply(node: AccessibilityNode) {29
@Unsafe30
{31
node.setGeometry(32
geometry,33
NativeSemanticHitTest(hit.context, hit.retain, hit.release, hit.contains),34
order35
)36
}37
}38
public override fun close() {39
if (closed)40
return41
closed = true42
@Unsafe43
{44
hit.release(hit.context)45
}46
}47
}49
/** A candidate owns immutable native snapshots until matching accepted geometry is installed. */50
internal class SemanticScene: Closeable {51
private val placements = HashMap<SemanticNodeId, SemanticPlacement>()52
private val text = HashMap<SemanticNodeId, SemanticTextPlacement>()54
fun captureText(55
binding: SemanticTextBinding,56
canvas: Canvas,57
bounds: Rect,58
source: NativeTextSource,59
path: Array<ScrollReveal>60
) {61
val handle = binding.handle62
requireUi(!text.containsKey(handle.id), "semantic text was already placed this frame")63
@Unsafe64
{65
captureGeometry(canvas, bounds).use {66
geometry -> @Unsafe67
{68
geometry.withNativeGeometry {69
scene -> @Unsafe70
{71
source.withGeometry(scene) {72
native ->73
val placement =74
SemanticTextPlacement(binding, handle.generation, native, path)75
try {76
text[handle.id] = placement77
} catch (error: Throwable) {78
placement.close()79
throw error80
}81
}82
}83
}84
}85
}86
}87
}89
fun capture(handle: SemanticsHandle, canvas: Canvas, rect: Rect, path: Array<ScrollReveal>) {90
check(handle)91
@Unsafe92
{93
captureGeometry(canvas, rect).use { shape -> append(handle, shape, path) }94
}95
}97
fun capture(handle: SemanticsHandle, canvas: Canvas, shape: Path, path: Array<ScrollReveal>) {98
check(handle)99
@Unsafe100
{101
captureGeometry(canvas, shape).use { value -> append(handle, value, path) }102
}103
}105
private fun check(handle: SemanticsHandle) {106
requireUi(!placements.containsKey(handle.id), "semantic node was already placed this frame")107
}109
private fun append(handle: SemanticsHandle, shape: CanvasGeometry, path: Array<ScrollReveal>) {110
val visible =111
when (val value = shape.visibleBounds) {112
case Some(val bounds) -> Some(semanticRect(bounds))113
case None -> None114
}115
val rect = shape.bounds116
val bounds = SemanticGeometry(semanticRect(rect), visible)117
val revealed =118
when (visible) {119
case Some(val value) -> value.size.width > 0.0 && value.size.height > 0.0120
case None -> false121
}122
@Unsafe123
{124
shape.withNativeGeometry {125
hit ->126
val placement =127
SemanticPlacement(128
handle.generation,129
bounds,130
placements.size,131
hit,132
SemanticRevealPlacement(handle.generation, path, rect, revealed)133
)134
try {135
placements[handle.id] = placement136
} catch (error: Throwable) {137
placement.close()138
throw error139
}140
}141
}142
}144
fun apply(handle: SemanticsHandle) {145
when (handle.textBinding) {146
case Some(val binding) -> binding.path = None;147
case None -> {}148
}149
when (val placement = placements[handle.id]) {150
case Some(val value) -> if (value.generation == handle.generation) {151
value.apply(handle.resource)152
handle.revealPlacement = Some(value.reveal)153
text[handle.id]?.apply(handle)154
return155
}156
case None -> {}157
}158
handle.resource.geometry = None159
handle.revealPlacement = None160
}162
public override fun close() {163
for (placement in placements.values)164
placement.close()165
placements.clear()166
for (placement in text.values)167
placement.close()168
text.clear()169
}170
}172
private class SemanticTextPlacement(173
private val binding: SemanticTextBinding,174
private val generation: ULong,175
private val native: NativeSemanticTextGeometry,176
private val path: Array<ScrollReveal>177
): Closeable {178
private var closed = false179
init {180
@Unsafe181
{182
native.retain(native.context)183
}184
}185
fun apply(handle: SemanticsHandle) {186
if (binding.active && generation == handle.generation)187
@Unsafe188
{189
handle.resource.setTextGeometry(Some(native))190
binding.path = Some(path)191
}192
}193
public override fun close() {194
if (closed)195
return196
closed = true197
@Unsafe198
{199
native.release(native.context)200
}201
}202
}204
private fun semanticRect(rect: Rect): LogicalRect =205
LogicalRect(LogicalPosition(rect.x, rect.y), LogicalSize(rect.width, rect.height))