Scoop API

scoop:scoop.ui.component:0.1.0

platform/desktop/native/menu/builder.scoop

Selected production source · platform/desktop/native/menu/builder.scoop

1package scoop.ui.component
3import scoop.ui.*
4import scoop.window.*
6public class NativeMenuItemBuilder internal constructor() {
7 public var enabled: Boolean = true
8 public var image: DesktopImage? = None
9 public var shortcut: ShortcutChord? = None
10 internal fun snapshot(key: ViewKey, label: String): NativeMenuItemSpec =
11 NativeMenuItemSpec(key, label, enabled, image, shortcut)
14public class NativeMenuBuilder internal constructor() {
15 private val entries = ArrayList<NativeMenuEntry>()
16 public fun add(entry: NativeMenuEntry) {
17 entries.add(entry)
18 }
19 public fun item(
20 key: ViewKey,
21 label: String,
22 onSelect: (MenuActivation) -> Unit,
23 configure: NativeMenuItemBuilder.() -> Unit = {}
24 ) {
25 val builder = NativeMenuItemBuilder()
26 builder.configure()
27 add(NativeMenuEntry.Action(builder.snapshot(key, label), onSelect))
28 }
29 public fun role(
30 key: ViewKey,
31 label: String,
32 role: MenuRole,
33 configure: NativeMenuItemBuilder.() -> Unit = {}
34 ) {
35 val builder = NativeMenuItemBuilder()
36 builder.configure()
37 add(NativeMenuEntry.Role(builder.snapshot(key, label), role))
38 }
39 public fun checkboxItem(
40 key: ViewKey,
41 label: String,
42 checked: Binding<Boolean>,
43 onCheckedChange: (Boolean) -> Unit = {},
44 configure: NativeMenuItemBuilder.() -> Unit = {}
45 ) {
46 val builder = NativeMenuItemBuilder()
47 builder.configure()
48 add(NativeMenuEntry.Checkbox(builder.snapshot(key, label), checked, onCheckedChange))
49 }
50 public fun <T: Equals<T>> radioItem(
51 key: ViewKey,
52 label: String,
53 value: T,
54 selected: Binding<T>,
55 onValueChange: (T) -> Unit = {},
56 configure: NativeMenuItemBuilder.() -> Unit = {}
57 ) {
58 val builder = NativeMenuItemBuilder()
59 builder.configure()
60 add(
61 NativeMenuEntry.Radio(
62 builder.snapshot(key, label),
63 { selected.value == value },
64 {
65 val before = selected.value
66 if (before != value) {
67 selected.value = value
68 val accepted = selected.value
69 if (accepted != before)
70 onValueChange(accepted)
71 }
72 }
73 )
74 )
75 }
76 public fun submenu(
77 key: ViewKey,
78 label: String,
79 content: NativeMenuContent,
80 configure: NativeMenuItemBuilder.() -> Unit = {}
81 ) {
82 val builder = NativeMenuItemBuilder()
83 builder.configure()
84 add(NativeMenuEntry.Submenu(builder.snapshot(key, label), content))
85 }
86 public fun separator(key: ViewKey) {
87 add(NativeMenuEntry.Separator(key))
88 }
89 internal fun snapshot(): NativeMenuContent = NativeMenuContent(entries.toArray())
92public fun nativeMenu(configure: NativeMenuBuilder.() -> Unit): NativeMenuContent {
93 val builder = NativeMenuBuilder()
94 builder.configure()
95 return builder.snapshot()