5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 17:22:01 +08:00

Feature: Add menu helpers

This commit is contained in:
Lea Anthony 2022-04-09 20:36:48 +10:00
parent f08176079c
commit ebf4cf13f2
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
2 changed files with 72 additions and 19 deletions

View File

@ -1,5 +1,7 @@
package menu package menu
import "github.com/wailsapp/wails/v2/pkg/menu/keys"
type Menu struct { type Menu struct {
Items []*MenuItem Items []*MenuItem
} }
@ -20,6 +22,40 @@ func (m *Menu) Merge(menu *Menu) {
} }
} }
// AddText adds a TextMenu item to the menu
func (m *Menu) AddText(label string, accelerator *keys.Accelerator, click Callback) *MenuItem {
item := Text(label, accelerator, click)
m.Append(item)
return item
}
// AddCheckbox adds a CheckboxMenu item to the menu
func (m *Menu) AddCheckbox(label string, checked bool, accelerator *keys.Accelerator, click Callback) *MenuItem {
item := Checkbox(label, checked, accelerator, click)
m.Append(item)
return item
}
// AddRadio adds a radio item to the menu
func (m *Menu) AddRadio(label string, checked bool, accelerator *keys.Accelerator, click Callback) *MenuItem {
item := Radio(label, checked, accelerator, click)
m.Append(item)
return item
}
// AddSeparator adds a separator to the menu
func (m *Menu) AddSeparator() {
item := Separator()
m.Append(item)
}
func (m *Menu) AddSubmenu(label string) *Menu {
submenu := NewMenu()
item := SubMenu(label, submenu)
m.Append(item)
return submenu
}
func (m *Menu) Prepend(item *MenuItem) { func (m *Menu) Prepend(item *MenuItem) {
m.Items = append([]*MenuItem{item}, m.Items...) m.Items = append([]*MenuItem{item}, m.Items...)
} }

View File

@ -5,28 +5,35 @@ sidebar_position: 4
# Menus # Menus
It is possible to add an application menu to Wails projects. This is achieved by defining a [Menu](#menu) struct and It is possible to add an application menu to Wails projects. This is achieved by defining a [Menu](#menu) struct and
setting the [`Menu`](../reference/options.mdx#menu) option, or by calling the runtime method [MenuSetApplicationMenu](../reference/runtime/menu.mdx#menusetapplicationmenu). setting it in the [`Menu`](../reference/options.mdx#menu) application config, or by calling the runtime method
[MenuSetApplicationMenu](../reference/runtime/menu.mdx#menusetapplicationmenu).
An example of how to create a menu:
```go
AppMenu := menu.NewMenu()
FileMenu := AppMenu.AddSubmenu("File")
FileMenu.AddText("&Open", keys.CmdOrCtrl("o"), openFile)
FileMenu.AddSeparator()
FileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) {
runtime.Quit()
})
err := wails.Run(&options.App{
Title: "Menus Demo",
Width: 800,
Height: 600,
Menu: AppMenu,
Bind: []interface{}{
app,
},
)
// ...
````
It is also possible to dynamically update the menu, by updating the menu struct and calling It is also possible to dynamically update the menu, by updating the menu struct and calling
[MenuUpdateApplicationMenu](../reference/runtime/menu.mdx#menuupdateapplicationmenu). [MenuUpdateApplicationMenu](../reference/runtime/menu.mdx#menuupdateapplicationmenu).
Example:
```go
myMenu := menu.NewMenuFromItems(
menu.SubMenu("File", menu.NewMenuFromItems(
menu.Text("&Open", keys.CmdOrCtrl("o"), openFile),
menu.Separator(),
menu.Text("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) {
runtime.Quit()
}),
)),
)
runtime.MenuSetApplicationMenu(myMenu)
```
The example above uses helper methods, however it's possible to build the menu structs manually. The example above uses helper methods, however it's possible to build the menu structs manually.
## Menu ## Menu
@ -211,8 +218,18 @@ func Text(label string, accelerator *keys.Accelerator, click Callback) *MenuItem
func Separator() *MenuItem func Separator() *MenuItem
func Radio(label string, selected bool, accelerator *keys.Accelerator, click Callback) *MenuItem func Radio(label string, selected bool, accelerator *keys.Accelerator, click Callback) *MenuItem
func Checkbox(label string, checked bool, accelerator *keys.Accelerator, click Callback) *MenuItem func Checkbox(label string, checked bool, accelerator *keys.Accelerator, click Callback) *MenuItem
func SubMenu(label string, menu *Menu) *MenuItem func SubMenu(label string, menu *Menu) *Menu
``` ```
You can also create menu items directly on a menu by using the "Add" helpers:
```go title="Package: github.com/wailsapp/wails/v2/pkg/menu"
func (m *Menu) AddText(label string, accelerator *keys.Accelerator, click Callback) *MenuItem
func (m *Menu) AddSeparator() *MenuItem
func (m *Menu) AddRadio(label string, selected bool, accelerator *keys.Accelerator, click Callback) *MenuItem
func (m *Menu) AddCheckbox(label string, checked bool, accelerator *keys.Accelerator, click Callback) *MenuItem
func (m *Menu) AddSubMenu(label string, menu *Menu) *MenuI
```
A note on radio groups: A radio group is defined as a number of radio menu items that are next to each other in the menu. A note on radio groups: A radio group is defined as a number of radio menu items that are next to each other in the menu.
This means that you do not need to group items together as it is automatic. However, that also means you cannot have 2 This means that you do not need to group items together as it is automatic. However, that also means you cannot have 2