Scoop API

scoop:scoop.ui.component:0.1.0

src/select/builder.scoop

Selected production source · src/select/builder.scoop

1package scoop.ui.component
3import scoop.graphics.text.FontCollection
4import scoop.surface.AnimationSpec
5import scoop.ui.*
6import scoop.ui.style.*
8public class SelectItemBuilder internal constructor() {
9 public var content: View? = None
10 public var icon: View? = None
11 public var focus: FocusOptions = FocusOptions()
12 public var style: ButtonStyle = buttonStyle()
13 public var semantics: ComponentSemantics = ComponentSemantics()
14 public fun focus(configure: FocusOptionsBuilder.() -> Unit) {
15 focus = focus.copy(configure)
16 }
17 public fun style(configure: ButtonStyleBuilder.() -> Unit) {
18 style = style.copy(configure)
19 }
22public open class SelectContentBuilder<T: Equals<T>> internal constructor() {
23 private val entries = ArrayList<SelectEntry<T>>()
24 public fun item(choice: Choice<T>, configure: SelectItemBuilder.() -> Unit = {}) {
25 val b = SelectItemBuilder();
26 b.configure()
27 entries.add(
28 SelectEntry.Item(SelectItem(choice, b.content, b.icon, b.focus, b.style, b.semantics))
29 )
30 }
31 public fun label(key: ViewKey, text: String) {
32 label(key, PanelContent.Label(LabelProps(LabelText.Plain(text, None))))
33 }
34 public fun label(key: ViewKey, content: PanelContent) {
35 entries.add(SelectEntry.Label(key, content))
36 }
37 public fun separator(key: ViewKey) {
38 entries.add(SelectEntry.Separator(key))
39 }
40 public fun group(key: ViewKey, text: String, configure: SelectContentBuilder<T>.() -> Unit) {
41 label(key, text);
42 this.configure()
43 }
44 internal fun content(): SelectContent<T> = SelectContent(entries.toArray())
47public class SelectBuilder<T: Equals<T>> internal constructor(): SelectContentBuilder<T>() {
48 public var key: ViewKey = ViewKey.Position
49 public var open: Binding<Boolean>? = None
50 public var placeholder: String = "Select an option"
51 public var unavailableText: String = "Unavailable selection"
52 public var enabled: Boolean = true
53 public var invalid: Boolean = false
54 public var size: SelectSize = SelectSize.Default
55 public var position: SelectPosition = SelectPosition.ItemAligned
56 public var width: Double? = None
57 public var maxHeight: Double = 320.0
58 public var scrollButtons: Boolean = true
59 public var scroll: ScrollState? = None
60 public var style: ButtonStyle = buttonStyle()
61 public var semantics: ComponentSemantics = ComponentSemantics()
62 public var popupStyle: StyleOverride = styleOverride()
63 public var popupSemantics: ComponentSemantics = ComponentSemantics()
64 public var theme: ThemeSource? = None
65 public var fonts: FontCollection? = None
66 public var focus: FocusOptions = FocusOptions()
67 public var labelTarget: FieldTarget? = None
68 public var popupFocus: OverlayFocusOptions = OverlayFocusOptions()
69 public var mode: OverlayMode = OverlayMode.NonModal
70 public var loop: Boolean = true
71 public var typeahead: TypeaheadOptions? = None
72 public var transition: StyleValue<AnimationSpec?> = StyleValue.Inherit
73 public var handle: OverlayHandle? = None
74 public var group: GroupPosition? = None
75 public var valueContent: ((SelectValue<T>) -> View)? = None
76 public var icon: View? = None
77 public var onValueChange: (T?) -> Unit = {}
78 public var onOpenChange: (Boolean) -> Unit = {}
79 public var onCloseRequest: (OverlayCloseReason) -> Boolean = { _ -> true }
80 public var onClosed: (OverlayCloseReason) -> Unit = {}
81 public fun style(configure: ButtonStyleBuilder.() -> Unit) {
82 style = style.copy(configure)
83 }
84 public fun popupStyle(configure: StyleOverrideBuilder.() -> Unit) {
85 popupStyle = popupStyle.then(styleOverride(configure))
86 }
87 public fun focus(configure: FocusOptionsBuilder.() -> Unit) {
88 focus = focus.copy(configure)
89 }
90 public fun popupFocus(configure: OverlayFocusOptionsBuilder.() -> Unit) {
91 popupFocus = popupFocus.copy(configure)
92 }
93 internal fun snapshot(value: Binding<T?>): SelectProps<T> =
94 SelectProps(
95 value,
96 content(),
97 open,
98 placeholder,
99 unavailableText,
100 enabled,
101 invalid,
102 size,
103 position,
104 width,
105 maxHeight,
106 scrollButtons,
107 scroll,
108 style,
109 popupStyle,
110 theme,
111 fonts,
112 focus,
113 labelTarget,
114 popupFocus,
115 mode,
116 loop,
117 typeahead,
118 transition,
119 handle,
120 group,
121 valueContent,
122 icon,
123 onValueChange,
124 onOpenChange,
125 onCloseRequest,
126 onClosed,
127 semantics,
128 popupSemantics
129 )
132public fun <T: Equals<T>> select(
133 value: Binding<T?>,
134 configure: SelectBuilder<T>.() -> Unit = {}
135): View = makeSelect(value, [], configure)
137public fun <T: Equals<T>> select(
138 value: Binding<T?>,
139 choices: Array<Choice<T>>,
140 configure: SelectBuilder<T>.() -> Unit = {}
141): View = makeSelect(value, choices, configure)
143internal fun <T: Equals<T>> makeSelect(
144 value: Binding<T?>,
145 choices: Array<Choice<T>>,
146 configure: SelectBuilder<T>.() -> Unit
147): View {
148 val b = SelectBuilder<T>()
149 for (choice in choices)
150 b.item(choice)
151 b.configure()
152 return component(b.snapshot(value), { current, mount -> Select(current, scope = mount) }, b.key)