scoop:scoop.ui:0.1.0
src/text/selectable/events.scoop
Selected production source · src/text/selectable/events.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.*6
import scoop.ui.*7
import scoop.window.*9
internal class SelectableDrag(10
val origin: Point,11
val unit: EditorDragUnit,12
val initial: TextRange,13
val anchor: TextPosition,14
val cursor: TextPosition,15
var started: Boolean = false16
)18
internal fun selectableHandle(19
host: SelectableTextHost,20
context: RoutedEventContext,21
event: InputEvent22
) {23
if (host.closed)24
return25
if (context.phase == RoutedPhase.Notification) {26
when (event) {27
case FocusChanged(false) -> if (!host.area.scope.activity.hasFocus) {28
host.dataSource?.cancelCandidate()29
selectableStopDrag(host)30
}31
case Pointer(InputPointerEvent.Cancelled(_, _)) -> {32
host.dataSource?.cancelCandidate()33
selectableStopDrag(host)34
}35
case Contact(InputContactEvent.Cancelled(_, _, _)) -> host.dataSource?.cancelCandidate()36
else -> {}37
}38
return39
}40
if (41
!host.options.enabled ||42
(context.phase != RoutedPhase.Capture && context.phase != RoutedPhase.Default)43
)44
return45
when (event) {46
case Pointer(47
InputPointerEvent.Button(val pointer, val button, ElementState.Pressed, _)48
) -> {49
if (isContextMenuClick(button, pointer.state.modifiers)) {50
if (51
context.phase ==52
RoutedPhase.Default &&53
selectableContextMenu(host, Some(pointer.state), context.localPosition)54
)55
context.consume()56
return57
}58
}59
case Keyboard(val key) -> {60
if (61
context.phase == RoutedPhase.Default && textMenuKey(key) && host.showContextMenu()62
) {63
context.consume()64
return65
}66
}67
else -> {}68
}69
if (host.dataSource?.handle(context, event) ?: false) {70
if (context.phase == RoutedPhase.Default)71
context.consume()72
return73
}74
when (event) {75
case TextCommand(val command) -> if (76
context.phase == RoutedPhase.Default && host.execute(command)77
)78
context.consume()79
case Pointer(80
InputPointerEvent.Button(val pointer, MouseButton.Left, val state, val count)81
) -> {82
if (state == ElementState.Pressed) {83
if (context.phase == RoutedPhase.Capture) {84
selectablePrepareDrag(host, pointer, context.localPosition, count)85
} else {86
when (host.drag) {87
case None -> selectablePrepareDrag(88
host,89
pointer,90
context.localPosition,91
count92
)93
case Some(_) -> {}94
}95
selectableStartDrag(host)96
context.consume()97
}98
} else99
if (context.phase == RoutedPhase.Default) {100
selectableStopDrag(host)101
context.consume()102
}103
}104
case Pointer(InputPointerEvent.Moved(val pointer)) -> {105
val drag = host.drag ?: return106
if (!drag.started) {107
val x = pointer.state.logicalPosition.x - drag.origin.x108
val y = pointer.state.logicalPosition.y - drag.origin.y109
val threshold = host.options.dragThreshold110
if (x * x + y * y < threshold * threshold)111
return112
if (!selectableStartDrag(host))113
return114
}115
val point = context.localPosition ?: return116
selectableExtend(host, drag, point)117
context.consume()118
}119
case Keyboard(val key) -> {120
if (121
context.phase !=122
RoutedPhase.Default ||123
key.state !=124
ElementState.Pressed ||125
key.synthetic ||126
!host.area.scope.activity.hasFocus127
)128
return129
var handled = false130
for (command in key.textCommands) {131
if (132
host.closed ||133
!host.area.scope.isInputAllowed ||134
!host.area.scope.activity.hasFocus135
)136
break137
if (selectableCommand(host, command))138
handled = true139
}140
if (handled) {141
selectableStopDrag(host)142
context.consume()143
}144
}145
else -> {}146
}147
}149
private fun selectablePrepareDrag(150
host: SelectableTextHost,151
pointer: InputPointer,152
local: Point?,153
count: Int154
) {155
val frame = host.inputFrame() ?: return156
val point = local ?: return157
val position = frame.layout.hitTest(point)158
val shift = pointer.state.modifiers.shift159
val unit =160
if (shift)161
EditorDragUnit.Character162
else163
editorClickUnit(count)164
val range = editorDragRange(TextEditLayout(frame.layout), position, unit)165
val anchor =166
if (shift)167
host.anchor168
else169
if (unit == EditorDragUnit.Character)170
position171
else172
TextPosition(range.start)173
host.drag =174
Some(175
SelectableDrag(176
Point(pointer.state.logicalPosition.x, pointer.state.logicalPosition.y),177
unit,178
range,179
anchor,180
position181
)182
)183
}185
private fun selectableStartDrag(host: SelectableTextHost): Boolean {186
val drag = host.drag ?: return false187
if (drag.started)188
return true189
if (!host.area.scope.claimPointer()) {190
host.drag = None191
return false192
}193
drag.started = true194
host.area.requestFocus(FocusOrigin.Pointer)195
if (host.closed)196
return false197
val cursor =198
if (drag.unit == EditorDragUnit.Character)199
drag.cursor200
else201
TextPosition(drag.initial.end, TextAffinity.Upstream)202
host.setSelection(drag.anchor, cursor)203
return !host.closed204
}206
private fun selectableExtend(host: SelectableTextHost, drag: SelectableDrag, point: Point) {207
val frame = host.inputFrame() ?: return208
val position = frame.layout.hitTest(point)209
val range = editorDragRange(TextEditLayout(frame.layout), position, drag.unit)210
val value =211
if (drag.unit == EditorDragUnit.Character)212
EditorSelection(drag.anchor, position)213
else214
if (range.start < drag.initial.start)215
EditorSelection(216
TextPosition(drag.initial.end, TextAffinity.Upstream),217
TextPosition(range.start)218
)219
else220
EditorSelection(221
TextPosition(drag.initial.start),222
TextPosition(range.end, TextAffinity.Upstream)223
)224
host.setSelection(value.anchor, value.cursor)225
}227
internal fun selectableStopDrag(host: SelectableTextHost) {228
val drag = host.drag ?: return229
host.drag = None230
if (drag.started && !host.area.isClosed)231
host.area.scope.cancelPointerClaim()232
}234
internal fun selectableFinished(host: SelectableTextHost, event: InputEvent) {235
if (host.closed)236
return237
when (event) {238
case Pointer(InputPointerEvent.Button(_, MouseButton.Left, ElementState.Released, _)) -> {239
host.dataSource?.cancelCandidate()240
selectableStopDrag(host)241
}242
else -> {}243
}244
}246
internal fun selectableCommand(host: SelectableTextHost, command: TextEditCommand): Boolean {247
val frame = host.inputFrame() ?: return false248
when (command) {249
case Copy -> {250
val range = host.selection.range251
if (host.options.copy && range.start != range.end)252
host.scope.nativeWindow.clipboard.writeText(253
frame.content.toPlainText(TextRange(range.start, range.end))254
)255
}256
case SelectAll -> {257
host.area.requestFocus()258
host.setSelection(TextPosition(0L), TextPosition(frame.content.text.byteLength))259
}260
case Move(val movement, val extend) -> {261
val result =262
editorMove(263
TextEditLayout(frame.layout),264
host.selected.value,265
movement,266
extend,267
host.preferredX,268
host.area.visibleBounds?.height ?: frame.layout.size.height269
)270
host.area.requestFocus()271
host.setSelection(result.selection.anchor, result.selection.cursor)272
if (!host.closed) {273
host.preferredX = result.preferredX274
when (275
frame.layout.caret(276
editorVisualPosition(TextEditLayout(frame.layout), host.cursor)277
)278
) {279
case Some(val rect) -> host.focus.reveal(rect)280
case None -> {}281
}282
}283
}284
else -> return false285
}286
return true287
}