Scoop API

scoop:scoop.ui:0.1.0

src/text/contact_press.scoop

Selected production source · src/text/contact_press.scoop

1package scoop.ui.text
3import scoop.graphics.text.*
4import scoop.surface.FocusOrigin
5import scoop.ui.*
6import scoop.window.requestHapticFeedback
8internal 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 )
22internal fun editorTouchReady(host: TextEditHost): Boolean {
23 if (host.closed || !host.scope.isMounted || host.contextMenu.pending)
24 return false
25 val frame = host.accepted ?: return false
26 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.text
36private fun editorTouchTap(host: TextEditHost, sample: ContactPressSample): Boolean {
37 val document = host.current
38 if (!editorTouchReady(host) || !host.area.requestFocus(FocusOrigin.Pointer))
39 return false
40 if (!editorTouchReady(host) || host.current !== document)
41 return false
42 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 else
55 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 = false
65 )
66 host.keyboard.touch()
67 return !host.closed
70private 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 = true
78 ) &&
79 !host.closed &&
80 host.options.touch.hapticFeedback
81 )
82 host.window.requestHapticFeedback()
85internal 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 )
99internal fun selectableTouchReady(host: SelectableTextHost): Boolean {
100 if (host.closed || !host.scope.isMounted || host.contextMenu.pending)
101 return false
102 return when (host.inputFrame()) {
103 case Some(_) -> true
104 case None -> false
105 }
108private fun selectableTouchTap(host: SelectableTextHost, sample: ContactPressSample): Boolean {
109 val document = host.content
110 if (!selectableTouchReady(host) || !host.area.requestFocus(FocusOrigin.Pointer))
111 return false
112 if (!selectableTouchReady(host) || host.content !== document)
113 return false
114 val frame = host.inputFrame() ?: return false
115 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 else
122 EditorSelection(
123 TextPosition(range.start),
124 TextPosition(range.end, TextAffinity.Upstream)
125 )
126 selectableStopDrag(host)
127 host.preferredX = None
128 host.touchHandles.show()
129 host.publishSelection(selection, cancelContacts = false)
130 return !host.closed
133private 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 = true
141 ) &&
142 !host.closed &&
143 host.options.touch.hapticFeedback
144 )
145 host.scope.nativeWindow.requestHapticFeedback()
148internal fun editorTouchAxes(options: TextEditOptions): ScrollAxes =
149 if (options.multiline)
150 ScrollAxes.Both
151 else
152 ScrollAxes.Horizontal
154internal fun editorTouchScroll(options: TextEditOptions): TouchScrollOptions =
155 if (options.enabled)
156 options.touchScroll
157 else
158 TouchScrollOptions(
159 false,
160 options.touchScroll.threshold,
161 options.touchScroll.lockAxis,
162 options.touchScroll.claimAtBoundary
163 )