Scoop API

scoop:scoop.ui:0.1.0

src/text/selectable/actions.scoop

Selected production source · src/text/selectable/actions.scoop

1package scoop.ui.text
3import scoop.graphics.text.*
4import scoop.window.*
5import scoop.window.accessibility.*
6import scoop.surface.*
7import scoop.ui.*
9internal struct SelectableRange(val key: String, val range: TextRange)
11internal class SelectableAction(
12 val host: SelectableTextHost,
13 var input: TextRangeAction,
14 order: Int
15): Closeable {
16 private var qualified = false
17 var semantics: SemanticsHandle? = None
18 val area = host.scope.createArea(focusable = true, parent = Some(host.area))
19 val focus = host.scope.focusTarget(area, input.focus ?: FocusOptions(order = order))
20 val press =
21 Press(
22 area,
23 PressOptions(enabled = false),
24 {
25 if (qualified && !host.closed)
26 this.input.onActivate()
27 }
28 )
29 init {
30 area.scope.enabled = false
31 }
32 fun update(value: TextRangeAction, order: Int) {
33 input = value
34 focus.options = value.focus ?: FocusOptions(order = order)
35 }
36 fun refreshSemantics() {
37 val parent = host.semantics.binding?.handle
38 if (parent?.isClosed ?: true) {
39 semantics?.close()
40 semantics = None
41 return
42 }
43 when (semantics) {
44 case Some(val old) -> if (old.isClosed)
45 semantics = None
46 case None -> {}
47 }
48 val text = selectableSemanticText(host)
49 val value = semantics ?: host.scope.semantics(SemanticRole.Link, parent)
50 semantics = Some(value)
51 val range =
52 Utf8Range(
53 text.mapping.forward(
54 TextPosition(input.range.start, TextAffinity.Upstream)
55 ).byteOffset,
56 text.mapping.forward(
57 TextPosition(input.range.end, TextAffinity.Downstream)
58 ).byteOffset
59 )
60 value.textChild = Some(SemanticTextChildBinding(parent!!, text.text, range))
61 value.bindFocus(Some(focus))
62 value.update(
63 SemanticProperties(name = Some(text.text.utf8Slice(range)), enabled = qualified),
64 SemanticActions(
65 invoke =
66 Some {
67 if (host.closed || !qualified || !area.scope.isInputAllowed)
68 SemanticActionResult.Rejected
69 else {
70 this.input.onActivate();
71 SemanticActionResult.Completed
72 }
73 }
74 )
75 )
76 }
77 fun refresh(available: Boolean, frame: SelectableFrame?) {
78 var accepted = false
79 when (frame) {
80 case Some(val value) -> for (range in value.ranges)
81 if (range.key == input.key && range.range == input.range)
82 accepted = true
83 case None -> {}
84 }
85 qualified = available && accepted && input.enabled
86 area.scope.enabled = qualified
87 press.update(PressOptions(enabled = qualified)) {
88 if (qualified && !host.closed)
89 this.input.onActivate()
90 }
91 refreshSemantics()
92 }
93 public override fun close() {
94 qualified = false
95 var failure: Throwable? = None
96 failure = cleanupUi(failure) { semantics?.close() }
97 semantics = None
98 failure = cleanupUi(failure) { press.close() }
99 failure = cleanupUi(failure) { focus.close() }
100 failure = cleanupUi(failure) { area.close() }
101 throwUi(failure)
102 }
105internal fun selectableActions(host: SelectableTextHost, inputs: Array<TextRangeAction>) {
106 val next = ArrayList<SelectableAction>()
107 for (index in 0L ..< inputs.size) {
108 val input = inputs[index]
109 var found: SelectableAction? = None
110 for (action in host.actions)
111 if (action.input.key == input.key)
112 found = Some(action)
113 val action = found ?: SelectableAction(host, input, index.toInt())
114 action.update(input, index.toInt())
115 next.add(action)
116 }
117 for (action in host.actions) {
118 var retained = false
119 for (value in next)
120 if (value === action)
121 retained = true
122 if (!retained)
123 action.close()
124 }
125 host.actions = next.toArray()