scoop:scoop.ui.component:0.1.0
src/conversation/transcript.scoop
Selected production source · src/conversation/transcript.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.ui.*5
import scoop.ui.style.LogicalInsets6
import scoop.ui.component.frame as transcriptFrame8
/** Immutable item metadata; constructing the source does not construct its item views. */9
public interface TranscriptSource: ItemSource {10
public fun anchorAtOrBefore(index: Long): Long?11
}13
public class TranscriptItem public constructor(14
public val key: ViewKey,15
public val content: () -> View,16
public val scrollAnchor: Boolean = false17
)19
public 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.size37
public override fun keyAt(index: Long): ViewKey = items[index].key38
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 = 0L42
var high = anchors.size43
while (low < high) {44
val middle = low + (high - low) / 2L45
if (anchors[middle] <= index)46
low = middle + 1L47
else48
high = middle49
}50
return if (low == 0L)51
None52
else53
Some(anchors[low - 1L])54
}55
}57
public 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())66
}68
public fun transcript(configure: TranscriptBuilder.() -> Unit): TranscriptList {69
val builder = TranscriptBuilder()70
builder.configure()71
return builder.snapshot()72
}74
internal class SpacedTranscript(val source: TranscriptSource, val spacing: Double): ItemSource {75
public override val count: Long get() = source.count76
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
spacing82
else83
0.084
return transcriptFrame(source.build(index)) {85
this.logicalPadding = Some(LogicalInsets(bottom = bottom))86
}87
}88
}90
internal fun transcriptAnchor(source: TranscriptSource, index: Long): Long? {91
val at = source.anchorAtOrBefore(index) ?: return None92
requireComponent(93
at >= 0L && at <= index && at < source.count,94
"invalid transcript anchor index"95
)96
return Some(at)97
}