Scoop API

scoop:scoop.ui.component:0.1.0

src/choice/source.scoop

Selected production source · src/choice/source.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.ui.*
5import scoop.ui.style.*
7public class ChoiceItem<T: Equals<T>> public constructor(
8 public val choice: Choice<T>,
9 public val keywords: Array<String> = [],
10 public val content: View? = None,
11 public val icon: View? = None,
12 public val style: StyleOverride = styleOverride()
15public enum ChoiceEntry<T: Equals<T>> {
16 Item(ChoiceItem<T>), Label(ViewKey, PanelContent), Separator(ViewKey)
19/** Immutable row metadata. Large sources implement key and domain-value lookup directly. */
20public interface ChoiceSource<T: Equals<T>>: NavigationSource {
21 public val choiceCount: Long
22 public fun entryAt(index: Long): ChoiceEntry<T>
23 public fun indexOfValue(value: T): Long?
24 public override fun keyAt(index: Long): ViewKey = choiceEntryKey(entryAt(index))
25 public override fun enabledAt(index: Long): Boolean =
26 choiceEntryItem(entryAt(index))?.choice?.enabled ?: false
27 public override fun searchTextAt(index: Long): String =
28 choiceEntryItem(entryAt(index))?.choice?.label ?: ""
31public class ChoiceContent<T: Equals<T>> public constructor(
32 public val entries: Array<ChoiceEntry<T>>
33): ChoiceSource<T> {
34 private val indices = HashMap<ViewKey, Long>()
35 private val values = ArrayList<Choice<T>>()
36 init {
37 for (index in 0L ..< entries.size) {
38 val key = choiceEntryKey(entries[index])
39 requireComponent(key != ViewKey.Position, "choice entry requires an explicit key")
40 requireComponent(indices[key] == None, "duplicate choice entry key")
41 indices.put(key, index)
42 when (choiceEntryItem(entries[index])) {
43 case Some(val item) -> {
44 for (previous in values)
45 requireComponent(
46 previous.value != item.choice.value,
47 "duplicate choice value"
48 )
49 values.add(item.choice)
50 }
51 case None -> {}
52 }
53 }
54 }
55 public override val count: Long get() = entries.size
56 public override val choiceCount: Long get() = values.size
57 public override fun entryAt(index: Long): ChoiceEntry<T> = entries[index]
58 public override fun indexOfKey(key: ViewKey): Long? = indices[key]
59 public override fun indexOfValue(value: T): Long? {
60 for (choice in values)
61 if (choice.value == value)
62 return indices[choice.key]
63 return None
64 }
67public fun <T: Equals<T>> choiceSource(choices: Array<Choice<T>>): ChoiceContent<T> =
68 ChoiceContent(
69 Array<ChoiceEntry<T>>(choices.size) {
70 index -> ChoiceEntry.Item(ChoiceItem(choices[index]))
71 }
72 )
74internal fun <T: Equals<T>> choiceEntryKey(entry: ChoiceEntry<T>): ViewKey =
75 when (entry) {
76 case Item(val item) -> item.choice.key
77 case Label(val key, _) -> key
78 case Separator(val key) -> key
79 }
81internal fun <T: Equals<T>> choiceEntryItem(entry: ChoiceEntry<T>): ChoiceItem<T>? =
82 when (entry) {
83 case Item(val item) -> Some(item)
84 else -> None
85 }
87internal fun <T: Equals<T>> sourceChoice(source: ChoiceSource<T>, value: T): Choice<T>? =
88 when (source.indexOfValue(value)) {
89 case Some(val index) -> choiceEntryItem(source.entryAt(index))?.choice
90 case None -> None
91 }