Scoop API

scoop:scoop.ui:0.1.0

src/text/selectable/semantics.scoop

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

1package scoop.ui.text
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.window.*
6import scoop.window.accessibility.*
8/** Publishes this mount's styled read-only text, including inline alternative text. */
9public 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.content
22 val snapshot =
23 if (properties.protectedText) {
24 host.semanticText = None
25 None
26 } else
27 Some(selectableSemanticText(host))
28 val available = { !host.closed && host.content === content }
29 val select: ((Utf8Selection) -> SemanticActionResult)? =
30 when (snapshot) {
31 case None -> None
32 case Some(val text) -> Some {
33 selection -> if (!available())
34 SemanticActionResult.Unavailable
35 else
36 if (!host.options.enabled || !host.area.scope.isInputAllowed)
37 SemanticActionResult.Rejected
38 else {
39 val caret = selection.anchor == selection.cursor
40 val anchor =
41 text.mapping.reverse(
42 TextPosition(
43 selection.anchor,
44 if (caret || selection.anchor > selection.cursor)
45 TextAffinity.Downstream
46 else
47 TextAffinity.Upstream
48 )
49 )
50 val cursor =
51 text.mapping.reverse(
52 TextPosition(
53 selection.cursor,
54 if (caret || selection.cursor > selection.anchor)
55 TextAffinity.Downstream
56 else
57 TextAffinity.Upstream
58 )
59 )
60 selectableStopDrag(host)
61 host.setSelection(anchor, cursor)
62 SemanticActionResult.Completed
63 }
64 }
65 }
66 val showContextMenu: (() -> SemanticActionResult)? =
67 if (host.options.contextMenu.mode == TextContextMenuMode.Disabled)
68 None
69 else
70 Some {
71 if (!available())
72 SemanticActionResult.Unavailable
73 else
74 if (host.options.contextMenu.mode == TextContextMenuMode.Disabled)
75 SemanticActionResult.Unsupported
76 else
77 semanticTextMenu(host.focus, host.contextMenu) {
78 available() && host.showContextMenu()
79 }
80 }
81 bindFocus(Some(host.focus))
82 updateText(
83 if (properties.protectedText)
84 None
85 else
86 Some(host.semantics),
87 properties.copy {
88 value = snapshot?.text
89 selection =
90 when (snapshot) {
91 case None -> None
92 case Some(val text) -> Some(
93 Utf8Selection(
94 text.mapping.forward(host.anchor).byteOffset,
95 text.mapping.forward(host.cursor).byteOffset
96 )
97 )
98 }
99 enabled = properties.enabled && host.options.enabled
100 readOnly = true
101 protectedText = properties.protectedText
102 cursorAffinity =
103 if (!properties.protectedText && host.cursor.affinity == TextAffinity.Upstream)
104 SemanticTextAffinity.Upstream
105 else
106 SemanticTextAffinity.Downstream
107 },
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.Unavailable
117 else
118 semanticSelectableReveal(this, host, text, range, scroll)
119 }
120 case None -> None
121 }
122 ),
123 relations
124 )
125 for (action in host.actions)
126 action.refreshSemantics()
129/** Immutable derived text is reused while pointer selection changes. */
130internal 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)
142internal fun selectableSemanticText(host: SelectableTextHost): SelectableSemanticText {
143 when (host.semanticText) {
144 case Some(val text) -> return text
145 case None -> {}
146 }
147 val text = SelectableSemanticText(host.content)
148 host.semanticText = Some(text)
149 return text