Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/scroll_state.scoop

Selected production source · src/conversation/scroll_state.scoop

1package scoop.ui.component
3import scoop.platform.concurrent.currentThreadId
4import scoop.ui.*
6internal struct MessagePresentation(
7 val mode: MessageScrollMode,
8 val padding: Double,
9 val ready: Boolean,
10 val generation: Long
13internal enum MessageDestination {
14 Initial(val position: MessageInitialPosition), Start, End,
15 Turn(val key: ViewKey), Item(val key: ViewKey, val alignment: ScrollAlignment),
16 Reading(val anchor: VirtualReadingAnchor)
19internal class MessageRequest(val destination: MessageDestination) {
20 var issued: ULong? = None
21 var source: TranscriptSource? = None
24/** Owns reading intent, while ScrollState and VirtualList own physical positioning. */
25public class MessageScrollState public constructor(
26 options: MessageScrollOptions = MessageScrollOptions()
27) {
28 private val thread = currentThreadId()
29 private val settings = state(options)
30 private val observed = state<MessageReadingSnapshot?>(None)
31 private val sourceSignal = state<TranscriptSource?>(None)
32 private val pending =
33 state<MessageRequest?>(
34 Some(MessageRequest(MessageDestination.Initial(options.initialPosition)))
35 )
36 internal val presentation =
37 state(MessagePresentation(MessageScrollMode.Holding, 0.0, false, 0L))
38 public val scroll: ScrollState = ScrollState()
39 public val list: VirtualListHandle = VirtualListHandle()
40 internal var owner: MessageViewport? = None
41 internal var request: MessageRequest?
42 get() = pending.value
43 set(value) {
44 pending.value = value
45 }
46 internal val hasRequest: Boolean get() =
47 when (request) {
48 case Some(_) -> true;
49 case None -> false
50 }
51 internal var epoch = 0L
52 internal var internalRequest = false
53 internal var userRevision: ULong? = None
54 internal var selecting = false
55 internal var previousSource: TranscriptSource?
56 get() = sourceSignal.value
57 set(value) {
58 sourceSignal.value = value
59 }
60 internal var lastVirtual: VirtualReadingSnapshot? = None
61 internal var lastScroll: ScrollSnapshot? = None
62 private var mounted = false
64 private fun checkThread() {
65 requireComponent(
66 currentThreadId() == thread,
67 "message scroll state used from another thread"
68 )
69 }
70 public var options: MessageScrollOptions
71 get() = settings.value
72 set(value) {
73 checkThread()
74 settings.value = value
75 if (!value.autoScroll && mode == MessageScrollMode.Following)
76 hold()
77 }
78 public val mode: MessageScrollMode get() = presentation.value.mode
79 public val isAttached: Boolean get() {
80 checkThread();
81 return owner?.active ?: false
82 }
83 public val isPending: Boolean get() {
84 checkThread()
85 return hasRequest || list.isPending
86 }
87 public val reading: MessageReadingSnapshot? get() = observed.value
88 public val visibleKeys: Array<ViewKey> get() = reading?.visibleKeys ?: []
89 public val currentAnchor: ViewKey? get() = reading?.currentAnchor ?: None
90 public val atStart: Boolean get() = reading?.atStart ?: true
91 public val atEnd: Boolean get() = reading?.atEnd ?: true
93 private fun canQueue(): Boolean = !mounted || (owner?.active ?: false)
94 private fun known(key: ViewKey): Boolean {
95 requireComponent(key != ViewKey.Position, "message positioning requires an explicit key")
96 val source = owner?.input?.props?.source ?: return !mounted
97 return source.count == 0L || virtualIndex(source, source.count, key) != None
98 }
99 internal fun present(mode: MessageScrollMode, padding: Double, ready: Boolean) {
100 val old = presentation.value
101 if (old.mode != mode || old.padding != padding || old.ready != ready)
102 presentation.value = MessagePresentation(mode, padding, ready, old.generation + 1L)
103 }
104 internal fun changed() {
105 val old = presentation.value
106 presentation.value =
107 MessagePresentation(old.mode, old.padding, old.ready, old.generation + 1L)
108 }
109 private fun queue(destination: MessageDestination): Boolean {
110 checkThread()
111 if (!canQueue())
112 return false
113 epoch += 1L
114 userRevision = None
115 val next = MessageRequest(destination)
116 when (destination) {
117 case Reading(_) -> next.source = owner?.input?.props?.source
118 else -> {}
119 }
120 request = Some(next)
121 owner?.cancelPosition()
122 changed()
123 return true
124 }
125 public fun beginTurn(key: ViewKey) {
126 requireComponent(key != ViewKey.Position, "message positioning requires an explicit key")
127 queue(MessageDestination.Turn(key))
128 }
129 public fun scrollToMessage(
130 key: ViewKey,
131 alignment: ScrollAlignment = ScrollAlignment.Nearest
132 ): Boolean {
133 checkThread()
134 return known(key) && queue(MessageDestination.Item(key, alignment))
135 }
136 public fun scrollToStart(): Boolean = queue(MessageDestination.Start)
137 public fun scrollToEnd(): Boolean = queue(MessageDestination.End)
138 public fun restore(anchor: VirtualReadingAnchor): Boolean {
139 checkThread()
140 nonnegative(anchor.offset, "invalid message reading offset")
141 return known(anchor.key) && queue(MessageDestination.Reading(anchor))
142 }
143 public fun hold() {
144 checkThread()
145 val value = presentation.value
146 val moving = value.mode != MessageScrollMode.Holding || hasRequest || scroll.isAnimating
147 epoch += 1L
148 request = None
149 userRevision = None
150 present(MessageScrollMode.Holding, value.padding, true)
151 changed()
152 if (moving)
153 owner?.cancelPosition()
154 }
155 public fun reset(initialPosition: MessageInitialPosition = MessageInitialPosition.End) {
156 checkThread()
157 epoch += 1L
158 request = Some(MessageRequest(MessageDestination.Initial(initialPosition)))
159 userRevision = None
160 selecting = false
161 previousSource = None
162 lastVirtual = None
163 lastScroll = None
164 observed.value = None
165 owner?.cancelPosition()
166 val old = presentation.value
167 presentation.value =
168 MessagePresentation(MessageScrollMode.Holding, 0.0, false, old.generation + 1L)
169 }
170 internal fun bind(value: MessageViewport) {
171 checkThread()
172 when (owner) {
173 case Some(val old) -> requireComponent(
174 old === value || !old.active,
175 "message scroll state already has a host"
176 )
177 case None -> {}
178 }
179 owner = Some(value)
180 mounted = true
181 }
182 internal fun unbind(value: MessageViewport) {
183 when (owner) {
184 case Some(val old) -> if (old === value) {
185 owner = None
186 request = None
187 userRevision = None
188 selecting = false
189 present(
190 MessageScrollMode.Holding,
191 presentation.value.padding,
192 presentation.value.ready
193 )
194 }
195 case None -> {}
196 }
197 }
198 internal fun publish(
199 source: TranscriptSource,
200 virtual: VirtualReadingSnapshot,
201 physical: ScrollSnapshot,
202 density: Double
203 ) {
204 val sameSource =
205 when (previousSource) {
206 case Some(val old) -> old === source
207 case None -> false
208 }
209 val threshold = options.edgeThreshold * density
210 val start = physical.offset.y <= threshold
211 val end = physical.maximum.y - physical.offset.y <= threshold
212 if (
213 sameSource &&
214 lastVirtual ==
215 Some(virtual) &&
216 lastScroll ==
217 Some(physical) &&
218 reading?.atStart ==
219 Some(start) &&
220 reading?.atEnd ==
221 Some(end)
222 )
223 return
224 if (!sameSource)
225 previousSource = Some(source)
226 lastVirtual = Some(virtual)
227 lastScroll = Some(physical)
228 val range = virtual.visible
229 val keys = Array<ViewKey>(range.end - range.start) { at -> source.keyAt(range.start + at) }
230 val anchor =
231 if (range.start < source.count)
232 transcriptAnchor(source, range.start)
233 else
234 None
235 observed.value =
236 Some(
237 MessageReadingSnapshot(
238 keys,
239 virtual.anchor,
240 when (anchor) {
241 case Some(val at) -> Some(source.keyAt(at));
242 case None -> None
243 },
244 start,
245 end
246 )
247 )
248 }