scoop:scoop.ui:0.1.0
src/accessibility/actions.scoop
Selected production source · src/accessibility/actions.scoop
1
package scoop.ui3
import scoop.window.accessibility.*4
import scoop.window.Utf8Range5
import scoop.window.Utf8Selection7
/** Capabilities follow actual handlers; every native entry shares the root boundary. */8
internal 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 -> None13
}14
val toggle: (() -> SemanticActionResult)? =15
when (actions.toggle) {16
case Some(val action) -> Some { handle.controller.perform(handle, action) }17
case None -> None18
}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 -> None25
}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 -> None36
}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 -> None44
}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 -> None52
}53
val showContextMenu: (() -> SemanticActionResult)? =54
when (actions.showContextMenu) {55
case Some(val action) -> Some { handle.controller.perform(handle, action) }56
case None -> None57
}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 -> None64
}65
val backward: (() -> SemanticActionResult)? =66
when (actions.scrollBackward) {67
case Some(val action) -> Some { handle.controller.perform(handle, action) }68
case None -> None69
}70
val forward: (() -> SemanticActionResult)? =71
when (actions.scrollForward) {72
case Some(val action) -> Some { handle.controller.perform(handle, action) }73
case None -> None74
}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 -> None101
},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 -> None113
}114
)115
}117
private fun semanticVoidAction(handle: SemanticsHandle, action: (() -> SemanticActionResult)?): (118
() -> SemanticActionResult119
)? =120
when (action) {121
case Some(val callback) -> Some { handle.controller.perform(handle, callback) }122
case None -> None123
}125
private 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 -> None134
}136
private fun semanticFocus(handle: SemanticsHandle, focused: Boolean): SemanticActionResult {137
val target = handle.focus ?: return SemanticActionResult.Unavailable138
if (!target.active)139
return SemanticActionResult.Unavailable140
if (!target.available)141
return SemanticActionResult.Rejected142
if (!focused) {143
target.handle.clearFocus()144
return if (!target.handle.hasFocus)145
SemanticActionResult.Completed146
else147
SemanticActionResult.Rejected148
}149
if (target.handle.isFocusActive)150
return SemanticActionResult.Completed151
if (!target.handle.requestFocus())152
return SemanticActionResult.Rejected153
return if (target.handle.isFocusActive)154
SemanticActionResult.Completed155
else156
SemanticActionResult.Busy157
}