scoop:scoop.ui.component:0.1.0
src/command/builder.scoop
Selected production source · src/command/builder.scoop
1
package scoop.ui.component3
import scoop.graphics.text.StyledText4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.ui.text.TextEditorState7
import scoop.window.ShortcutChord9
public class CommandItemBuilder internal constructor() {10
public var enabled: Boolean = true11
public var keywords: Array<String> = []12
public var content: View? = None13
public var icon: View? = None14
public var shortcut: ShortcutChord? = None15
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)21
}23
public 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.() -> Unit51
) {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())62
}64
public fun commandItems(configure: CommandContentBuilder.() -> Unit): CommandContent {65
val builder = CommandContentBuilder()66
builder.configure()67
return builder.snapshot()68
}70
public class CommandBuilder internal constructor(): InputBuilder() {71
public var active: Binding<ViewKey?>? = None72
public var filter: CommandFilter = CommandFilter.Ranked73
public var autoHighlight: Boolean = true74
public var loop: Boolean = true75
public var width: Double = 320.076
public var maxHeight: Double = 288.077
public var empty: PanelContent =78
PanelContent.Label(LabelProps(LabelText.Plain("No results found.", None)))79
public var showSearchIcon: Boolean = true80
public var searchIcon: View? = None81
public var scroll: ScrollState? = None82
public var listHandle: VirtualListHandle? = None83
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
onSelect117
)118
}120
public 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.key131
)132
}