scoop:scoop.ui:0.1.0
src/text/editor/drag_source.scoop
Selected production source · src/text/editor/drag_source.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.window.*8
private class EditorDragCandidate(val editor: TextEditorState) {9
val revision = editor.contentRevision10
val selection = editor.selection11
}13
/** Delays only an ordinary click inside the painted selection. */14
internal class EditorDragSource(15
private val host: TextEditHost,16
create: (DragSourceOptions, (DragSourceStart) -> DragSourceData?) -> DragSource17
): Closeable {18
private var closed = false19
private var pending: EditorDragCandidate? = None20
private var active: EditorTextTransfer? = None21
private val binding = create(options()) { start -> prepare(start) }22
private val enabled: Boolean get() =23
!closed &&24
!host.closed &&25
host.scope.isMounted &&26
host.options.enabled &&27
host.area.scope.isInputAllowed &&28
textDragAllowed(29
host.options.drag,30
host.copyAllowed && !host.options.protectedText && !host.options.projection.masked31
) &&32
(33
host.options.drag.effects.copy ||34
(host.options.drag.effects.move && !host.options.readOnly)35
)37
private fun options(): DragSourceOptions =38
DragSourceOptions(39
host.options.drag.threshold,40
enabled,41
contacts = host.options.touch.enabled42
)44
private fun valid(candidate: EditorDragCandidate): Boolean =45
enabled &&46
candidate.editor ===47
host.current &&48
candidate.revision ==49
host.current.contentRevision &&50
candidate.selection ==51
host.current.selection &&52
!host.current.isComposing54
fun update() {55
if (closed)56
return57
if (host.closed || !host.scope.isMounted || binding.isClosed) {58
close()59
return60
}61
when (pending) {62
case Some(val value) -> if (!valid(value))63
cancelCandidate()64
case None -> {}65
}66
binding.update(options()) { start -> prepare(start) }67
when (active) {68
case Some(val value) -> if (binding.isDragging) {69
val configured = host.options.drag.effects70
binding.updateEffects(71
DragEffects(72
value.effects.copy && configured.copy,73
value.effects.move && configured.move && value.canMove,74
false75
)76
)77
} else {78
active = None79
}80
case None -> {}81
}82
}84
fun cancelCandidate() {85
pending = None86
}88
fun handle(context: RoutedEventContext, event: InputEvent): Boolean {89
if (closed)90
return false91
when (event) {92
case Contact(InputContactEvent.Began(val value)) -> {93
pending = None94
if (95
!enabled ||96
!binding.hasContactCandidate(value.id) ||97
host.current.isComposing ||98
value.pointer.modifiers.shift ||99
host.touchHandles.contact?.start?.id ==100
Some(value.id)101
)102
return false103
val point = context.localPosition ?: return false104
val frame = host.accepted ?: return false105
if (106
frame.editor !==107
host.current ||108
frame.revision !=109
host.current.revision ||110
frame.projection !==111
host.options.projection112
)113
return false114
val range = host.current.selection.range115
if (range.start == range.end)116
return false117
var inside = false118
for (box in frame.layout.selection(frame.display.displayRange(range)))119
if (textMenuContains(box.rect, point)) {120
inside = true121
break122
}123
if (!inside)124
return false125
pending = Some(EditorDragCandidate(host.current))126
return advanceContact(context, event)127
}128
case Contact(InputContactEvent.Moved(val value)) -> {129
val candidate = pending ?: return false130
if (131
!valid(candidate) ||132
!binding.hasContactCandidate(value.id) ||133
host.touchHandles.contact?.start?.id !=134
None135
) {136
cancelCandidate()137
return false138
}139
return advanceContact(context, event)140
}141
case Contact(InputContactEvent.Ended(_)) -> {142
pending = None143
return false144
}145
case Pointer(146
InputPointerEvent.Button(147
val pointer,148
MouseButton.Left,149
ElementState.Pressed,150
val count151
)152
) -> {153
pending = None154
if (155
!enabled ||156
host.current.isComposing ||157
count !=158
1 ||159
pointer.state.modifiers.shift160
)161
return false162
val point = context.localPosition ?: return false163
val frame = host.accepted ?: return false164
if (165
!(frame.editor === host.current) ||166
frame.revision !=167
host.current.revision ||168
!(frame.projection === host.options.projection)169
)170
return false171
val range = host.current.selection.range172
if (range.start == range.end)173
return false174
var inside = false175
for (box in frame.layout.selection(frame.display.displayRange(range)))176
if (textMenuContains(box.rect, point)) {177
inside = true178
break179
}180
if (!inside)181
return false182
editorStopDrag(host, false)183
pending = Some(EditorDragCandidate(host.current))184
binding.handle(context, event)185
if (!binding.isDragging)186
active = None187
return true188
}189
case Pointer(InputPointerEvent.Moved(_)) -> {190
val candidate = pending ?: return false191
if (!valid(candidate)) {192
cancelCandidate()193
return false194
}195
binding.handle(context, event)196
if (!binding.isDragging)197
active = None198
return true199
}200
case Pointer(201
InputPointerEvent.Button(val pointer, MouseButton.Left, ElementState.Released, _)202
) -> {203
val candidate = pending ?: return false204
pending = None205
if (valid(candidate)) {206
editorPress(host, pointer, context.localPosition, 1)207
editorStopDrag(host, true)208
}209
return true210
}211
else -> return false212
}213
}215
private fun advanceContact(context: RoutedEventContext, event: InputEvent): Boolean {216
binding.handle(context, event)217
if (!binding.isDragging)218
return false219
host.keyboard.cancelPending()220
host.touchPress.cancel()221
host.touchPan.cancel()222
host.touchHandles.hide()223
return true224
}226
private fun prepare(start: DragSourceStart): DragSourceData? {227
val candidate = pending ?: return None228
pending = None229
if (!valid(candidate) || start.press.modifiers.shift)230
return None231
val editor = candidate.editor232
val config = host.options.drag233
val effects =234
DragEffects(config.effects.copy, config.effects.move && !host.options.readOnly, false)235
val transfer =236
EditorTextTransfer(editor, candidate.selection.range, effects) {237
enabled &&238
host.current ===239
editor &&240
!host.options.readOnly &&241
host.options.drag.effects.move242
}243
val frame = host.accepted ?: return None244
if (245
frame.editor !==246
editor ||247
frame.revision !=248
editor.revision ||249
frame.projection !==250
host.options.projection251
)252
return None253
val preview =254
config.preparePreview(start, transfer.text) {255
val boxes =256
frame.layout.selection(frame.display.displayRange(candidate.selection.range))257
start.renderSelectionPreview(258
Array<Rect>(boxes.size) { boxes[it].rect },259
host.area260
) { canvas -> canvas.drawText(frame.layout) }261
}262
if (!valid(candidate))263
return None264
active = Some(transfer)265
return Some(266
DragSourceData(DragContent.Text(transfer.text), effects, Some(transfer), preview) {267
result -> completed(transfer, result, config.onCompleted)268
}269
)270
}272
private fun completed(273
transfer: EditorTextTransfer,274
native: DragResult,275
notify: (TextDragResult) -> Unit276
) {277
active = None278
var result = textDragResult(native)279
var failure: Throwable? = None280
val moving =281
when (native) {282
case Dropped(DragEffect.Move) -> true283
else -> false284
}285
if (moving && !transfer.moved) {286
result = TextDragResult.SourceRetained287
try {288
when (289
val edit =290
prepareEditorDragRemoval(291
transfer,292
host.options.policy,293
!host.options.multiline294
)295
) {296
case Some(val value) -> {297
host.perform(298
{299
val changed = value.publish(transfer.editor, false)300
transfer.moved = true301
changed302
}303
)304
result = TextDragResult.Moved305
}306
case None -> {}307
}308
} catch (error: Throwable) {309
if (transfer.moved)310
result = TextDragResult.Moved311
failure = Some(error)312
}313
}314
if (!host.closed && host.scope.isMounted) {315
val completed = result316
failure = cleanupUi(failure) { notify(completed) }317
}318
throwUi(failure)319
}321
public override fun close() {322
if (closed)323
return324
closed = true325
pending = None326
active = None327
binding.close()328
}329
}