Scoop API

scoop:scoop.ui:0.1.0

src/accessibility/actions.scoop

Selected production source · src/accessibility/actions.scoop

1package scoop.ui
3import scoop.window.accessibility.*
4import scoop.window.Utf8Range
5import scoop.window.Utf8Selection
7/** Capabilities follow actual handlers; every native entry shares the root boundary. */
8internal fun semanticActions(handle: SemanticsHandle, actions: SemanticActions): SemanticActions {
9 val invoke: (() -> SemanticActionResult)? =
10 when (actions.invoke) {
11 case Some(val action) -> Some { handle.controller.perform(handle, action) }
12 case None -> None
13 }
14 val toggle: (() -> SemanticActionResult)? =
15 when (actions.toggle) {
16 case Some(val action) -> Some { handle.controller.perform(handle, action) }
17 case None -> None
18 }
19 val setValue: ((String) -> SemanticActionResult)? =
20 when (actions.setValue) {
21 case Some(val action) -> Some {
22 text -> handle.controller.perform(handle) { action(text) }
23 }
24 case None -> None
25 }
26 val focus: ((Boolean) -> SemanticActionResult)? =
27 when (actions.focus) {
28 case Some(val action) -> Some {
29 value -> handle.controller.perform(handle) { action(value) }
30 }
31 case None -> when (handle.focus) {
32 case Some(_) -> Some {
33 value -> handle.controller.perform(handle) { semanticFocus(handle, value) }
34 }
35 case None -> None
36 }
37 }
38 val setSelection: ((Utf8Selection) -> SemanticActionResult)? =
39 when (actions.setSelection) {
40 case Some(val action) -> Some {
41 value -> handle.controller.perform(handle) { action(value) }
42 }
43 case None -> None
44 }
45 val replaceText: ((Utf8Range, String) -> SemanticActionResult)? =
46 when (actions.replaceText) {
47 case Some(val action) -> Some {
48 range,
49 text -> handle.controller.perform(handle) { action(range, text) }
50 }
51 case None -> None
52 }
53 val showContextMenu: (() -> SemanticActionResult)? =
54 when (actions.showContextMenu) {
55 case Some(val action) -> Some { handle.controller.perform(handle, action) }
56 case None -> None
57 }
58 val position: ((Double) -> SemanticActionResult)? =
59 when (actions.setScrollPosition) {
60 case Some(val action) -> Some {
61 value -> handle.controller.perform(handle) { action(value) }
62 }
63 case None -> None
64 }
65 val backward: (() -> SemanticActionResult)? =
66 when (actions.scrollBackward) {
67 case Some(val action) -> Some { handle.controller.perform(handle, action) }
68 case None -> None
69 }
70 val forward: (() -> SemanticActionResult)? =
71 when (actions.scrollForward) {
72 case Some(val action) -> Some { handle.controller.perform(handle, action) }
73 case None -> None
74 }
75 val reveal: (() -> SemanticActionResult)? =
76 Some {
77 handle.controller.perform(handle) {
78 when (actions.reveal) {
79 case Some(val action) -> action()
80 case None -> handle.controller.reveal(handle)
81 }
82 }
83 }
84 return SemanticActions(
85 invoke,
86 toggle,
87 setValue,
88 focus,
89 setSelection,
90 replaceText,
91 showContextMenu,
92 position,
93 backward,
94 forward,
95 reveal,
96 when (actions.setNumber) {
97 case Some(val action) -> Some {
98 value -> handle.controller.perform(handle) { action(value) }
99 }
100 case None -> None
101 },
102 semanticVoidAction(handle, actions.increment),
103 semanticVoidAction(handle, actions.decrement),
104 semanticBooleanAction(handle, actions.setSelected),
105 semanticBooleanAction(handle, actions.setExpanded),
106 semanticVoidAction(handle, actions.dismiss),
107 when (actions.revealText) {
108 case Some(val action) -> Some {
109 range,
110 scroll -> handle.controller.perform(handle) { action(range, scroll) }
111 }
112 case None -> None
113 }
114 )
117private fun semanticVoidAction(handle: SemanticsHandle, action: (() -> SemanticActionResult)?): (
118 () -> SemanticActionResult
119)? =
120 when (action) {
121 case Some(val callback) -> Some { handle.controller.perform(handle, callback) }
122 case None -> None
123 }
125private fun semanticBooleanAction(
126 handle: SemanticsHandle,
127 action: ((Boolean) -> SemanticActionResult)?
128): ((Boolean) -> SemanticActionResult)? =
129 when (action) {
130 case Some(val callback) -> Some {
131 value -> handle.controller.perform(handle) { callback(value) }
132 }
133 case None -> None
134 }
136private fun semanticFocus(handle: SemanticsHandle, focused: Boolean): SemanticActionResult {
137 val target = handle.focus ?: return SemanticActionResult.Unavailable
138 if (!target.active)
139 return SemanticActionResult.Unavailable
140 if (!target.available)
141 return SemanticActionResult.Rejected
142 if (!focused) {
143 target.handle.clearFocus()
144 return if (!target.handle.hasFocus)
145 SemanticActionResult.Completed
146 else
147 SemanticActionResult.Rejected
148 }
149 if (target.handle.isFocusActive)
150 return SemanticActionResult.Completed
151 if (!target.handle.requestFocus())
152 return SemanticActionResult.Rejected
153 return if (target.handle.isFocusActive)
154 SemanticActionResult.Completed
155 else
156 SemanticActionResult.Busy