-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu_test.go
83 lines (70 loc) · 2.34 KB
/
menu_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ui_test
import (
"git.kirsle.net/go/ui"
)
// Example of using the menu widgets.
func ExampleMenu() {
mw, err := ui.NewMainWindow("Menu Bar Example", 800, 600)
if err != nil {
panic(err)
}
// Create a main menu for your window.
menu := ui.NewMenuBar("Main Menu")
// File menu. Some items with accelerators, some without.
// NOTE: key bindings are up to you, the accelerators are
// purely decorative.
file := menu.AddMenu("File")
file.AddItemAccel("New", "Ctrl-N", func() {})
file.AddItemAccel("Open", "Ctrl-O", func() {})
file.AddItemAccel("Save", "Ctrl-S", func() {})
file.AddItem("Save as...", func() {})
file.AddSeparator()
file.AddItem("Close window", func() {})
file.AddItemAccel("Exit", "Alt-F4", func() {})
// Help menu.
help := menu.AddMenu("Help")
help.AddItemAccel("Contents", "F1", func() {})
help.AddItem("About", func() {})
// Give the menu bar your Supervisor so it can wire all
// events up and make the menus work.
menu.Supervise(mw.Supervisor())
// Compute and pack the menu bar against the top of
// the main window (or other parent container)
menu.Compute(mw.Engine)
mw.Pack(menu, menu.PackTop()) // Side: N, FillX: true
// Each loop you must then:
// - Call Supervisor.Loop() as normal to handle events.
// - Call Supervisor.Present() to draw the modal popup menus.
// MainLoop() of the MainWindow does this for you.
mw.MainLoop()
}
// Example of using the MenuButton.
func ExampleMenuButton() {
mw, err := ui.NewMainWindow("Menu Button", 800, 600)
if err != nil {
panic(err)
}
// Create a MenuButton much as you would a normal Button.
btn := ui.NewMenuButton("Button1", ui.NewLabel(ui.Label{
Text: "File",
}))
mw.Place(btn, ui.Place{ // place it in the center
Center: true,
Middle: true,
})
// Add menu items to it.
btn.AddItemAccel("New", "Ctrl-N", func() {})
btn.AddItemAccel("Open", "Ctrl-O", func() {})
btn.AddItemAccel("Save", "Ctrl-S", func() {})
btn.AddItem("Save as...", func() {})
btn.AddSeparator()
btn.AddItem("Close window", func() {})
btn.AddItemAccel("Exit", "Alt-F4", func() {})
// Add the button to Supervisor for events to work.
btn.Supervise(mw.Supervisor())
// Each loop you must then:
// - Call Supervisor.Loop() as normal to handle events.
// - Call Supervisor.Present() to draw the modal popup menus.
// MainLoop() of the MainWindow does this for you.
mw.MainLoop()
}