Scoop API

scoop:scoop.ui:0.1.0

src/text/selectable/events.scoop

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

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.*
6import scoop.ui.*
7import scoop.window.*
9internal class SelectableDrag(
10 val origin: Point,
11 val unit: EditorDragUnit,
12 val initial: TextRange,
13 val anchor: TextPosition,
14 val cursor: TextPosition,
15 var started: Boolean = false
18internal fun selectableHandle(
19 host: SelectableTextHost,
20 context: RoutedEventContext,
21 event: InputEvent
22) {
23 if (host.closed)
24 return
25 if (context.phase == RoutedPhase.Notification) {
26 when (event) {
27 case FocusChanged(false) -> if (!host.area.scope.activity.hasFocus) {
28 host.dataSource?.cancelCandidate()
29 selectableStopDrag(host)
30 }
31 case Pointer(InputPointerEvent.Cancelled(_, _)) -> {
32 host.dataSource?.cancelCandidate()
33 selectableStopDrag(host)
34 }
35 case Contact(InputContactEvent.Cancelled(_, _, _)) -> host.dataSource?.cancelCandidate()
36 else -> {}
37 }
38 return
39 }
40 if (
41 !host.options.enabled ||
42 (context.phase != RoutedPhase.Capture && context.phase != RoutedPhase.Default)
43 )
44 return
45 when (event) {
46 case Pointer(
47 InputPointerEvent.Button(val pointer, val button, ElementState.Pressed, _)
48 ) -> {
49 if (isContextMenuClick(button, pointer.state.modifiers)) {
50 if (
51 context.phase ==
52 RoutedPhase.Default &&
53 selectableContextMenu(host, Some(pointer.state), context.localPosition)
54 )
55 context.consume()
56 return
57 }
58 }
59 case Keyboard(val key) -> {
60 if (
61 context.phase == RoutedPhase.Default && textMenuKey(key) && host.showContextMenu()
62 ) {
63 context.consume()
64 return
65 }
66 }
67 else -> {}
68 }
69 if (host.dataSource?.handle(context, event) ?: false) {
70 if (context.phase == RoutedPhase.Default)
71 context.consume()
72 return
73 }
74 when (event) {
75 case TextCommand(val command) -> if (
76 context.phase == RoutedPhase.Default && host.execute(command)
77 )
78 context.consume()
79 case Pointer(
80 InputPointerEvent.Button(val pointer, MouseButton.Left, val state, val count)
81 ) -> {
82 if (state == ElementState.Pressed) {
83 if (context.phase == RoutedPhase.Capture) {
84 selectablePrepareDrag(host, pointer, context.localPosition, count)
85 } else {
86 when (host.drag) {
87 case None -> selectablePrepareDrag(
88 host,
89 pointer,
90 context.localPosition,
91 count
92 )
93 case Some(_) -> {}
94 }
95 selectableStartDrag(host)
96 context.consume()
97 }
98 } else
99 if (context.phase == RoutedPhase.Default) {
100 selectableStopDrag(host)
101 context.consume()
102 }
103 }
104 case Pointer(InputPointerEvent.Moved(val pointer)) -> {
105 val drag = host.drag ?: return
106 if (!drag.started) {
107 val x = pointer.state.logicalPosition.x - drag.origin.x
108 val y = pointer.state.logicalPosition.y - drag.origin.y
109 val threshold = host.options.dragThreshold
110 if (x * x + y * y < threshold * threshold)
111 return
112 if (!selectableStartDrag(host))
113 return
114 }
115 val point = context.localPosition ?: return
116 selectableExtend(host, drag, point)
117 context.consume()
118 }
119 case Keyboard(val key) -> {
120 if (
121 context.phase !=
122 RoutedPhase.Default ||
123 key.state !=
124 ElementState.Pressed ||
125 key.synthetic ||
126 !host.area.scope.activity.hasFocus
127 )
128 return
129 var handled = false
130 for (command in key.textCommands) {
131 if (
132 host.closed ||
133 !host.area.scope.isInputAllowed ||
134 !host.area.scope.activity.hasFocus
135 )
136 break
137 if (selectableCommand(host, command))
138 handled = true
139 }
140 if (handled) {
141 selectableStopDrag(host)
142 context.consume()
143 }
144 }
145 else -> {}
146 }
149private fun selectablePrepareDrag(
150 host: SelectableTextHost,
151 pointer: InputPointer,
152 local: Point?,
153 count: Int
154) {
155 val frame = host.inputFrame() ?: return
156 val point = local ?: return
157 val position = frame.layout.hitTest(point)
158 val shift = pointer.state.modifiers.shift
159 val unit =
160 if (shift)
161 EditorDragUnit.Character
162 else
163 editorClickUnit(count)
164 val range = editorDragRange(TextEditLayout(frame.layout), position, unit)
165 val anchor =
166 if (shift)
167 host.anchor
168 else
169 if (unit == EditorDragUnit.Character)
170 position
171 else
172 TextPosition(range.start)
173 host.drag =
174 Some(
175 SelectableDrag(
176 Point(pointer.state.logicalPosition.x, pointer.state.logicalPosition.y),
177 unit,
178 range,
179 anchor,
180 position
181 )
182 )
185private fun selectableStartDrag(host: SelectableTextHost): Boolean {
186 val drag = host.drag ?: return false
187 if (drag.started)
188 return true
189 if (!host.area.scope.claimPointer()) {
190 host.drag = None
191 return false
192 }
193 drag.started = true
194 host.area.requestFocus(FocusOrigin.Pointer)
195 if (host.closed)
196 return false
197 val cursor =
198 if (drag.unit == EditorDragUnit.Character)
199 drag.cursor
200 else
201 TextPosition(drag.initial.end, TextAffinity.Upstream)
202 host.setSelection(drag.anchor, cursor)
203 return !host.closed
206private fun selectableExtend(host: SelectableTextHost, drag: SelectableDrag, point: Point) {
207 val frame = host.inputFrame() ?: return
208 val position = frame.layout.hitTest(point)
209 val range = editorDragRange(TextEditLayout(frame.layout), position, drag.unit)
210 val value =
211 if (drag.unit == EditorDragUnit.Character)
212 EditorSelection(drag.anchor, position)
213 else
214 if (range.start < drag.initial.start)
215 EditorSelection(
216 TextPosition(drag.initial.end, TextAffinity.Upstream),
217 TextPosition(range.start)
218 )
219 else
220 EditorSelection(
221 TextPosition(drag.initial.start),
222 TextPosition(range.end, TextAffinity.Upstream)
223 )
224 host.setSelection(value.anchor, value.cursor)
227internal fun selectableStopDrag(host: SelectableTextHost) {
228 val drag = host.drag ?: return
229 host.drag = None
230 if (drag.started && !host.area.isClosed)
231 host.area.scope.cancelPointerClaim()
234internal fun selectableFinished(host: SelectableTextHost, event: InputEvent) {
235 if (host.closed)
236 return
237 when (event) {
238 case Pointer(InputPointerEvent.Button(_, MouseButton.Left, ElementState.Released, _)) -> {
239 host.dataSource?.cancelCandidate()
240 selectableStopDrag(host)
241 }
242 else -> {}
243 }
246internal fun selectableCommand(host: SelectableTextHost, command: TextEditCommand): Boolean {
247 val frame = host.inputFrame() ?: return false
248 when (command) {
249 case Copy -> {
250 val range = host.selection.range
251 if (host.options.copy && range.start != range.end)
252 host.scope.nativeWindow.clipboard.writeText(
253 frame.content.toPlainText(TextRange(range.start, range.end))
254 )
255 }
256 case SelectAll -> {
257 host.area.requestFocus()
258 host.setSelection(TextPosition(0L), TextPosition(frame.content.text.byteLength))
259 }
260 case Move(val movement, val extend) -> {
261 val result =
262 editorMove(
263 TextEditLayout(frame.layout),
264 host.selected.value,
265 movement,
266 extend,
267 host.preferredX,
268 host.area.visibleBounds?.height ?: frame.layout.size.height
269 )
270 host.area.requestFocus()
271 host.setSelection(result.selection.anchor, result.selection.cursor)
272 if (!host.closed) {
273 host.preferredX = result.preferredX
274 when (
275 frame.layout.caret(
276 editorVisualPosition(TextEditLayout(frame.layout), host.cursor)
277 )
278 ) {
279 case Some(val rect) -> host.focus.reveal(rect)
280 case None -> {}
281 }
282 }
283 }
284 else -> return false
285 }
286 return true