Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/pointer.scoop

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

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.math.abs
6import scoop.math.max
7import scoop.math.min
8import scoop.platform.time.MonotonicTimeSource
9import scoop.surface.*
10import scoop.time.Duration
11import scoop.ui.*
13internal enum EditorDragUnit {
14 Character, Word, Line
17internal fun editorClickUnit(count: Int): EditorDragUnit =
18 if (count < 2)
19 EditorDragUnit.Character
20 else
21 if (count == 2)
22 EditorDragUnit.Word
23 else
24 EditorDragUnit.Line
26internal class EditorDrag(
27 var windowPoint: Point,
28 val unit: EditorDragUnit,
29 val initial: TextRange,
30 val anchor: TextPosition
31) {
32 var waiting = false
35internal fun editorDragging(host: TextEditHost): Boolean =
36 when (host.drag) {
37 case Some(_) -> true
38 case None -> false
39 }
41internal fun editorPress(host: TextEditHost, pointer: InputPointer, local: Point?, clicks: Int) {
42 val point = local ?: return
43 host.area.requestFocus(FocusOrigin.Pointer)
44 val input = editorInputText(host)
45 val layout = input.layout
46 val displayed = layout.hitTest(point)
47 val position = input.display.documentPosition(input.display.mapping.reverse(displayed))
48 val shift = pointer.state.modifiers.shift
49 val unit =
50 if (shift)
51 EditorDragUnit.Character
52 else
53 editorClickUnit(clicks)
54 val range = editorProjectedRange(input, editorDragRange(layout, displayed, unit))
55 val anchor =
56 if (shift)
57 host.current.anchor
58 else
59 if (unit == EditorDragUnit.Character)
60 position
61 else
62 TextPosition(range.start)
63 val cursor =
64 if (unit == EditorDragUnit.Character)
65 position
66 else
67 TextPosition(range.end, TextAffinity.Upstream)
68 editorStopDrag(host, false)
69 host.perform(
70 { editorSetSelection(host, EditorSelection(anchor, cursor)) },
71 endComposition = true
72 )
73 val selected = host.current.selection.range
74 host.drag =
75 Some(
76 EditorDrag(
77 Point(pointer.state.logicalPosition.x, pointer.state.logicalPosition.y),
78 unit,
79 TextRange(selected.start, selected.end),
80 host.current.anchor
81 )
82 )
83 host.invalidate()
84 host.revealNeeded = false
87internal fun editorDragMove(host: TextEditHost, pointer: InputPointer) {
88 val drag = host.drag ?: return
89 drag.windowPoint = Point(pointer.state.logicalPosition.x, pointer.state.logicalPosition.y)
90 if (!drag.waiting)
91 editorUpdateDrag(host, drag, Point(0.0, 0.0))
92 editorScheduleDrag(host)
95private fun editorUpdateDrag(host: TextEditHost, drag: EditorDrag, displacement: Point) {
96 val frame = host.accepted ?: return
97 val point = host.area.toLocal(drag.windowPoint) ?: return
98 val offset = frame.presentationOffset
99 val viewport = frame.snapshot.visibleSize
100 val hit =
101 Point(
102 min(max(point.x, offset.x), offset.x + viewport.width) + displacement.x,
103 min(max(point.y, offset.y), offset.y + viewport.height) + displacement.y
104 )
105 val input = editorInputText(host)
106 val layout = input.layout
107 val displayed = layout.hitTest(hit)
108 val position = input.display.documentPosition(input.display.mapping.reverse(displayed))
109 val range = editorProjectedRange(input, editorDragRange(layout, displayed, drag.unit))
110 val selection =
111 if (drag.unit == EditorDragUnit.Character)
112 EditorSelection(drag.anchor, position)
113 else
114 if (range.start < drag.initial.start)
115 EditorSelection(
116 TextPosition(drag.initial.end, TextAffinity.Upstream),
117 TextPosition(range.start)
118 )
119 else
120 EditorSelection(
121 TextPosition(drag.initial.start),
122 TextPosition(range.end, TextAffinity.Upstream)
123 )
124 host.perform({ editorSetSelection(host, selection) }, endComposition = true)
125 host.revealNeeded = false
128internal fun editorDragRange(
129 layout: TextEditLayout,
130 position: TextPosition,
131 unit: EditorDragUnit
132): TextRange =
133 when (unit) {
134 case Character -> TextRange(position.byteOffset, position.byteOffset)
135 case Word -> layout.wordRange(position)
136 case Line -> layout.logicalLineRange(position) ?:
137 TextRange(position.byteOffset, position.byteOffset)
138 }
140internal fun editorStopDrag(host: TextEditHost, reveal: Boolean) {
141 if (!editorDragging(host))
142 return
143 host.drag = None
144 host.dragTimer.cancel()
145 if (reveal && !host.closed && host.scope.isMounted)
146 host.invalidate()
149internal fun editorEdgeDelta(host: TextEditHost, windowPoint: Point, inset: Double = 0.0): Point {
150 val frame = host.accepted ?: return Point(0.0, 0.0)
151 val point = host.area.toLocal(windowPoint) ?: return Point(0.0, 0.0)
152 val snapshot = frame.snapshot
153 val x = point.x - frame.presentationOffset.x
154 val y = point.y - frame.presentationOffset.y
155 val horizontalInset = min(inset, snapshot.visibleSize.width / 2.0)
156 val verticalInset = min(inset, snapshot.visibleSize.height / 2.0)
157 val horizontal =
158 if (x < horizontalInset)
159 editorEdgeStep(x - horizontalInset)
160 else
161 if (x > snapshot.visibleSize.width - horizontalInset)
162 editorEdgeStep(x - snapshot.visibleSize.width + horizontalInset)
163 else
164 0.0
165 val vertical =
166 if (!host.options.multiline)
167 0.0
168 else
169 if (y < verticalInset)
170 editorEdgeStep(y - verticalInset)
171 else
172 if (y > snapshot.visibleSize.height - verticalInset)
173 editorEdgeStep(y - snapshot.visibleSize.height + verticalInset)
174 else
175 0.0
176 val offset = host.scroll.offset
177 val maximum = host.scroll.maximum
178 return Point(
179 min(max(horizontal, -offset.x), maximum.x - offset.x),
180 min(max(vertical, -offset.y), maximum.y - offset.y)
181 )
184private fun editorEdgeStep(distance: Double): Double {
185 val amount = min(40.0, max(2.0, abs(distance) * 0.25))
186 return if (distance < 0.0)
187 -amount
188 else
189 amount
192private fun editorScheduleDrag(host: TextEditHost) {
193 val drag = host.drag ?: return
194 if (
195 drag.waiting ||
196 host.closed ||
197 !host.scope.isMounted ||
198 !host.area.isFocusActive ||
199 !host.window.isVisible ||
200 host.window.isOccluded
201 ) {
202 host.dragTimer.cancel()
203 return
204 }
205 if (editorEdgeDelta(host, drag.windowPoint) == Point(0.0, 0.0)) {
206 host.dragTimer.cancel()
207 return
208 }
209 if (!host.dragTimer.isScheduled)
210 host.dragTimer.schedule(MonotonicTimeSource.now() + Duration.milliseconds(16L))
213internal fun editorDragTick(host: TextEditHost) {
214 val drag = host.drag ?: return
215 if (
216 host.closed ||
217 !host.scope.isMounted ||
218 !host.area.isFocusActive ||
219 !host.window.isVisible ||
220 host.window.isOccluded
221 )
222 return
223 val consumed = host.scroll.scrollBy(editorEdgeDelta(host, drag.windowPoint), ScrollOrigin.User)
224 if (consumed == Point(0.0, 0.0) || host.closed || !host.scope.isMounted)
225 return
226 drag.waiting = true
227 editorUpdateDrag(host, drag, consumed)
230internal fun editorDragAccepted(host: TextEditHost) {
231 val drag = host.drag ?: return
232 drag.waiting = false
233 editorUpdateDrag(host, drag, Point(0.0, 0.0))
234 editorScheduleDrag(host)