scoop:scoop.ui:0.1.0
src/text/contact_press.scoop
Selected production source · src/text/contact_press.scoop
1
package scoop.ui.text3
import scoop.graphics.text.*4
import scoop.surface.FocusOrigin5
import scoop.ui.*6
import scoop.window.requestHapticFeedback8
internal fun createEditorContactPress(host: TextEditHost): ContactPress =9
ContactPress(10
host.scope,11
host.area,12
host.options.enabled && host.options.touch.enabled,13
host.options.touch.threshold,14
host.options.touch.longPressDelay,15
host.options.touch.multiTap,16
host.options.touch.multiTapInterval,17
{ editorTouchReady(host) },18
Some({ sample -> editorTouchTap(host, sample) }),19
{ sample -> editorTouchHold(host, sample) }20
)22
internal fun editorTouchReady(host: TextEditHost): Boolean {23
if (host.closed || !host.scope.isMounted || host.contextMenu.pending)24
return false25
val frame = host.accepted ?: return false26
return frame.editor ===27
host.current &&28
frame.revision ==29
host.current.revision &&30
frame.projection ===31
host.options.projection &&32
frame.display.source.text ==33
host.current.text34
}36
private fun editorTouchTap(host: TextEditHost, sample: ContactPressSample): Boolean {37
val document = host.current38
if (!editorTouchReady(host) || !host.area.requestFocus(FocusOrigin.Pointer))39
return false40
if (!editorTouchReady(host) || host.current !== document)41
return false42
val frame = host.accepted!!43
val displayed = frame.layout.hitTest(sample.position)44
val position = frame.display.documentPosition(frame.display.mapping.reverse(displayed))45
val unit = editorClickUnit(sample.count)46
val range =47
editorProjectedRange(48
EditorInputLayout(frame.layout, frame.display, frame.caretStyle),49
editorDragRange(frame.layout, displayed, unit)50
)51
val selection =52
if (unit == EditorDragUnit.Character)53
EditorSelection(position, position)54
else55
EditorSelection(56
TextPosition(range.start),57
TextPosition(range.end, TextAffinity.Upstream)58
)59
editorStopDrag(host, false)60
host.touchHandles.show()61
host.perform(62
{ editorSetSelection(host, selection) },63
endComposition = true,64
cancelContacts = false65
)66
host.keyboard.touch()67
return !host.closed68
}70
private fun editorTouchHold(host: TextEditHost, sample: ContactPressSample) {71
if (72
editorTouchReady(host) &&73
editorContextMenu(74
host,75
Some(sample.contact.pointer),76
Some(sample.position),77
touch = true78
) &&79
!host.closed &&80
host.options.touch.hapticFeedback81
)82
host.window.requestHapticFeedback()83
}85
internal fun createSelectableContactPress(host: SelectableTextHost): ContactPress =86
ContactPress(87
host.scope,88
host.area,89
host.options.enabled && host.options.touch.enabled,90
host.options.touch.threshold,91
host.options.touch.longPressDelay,92
host.options.touch.multiTap,93
host.options.touch.multiTapInterval,94
{ selectableTouchReady(host) },95
Some({ sample -> selectableTouchTap(host, sample) }),96
{ sample -> selectableTouchHold(host, sample) }97
)99
internal fun selectableTouchReady(host: SelectableTextHost): Boolean {100
if (host.closed || !host.scope.isMounted || host.contextMenu.pending)101
return false102
return when (host.inputFrame()) {103
case Some(_) -> true104
case None -> false105
}106
}108
private fun selectableTouchTap(host: SelectableTextHost, sample: ContactPressSample): Boolean {109
val document = host.content110
if (!selectableTouchReady(host) || !host.area.requestFocus(FocusOrigin.Pointer))111
return false112
if (!selectableTouchReady(host) || host.content !== document)113
return false114
val frame = host.inputFrame() ?: return false115
val position = frame.layout.hitTest(sample.position)116
val unit = editorClickUnit(sample.count)117
val range = editorDragRange(TextEditLayout(frame.layout), position, unit)118
val selection =119
if (unit == EditorDragUnit.Character)120
EditorSelection(position, position)121
else122
EditorSelection(123
TextPosition(range.start),124
TextPosition(range.end, TextAffinity.Upstream)125
)126
selectableStopDrag(host)127
host.preferredX = None128
host.touchHandles.show()129
host.publishSelection(selection, cancelContacts = false)130
return !host.closed131
}133
private fun selectableTouchHold(host: SelectableTextHost, sample: ContactPressSample) {134
if (135
selectableTouchReady(host) &&136
selectableContextMenu(137
host,138
Some(sample.contact.pointer),139
Some(sample.position),140
touch = true141
) &&142
!host.closed &&143
host.options.touch.hapticFeedback144
)145
host.scope.nativeWindow.requestHapticFeedback()146
}148
internal fun editorTouchAxes(options: TextEditOptions): ScrollAxes =149
if (options.multiline)150
ScrollAxes.Both151
else152
ScrollAxes.Horizontal154
internal fun editorTouchScroll(options: TextEditOptions): TouchScrollOptions =155
if (options.enabled)156
options.touchScroll157
else158
TouchScrollOptions(159
false,160
options.touchScroll.threshold,161
options.touchScroll.lockAxis,162
options.touchScroll.claimAtBoundary163
)