Scoop API

scoop:scoop.ui:0.1.0

src/text/selectable/drag_source.scoop

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

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.TextRange
5import scoop.graphics.text.drawText
6import scoop.surface.*
7import scoop.ui.*
8import scoop.window.*
10private class SelectableDragCandidate(val host: SelectableTextHost) {
11 val content = host.content
12 val selection = host.selection
15/** Read-only source preparation reuses the paragraph's normal copy representation. */
16internal class SelectableDragSource(
17 private val host: SelectableTextHost,
18 create: (DragSourceOptions, (DragSourceStart) -> DragSourceData?) -> DragSource
19): Closeable {
20 private var closed = false
21 private var pending: SelectableDragCandidate? = None
22 private val binding = create(options()) { start -> prepare(start) }
23 private val enabled: Boolean get() =
24 !closed &&
25 !host.closed &&
26 host.scope.isMounted &&
27 host.options.enabled &&
28 host.area.scope.isInputAllowed &&
29 host.options.drag.effects.copy &&
30 textDragAllowed(host.options.drag, host.options.copy)
31 private fun options(): DragSourceOptions =
32 DragSourceOptions(
33 host.options.drag.threshold,
34 enabled,
35 contacts = host.options.touch.enabled
36 )
38 private fun valid(candidate: SelectableDragCandidate): Boolean =
39 enabled && candidate.content === host.content && candidate.selection == host.selection
41 fun update() {
42 if (closed)
43 return
44 if (host.closed || !host.scope.isMounted || binding.isClosed) {
45 close()
46 return
47 }
48 when (pending) {
49 case Some(val value) -> if (!valid(value))
50 pending = None
51 case None -> {}
52 }
53 binding.update(options()) { start -> prepare(start) }
54 }
56 fun cancelCandidate() {
57 pending = None
58 }
60 fun handle(context: RoutedEventContext, event: InputEvent): Boolean {
61 if (closed)
62 return false
63 when (event) {
64 case Contact(InputContactEvent.Began(val value)) -> {
65 if (context.phase != RoutedPhase.Default)
66 return false
67 pending = None
68 if (
69 !enabled ||
70 !binding.hasContactCandidate(value.id) ||
71 value.pointer.modifiers.shift ||
72 host.touchHandles.contact?.start?.id ==
73 Some(value.id)
74 )
75 return false
76 val point = context.localPosition ?: return false
77 val frame = host.inputFrame() ?: return false
78 val range = host.selection.range
79 if (frame.content !== host.content || range.start == range.end)
80 return false
81 var inside = false
82 for (box in frame.layout.selection(TextRange(range.start, range.end)))
83 if (textMenuContains(box.rect, point)) {
84 inside = true
85 break
86 }
87 if (!inside)
88 return false
89 pending = Some(SelectableDragCandidate(host))
90 return advanceContact(context, event)
91 }
92 case Contact(InputContactEvent.Moved(val value)) -> {
93 if (context.phase != RoutedPhase.Default)
94 return false
95 val candidate = pending ?: return false
96 if (
97 !valid(candidate) ||
98 !binding.hasContactCandidate(value.id) ||
99 host.touchHandles.contact?.start?.id !=
100 None
101 ) {
102 pending = None
103 return false
104 }
105 return advanceContact(context, event)
106 }
107 case Contact(InputContactEvent.Ended(_)) -> {
108 pending = None
109 return false
110 }
111 case Pointer(
112 InputPointerEvent.Button(
113 val pointer,
114 MouseButton.Left,
115 ElementState.Pressed,
116 val count
117 )
118 ) -> {
119 if (context.phase == RoutedPhase.Capture || !hasCandidate()) {
120 pending = None
121 if (!enabled || count != 1 || pointer.state.modifiers.shift)
122 return false
123 val point = context.localPosition ?: return false
124 val frame = host.inputFrame() ?: return false
125 if (!(frame.content === host.content))
126 return false
127 val range = host.selection.range
128 if (range.start == range.end)
129 return false
130 var inside = false
131 for (box in frame.layout.selection(TextRange(range.start, range.end)))
132 if (textMenuContains(box.rect, point)) {
133 inside = true
134 break
135 }
136 if (!inside)
137 return false
138 selectableStopDrag(host)
139 pending = Some(SelectableDragCandidate(host))
140 }
141 if (context.phase == RoutedPhase.Default)
142 binding.handle(context, event)
143 return true
144 }
145 case Pointer(InputPointerEvent.Moved(_)) -> {
146 val candidate = pending ?: return false
147 if (!valid(candidate)) {
148 pending = None
149 return false
150 }
151 if (context.phase == RoutedPhase.Default)
152 binding.handle(context, event)
153 return true
154 }
155 case Pointer(
156 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Released, _)
157 ) -> {
158 val candidate = pending ?: return false
159 if (context.phase != RoutedPhase.Default)
160 return true
161 pending = None
162 if (valid(candidate)) {
163 val frame = host.inputFrame() ?: return true
164 val point = context.localPosition ?: return true
165 val position = frame.layout.hitTest(point)
166 host.setSelection(position, position)
167 }
168 return true
169 }
170 else -> return false
171 }
172 }
174 private fun advanceContact(context: RoutedEventContext, event: InputEvent): Boolean {
175 binding.handle(context, event)
176 if (!binding.isDragging)
177 return false
178 host.touchPress.cancel()
179 host.touchHandles.hide()
180 return true
181 }
183 private fun hasCandidate(): Boolean =
184 when (pending) {
185 case Some(_) -> true
186 case None -> false
187 }
189 private fun prepare(start: DragSourceStart): DragSourceData? {
190 val candidate = pending ?: return None
191 pending = None
192 if (!valid(candidate) || start.press.modifiers.shift)
193 return None
194 val frame = host.inputFrame() ?: return None
195 if (!(frame.content === candidate.content))
196 return None
197 val range = candidate.selection.range
198 val text = frame.content.toPlainText(TextRange(range.start, range.end))
199 val preview =
200 host.options.drag.preparePreview(start, text) {
201 val boxes = frame.layout.selection(TextRange(range.start, range.end))
202 start.renderSelectionPreview(
203 Array<Rect>(boxes.size) { boxes[it].rect },
204 host.area
205 ) { canvas -> canvas.drawText(frame.layout) }
206 }
207 if (!valid(candidate))
208 return None
209 val notify = host.options.drag.onCompleted
210 return Some(
211 DragSourceData(
212 DragContent.Text(text),
213 DragEffects(true, false, false),
214 preview = preview
215 ) { result -> notify(textDragResult(result)) }
216 )
217 }
219 public override fun close() {
220 if (closed)
221 return
222 closed = true
223 pending = None
224 binding.close()
225 }