Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/drag_source.scoop

Selected production source · src/text/editor/drag_source.scoop

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.window.*
8private class EditorDragCandidate(val editor: TextEditorState) {
9 val revision = editor.contentRevision
10 val selection = editor.selection
13/** Delays only an ordinary click inside the painted selection. */
14internal class EditorDragSource(
15 private val host: TextEditHost,
16 create: (DragSourceOptions, (DragSourceStart) -> DragSourceData?) -> DragSource
17): Closeable {
18 private var closed = false
19 private var pending: EditorDragCandidate? = None
20 private var active: EditorTextTransfer? = None
21 private val binding = create(options()) { start -> prepare(start) }
22 private val enabled: Boolean get() =
23 !closed &&
24 !host.closed &&
25 host.scope.isMounted &&
26 host.options.enabled &&
27 host.area.scope.isInputAllowed &&
28 textDragAllowed(
29 host.options.drag,
30 host.copyAllowed && !host.options.protectedText && !host.options.projection.masked
31 ) &&
32 (
33 host.options.drag.effects.copy ||
34 (host.options.drag.effects.move && !host.options.readOnly)
35 )
37 private fun options(): DragSourceOptions =
38 DragSourceOptions(
39 host.options.drag.threshold,
40 enabled,
41 contacts = host.options.touch.enabled
42 )
44 private fun valid(candidate: EditorDragCandidate): Boolean =
45 enabled &&
46 candidate.editor ===
47 host.current &&
48 candidate.revision ==
49 host.current.contentRevision &&
50 candidate.selection ==
51 host.current.selection &&
52 !host.current.isComposing
54 fun update() {
55 if (closed)
56 return
57 if (host.closed || !host.scope.isMounted || binding.isClosed) {
58 close()
59 return
60 }
61 when (pending) {
62 case Some(val value) -> if (!valid(value))
63 cancelCandidate()
64 case None -> {}
65 }
66 binding.update(options()) { start -> prepare(start) }
67 when (active) {
68 case Some(val value) -> if (binding.isDragging) {
69 val configured = host.options.drag.effects
70 binding.updateEffects(
71 DragEffects(
72 value.effects.copy && configured.copy,
73 value.effects.move && configured.move && value.canMove,
74 false
75 )
76 )
77 } else {
78 active = None
79 }
80 case None -> {}
81 }
82 }
84 fun cancelCandidate() {
85 pending = None
86 }
88 fun handle(context: RoutedEventContext, event: InputEvent): Boolean {
89 if (closed)
90 return false
91 when (event) {
92 case Contact(InputContactEvent.Began(val value)) -> {
93 pending = None
94 if (
95 !enabled ||
96 !binding.hasContactCandidate(value.id) ||
97 host.current.isComposing ||
98 value.pointer.modifiers.shift ||
99 host.touchHandles.contact?.start?.id ==
100 Some(value.id)
101 )
102 return false
103 val point = context.localPosition ?: return false
104 val frame = host.accepted ?: return false
105 if (
106 frame.editor !==
107 host.current ||
108 frame.revision !=
109 host.current.revision ||
110 frame.projection !==
111 host.options.projection
112 )
113 return false
114 val range = host.current.selection.range
115 if (range.start == range.end)
116 return false
117 var inside = false
118 for (box in frame.layout.selection(frame.display.displayRange(range)))
119 if (textMenuContains(box.rect, point)) {
120 inside = true
121 break
122 }
123 if (!inside)
124 return false
125 pending = Some(EditorDragCandidate(host.current))
126 return advanceContact(context, event)
127 }
128 case Contact(InputContactEvent.Moved(val value)) -> {
129 val candidate = pending ?: return false
130 if (
131 !valid(candidate) ||
132 !binding.hasContactCandidate(value.id) ||
133 host.touchHandles.contact?.start?.id !=
134 None
135 ) {
136 cancelCandidate()
137 return false
138 }
139 return advanceContact(context, event)
140 }
141 case Contact(InputContactEvent.Ended(_)) -> {
142 pending = None
143 return false
144 }
145 case Pointer(
146 InputPointerEvent.Button(
147 val pointer,
148 MouseButton.Left,
149 ElementState.Pressed,
150 val count
151 )
152 ) -> {
153 pending = None
154 if (
155 !enabled ||
156 host.current.isComposing ||
157 count !=
158 1 ||
159 pointer.state.modifiers.shift
160 )
161 return false
162 val point = context.localPosition ?: return false
163 val frame = host.accepted ?: return false
164 if (
165 !(frame.editor === host.current) ||
166 frame.revision !=
167 host.current.revision ||
168 !(frame.projection === host.options.projection)
169 )
170 return false
171 val range = host.current.selection.range
172 if (range.start == range.end)
173 return false
174 var inside = false
175 for (box in frame.layout.selection(frame.display.displayRange(range)))
176 if (textMenuContains(box.rect, point)) {
177 inside = true
178 break
179 }
180 if (!inside)
181 return false
182 editorStopDrag(host, false)
183 pending = Some(EditorDragCandidate(host.current))
184 binding.handle(context, event)
185 if (!binding.isDragging)
186 active = None
187 return true
188 }
189 case Pointer(InputPointerEvent.Moved(_)) -> {
190 val candidate = pending ?: return false
191 if (!valid(candidate)) {
192 cancelCandidate()
193 return false
194 }
195 binding.handle(context, event)
196 if (!binding.isDragging)
197 active = None
198 return true
199 }
200 case Pointer(
201 InputPointerEvent.Button(val pointer, MouseButton.Left, ElementState.Released, _)
202 ) -> {
203 val candidate = pending ?: return false
204 pending = None
205 if (valid(candidate)) {
206 editorPress(host, pointer, context.localPosition, 1)
207 editorStopDrag(host, true)
208 }
209 return true
210 }
211 else -> return false
212 }
213 }
215 private fun advanceContact(context: RoutedEventContext, event: InputEvent): Boolean {
216 binding.handle(context, event)
217 if (!binding.isDragging)
218 return false
219 host.keyboard.cancelPending()
220 host.touchPress.cancel()
221 host.touchPan.cancel()
222 host.touchHandles.hide()
223 return true
224 }
226 private fun prepare(start: DragSourceStart): DragSourceData? {
227 val candidate = pending ?: return None
228 pending = None
229 if (!valid(candidate) || start.press.modifiers.shift)
230 return None
231 val editor = candidate.editor
232 val config = host.options.drag
233 val effects =
234 DragEffects(config.effects.copy, config.effects.move && !host.options.readOnly, false)
235 val transfer =
236 EditorTextTransfer(editor, candidate.selection.range, effects) {
237 enabled &&
238 host.current ===
239 editor &&
240 !host.options.readOnly &&
241 host.options.drag.effects.move
242 }
243 val frame = host.accepted ?: return None
244 if (
245 frame.editor !==
246 editor ||
247 frame.revision !=
248 editor.revision ||
249 frame.projection !==
250 host.options.projection
251 )
252 return None
253 val preview =
254 config.preparePreview(start, transfer.text) {
255 val boxes =
256 frame.layout.selection(frame.display.displayRange(candidate.selection.range))
257 start.renderSelectionPreview(
258 Array<Rect>(boxes.size) { boxes[it].rect },
259 host.area
260 ) { canvas -> canvas.drawText(frame.layout) }
261 }
262 if (!valid(candidate))
263 return None
264 active = Some(transfer)
265 return Some(
266 DragSourceData(DragContent.Text(transfer.text), effects, Some(transfer), preview) {
267 result -> completed(transfer, result, config.onCompleted)
268 }
269 )
270 }
272 private fun completed(
273 transfer: EditorTextTransfer,
274 native: DragResult,
275 notify: (TextDragResult) -> Unit
276 ) {
277 active = None
278 var result = textDragResult(native)
279 var failure: Throwable? = None
280 val moving =
281 when (native) {
282 case Dropped(DragEffect.Move) -> true
283 else -> false
284 }
285 if (moving && !transfer.moved) {
286 result = TextDragResult.SourceRetained
287 try {
288 when (
289 val edit =
290 prepareEditorDragRemoval(
291 transfer,
292 host.options.policy,
293 !host.options.multiline
294 )
295 ) {
296 case Some(val value) -> {
297 host.perform(
298 {
299 val changed = value.publish(transfer.editor, false)
300 transfer.moved = true
301 changed
302 }
303 )
304 result = TextDragResult.Moved
305 }
306 case None -> {}
307 }
308 } catch (error: Throwable) {
309 if (transfer.moved)
310 result = TextDragResult.Moved
311 failure = Some(error)
312 }
313 }
314 if (!host.closed && host.scope.isMounted) {
315 val completed = result
316 failure = cleanupUi(failure) { notify(completed) }
317 }
318 throwUi(failure)
319 }
321 public override fun close() {
322 if (closed)
323 return
324 closed = true
325 pending = None
326 active = None
327 binding.close()
328 }