Scoop API

scoop:scoop.ui:0.1.0

src/text/handles/input.scoop

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

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.math.hypot
5import scoop.surface.*
6import scoop.ui.*
7import scoop.window.*
9internal fun textHandlePoint(value: ContactState): Point =
10 Point(value.pointer.logicalPosition.x, value.pointer.logicalPosition.y)
12internal fun textHandleObserve(
13 host: TextSelectionHandles,
14 context: RoutedEventContext,
15 event: InputEvent
16) {
17 if (host.closed)
18 return
19 when (event) {
20 case Contact(InputContactEvent.Began(val value)) -> {
21 if (context.phase == RoutedPhase.Notification)
22 return
23 rememberContact(host.contacts, value.id)
24 if (host.contacts.size > 1L) {
25 // Releasing the first route must not admit this second begin as a fresh tap.
26 if (host.contact?.claimed ?: false)
27 context.preventDefault()
28 host.clear(true)
29 }
30 }
31 case Contact(InputContactEvent.Cancelled(val id, _, _)) -> {
32 forgetContact(host.contacts, id)
33 if (host.contact?.start?.id == Some(id))
34 host.clear(false)
35 }
36 case FocusChanged(false) -> {
37 if (!host.area.scope.activity.hasFocus)
38 host.hide()
39 }
40 else -> {}
41 }
44internal fun textHandleWindow(host: TextSelectionHandles, event: WindowEvent) {
45 if (host.closed || host.area.isClosed)
46 return
47 when (event) {
48 case Contact(ContactEvent.Began(val value)) -> {
49 if (!host.area.hitTest(textHandlePoint(value)))
50 host.hide()
51 }
52 else -> if (interruptsTouch(event)) host.hide()
53 }
56internal fun textHandleInput(
57 host: TextSelectionHandles,
58 area: InputArea,
59 anchor: Boolean,
60 context: RoutedEventContext,
61 event: InputEvent
62) {
63 if (host.closed)
64 return
65 when (event) {
66 case Contact(InputContactEvent.Began(val value)) -> {
67 if (!host.allowed() || !host.ready() || host.contacts.size != 1L)
68 return
69 val selection = host.selection()
70 val rect =
71 host.caret(
72 if (anchor)
73 selection.anchor
74 else
75 selection.cursor
76 ) ?:
77 return
78 val caret =
79 host.area.toWindow(
80 Rect(rect.x + rect.width / 2.0, rect.y + rect.height / 2.0, 0.0, 0.0)
81 ) ?:
82 return
83 val pointer = textHandlePoint(value)
84 host.contact =
85 Some(
86 TextHandleContact(
87 value,
88 area,
89 anchor,
90 selection,
91 Point(pointer.x - caret.x, pointer.y - caret.y)
92 )
93 )
94 }
95 case Contact(InputContactEvent.Moved(val value)) -> {
96 host.moved = true
97 val current = host.contact ?: return
98 if (current.start.id != value.id)
99 return
100 current.latest = value
101 if (!current.claimed) {
102 val start = textHandlePoint(current.start)
103 val point = textHandlePoint(value)
104 val distance = hypot(point.x - start.x, point.y - start.y)
105 if (distance == 0.0 || distance < host.threshold)
106 return
107 if (
108 !host.allowed() ||
109 !host.ready() ||
110 !current.area.scope.claimContacts([value.id])
111 ) {
112 host.clear(false)
113 return
114 }
115 current.claimed = true
116 if (!host.same(current) || !host.allowed()) {
117 current.area.scope.cancelContactClaim(value.id)
118 return
119 }
120 host.takeContact()
121 if (!host.same(current) || !host.allowed())
122 return
123 current.scroll = Some(TextHandleScroll(host.focus, host.area, { host.hide() }))
124 }
125 context.consume()
126 current.pendingHit = true
127 host.move(current)
128 }
129 case Contact(InputContactEvent.Ended(val value)) -> {
130 val current = host.contact ?: return
131 if (current.start.id != value.id)
132 return
133 if (!current.claimed) {
134 host.clear(false)
135 return
136 }
137 current.latest = value
138 current.ended = true
139 current.pendingHit = true
140 context.consume()
141 host.move(current)
142 }
143 else -> {}
144 }
147internal fun textHandleFinished(host: TextSelectionHandles, event: InputEvent) {
148 if (host.closed)
149 return
150 when (event) {
151 case Contact(InputContactEvent.Moved(val value)) -> {
152 if (!host.moved && host.contact?.start?.id == Some(value.id))
153 host.clear(true)
154 host.moved = false
155 }
156 case Contact(InputContactEvent.Ended(val value)) -> {
157 forgetContact(host.contacts, value.id)
158 val current = host.contact ?: return
159 if (current.start.id == value.id && !current.ended)
160 host.clear(false)
161 }
162 else -> {}
163 }