scoop:scoop.ui.component:0.1.0
src/choice/source.scoop
Selected production source · src/choice/source.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.ui.*5
import scoop.ui.style.*7
public 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()13
)15
public enum ChoiceEntry<T: Equals<T>> {16
Item(ChoiceItem<T>), Label(ViewKey, PanelContent), Separator(ViewKey)17
}19
/** Immutable row metadata. Large sources implement key and domain-value lookup directly. */20
public interface ChoiceSource<T: Equals<T>>: NavigationSource {21
public val choiceCount: Long22
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 ?: false27
public override fun searchTextAt(index: Long): String =28
choiceEntryItem(entryAt(index))?.choice?.label ?: ""29
}31
public 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.size56
public override val choiceCount: Long get() = values.size57
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 None64
}65
}67
public 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
)74
internal fun <T: Equals<T>> choiceEntryKey(entry: ChoiceEntry<T>): ViewKey =75
when (entry) {76
case Item(val item) -> item.choice.key77
case Label(val key, _) -> key78
case Separator(val key) -> key79
}81
internal fun <T: Equals<T>> choiceEntryItem(entry: ChoiceEntry<T>): ChoiceItem<T>? =82
when (entry) {83
case Item(val item) -> Some(item)84
else -> None85
}87
internal 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))?.choice90
case None -> None91
}