Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/transcript.scoop

Selected production source · src/conversation/transcript.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.ui.*
5import scoop.ui.style.LogicalInsets
6import scoop.ui.component.frame as transcriptFrame
8/** Immutable item metadata; constructing the source does not construct its item views. */
9public interface TranscriptSource: ItemSource {
10 public fun anchorAtOrBefore(index: Long): Long?
13public class TranscriptItem public constructor(
14 public val key: ViewKey,
15 public val content: () -> View,
16 public val scrollAnchor: Boolean = false
19public class TranscriptList public constructor(
20 private val items: Array<TranscriptItem>
21): TranscriptSource {
22 private val indexes = HashMap<ViewKey, Long>()
23 private val anchors = buildIndex()
24 private fun buildIndex(): Array<Long> {
25 val found = ArrayList<Long>()
26 for (at in 0L ..< items.size) {
27 val item = items[at]
28 requireComponent(item.key != ViewKey.Position, "transcript requires explicit keys")
29 requireComponent(indexes.get(item.key) == None, "duplicate transcript key")
30 indexes.put(item.key, at)
31 if (item.scrollAnchor)
32 found.add(at)
33 }
34 return found.toArray()
35 }
36 public override val count: Long get() = items.size
37 public override fun keyAt(index: Long): ViewKey = items[index].key
38 public override fun indexOfKey(key: ViewKey): Long? = indexes.get(key)
39 public override fun build(index: Long): View = items[index].content()
40 public override fun anchorAtOrBefore(index: Long): Long? {
41 var low = 0L
42 var high = anchors.size
43 while (low < high) {
44 val middle = low + (high - low) / 2L
45 if (anchors[middle] <= index)
46 low = middle + 1L
47 else
48 high = middle
49 }
50 return if (low == 0L)
51 None
52 else
53 Some(anchors[low - 1L])
54 }
57public class TranscriptBuilder internal constructor() {
58 private val values = ArrayList<TranscriptItem>()
59 public fun item(key: ViewKey, scrollAnchor: Boolean = false, content: () -> View) {
60 values.add(TranscriptItem(key, content, scrollAnchor))
61 }
62 public fun item(key: ViewKey, content: View, scrollAnchor: Boolean = false) {
63 values.add(TranscriptItem(key, { content }, scrollAnchor))
64 }
65 internal fun snapshot(): TranscriptList = TranscriptList(values.toArray())
68public fun transcript(configure: TranscriptBuilder.() -> Unit): TranscriptList {
69 val builder = TranscriptBuilder()
70 builder.configure()
71 return builder.snapshot()
74internal class SpacedTranscript(val source: TranscriptSource, val spacing: Double): ItemSource {
75 public override val count: Long get() = source.count
76 public override fun keyAt(index: Long): ViewKey = source.keyAt(index)
77 public override fun indexOfKey(key: ViewKey): Long? = source.indexOfKey(key)
78 public override fun build(index: Long): View {
79 val bottom =
80 if (index + 1L < source.count)
81 spacing
82 else
83 0.0
84 return transcriptFrame(source.build(index)) {
85 this.logicalPadding = Some(LogicalInsets(bottom = bottom))
86 }
87 }
90internal fun transcriptAnchor(source: TranscriptSource, index: Long): Long? {
91 val at = source.anchorAtOrBefore(index) ?: return None
92 requireComponent(
93 at >= 0L && at <= index && at < source.count,
94 "invalid transcript anchor index"
95 )
96 return Some(at)