Scoop API

scoop:scoop.ui.component:0.1.0

src/select/values.scoop

Selected production source · src/select/values.scoop

1package scoop.ui.component
3import scoop.graphics.text.FontCollection
4import scoop.surface.AnimationSpec
5import scoop.ui.*
6import scoop.ui.style.*
8public enum SelectSize {
9 Default, Sm
12public enum SelectPosition {
13 ItemAligned, Popup
16public class SelectItem<T: Equals<T>> public constructor(
17 public val choice: Choice<T>,
18 public val content: View? = None,
19 public val icon: View? = None,
20 public val focus: FocusOptions = FocusOptions(),
21 public val style: ButtonStyle = buttonStyle(),
22 public val semantics: ComponentSemantics = ComponentSemantics()
25public enum SelectEntry<T: Equals<T>> {
26 Item(SelectItem<T>), Label(ViewKey, PanelContent), Separator(ViewKey)
29public class SelectContent<T: Equals<T>> public constructor(
30 public val entries: Array<SelectEntry<T>>
31) {
32 init {
33 val choices = ArrayList<Choice<T>>()
34 for (index in 0L ..< entries.size) {
35 val key = selectKey(entries[index])
36 requireComponent(key != ViewKey.Position, "select entry requires an explicit key")
37 for (previous in 0L ..< index)
38 requireComponent(selectKey(entries[previous]) != key, "duplicate select entry key")
39 when (entries[index]) {
40 case Item(val item) -> choices.add(item.choice)
41 else -> {}
42 }
43 }
44 validateChoices(choices.toArray())
45 }
48public class SelectValue<T: Equals<T>> internal constructor(
49 public val value: T?,
50 public val choice: Choice<T>?,
51 public val style: ActionContentStyle
54public class SelectProps<T: Equals<T>> public constructor(
55 public val value: Binding<T?>,
56 public val content: SelectContent<T>,
57 public val open: Binding<Boolean>? = None,
58 public val placeholder: String = "Select an option",
59 public val unavailableText: String = "Unavailable selection",
60 public val enabled: Boolean = true,
61 public val invalid: Boolean = false,
62 public val size: SelectSize = SelectSize.Default,
63 public val position: SelectPosition = SelectPosition.ItemAligned,
64 public val width: Double? = None,
65 public val maxHeight: Double = 320.0,
66 public val scrollButtons: Boolean = true,
67 public val scroll: ScrollState? = None,
68 public val style: ButtonStyle = buttonStyle(),
69 public val popupStyle: StyleOverride = styleOverride(),
70 public val theme: ThemeSource? = None,
71 public val fonts: FontCollection? = None,
72 public val focus: FocusOptions = FocusOptions(),
73 public val labelTarget: FieldTarget? = None,
74 public val popupFocus: OverlayFocusOptions = OverlayFocusOptions(),
75 public val mode: OverlayMode = OverlayMode.NonModal,
76 public val loop: Boolean = true,
77 typeahead: TypeaheadOptions? = None,
78 public val transition: StyleValue<AnimationSpec?> = StyleValue.Inherit,
79 public val handle: OverlayHandle? = None,
80 public val group: GroupPosition? = None,
81 public val valueContent: ((SelectValue<T>) -> View)? = None,
82 public val icon: View? = None,
83 public val onValueChange: (T?) -> Unit = {},
84 public val onOpenChange: (Boolean) -> Unit = {},
85 public val onCloseRequest: (OverlayCloseReason) -> Boolean = { _ -> true },
86 public val onClosed: (OverlayCloseReason) -> Unit = {},
87 public val semantics: ComponentSemantics = ComponentSemantics(),
88 public val popupSemantics: ComponentSemantics = ComponentSemantics()
89) {
90 public val typeahead: TypeaheadOptions = typeahead ?: menuTypeahead()
91 init {
92 when (width) {
93 case Some(val value) -> nonnegative(value, "invalid select width");
94 case None -> {}
95 }
96 nonnegative(maxHeight, "invalid select max height")
97 }
100internal fun <T: Equals<T>> selectKey(entry: SelectEntry<T>): ViewKey =
101 when (entry) {
102 case Item(val item) -> item.choice.key
103 case Label(val key, _) -> key
104 case Separator(val key) -> key
105 }
107internal fun <T: Equals<T>> selectedChoice(content: SelectContent<T>, value: T?): Choice<T>? {
108 for (entry in content.entries)
109 when (entry) {
110 case Item(val item) -> {
111 if (Some(item.choice.value) == value)
112 return Some(item.choice)
113 }
114 else -> {}
115 }
116 return None
119internal fun <T: Equals<T>> selectChoice(input: SelectProps<T>, key: ViewKey) {
120 if (!input.enabled)
121 return
122 for (entry in input.content.entries)
123 when (entry) {
124 case Item(val item) -> {
125 if (item.choice.key == key && item.choice.enabled) {
126 val previous = input.value.value
127 if (previous != Some(item.choice.value))
128 input.value.value = Some(item.choice.value)
129 val accepted = input.value.value
130 if (accepted != previous)
131 input.onValueChange(accepted)
132 return
133 }
134 }
135 else -> {}
136 }