scoop:scoop.ui:0.1.0
src/text/selectable/semantics.scoop
Selected production source · src/text/selectable/semantics.scoop
1
package scoop.ui.text3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.window.*6
import scoop.window.accessibility.*8
/** Publishes this mount's styled read-only text, including inline alternative text. */9
public fun SemanticsHandle.updateSelectableText(10
host: SelectableTextHost,11
properties: SemanticProperties = SemanticProperties(),12
relations: SemanticRelations = SemanticRelations()13
) {14
checkOpen()15
checkSemanticsMutation()16
host.checkOpen()17
requireUi(18
role == SemanticRole.Text && owner === host.focus.node,19
"selectable semantics require this mount's text node and host"20
)21
val content = host.content22
val snapshot =23
if (properties.protectedText) {24
host.semanticText = None25
None26
} else27
Some(selectableSemanticText(host))28
val available = { !host.closed && host.content === content }29
val select: ((Utf8Selection) -> SemanticActionResult)? =30
when (snapshot) {31
case None -> None32
case Some(val text) -> Some {33
selection -> if (!available())34
SemanticActionResult.Unavailable35
else36
if (!host.options.enabled || !host.area.scope.isInputAllowed)37
SemanticActionResult.Rejected38
else {39
val caret = selection.anchor == selection.cursor40
val anchor =41
text.mapping.reverse(42
TextPosition(43
selection.anchor,44
if (caret || selection.anchor > selection.cursor)45
TextAffinity.Downstream46
else47
TextAffinity.Upstream48
)49
)50
val cursor =51
text.mapping.reverse(52
TextPosition(53
selection.cursor,54
if (caret || selection.cursor > selection.anchor)55
TextAffinity.Downstream56
else57
TextAffinity.Upstream58
)59
)60
selectableStopDrag(host)61
host.setSelection(anchor, cursor)62
SemanticActionResult.Completed63
}64
}65
}66
val showContextMenu: (() -> SemanticActionResult)? =67
if (host.options.contextMenu.mode == TextContextMenuMode.Disabled)68
None69
else70
Some {71
if (!available())72
SemanticActionResult.Unavailable73
else74
if (host.options.contextMenu.mode == TextContextMenuMode.Disabled)75
SemanticActionResult.Unsupported76
else77
semanticTextMenu(host.focus, host.contextMenu) {78
available() && host.showContextMenu()79
}80
}81
bindFocus(Some(host.focus))82
updateText(83
if (properties.protectedText)84
None85
else86
Some(host.semantics),87
properties.copy {88
value = snapshot?.text89
selection =90
when (snapshot) {91
case None -> None92
case Some(val text) -> Some(93
Utf8Selection(94
text.mapping.forward(host.anchor).byteOffset,95
text.mapping.forward(host.cursor).byteOffset96
)97
)98
}99
enabled = properties.enabled && host.options.enabled100
readOnly = true101
protectedText = properties.protectedText102
cursorAffinity =103
if (!properties.protectedText && host.cursor.affinity == TextAffinity.Upstream)104
SemanticTextAffinity.Upstream105
else106
SemanticTextAffinity.Downstream107
},108
SemanticActions(109
setSelection = select,110
showContextMenu = showContextMenu,111
revealText =112
when (snapshot) {113
case Some(val text) -> Some {114
range,115
scroll -> if (!available())116
SemanticActionResult.Unavailable117
else118
semanticSelectableReveal(this, host, text, range, scroll)119
}120
case None -> None121
}122
),123
relations124
)125
for (action in host.actions)126
action.refreshSemantics()127
}129
/** Immutable derived text is reused while pointer selection changes. */130
internal class SelectableSemanticText(content: StyledText) {131
val text = content.toPlainText()132
val edits =133
Array<TextEditReplacement>(content.inlineObjects.size) {134
index ->135
val item = content.inlineObjects[index]136
TextEditReplacement(Utf8Range(item.range.start, item.range.end), item.alternateText)137
}138
val mapping = EditorReplacementMap(edits)139
val boundaries = editorGraphemeBytes(text)140
}142
internal fun selectableSemanticText(host: SelectableTextHost): SelectableSemanticText {143
when (host.semanticText) {144
case Some(val text) -> return text145
case None -> {}146
}147
val text = SelectableSemanticText(host.content)148
host.semanticText = Some(text)149
return text150
}