Scoop API

scoop:scoop.ui.component:0.1.0

src/builders/menu_content.scoop

Selected production source · src/builders/menu_content.scoop

1package scoop.ui.component
3import scoop.ui.*
4import scoop.ui.style.*
5import scoop.window.ShortcutChord
7public class MenuItemBuilder internal constructor() {
8 public var enabled: Boolean = true
9 public var destructive: Boolean = false
10 public var inset: Boolean = false
11 public var content: View? = None
12 public var icon: View? = None
13 public var shortcut: ShortcutChord? = None
14 public var focus: FocusOptions = FocusOptions()
15 public var style: ButtonStyle = buttonStyle()
16 public var closeOnSelect: Boolean? = None
17 public var semantics: ComponentSemantics = ComponentSemantics()
18 public fun focus(configure: FocusOptionsBuilder.() -> Unit) {
19 focus = focus.copy(configure)
20 }
21 public fun style(configure: ButtonStyleBuilder.() -> Unit) {
22 style = style.copy(configure)
23 }
24 internal fun snapshot(key: ViewKey, label: String): MenuItemSpec =
25 MenuItemSpec(
26 key,
27 label,
28 enabled,
29 destructive,
30 inset,
31 content,
32 icon,
33 shortcut,
34 focus,
35 style,
36 semantics
37 )
40public class MenuBuilder internal constructor() {
41 private val entries = ArrayList<MenuEntry>()
42 public fun add(entry: MenuEntry) {
43 entries.add(entry)
44 }
45 public fun item(
46 key: ViewKey,
47 label: String,
48 onSelect: () -> Unit,
49 configure: MenuItemBuilder.() -> Unit = {}
50 ) {
51 val builder = MenuItemBuilder()
52 builder.configure()
53 add(MenuEntry.Action(builder.snapshot(key, label), onSelect, builder.closeOnSelect ?: true))
54 }
55 public fun checkboxItem(
56 key: ViewKey,
57 label: String,
58 checked: Binding<Boolean>,
59 onCheckedChange: (Boolean) -> Unit = {},
60 configure: MenuItemBuilder.() -> Unit = {}
61 ) {
62 val builder = MenuItemBuilder()
63 builder.configure()
64 add(
65 MenuEntry.Checkbox(
66 builder.snapshot(key, label),
67 checked,
68 builder.closeOnSelect ?: false,
69 onCheckedChange
70 )
71 )
72 }
73 public fun <T: Equals<T>> radioItem(
74 key: ViewKey,
75 label: String,
76 value: T,
77 selected: Binding<T>,
78 onValueChange: (T) -> Unit = {},
79 configure: MenuItemBuilder.() -> Unit = {}
80 ) {
81 val builder = MenuItemBuilder()
82 builder.configure()
83 add(
84 MenuEntry.Radio(
85 builder.snapshot(key, label),
86 { selected.value == value },
87 {
88 val before = selected.value
89 if (before != value) {
90 selected.value = value
91 val accepted = selected.value
92 if (accepted != before)
93 onValueChange(accepted)
94 }
95 },
96 builder.closeOnSelect ?: false
97 )
98 )
99 }
100 public fun submenu(
101 key: ViewKey,
102 label: String,
103 content: MenuContent,
104 configure: MenuItemBuilder.() -> Unit = {}
105 ) {
106 val builder = MenuItemBuilder()
107 builder.configure()
108 add(MenuEntry.Submenu(builder.snapshot(key, label), content))
109 }
110 public fun label(key: ViewKey, text: String, inset: Boolean = false) {
111 add(
112 MenuEntry.Label(key, PanelContent.Label(LabelProps(LabelText.Plain(text, None))), inset)
113 )
114 }
115 public fun label(key: ViewKey, content: View, inset: Boolean = false) {
116 add(MenuEntry.Label(key, PanelContent.Custom(content), inset))
117 }
118 public fun separator(key: ViewKey) {
119 add(MenuEntry.Separator(key))
120 }
121 internal fun snapshot(): MenuContent = MenuContent(entries.toArray())
124public fun menu(configure: MenuBuilder.() -> Unit): MenuContent {
125 val builder = MenuBuilder()
126 builder.configure()
127 return builder.snapshot()