Scoop API

scoop:scoop.ui:0.1.0

src/accessibility/reference.scoop

Selected production source · src/accessibility/reference.scoop

1package scoop.ui
3import scoop.platform.concurrent.currentThreadId
4import scoop.window.accessibility.SemanticNodeId
6/** A render-observable reference to one actual mounted semantic node. */
7public class SemanticsReference public constructor(): Equals<SemanticsReference> {
8 private val thread = currentThreadId()
9 private val current = state<SemanticNodeId?>(None)
10 private var owner: SemanticsHandle? = None
11 public val id: SemanticNodeId? get() {
12 checkThread()
13 return current.value
14 }
15 public override operator fun equals(other: SemanticsReference): Boolean = this === other
16 private fun checkThread() {
17 if (currentThreadId() != thread)
18 throw IllegalStateException(Some("semantic reference used from another thread"))
19 }
20 internal fun checkTarget(handle: SemanticsHandle) {
21 checkThread()
22 when (owner) {
23 case Some(val previous) -> requireUi(
24 previous === handle || previous.isClosed,
25 "semantic reference already has an active target"
26 )
27 case None -> {}
28 }
29 }
30 internal fun attach(handle: SemanticsHandle) {
31 checkTarget(handle)
32 when (owner) {
33 case Some(val previous) -> if (previous === handle)
34 return
35 case None -> {}
36 }
37 owner = Some(handle)
38 current.value = Some(handle.id)
39 }
40 internal fun detach(handle: SemanticsHandle) {
41 checkThread()
42 when (owner) {
43 case Some(val previous) -> if (previous === handle) {
44 owner = None
45 current.value = None
46 }
47 case None -> {}
48 }
49 }