Scoop API

scoop:scoop.ui:0.1.0

src/text/handles/scroll.scoop

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

1package scoop.ui.text
3import scoop.collections.map
4import scoop.graphics.*
5import scoop.math.*
6import scoop.surface.InputArea
7import scoop.ui.*
9private class TextHandleScrollBinding(boundary: ScrollReveal) {
10 val viewport = boundary.viewport
11 val state = boundary.state
12 val binding = boundary.layout.binding
15/** Uses the current focus/reveal path, including enclosing read-only viewports. */
16internal class TextHandleScroll(
17 private val focus: FocusTarget,
18 private val area: InputArea,
19 private val interrupt: () -> Unit
20): Closeable {
21 private var closed = false
22 private val bindings =
23 focus.node.root.focus.textSelectionScrollPath(area).map {
24 value -> TextHandleScrollBinding(value)
25 }
26 private val subscriptions = ArrayList<Closeable>()
27 private var issuing: ScrollState? = None
28 private var revision = 0UL
30 init {
31 for (binding in bindings)
32 subscriptions.add(
33 binding.state.observeIntent {
34 event -> when (event) {
35 case LayoutCorrection(_) -> {}
36 case User(val value) -> {
37 val own =
38 when (issuing) {
39 case Some(val state) -> state ===
40 binding.state &&
41 revision ==
42 value
43 case None -> false
44 }
45 if (!closed && !own)
46 interrupt()
47 }
48 case Programmatic(_) -> if (!closed)
49 interrupt()
50 }
51 }
52 )
53 }
55 fun valid(): Boolean {
56 if (closed)
57 return false
58 val route = focus.node.root.focus.textSelectionScrollPath(area)
59 if (route.size != bindings.size)
60 return false
61 for (index in 0L ..< route.size) {
62 val actual = route[index]
63 val expected = bindings[index]
64 if (
65 actual.viewport !==
66 expected.viewport ||
67 actual.state !==
68 expected.state ||
69 actual.layout.binding !=
70 expected.binding ||
71 !actual.active
72 )
73 return false
74 }
75 return true
76 }
78 fun canMove(pointer: Point): Boolean {
79 if (!valid())
80 return false
81 for (boundary in focus.node.root.focus.textSelectionScrollPath(area))
82 if (textHandleScrollDelta(boundary, pointer) != Point(0.0, 0.0))
83 return true
84 return false
85 }
87 fun move(pointer: Point): Boolean {
88 if (!valid())
89 return false
90 val route = focus.node.root.focus.textSelectionScrollPath(area)
91 var index = route.size
92 while (index > 0L) {
93 index -= 1L
94 val boundary = route[index]
95 val delta = textHandleScrollDelta(boundary, pointer)
96 if (delta == Point(0.0, 0.0))
97 continue
98 issuing = Some(boundary.state)
99 revision = boundary.state.requestRevision + 1UL
100 val consumed =
101 try {
102 boundary.state.scrollBy(delta, ScrollOrigin.User)
103 } finally {
104 issuing = None
105 }
106 return consumed != Point(0.0, 0.0)
107 }
108 return false
109 }
111 public override fun close() {
112 if (closed)
113 return
114 closed = true
115 var failure: Throwable? = None
116 for (subscription in subscriptions)
117 failure = cleanupUi(failure) { subscription.close() }
118 subscriptions.clear()
119 throwUi(failure)
120 }
123private fun textHandleScrollDelta(boundary: ScrollReveal, pointer: Point): Point {
124 if (!boundary.current)
125 return Point(0.0, 0.0)
126 val visible = boundary.visibleBounds ?: return Point(0.0, 0.0)
127 val bounds = localReveal(boundary.geometry, visible) ?: return Point(0.0, 0.0)
128 val point = boundary.geometry.toLocal(pointer) ?: return Point(0.0, 0.0)
129 val snapshot = boundary.layout.snapshot
130 return Point(
131 if (boundary.horizontal)
132 textHandleEdge(point.x, bounds.x, bounds.width, snapshot.offset.x, snapshot.maximum.x)
133 else
134 0.0,
135 if (boundary.vertical)
136 textHandleEdge(point.y, bounds.y, bounds.height, snapshot.offset.y, snapshot.maximum.y)
137 else
138 0.0
139 )
142private fun textHandleEdge(
143 value: Double,
144 start: Double,
145 size: Double,
146 offset: Double,
147 limit: Double
148): Double {
149 val inset = min(24.0, size / 3.0)
150 val distance =
151 if (value < start + inset && offset > 0.0)
152 value - start - inset
153 else
154 if (value > start + size - inset && offset < limit)
155 value - start - size + inset
156 else
157 0.0
158 if (distance == 0.0)
159 return 0.0
160 val step = min(40.0, max(2.0, abs(distance) * 0.25))
161 return if (distance < 0.0)
162 -step
163 else
164 step