Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/drop.scoop

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

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.platform.time.MonotonicTimeSource
6import scoop.surface.*
7import scoop.time.Duration
8import scoop.ui.*
9import scoop.window.*
11private class EditorDropPosition(
12 val frame: EditorFrame,
13 val windowPoint: Point,
14 val local: Point,
15 val position: TextPosition
18/** Metadata feedback and final text transfer share the editor's accepted model and layout. */
19internal class EditorDropTarget(
20 private val host: TextEditHost,
21 create: (DropAreaHandlers, Boolean) -> DropArea
22): Closeable {
23 private var closed = false
24 private var location: EditorDropPosition? = None
25 private var waiting = false
26 private var source: EditorTextTransfer? = None
27 private var effect = DragEffect.Copy
28 private val timer = host.scope.createDeadline { tick() }
29 private val handlers =
30 DropAreaHandlers(
31 { value, point -> offer(value, point) },
32 { request, point -> receive(request, point) },
33 { clear() }
34 )
35 private val binding = create(handlers, enabled)
36 private val activity =
37 host.scope.observeWindowActivity {
38 value -> if (!closed && (!value.visible || value.occluded))
39 clear()
40 }
41 private val enabled: Boolean get() =
42 !closed &&
43 !host.closed &&
44 host.options.enabled &&
45 host.options.acceptDrops &&
46 !host.options.readOnly &&
47 host.scope.isMounted &&
48 host.area.scope.isInputAllowed
49 val isHovering: Boolean get() =
50 when (validLocation()) {
51 case Some(_) -> binding.hover != None
52 case None -> false
53 }
55 fun update() {
56 if (closed)
57 return
58 clear()
59 binding.update(handlers, enabled)
60 }
62 private fun locate(windowPoint: Point, local: Point): EditorDropPosition? {
63 if (!enabled)
64 return None
65 val frame = host.accepted ?: return None
66 if (
67 !(frame.editor === host.current) ||
68 frame.revision !=
69 host.current.revision ||
70 !(frame.projection === host.options.projection)
71 )
72 return None
73 val visual = frame.layout.hitTest(local)
74 val point = frame.display.documentPosition(frame.display.mapping.reverse(visual))
75 return Some(EditorDropPosition(frame, windowPoint, local, point))
76 }
78 private fun validLocation(): EditorDropPosition? {
79 if (!enabled)
80 return None
81 val value = location ?: return None
82 val frame = host.accepted ?: return None
83 if (
84 !(frame === value.frame) ||
85 !(frame.editor === host.current) ||
86 frame.revision !=
87 host.current.revision ||
88 !(frame.projection === host.options.projection)
89 )
90 return None
91 return Some(value)
92 }
94 private fun offer(value: DropOffer, point: Point): DropDecision {
95 val origin = editorDropSource(value.localData)
96 val operation = editorDropEffect(value, origin)
97 if (operation == None) {
98 clear()
99 return DropDecision.Reject
100 }
101 val format =
102 if (value.formats.text)
103 Some(DragFormat.Text)
104 else
105 if (value.formats.url)
106 Some(DragFormat.Url)
107 else
108 None
109 if (format == None) {
110 clear()
111 return DropDecision.Reject
112 }
113 val next =
114 when (val result = locate(Point(value.position.x, value.position.y), point)) {
115 case Some(val position) -> position
116 case None -> {
117 clear();
118 return DropDecision.Reject
119 }
120 }
121 location = Some(next)
122 source = origin
123 effect = operation!!
124 schedule()
125 return DropDecision.Accept(format!!, effect)
126 }
128 private fun receive(request: DropRequest, point: Point): Boolean {
129 val value = validLocation() ?: return false
130 if (
131 request.effect !=
132 effect ||
133 value.local !=
134 point ||
135 !sameEditorDropSource(source, editorDropSource(request.offer.localData))
136 )
137 return false
138 val origin = source
139 clear()
140 return receiveEditorDrop(host, request, value.position.byteOffset, origin)
141 }
143 fun caret(frame: EditorFrame): Rect? {
144 val value = validLocation() ?: return None
145 if (
146 binding.hover ==
147 None ||
148 !(frame.editor === host.current) ||
149 frame.revision !=
150 value.frame.revision
151 )
152 return None
153 return frame.caretStyle.caret(
154 frame.layout,
155 editorVisualPosition(frame.layout, frame.display.toDisplay(value.position))
156 )
157 }
159 private fun schedule() {
160 val value = validLocation() ?: return
161 if (
162 waiting ||
163 !host.window.isVisible ||
164 host.window.isOccluded ||
165 editorEdgeDelta(host, value.windowPoint, 16.0) ==
166 Point(0.0, 0.0)
167 ) {
168 timer.cancel()
169 return
170 }
171 if (!timer.isScheduled)
172 timer.schedule(MonotonicTimeSource.now() + Duration.milliseconds(16L))
173 }
175 private fun tick() {
176 val value = validLocation() ?: return
177 if (binding.hover == None || !host.window.isVisible || host.window.isOccluded)
178 return
179 val consumed =
180 host.scroll.scrollBy(editorEdgeDelta(host, value.windowPoint, 16.0), ScrollOrigin.User)
181 if (consumed == Point(0.0, 0.0))
182 return
183 waiting = true
184 host.revealNeeded = false
185 }
187 fun accepted() {
188 if (closed)
189 return
190 waiting = false
191 val previous = location ?: return
192 if (binding.hover == None) {
193 clear()
194 return
195 }
196 val local = host.area.toLocal(previous.windowPoint)
197 val next =
198 when (local) {
199 case Some(val point) -> locate(previous.windowPoint, point)
200 case None -> None
201 }
202 location = next
203 val current =
204 when (next) {
205 case Some(val value) -> value
206 case None -> {
207 timer.cancel();
208 return
209 }
210 }
211 if (previous.position != current.position || previous.local != current.local)
212 host.scope.requestFrame()
213 schedule()
214 }
216 private fun clear() {
217 location = None
218 source = None
219 effect = DragEffect.Copy
220 waiting = false
221 timer.cancel()
222 }
224 public override fun close() {
225 if (closed)
226 return
227 closed = true
228 clear()
229 var failure = cleanupUi(None) { activity.close() }
230 failure = cleanupUi(failure) { binding.close() }
231 failure = cleanupUi(failure) { timer.close() }
232 throwUi(failure)
233 }