Skip to content

Commit

Permalink
Menu improvements (#3492)
Browse files Browse the repository at this point in the history
* Expose `DefaultApplicationMenu`.
Add `FindByLabel` and `ItemAt` for finding menu items in a menu

* Add `Menu.RemoveMenuItem()`, `MneuItem.GetAccelerator()` and `MenuItem.RemoveAccelerator()`

* Remove `Update`

* Iterate when removing menu items

* Add `GetSubmenu()`
  • Loading branch information
leaanthony authored May 20, 2024
1 parent 939d22d commit e424a85
Show file tree
Hide file tree
Showing 16 changed files with 364 additions and 94 deletions.
2 changes: 1 addition & 1 deletion v3/pkg/application/application_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (m *macosApp) getCurrentWindowID() uint {
func (m *macosApp) setApplicationMenu(menu *Menu) {
if menu == nil {
// Create a default menu for mac
menu = defaultApplicationMenu()
menu = DefaultApplicationMenu()
}
menu.Update()

Expand Down
2 changes: 1 addition & 1 deletion v3/pkg/application/application_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (a *linuxApp) setApplicationMenu(menu *Menu) {
// FIXME: How do we avoid putting a menu?
if menu == nil {
// Create a default menu
menu = defaultApplicationMenu()
menu = DefaultApplicationMenu()
globalApplication.ApplicationMenu = menu
}
}
Expand Down
2 changes: 1 addition & 1 deletion v3/pkg/application/application_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (m *windowsApp) getCurrentWindowID() uint {
func (m *windowsApp) setApplicationMenu(menu *Menu) {
if menu == nil {
// Create a default menu for windows
menu = defaultApplicationMenu()
menu = DefaultApplicationMenu()
}
menu.Update()

Expand Down
68 changes: 60 additions & 8 deletions v3/pkg/application/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ func NewMenu() *Menu {
}

func (m *Menu) Add(label string) *MenuItem {
result := newMenuItem(label)
result := NewMenuItem(label)
m.items = append(m.items, result)
return result
}

func (m *Menu) AddSeparator() {
result := newMenuItemSeparator()
result := NewMenuItemSeparator()
m.items = append(m.items, result)
}

func (m *Menu) AddCheckbox(label string, enabled bool) *MenuItem {
result := newMenuItemCheckbox(label, enabled)
result := NewMenuItemCheckbox(label, enabled)
m.items = append(m.items, result)
return result
}

func (m *Menu) AddRadio(label string, enabled bool) *MenuItem {
result := newMenuItemRadio(label, enabled)
result := NewMenuItemRadio(label, enabled)
m.items = append(m.items, result)
return result
}
Expand All @@ -47,13 +47,13 @@ func (m *Menu) Update() {
}

func (m *Menu) AddSubmenu(s string) *Menu {
result := newSubMenuItem(s)
result := NewSubMenuItem(s)
m.items = append(m.items, result)
return result.submenu
}

func (m *Menu) AddRole(role Role) *Menu {
result := newRole(role)
result := NewRole(role)
if result != nil {
m.items = append(m.items, result)
}
Expand Down Expand Up @@ -95,13 +95,51 @@ func (m *Menu) setContextData(data *ContextMenuData) {
}
}

// FindByLabel recursively searches for a menu item with the given label
// and returns the first match, or nil if not found.
func (m *Menu) FindByLabel(label string) *MenuItem {
for _, item := range m.items {
if item.label == label {
return item
}
if item.submenu != nil {
found := item.submenu.FindByLabel(label)
if found != nil {
return found
}
}
}
return nil
}

func (m *Menu) RemoveMenuItem(target *MenuItem) {
for i, item := range m.items {
if item == target {
// Remove the item from the slice
m.items = append(m.items[:i], m.items[i+1:]...)
break
}
if item.submenu != nil {
item.submenu.RemoveMenuItem(target)
}
}
}

// ItemAt returns the menu item at the given index, or nil if the index is out of bounds.
func (m *Menu) ItemAt(index int) *MenuItem {
if index < 0 || index >= len(m.items) {
return nil
}
return m.items[index]
}

// Clone recursively clones the menu and all its submenus.
func (m *Menu) clone() *Menu {
func (m *Menu) Clone() *Menu {
result := &Menu{
label: m.label,
}
for _, item := range m.items {
result.items = append(result.items, item.clone())
result.items = append(result.items, item.Clone())
}
return result
}
Expand All @@ -113,3 +151,17 @@ func (m *Menu) Append(in *Menu) {
func (a *App) NewMenu() *Menu {
return &Menu{}
}

func NewMenuFromItems(item *MenuItem, items ...*MenuItem) *Menu {
result := &Menu{
items: []*MenuItem{item},
}
result.items = append(result.items, items...)
return result
}

func NewSubmenu(s string, items *Menu) *MenuItem {
result := NewSubMenuItem(s)
result.submenu = items
return result
}
2 changes: 1 addition & 1 deletion v3/pkg/application/menu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (m *macosMenu) processMenu(parent unsafe.Pointer, menu *Menu) {
}
}

func defaultApplicationMenu() *Menu {
func DefaultApplicationMenu() *Menu {
menu := NewMenu()
menu.AddRole(AppMenu)
menu.AddRole(FileMenu)
Expand Down
2 changes: 1 addition & 1 deletion v3/pkg/application/menu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (l *linuxMenu) createMenu(name string, items []*MenuItem) *Menu {
return menu
}

func defaultApplicationMenu() *Menu {
func DefaultApplicationMenu() *Menu {
menu := NewMenu()
menu.AddRole(AppMenu)
menu.AddRole(FileMenu)
Expand Down
140 changes: 140 additions & 0 deletions v3/pkg/application/menu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package application_test

import (
"testing"

"github.com/wailsapp/wails/v3/pkg/application"
)

func TestMenu_FindByLabel(t *testing.T) {
tests := []struct {
name string
menu *application.Menu
label string
shouldError bool
}{
{
name: "Find top-level item",
menu: application.NewMenuFromItems(
application.NewMenuItem("Target"),
),
label: "Target",
shouldError: false,
},
{
name: "Find item in submenu",
menu: application.NewMenuFromItems(
application.NewMenuItem("Item 1"),
application.NewSubmenu("Submenu", application.NewMenuFromItems(
application.NewMenuItem("Subitem 1"),
application.NewMenuItem("Target"),
)),
),
label: "Target",
shouldError: false,
},
{
name: "Not find item",
menu: application.NewMenuFromItems(
application.NewMenuItem("Item 1"),
application.NewSubmenu("Submenu", application.NewMenuFromItems(
application.NewMenuItem("Subitem 1"),
application.NewMenuItem("Target"),
)),
),
label: "Random",
shouldError: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
found := test.menu.FindByLabel(test.label)
if test.shouldError && found != nil {
t.Errorf("Expected error, but found %v", found)
}
if !test.shouldError && found == nil {
t.Errorf("Expected item, but found none")
}
})
}
}

func TestMenu_ItemAt(t *testing.T) {
tests := []struct {
name string
menu *application.Menu
index int
shouldError bool
}{
{
name: "Valid index",
menu: application.NewMenuFromItems(
application.NewMenuItem("Item 1"),
application.NewMenuItem("Item 2"),
application.NewMenuItem("Target"),
),
index: 2,
shouldError: false,
},
{
name: "Index out of bounds (negative)",
menu: application.NewMenuFromItems(
application.NewMenuItem("Item 1"),
application.NewMenuItem("Item 2"),
),
index: -1,
shouldError: true,
},
{
name: "Index out of bounds (too large)",
menu: application.NewMenuFromItems(
application.NewMenuItem("Item 1"),
application.NewMenuItem("Item 2"),
),
index: 2,
shouldError: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
item := test.menu.ItemAt(test.index)
if test.shouldError && item != nil {
t.Errorf("Expected error, but found %v", item)
}
if !test.shouldError && item == nil {
t.Errorf("Expected item, but found none")
}
})
}
}

func TestMenu_RemoveMenuItem(t *testing.T) {
itemToRemove := application.NewMenuItem("Target")
itemToKeep := application.NewMenuItem("Item 1")

tests := []struct {
name string
menu *application.Menu
item *application.MenuItem
shouldFind bool
}{
{
name: "Remove existing item",
menu: application.NewMenuFromItems(itemToKeep, itemToRemove),
item: itemToRemove,
shouldFind: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.menu.RemoveMenuItem(test.item)
found := test.menu.FindByLabel(test.item.Label())
if !test.shouldFind && found != nil {
t.Errorf("Expected item to be removed, but found %v", found)
}
})
}
}
2 changes: 1 addition & 1 deletion v3/pkg/application/menu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (w *windowsMenu) ProcessCommand(cmdMsgID int) {
item.handleClick()
}

func defaultApplicationMenu() *Menu {
func DefaultApplicationMenu() *Menu {
menu := NewMenu()
menu.AddRole(FileMenu)
menu.AddRole(EditMenu)
Expand Down
Loading

0 comments on commit e424a85

Please sign in to comment.