Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More control over menus #4031

Open
wants to merge 13 commits into
base: v3-alpha
Choose a base branch
from
1 change: 1 addition & 0 deletions docs/src/content/docs/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New Menu guide by [@leaanthony](https://github.com/leaanthony)
- Add doc comments for Service API by [@fbbdev](https://github.com/fbbdev) in [#4024](https://github.com/wailsapp/wails/pull/4024)
- Add function `application.NewServiceWithOptions` to initialise services with additional configuration by [@leaanthony](https://github.com/leaanthony) in [#4024](https://github.com/wailsapp/wails/pull/4024)
- Add function `Menu.Clear` and `Menu.Prepend` for more menu control by [@FalcoG](https://github.com/FalcoG) in [#4031](https://github.com/wailsapp/wails/pull/4031)

### Fixed

Expand Down
34 changes: 34 additions & 0 deletions docs/src/content/docs/guides/menus.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ submenu.Add("Open")
submenu.Add("Save")
```

#### Combining menus
A menu can be added into another menu by appending or prepending it.
```go
menu := application.NewMenu()
menu.Add("First Menu")

secondaryMenu := application.NewMenu()
secondaryMenu.Add("Second Menu")

// insert 'secondaryMenu' after 'menu'
menu.Append(secondaryMenu)

// insert 'secondaryMenu' before 'menu'
menu.Prepend(secondaryMenu)

// update the menu
menu.Update()
```

#### Clearing a menu
In some cases it'll be better to construct a whole new menu if you are working with a variable amount of menu items.

This will clear all items on an existing menu and allows you to add items again.

```go
menu := application.NewMenu()
menu.Add("Waiting for update...")

// after certain logic, the menu has to be updated
menu.Clear()
menu.Add("Update complete!")
menu.Update()
```

### Menu Item Properties

Menu items have several properties that can be configured:
Expand Down
11 changes: 11 additions & 0 deletions v3/pkg/application/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ func (m *Menu) Update() {
m.impl.update()
}

// Remove all menu items
func (m *Menu) Clear() {
m.items = nil
}
leaanthony marked this conversation as resolved.
Show resolved Hide resolved

func (m *Menu) AddSubmenu(s string) *Menu {
result := NewSubMenuItem(s)
m.items = append(m.items, result)
Expand Down Expand Up @@ -186,10 +191,16 @@ func (m *Menu) Clone() *Menu {
return result
}

// Insert menu after an existing menu
func (m *Menu) Append(in *Menu) {
m.items = append(m.items, in.items...)
}
leaanthony marked this conversation as resolved.
Show resolved Hide resolved

// Insert menu before an existing menu
func (m *Menu) Prepend(in *Menu) {
m.items = append(in.items, m.items...)
}
leaanthony marked this conversation as resolved.
Show resolved Hide resolved
leaanthony marked this conversation as resolved.
Show resolved Hide resolved

func (a *App) NewMenu() *Menu {
return &Menu{}
}
Expand Down