scoop:scoop.ui:0.1.0
src/text/editor/pointer.scoop
Selected production source · src/text/editor/pointer.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.math.abs6
import scoop.math.max7
import scoop.math.min8
import scoop.platform.time.MonotonicTimeSource9
import scoop.surface.*10
import scoop.time.Duration11
import scoop.ui.*13
internal enum EditorDragUnit {14
Character, Word, Line15
}17
internal fun editorClickUnit(count: Int): EditorDragUnit =18
if (count < 2)19
EditorDragUnit.Character20
else21
if (count == 2)22
EditorDragUnit.Word23
else24
EditorDragUnit.Line26
internal class EditorDrag(27
var windowPoint: Point,28
val unit: EditorDragUnit,29
val initial: TextRange,30
val anchor: TextPosition31
) {32
var waiting = false33
}35
internal fun editorDragging(host: TextEditHost): Boolean =36
when (host.drag) {37
case Some(_) -> true38
case None -> false39
}41
internal fun editorPress(host: TextEditHost, pointer: InputPointer, local: Point?, clicks: Int) {42
val point = local ?: return43
host.area.requestFocus(FocusOrigin.Pointer)44
val input = editorInputText(host)45
val layout = input.layout46
val displayed = layout.hitTest(point)47
val position = input.display.documentPosition(input.display.mapping.reverse(displayed))48
val shift = pointer.state.modifiers.shift49
val unit =50
if (shift)51
EditorDragUnit.Character52
else53
editorClickUnit(clicks)54
val range = editorProjectedRange(input, editorDragRange(layout, displayed, unit))55
val anchor =56
if (shift)57
host.current.anchor58
else59
if (unit == EditorDragUnit.Character)60
position61
else62
TextPosition(range.start)63
val cursor =64
if (unit == EditorDragUnit.Character)65
position66
else67
TextPosition(range.end, TextAffinity.Upstream)68
editorStopDrag(host, false)69
host.perform(70
{ editorSetSelection(host, EditorSelection(anchor, cursor)) },71
endComposition = true72
)73
val selected = host.current.selection.range74
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.anchor81
)82
)83
host.invalidate()84
host.revealNeeded = false85
}87
internal fun editorDragMove(host: TextEditHost, pointer: InputPointer) {88
val drag = host.drag ?: return89
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)93
}95
private fun editorUpdateDrag(host: TextEditHost, drag: EditorDrag, displacement: Point) {96
val frame = host.accepted ?: return97
val point = host.area.toLocal(drag.windowPoint) ?: return98
val offset = frame.presentationOffset99
val viewport = frame.snapshot.visibleSize100
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.y104
)105
val input = editorInputText(host)106
val layout = input.layout107
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
else114
if (range.start < drag.initial.start)115
EditorSelection(116
TextPosition(drag.initial.end, TextAffinity.Upstream),117
TextPosition(range.start)118
)119
else120
EditorSelection(121
TextPosition(drag.initial.start),122
TextPosition(range.end, TextAffinity.Upstream)123
)124
host.perform({ editorSetSelection(host, selection) }, endComposition = true)125
host.revealNeeded = false126
}128
internal fun editorDragRange(129
layout: TextEditLayout,130
position: TextPosition,131
unit: EditorDragUnit132
): 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
}140
internal fun editorStopDrag(host: TextEditHost, reveal: Boolean) {141
if (!editorDragging(host))142
return143
host.drag = None144
host.dragTimer.cancel()145
if (reveal && !host.closed && host.scope.isMounted)146
host.invalidate()147
}149
internal 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.snapshot153
val x = point.x - frame.presentationOffset.x154
val y = point.y - frame.presentationOffset.y155
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
else161
if (x > snapshot.visibleSize.width - horizontalInset)162
editorEdgeStep(x - snapshot.visibleSize.width + horizontalInset)163
else164
0.0165
val vertical =166
if (!host.options.multiline)167
0.0168
else169
if (y < verticalInset)170
editorEdgeStep(y - verticalInset)171
else172
if (y > snapshot.visibleSize.height - verticalInset)173
editorEdgeStep(y - snapshot.visibleSize.height + verticalInset)174
else175
0.0176
val offset = host.scroll.offset177
val maximum = host.scroll.maximum178
return Point(179
min(max(horizontal, -offset.x), maximum.x - offset.x),180
min(max(vertical, -offset.y), maximum.y - offset.y)181
)182
}184
private 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
-amount188
else189
amount190
}192
private fun editorScheduleDrag(host: TextEditHost) {193
val drag = host.drag ?: return194
if (195
drag.waiting ||196
host.closed ||197
!host.scope.isMounted ||198
!host.area.isFocusActive ||199
!host.window.isVisible ||200
host.window.isOccluded201
) {202
host.dragTimer.cancel()203
return204
}205
if (editorEdgeDelta(host, drag.windowPoint) == Point(0.0, 0.0)) {206
host.dragTimer.cancel()207
return208
}209
if (!host.dragTimer.isScheduled)210
host.dragTimer.schedule(MonotonicTimeSource.now() + Duration.milliseconds(16L))211
}213
internal fun editorDragTick(host: TextEditHost) {214
val drag = host.drag ?: return215
if (216
host.closed ||217
!host.scope.isMounted ||218
!host.area.isFocusActive ||219
!host.window.isVisible ||220
host.window.isOccluded221
)222
return223
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
return226
drag.waiting = true227
editorUpdateDrag(host, drag, consumed)228
}230
internal fun editorDragAccepted(host: TextEditHost) {231
val drag = host.drag ?: return232
drag.waiting = false233
editorUpdateDrag(host, drag, Point(0.0, 0.0))234
editorScheduleDrag(host)235
}