Scoop API

scoop:scoop.ui.component:0.1.0

src/field/target.scoop

Selected production source · src/field/target.scoop

1package scoop.ui.component
3import scoop.surface.*
4import scoop.ui.*
5import scoop.window.accessibility.*
7/** Stable label association; the mounted control owns its lifetime and action. */
8public class FieldTarget public constructor() {
9 private val current = state<FieldLink?>(None)
10 private val annotations = ArrayList<FieldAnnotation>()
11 private val requirements = ArrayList<FieldRequirement>()
12 internal val connection: FieldLink? get() = current.value
13 public val isAttached: Boolean get() = current.value?.isAttached ?: false
14 public val isEnabled: Boolean get() = current.value?.isEnabled ?: false
15 internal fun hasSemantics(source: ComponentScope): Boolean =
16 current.value?.hasSemantics(source) ?: false
18 internal fun checkBinding(link: FieldLink) {
19 when (current.value) {
20 case Some(val previous) -> requireComponent(
21 previous === link || !previous.isAttached,
22 "field target already has a control"
23 )
24 case None -> {}
25 }
26 }
27 internal fun bind(link: FieldLink) {
28 current.value = Some(link)
29 }
30 internal fun unbind(link: FieldLink) {
31 when (current.value) {
32 case Some(val previous) -> if (previous === link)
33 current.value = None
34 case None -> {}
35 }
36 }
37 internal fun addAnnotation(annotation: FieldAnnotation) {
38 annotations.add(annotation)
39 current.value?.semanticsChanged()
40 }
41 internal fun removeAnnotation(annotation: FieldAnnotation) {
42 for (index in 0L ..< annotations.size)
43 if (annotations[index] === annotation) {
44 annotations.removeAt(index)
45 current.value?.semanticsChanged()
46 return
47 }
48 }
49 internal fun semanticsChanged() {
50 current.value?.semanticsChanged()
51 }
52 internal fun addRequirement(requirement: FieldRequirement) {
53 requirements.add(requirement)
54 semanticsChanged()
55 }
56 internal fun removeRequirement(requirement: FieldRequirement) {
57 for (index in 0L ..< requirements.size)
58 if (requirements[index] === requirement) {
59 requirements.removeAt(index)
60 semanticsChanged()
61 return
62 }
63 }
64 internal fun isRequired(source: SemanticsHandle): Boolean {
65 for (requirement in requirements)
66 if (requirement.appliesTo(source))
67 return true
68 return false
69 }
70 internal fun relations(
71 source: SemanticsHandle,
72 declared: SemanticRelations
73 ): SemanticRelations {
74 val labels = ArrayList<SemanticNodeId>()
75 val descriptions = ArrayList<SemanticNodeId>()
76 val errors = ArrayList<SemanticNodeId>()
77 for (annotation in annotations) {
78 val node = annotation.nodeFor(source) ?: continue
79 when (annotation.kind) {
80 case Label -> labels.add(node.id)
81 case Description -> descriptions.add(node.id)
82 case Error -> errors.add(node.id)
83 }
84 }
85 return SemanticRelations(
86 if (declared.labelledBy.size > 0L)
87 declared.labelledBy
88 else
89 labels.toArray(),
90 if (declared.describedBy.size > 0L)
91 declared.describedBy
92 else
93 descriptions.toArray(),
94 if (declared.errorMessages.size > 0L)
95 declared.errorMessages
96 else
97 errors.toArray(),
98 declared.controls,
99 declared.activeDescendant,
100 declared.rowHeaders,
101 declared.columnHeaders
102 )
103 }
106internal class FieldLink(
107 private val scope: ComponentScope,
108 private val area: InputArea,
109 private val focus: FocusTarget,
110 private val semantics: SemanticsHandle? = None,
111 private val refreshSemantics: () -> Unit = {}
112): Closeable {
113 private val enabled = state(false)
114 private var target: FieldTarget? = None
115 private var action: () -> Unit = {}
116 private var closed = false
117 val isAttached: Boolean get() = !closed && scope.isMounted
118 val isEnabled: Boolean get() = isAttached && enabled.value
119 fun hasSemantics(source: ComponentScope): Boolean =
120 isAttached && source.nativeWindow === scope.nativeWindow && !(semantics?.isClosed ?: true)
121 fun isRequired(source: SemanticsHandle): Boolean = target?.isRequired(source) ?: false
123 fun semanticsChanged() {
124 if (isAttached)
125 refreshSemantics()
126 }
127 fun relations(source: SemanticsHandle, declared: SemanticRelations): SemanticRelations =
128 when (val current = target) {
129 case Some(val value) -> value.relations(source, declared)
130 case None -> declared
131 }
133 fun update(value: FieldTarget?, accepts: Boolean, activate: () -> Unit = {}) {
134 value?.checkBinding(this)
135 if (!sameFieldTarget(target, value)) {
136 target?.unbind(this)
137 target = value
138 value?.bind(this)
139 }
140 if (enabled.value != accepts)
141 enabled.value = accepts
142 action = activate
143 }
144 fun activate(source: ComponentScope, expected: FieldTarget) {
145 if (!isEnabled || !source.isMounted || !sameFieldTarget(target, Some(expected)))
146 return
147 if (
148 source.nativeWindow !==
149 scope.nativeWindow ||
150 !source.input.isInputAllowed ||
151 !area.scope.isInputAllowed ||
152 !area.canFocus
153 )
154 return
155 if (focus.handle.requestFocus(FocusOrigin.Pointer))
156 action()
157 }
158 public override fun close() {
159 if (closed)
160 return
161 closed = true
162 target?.unbind(this)
163 target = None
164 action = {}
165 }
168internal fun sameFieldTarget(left: FieldTarget?, right: FieldTarget?): Boolean =
169 when (left) {
170 case Some(val a) -> when (right) {
171 case Some(val b) -> a === b
172 case None -> false
173 }
174 case None -> when (right) {
175 case Some(_) -> false
176 case None -> true
177 }
178 }