scoop:scoop.ui:0.1.0
src/text/selectable/drag_source.scoop
Selected production source · src/text/selectable/drag_source.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.graphics.text.TextRange5
import scoop.graphics.text.drawText6
import scoop.surface.*7
import scoop.ui.*8
import scoop.window.*10
private class SelectableDragCandidate(val host: SelectableTextHost) {11
val content = host.content12
val selection = host.selection13
}15
/** Read-only source preparation reuses the paragraph's normal copy representation. */16
internal class SelectableDragSource(17
private val host: SelectableTextHost,18
create: (DragSourceOptions, (DragSourceStart) -> DragSourceData?) -> DragSource19
): Closeable {20
private var closed = false21
private var pending: SelectableDragCandidate? = None22
private val binding = create(options()) { start -> prepare(start) }23
private val enabled: Boolean get() =24
!closed &&25
!host.closed &&26
host.scope.isMounted &&27
host.options.enabled &&28
host.area.scope.isInputAllowed &&29
host.options.drag.effects.copy &&30
textDragAllowed(host.options.drag, host.options.copy)31
private fun options(): DragSourceOptions =32
DragSourceOptions(33
host.options.drag.threshold,34
enabled,35
contacts = host.options.touch.enabled36
)38
private fun valid(candidate: SelectableDragCandidate): Boolean =39
enabled && candidate.content === host.content && candidate.selection == host.selection41
fun update() {42
if (closed)43
return44
if (host.closed || !host.scope.isMounted || binding.isClosed) {45
close()46
return47
}48
when (pending) {49
case Some(val value) -> if (!valid(value))50
pending = None51
case None -> {}52
}53
binding.update(options()) { start -> prepare(start) }54
}56
fun cancelCandidate() {57
pending = None58
}60
fun handle(context: RoutedEventContext, event: InputEvent): Boolean {61
if (closed)62
return false63
when (event) {64
case Contact(InputContactEvent.Began(val value)) -> {65
if (context.phase != RoutedPhase.Default)66
return false67
pending = None68
if (69
!enabled ||70
!binding.hasContactCandidate(value.id) ||71
value.pointer.modifiers.shift ||72
host.touchHandles.contact?.start?.id ==73
Some(value.id)74
)75
return false76
val point = context.localPosition ?: return false77
val frame = host.inputFrame() ?: return false78
val range = host.selection.range79
if (frame.content !== host.content || range.start == range.end)80
return false81
var inside = false82
for (box in frame.layout.selection(TextRange(range.start, range.end)))83
if (textMenuContains(box.rect, point)) {84
inside = true85
break86
}87
if (!inside)88
return false89
pending = Some(SelectableDragCandidate(host))90
return advanceContact(context, event)91
}92
case Contact(InputContactEvent.Moved(val value)) -> {93
if (context.phase != RoutedPhase.Default)94
return false95
val candidate = pending ?: return false96
if (97
!valid(candidate) ||98
!binding.hasContactCandidate(value.id) ||99
host.touchHandles.contact?.start?.id !=100
None101
) {102
pending = None103
return false104
}105
return advanceContact(context, event)106
}107
case Contact(InputContactEvent.Ended(_)) -> {108
pending = None109
return false110
}111
case Pointer(112
InputPointerEvent.Button(113
val pointer,114
MouseButton.Left,115
ElementState.Pressed,116
val count117
)118
) -> {119
if (context.phase == RoutedPhase.Capture || !hasCandidate()) {120
pending = None121
if (!enabled || count != 1 || pointer.state.modifiers.shift)122
return false123
val point = context.localPosition ?: return false124
val frame = host.inputFrame() ?: return false125
if (!(frame.content === host.content))126
return false127
val range = host.selection.range128
if (range.start == range.end)129
return false130
var inside = false131
for (box in frame.layout.selection(TextRange(range.start, range.end)))132
if (textMenuContains(box.rect, point)) {133
inside = true134
break135
}136
if (!inside)137
return false138
selectableStopDrag(host)139
pending = Some(SelectableDragCandidate(host))140
}141
if (context.phase == RoutedPhase.Default)142
binding.handle(context, event)143
return true144
}145
case Pointer(InputPointerEvent.Moved(_)) -> {146
val candidate = pending ?: return false147
if (!valid(candidate)) {148
pending = None149
return false150
}151
if (context.phase == RoutedPhase.Default)152
binding.handle(context, event)153
return true154
}155
case Pointer(156
InputPointerEvent.Button(_, MouseButton.Left, ElementState.Released, _)157
) -> {158
val candidate = pending ?: return false159
if (context.phase != RoutedPhase.Default)160
return true161
pending = None162
if (valid(candidate)) {163
val frame = host.inputFrame() ?: return true164
val point = context.localPosition ?: return true165
val position = frame.layout.hitTest(point)166
host.setSelection(position, position)167
}168
return true169
}170
else -> return false171
}172
}174
private fun advanceContact(context: RoutedEventContext, event: InputEvent): Boolean {175
binding.handle(context, event)176
if (!binding.isDragging)177
return false178
host.touchPress.cancel()179
host.touchHandles.hide()180
return true181
}183
private fun hasCandidate(): Boolean =184
when (pending) {185
case Some(_) -> true186
case None -> false187
}189
private fun prepare(start: DragSourceStart): DragSourceData? {190
val candidate = pending ?: return None191
pending = None192
if (!valid(candidate) || start.press.modifiers.shift)193
return None194
val frame = host.inputFrame() ?: return None195
if (!(frame.content === candidate.content))196
return None197
val range = candidate.selection.range198
val text = frame.content.toPlainText(TextRange(range.start, range.end))199
val preview =200
host.options.drag.preparePreview(start, text) {201
val boxes = frame.layout.selection(TextRange(range.start, range.end))202
start.renderSelectionPreview(203
Array<Rect>(boxes.size) { boxes[it].rect },204
host.area205
) { canvas -> canvas.drawText(frame.layout) }206
}207
if (!valid(candidate))208
return None209
val notify = host.options.drag.onCompleted210
return Some(211
DragSourceData(212
DragContent.Text(text),213
DragEffects(true, false, false),214
preview = preview215
) { result -> notify(textDragResult(result)) }216
)217
}219
public override fun close() {220
if (closed)221
return222
closed = true223
pending = None224
binding.close()225
}226
}