Scoop API

scoop:scoop.ui.component:0.1.0

src/command/builder.scoop

Selected production source · src/command/builder.scoop

1package scoop.ui.component
3import scoop.graphics.text.StyledText
4import scoop.ui.*
5import scoop.ui.style.*
6import scoop.ui.text.TextEditorState
7import scoop.window.ShortcutChord
9public class CommandItemBuilder internal constructor() {
10 public var enabled: Boolean = true
11 public var keywords: Array<String> = []
12 public var content: View? = None
13 public var icon: View? = None
14 public var shortcut: ShortcutChord? = None
15 public var style: StyleOverride = styleOverride()
16 public fun style(configure: StyleOverrideBuilder.() -> Unit) {
17 style = style.copy(configure)
18 }
19 internal fun snapshot(key: ViewKey, label: String, action: () -> Unit): CommandItem =
20 CommandItem(key, label, action, enabled, keywords, content, icon, shortcut, style)
23public class CommandContentBuilder internal constructor() {
24 private val entries = ArrayList<CommandEntry>()
25 public fun add(entry: CommandEntry) {
26 entries.add(entry)
27 }
28 public fun item(
29 key: ViewKey,
30 label: String,
31 onSelect: () -> Unit = {},
32 configure: CommandItemBuilder.() -> Unit = {}
33 ) {
34 val builder = CommandItemBuilder()
35 builder.configure()
36 entries.add(CommandEntry.Item(builder.snapshot(key, label, onSelect)))
37 }
38 public fun group(key: ViewKey, title: String, configure: CommandContentBuilder.() -> Unit) {
39 group(key, PanelContent.Label(LabelProps(LabelText.Plain(title, None))), configure)
40 }
41 public fun group(key: ViewKey, title: StyledText, configure: CommandContentBuilder.() -> Unit) {
42 group(key, PanelContent.Label(LabelProps(LabelText.Styled(title))), configure)
43 }
44 public fun group(key: ViewKey, title: View, configure: CommandContentBuilder.() -> Unit) {
45 group(key, PanelContent.Custom(title), configure)
46 }
47 private fun group(
48 key: ViewKey,
49 title: PanelContent,
50 configure: CommandContentBuilder.() -> Unit
51 ) {
52 entries.add(CommandEntry.Label(key, title))
53 val builder = CommandContentBuilder()
54 builder.configure()
55 for (entry in builder.entries)
56 entries.add(entry)
57 }
58 public fun separator(key: ViewKey) {
59 entries.add(CommandEntry.Separator(key))
60 }
61 internal fun snapshot(): CommandContent = CommandContent(entries.toArray())
64public fun commandItems(configure: CommandContentBuilder.() -> Unit): CommandContent {
65 val builder = CommandContentBuilder()
66 builder.configure()
67 return builder.snapshot()
70public class CommandBuilder internal constructor(): InputBuilder() {
71 public var active: Binding<ViewKey?>? = None
72 public var filter: CommandFilter = CommandFilter.Ranked
73 public var autoHighlight: Boolean = true
74 public var loop: Boolean = true
75 public var width: Double = 320.0
76 public var maxHeight: Double = 288.0
77 public var empty: PanelContent =
78 PanelContent.Label(LabelProps(LabelText.Plain("No results found.", None)))
79 public var showSearchIcon: Boolean = true
80 public var searchIcon: View? = None
81 public var scroll: ScrollState? = None
82 public var listHandle: VirtualListHandle? = None
83 public var panelStyle: StyleOverride = styleOverride()
84 public var onSelect: (ViewKey) -> Unit = { _ -> Unit }
85 init {
86 placeholder = "Type a command or search…"
87 }
88 public fun empty(text: String) {
89 empty = PanelContent.Label(LabelProps(LabelText.Plain(text, None)))
90 }
91 public fun empty(text: StyledText) {
92 empty = PanelContent.Label(LabelProps(LabelText.Styled(text)))
93 }
94 public fun empty(content: View) {
95 empty = PanelContent.Custom(content)
96 }
97 public fun panelStyle(configure: StyleOverrideBuilder.() -> Unit) {
98 panelStyle = panelStyle.copy(configure)
99 }
100 internal fun snapshot(source: CommandSource, query: TextEditorState): CommandProps =
101 CommandProps(
102 source,
103 inputProps(query),
104 active,
105 filter,
106 autoHighlight,
107 loop,
108 width,
109 maxHeight,
110 empty,
111 showSearchIcon,
112 searchIcon,
113 scroll,
114 listHandle,
115 panelStyle,
116 onSelect
117 )
120public fun command(
121 source: CommandSource,
122 query: TextEditorState,
123 configure: CommandBuilder.() -> Unit = {}
124): View {
125 val builder = CommandBuilder()
126 builder.configure()
127 return component(
128 builder.snapshot(source, query),
129 { current, mount -> Command(current, scope = mount) },
130 builder.key
131 )