scoop:scoop.ui:0.1.0
src/text/selectable/actions.scoop
Selected production source · src/text/selectable/actions.scoop
1
package scoop.ui.text3
import scoop.graphics.text.*4
import scoop.window.*5
import scoop.window.accessibility.*6
import scoop.surface.*7
import scoop.ui.*9
internal struct SelectableRange(val key: String, val range: TextRange)11
internal class SelectableAction(12
val host: SelectableTextHost,13
var input: TextRangeAction,14
order: Int15
): Closeable {16
private var qualified = false17
var semantics: SemanticsHandle? = None18
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 = false31
}32
fun update(value: TextRangeAction, order: Int) {33
input = value34
focus.options = value.focus ?: FocusOptions(order = order)35
}36
fun refreshSemantics() {37
val parent = host.semantics.binding?.handle38
if (parent?.isClosed ?: true) {39
semantics?.close()40
semantics = None41
return42
}43
when (semantics) {44
case Some(val old) -> if (old.isClosed)45
semantics = None46
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
).byteOffset59
)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.Rejected69
else {70
this.input.onActivate();71
SemanticActionResult.Completed72
}73
}74
)75
)76
}77
fun refresh(available: Boolean, frame: SelectableFrame?) {78
var accepted = false79
when (frame) {80
case Some(val value) -> for (range in value.ranges)81
if (range.key == input.key && range.range == input.range)82
accepted = true83
case None -> {}84
}85
qualified = available && accepted && input.enabled86
area.scope.enabled = qualified87
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 = false95
var failure: Throwable? = None96
failure = cleanupUi(failure) { semantics?.close() }97
semantics = None98
failure = cleanupUi(failure) { press.close() }99
failure = cleanupUi(failure) { focus.close() }100
failure = cleanupUi(failure) { area.close() }101
throwUi(failure)102
}103
}105
internal 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? = None110
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 = false119
for (value in next)120
if (value === action)121
retained = true122
if (!retained)123
action.close()124
}125
host.actions = next.toArray()126
}