Scoop API

scoop:scoop.ui:0.1.0

src/text/handles/selection.scoop

Selected production source · src/text/handles/selection.scoop

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.platform.time.MonotonicTimeSource
6import scoop.surface.*
7import scoop.time.Duration
8import scoop.ui.*
9import scoop.window.*
11internal class TextHandleContact(
12 val start: ContactState,
13 val area: InputArea,
14 val anchor: Boolean,
15 val initial: EditorSelection,
16 val grab: Point
17) {
18 val caretOnly = initial.anchor.byteOffset == initial.cursor.byteOffset
19 var latest = start
20 var claimed = false
21 var waiting = false
22 var pendingHit = false
23 var ended = false
24 var scroll: TextHandleScroll? = None
27/** Two real text hosts share contact ownership without synthesizing mouse input. */
28internal class TextSelectionHandles(
29 internal val scope: ComponentScope,
30 internal val area: InputArea,
31 internal val focus: FocusTarget,
32 enabled: Boolean,
33 touch: TextTouchOptions,
34 internal val ready: () -> Boolean,
35 internal val selection: () -> EditorSelection,
36 internal val caret: (TextPosition) -> Rect?,
37 internal val hit: (Point) -> TextPosition,
38 private val select: (EditorSelection) -> Boolean,
39 internal val takeContact: () -> Unit
40): Closeable {
41 internal var enabled = enabled && touch.enabled && touch.selectionHandles.enabled
42 internal var options = touch.selectionHandles
43 internal var threshold = touch.threshold
44 internal var closed = false
45 internal var visible = false
46 internal var contact: TextHandleContact? = None
47 internal val contacts = ArrayList<ContactId>()
48 internal var moved = false
49 internal val anchorArea = scope.createArea(parent = Some(area))
50 internal val cursorArea = scope.createArea(parent = Some(area))
51 private val anchorEvents =
52 anchorArea.scope.addDefaultAction {
53 context,
54 event -> textHandleInput(this, anchorArea, true, context, event)
55 }
56 private val cursorEvents =
57 cursorArea.scope.addDefaultAction {
58 context,
59 event -> textHandleInput(this, cursorArea, false, context, event)
60 }
61 private val events =
62 area.scope.addEventHandler { context, event -> textHandleObserve(this, context, event) }
63 private val finished = area.scope.addEventFinished { event -> textHandleFinished(this, event) }
64 private val windowEvents =
65 scope.observeWindow(scope.nativeWindow) { event -> textHandleWindow(this, event) }
66 private val activity =
67 scope.observeWindowActivity {
68 value -> if (!value.focused || !value.visible || value.occluded)
69 hide()
70 }
71 private var timer: WindowDeadline? = None
73 internal fun allowed(): Boolean =
74 !closed &&
75 !area.isClosed &&
76 enabled &&
77 visible &&
78 scope.isMounted &&
79 area.scope.activity.hasFocus &&
80 area.scope.isInputAllowed &&
81 scope.nativeWindow.state ==
82 WindowState.Open &&
83 scope.nativeWindow.isFocused &&
84 scope.nativeWindow.isVisible &&
85 !scope.nativeWindow.isOccluded
87 fun update(enabled: Boolean, options: TextTouchOptions) {
88 val next = options.selectionHandles
89 val active = enabled && options.enabled && next.enabled
90 if (
91 this.enabled !=
92 active ||
93 threshold !=
94 options.threshold ||
95 this.options.radius !=
96 next.radius ||
97 this.options.hitRadius !=
98 next.hitRadius ||
99 this.options.color !=
100 next.color
101 )
102 hide()
103 this.enabled = active
104 this.options = next
105 threshold = options.threshold
106 }
108 fun show() {
109 if (!closed && enabled && !area.isClosed && area.scope.activity.hasFocus && !visible) {
110 visible = true
111 scope.requestFrame()
112 }
113 }
115 fun hide() {
116 val changed = visible
117 visible = false
118 clear(true)
119 if (changed && !closed && scope.isMounted)
120 scope.requestFrame()
121 }
123 internal fun same(value: TextHandleContact): Boolean =
124 when (contact) {
125 case Some(val current) -> current === value
126 case None -> false
127 }
129 internal fun clear(release: Boolean) {
130 val previous = contact
131 contact = None
132 moved = false
133 timer?.cancel()
134 when (previous) {
135 case Some(val current) -> {
136 var failure = cleanupUi(None) { current.scroll?.close() }
137 if (release && current.claimed && !current.area.isClosed)
138 failure =
139 cleanupUi(failure) {
140 current.area.scope.cancelContactClaim(current.start.id)
141 }
142 throwUi(failure)
143 }
144 case None -> {}
145 }
146 }
148 internal fun move(current: TextHandleContact) {
149 if (!same(current))
150 return
151 if (!allowed() || !(current.scroll?.valid() ?: true)) {
152 hide()
153 return
154 }
155 if (current.waiting)
156 return
157 if (!ready()) {
158 current.waiting = true
159 return
160 }
161 val pointer = textHandlePoint(current.latest)
162 val local = area.toLocal(Point(pointer.x - current.grab.x, pointer.y - current.grab.y))
163 val point =
164 when (local) {
165 case Some(val value) -> value
166 case None -> {
167 hide()
168 return
169 }
170 }
171 val position = hit(point)
172 val value =
173 if (current.caretOnly)
174 EditorSelection(position, position)
175 else
176 if (current.anchor)
177 EditorSelection(position, current.initial.cursor)
178 else
179 EditorSelection(current.initial.anchor, position)
180 current.pendingHit = false
181 try {
182 current.waiting = select(value)
183 } catch (error: Throwable) {
184 val failure = cleanupUi(Some(error)) { hide() }
185 throwUi(failure)
186 return
187 }
188 if (!same(current))
189 return
190 if (!allowed() || selection() != value) {
191 hide()
192 return
193 }
194 if (current.ended)
195 clear(false)
196 else
197 schedule(current)
198 }
200 fun accepted() {
201 val current = contact ?: return
202 if (!current.claimed)
203 return
204 if (!allowed() || !(current.scroll?.valid() ?: true)) {
205 hide()
206 return
207 }
208 if (!ready())
209 return
210 current.waiting = false
211 if (current.pendingHit)
212 move(current)
213 else
214 schedule(current)
215 }
217 private fun schedule(current: TextHandleContact) {
218 timer?.cancel()
219 if (
220 !same(current) ||
221 current.waiting ||
222 current.ended ||
223 !(current.scroll?.canMove(textHandlePoint(current.latest)) ?: false)
224 )
225 return
226 val clock = timer ?: scope.createDeadline { tick() }
227 timer = Some(clock)
228 clock.schedule(MonotonicTimeSource.now() + Duration.milliseconds(16L))
229 }
231 private fun tick() {
232 val current = contact ?: return
233 if (!allowed() || !ready() || !(current.scroll?.valid() ?: false)) {
234 hide()
235 return
236 }
237 try {
238 if (current.scroll!!.move(textHandlePoint(current.latest)) && same(current)) {
239 current.waiting = true
240 current.pendingHit = true
241 }
242 } catch (error: Throwable) {
243 val failure = cleanupUi(Some(error)) { hide() }
244 throwUi(failure)
245 }
246 }
248 fun scene(
249 context: SceneContext,
250 bounds: Rect,
251 selection: EditorSelection,
252 anchor: Rect?,
253 cursor: Rect?,
254 color: Color
255 ) {
256 textHandleScene(this, context, bounds, selection, anchor, cursor, color)
257 }
259 public override fun close() {
260 if (closed)
261 return
262 closed = true
263 visible = false
264 var failure = cleanupUi(None) { clear(true) }
265 contacts.clear()
266 failure = cleanupUi(failure) { timer?.close() }
267 failure = cleanupUi(failure) { activity.close() }
268 failure = cleanupUi(failure) { windowEvents.close() }
269 failure = cleanupUi(failure) { events.close() }
270 failure = cleanupUi(failure) { finished.close() }
271 failure = cleanupUi(failure) { anchorEvents.close() }
272 failure = cleanupUi(failure) { cursorEvents.close() }
273 failure = cleanupUi(failure) { anchorArea.close() }
274 failure = cleanupUi(failure) { cursorArea.close() }
275 throwUi(failure)
276 }