Scoop API

scoop:scoop.ui:0.1.0

src/scroll/state.scoop

Selected production source · src/scroll/state.scoop

1package scoop.ui
3import scoop.graphics.*
4import scoop.platform.concurrent.currentThreadId
5import scoop.surface.AnimationSpec
6import scoop.time.Duration
8/** Observable scroll position, independent of a particular mount. */
9public class ScrollState public constructor(initialOffset: Point = Point(0.0, 0.0)) {
10 private val thread = currentThreadId()
11 private val signal = StateSignal()
12 private val subscriptions = ArrayList<ScrollIntentSubscription>()
13 private val notifications = ArrayList<ScrollIntent>()
14 private var notifying = false
15 private var viewportOwner: ScrollViewport? = None
16 private var initialized = false
17 internal var current = ScrollSnapshot(Point(0.0, 0.0), Size(0.0, 0.0), Size(0.0, 0.0))
18 internal var request = ScrollRequest.Position(initialOffset)
19 internal var revision =
20 if (initialOffset == Point(0.0, 0.0))
21 0UL
22 else
23 1UL
24 internal var publishedRevision = revision
26 init {
27 checkScrollPoint(initialOffset)
28 }
30 private fun observeState() {
31 checkUiThread(thread)
32 when (currentEvaluation()) {
33 case Some(val evaluation) -> evaluation.collector?.read(signal)
34 case None -> {}
35 }
36 }
37 internal fun checkMutation() {
38 checkUiThread(thread)
39 when (currentEvaluation()) {
40 case Some(_) -> throw IllegalStateException(
41 Some("scroll state cannot change during render, measure, scene or draw")
42 )
43 case None -> {}
44 }
45 }
46 public val snapshot: ScrollSnapshot get() {
47 observeState();
48 return current
49 }
50 public val offset: Point get() = snapshot.offset
51 public val viewport: Size get() = snapshot.viewport
52 public val content: Size get() = snapshot.content
53 public val maximum: Point get() = snapshot.maximum
54 public val hasSnapshot: Boolean get() {
55 observeState()
56 return initialized
57 }
58 public val requestRevision: ULong get() {
59 checkUiThread(thread)
60 return revision
61 }
62 public val hasPositionRequest: Boolean get() {
63 checkUiThread(thread)
64 return revision != 0UL
65 }
66 public val isAttached: Boolean get() {
67 observeState();
68 return viewportOwner?.active ?: false
69 }
70 public val isAnimating: Boolean get() {
71 observeState();
72 return viewportOwner?.moving ?: false
73 }
74 /** An explicit animateTo request, including its wait for the first accepted layout. */
75 public val hasPositionAnimation: Boolean get() {
76 observeState()
77 return viewportOwner?.positioning ?: false
78 }
80 public fun observeIntent(handler: (ScrollIntent) -> Unit): Closeable {
81 checkMutation()
82 val value = ScrollIntentSubscription(this, handler)
83 subscriptions.add(value)
84 return value
85 }
86 internal fun removeSubscription(value: ScrollIntentSubscription) {
87 var index = 0L
88 while (index < subscriptions.size) {
89 if (subscriptions[index] === value) {
90 subscriptions.removeAt(index)
91 return
92 }
93 index += 1L
94 }
95 }
96 internal fun notifyIntent(value: ScrollIntent) {
97 notifications.add(value)
98 if (notifying)
99 return
100 notifying = true
101 var failure: Throwable? = None
102 try {
103 var index = 0L
104 while (index < notifications.size) {
105 val event = notifications[index]
106 index += 1L
107 for (subscription in subscriptions.toArray())
108 if (!subscription.closed)
109 failure = cleanupUi(failure) { subscription.handler(event) }
110 }
111 } finally {
112 notifications.clear()
113 notifying = false
114 }
115 throwUi(failure)
116 }
117 private fun notifyRequest(origin: ScrollOrigin, token: ULong) {
118 notifyIntent(
119 when (origin) {
120 case User -> ScrollIntent.User(token)
121 case Programmatic -> ScrollIntent.Programmatic(token)
122 }
123 )
124 }
125 public fun scrollTo(offset: Point, origin: ScrollOrigin = ScrollOrigin.Programmatic) {
126 position(offset, origin)
127 }
128 internal fun position(offset: Point, origin: ScrollOrigin = ScrollOrigin.Programmatic): ULong {
129 checkMutation()
130 checkScrollPoint(offset)
131 return change(ScrollRequest.Position(offset), origin)
132 }
133 public fun scrollBy(delta: Point, origin: ScrollOrigin = ScrollOrigin.Programmatic): Point {
134 checkMutation()
135 checkScrollPoint(delta)
136 val base =
137 if (viewportOwner?.moving ?: false)
138 current.offset
139 else
140 if (initialized)
141 plan(current.viewport, current.content, current.padding).offset
142 else
143 when (request) {
144 case Position(val value) -> value
145 case Reveal(_, _, _) -> current.offset
146 }
147 val next = Point(base.x + delta.x, base.y + delta.y)
148 checkScrollPoint(next)
149 val bounded =
150 if (initialized)
151 clampScroll(next, current.viewport, current.content)
152 else
153 base
154 val consumed = Point(bounded.x - base.x, bounded.y - base.y)
155 change(ScrollRequest.Position(next), origin)
156 return consumed
157 }
158 /** Routed input preserves the current held edge; explicit position requests cancel it. */
159 internal fun consumeInput(delta: Point, notify: Boolean = true): Point {
160 checkMutation()
161 checkScrollPoint(delta)
162 val base = plan(current.viewport, current.content, current.padding).offset
163 val next = Point(base.x + delta.x, base.y + delta.y)
164 checkScrollPoint(next)
165 val bounded = clampScroll(next, current.viewport, current.content)
166 if (!notify && bounded == base)
167 return Point(0.0, 0.0)
168 request = ScrollRequest.Position(bounded)
169 if (notify)
170 revision += 1UL
171 val token = revision
172 viewportOwner?.requestFrame()
173 if (notify)
174 notifyRequest(ScrollOrigin.User, token)
175 return Point(bounded.x - base.x, bounded.y - base.y)
176 }
177 public fun reveal(
178 rect: Rect,
179 horizontal: ScrollAlignment = ScrollAlignment.Nearest,
180 vertical: ScrollAlignment = ScrollAlignment.Nearest,
181 origin: ScrollOrigin = ScrollOrigin.Programmatic
182 ) {
183 checkMutation()
184 checkScrollRect(rect)
185 change(ScrollRequest.Reveal(rect, horizontal, vertical), origin)
186 }
187 /** Registers a target that a layout will resolve through ScrollAdjustment. */
188 public fun beginLayoutRequest(origin: ScrollOrigin = ScrollOrigin.Programmatic): ULong {
189 checkMutation()
190 return change(ScrollRequest.Position(current.offset), origin)
191 }
192 public fun animateTo(
193 offset: Point,
194 spec: AnimationSpec = AnimationSpec.Tween(Duration.milliseconds(300L)),
195 origin: ScrollOrigin = ScrollOrigin.Programmatic
196 ) {
197 checkMutation()
198 checkScrollPoint(offset)
199 val owner =
200 viewportOwner ?:
201 throw IllegalStateException(Some("smooth scrolling requires an attached viewport"))
202 val token = owner.animateTo(offset, spec)
203 notifyRequest(origin, token)
204 }
205 public fun cancelAnimation() {
206 checkMutation()
207 viewportOwner?.cancelAnimation()
208 }
209 private fun change(next: ScrollRequest, origin: ScrollOrigin): ULong {
210 viewportOwner?.cancelAnimation()
211 request = next
212 revision += 1UL
213 val token = revision
214 viewportOwner?.requestFrame()
215 notifyRequest(origin, token)
216 return token
217 }
218 internal fun plan(
219 viewport: Size,
220 content: Size,
221 padding: ScrollPadding = ScrollPadding()
222 ): ScrollSnapshot {
223 val dx = padding.left - current.padding.left
224 val dy = padding.top - current.padding.top
225 val preserve =
226 if (initialized)
227 revision == publishedRevision
228 else
229 revision == 0UL
230 val next =
231 when (request) {
232 case Position(val point) -> if (preserve)
233 ScrollRequest.Position(Point(point.x + dx, point.y + dy))
234 else
235 request
236 case Reveal(_, _, _) -> request
237 }
238 return resolveScroll(
239 next,
240 Point(current.offset.x + dx, current.offset.y + dy),
241 viewport,
242 content,
243 padding
244 )
245 }
247 internal fun publish(
248 value: ScrollSnapshot,
249 settle: Boolean,
250 acceptedRevision: ULong = revision
251 ) {
252 val changed = current != value || !initialized
253 current = value
254 publishedRevision = acceptedRevision
255 initialized = true
256 if (settle)
257 request = ScrollRequest.Position(value.offset)
258 if (changed)
259 signal.changed()
260 }
261 internal fun changed() {
262 signal.changed()
263 }
264 internal fun checkBinding() {
265 checkMutation()
266 when (viewportOwner) {
267 case Some(_) -> throw IllegalStateException(Some("scroll state already has a viewport"))
268 case None -> {}
269 }
270 }
271 internal fun attach(owner: ScrollViewport) {
272 viewportOwner = Some(owner)
273 signal.changed()
274 }
275 internal fun detach(owner: ScrollViewport) {
276 when (viewportOwner) {
277 case Some(val value) -> if (value === owner) {
278 viewportOwner = None
279 signal.changed()
280 }
281 case None -> {}
282 }
283 }