wails icon indicating copy to clipboard operation
wails copied to clipboard

Menu improvements

Open leaanthony opened this issue 1 year ago • 9 comments

This PR cointains:

  • application.DefaultApplicationMenu() *Menu which will return the default application menu for the current platform
  • Menu.FindByLabel(label string) to find a menu item by its label. Returns nil if not found.
  • Menu.ItemAt(index int) for retrieving menu items by index. Returns nil if out of bounds.
  • Menu.RemoveMenuItem(*MenuItem) for removing a menu item.
  • MenuItem.RemoveAccelerator() for removing the accelerator.
  • MenuItem.GetAccelerator() string for getting a menu item's accelerator.
  • MenuItem.GetSubmenu() *Menu for getting a submenu (nil if not a submenu item).

Menu.Update() needs calling after any changes.

leaanthony avatar May 18 '24 23:05 leaanthony

Deploying wails with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3c6dd7e
Status: ✅  Deploy successful!
Preview URL: https://8480eef8.wails.pages.dev
Branch Preview URL: https://v3-alpha-feature-improve-men.wails.pages.dev

View logs

A small change that I had to make to fix an error when running wails3 dev

diff --git a/v3/pkg/application/menuitem_darwin.go b/v3/pkg/application/menuitem_darwin.go
index 74ee9c4b..8b1217e1 100644
--- a/v3/pkg/application/menuitem_darwin.go
+++ b/v3/pkg/application/menuitem_darwin.go
@@ -419,7 +419,7 @@ func newSpeechMenu() *MenuItem {
                OnClick(func(ctx *Context) {
                        C.stopSpeaking()
                })
-       subMenu := newSubMenuItem("Speech")
+       subMenu := NewSubMenuItem("Speech")
        subMenu.submenu = speechMenu
        return subMenu
 }

Etesam913 avatar May 19 '24 00:05 Etesam913

This pr does give you the ability to get the default menu, but I am not sure how you would update it besides adding.

Screenshot 2024-05-18 at 8 06 45 PM

If I wanted to remove Toggle Fullscreen, how would I go about that? I need to remove the item, but it seems like there's no direct access to the items array.

I tried setting it hidden, but that didn't work

menu := application.DefaultApplicationMenu()
viewMenu := menu.FindByLabel("Toggle Full Screen")
if viewMenu != nil {
    viewMenu.SetHidden(true)
}

Etesam913 avatar May 19 '24 00:05 Etesam913

This doesn't seem to work on the defaultMenu. 👇 is not working for me

	menu := application.DefaultApplicationMenu()
	menuItem := menu.FindByLabel("Toggle Fullscreen")
	if menuItem != nil {
		menu.RemoveMenuItem(menuItem)
	}

	app.SetMenu(menu)

Etesam913 avatar May 19 '24 01:05 Etesam913

Menu.Update() needs calling after any changes.

Try that ☝️

leaanthony avatar May 19 '24 01:05 leaanthony

Still does not work

        menu := application.DefaultApplicationMenu()
	menuItem := menu.FindByLabel("Toggle Fullscreen")
	if menuItem != nil {
		menu.RemoveMenuItem(menuItem)
	}
	menu.Update()
	app.SetMenu(menu)

Etesam913 avatar May 19 '24 01:05 Etesam913

Just pushed a fix. However, you'll need to search for the label "Toggle Full Screen" to find the right one.

leaanthony avatar May 19 '24 02:05 leaanthony

This works for me now, thanks

I had to do this:

menu := application.DefaultApplicationMenu()
menuItem := menu.FindByLabel("Toggle Full Screen")
if menuItem != nil {
    menu.RemoveMenuItem(menuItem)
}
app.SetMenu(menu)
menu.Update()

Etesam913 avatar May 19 '24 17:05 Etesam913

Is there a way to give access to the submenu of a menu item if it has one.

For example, if I want to add a menu item to the file menu, it doesn't seem possible.

Screenshot 2024-05-19 at 2 16 15 PM

Doing the below does not give me access to the Add function

Screenshot 2024-05-19 at 2 17 11 PM

Ideally, a GetSubmenu function can be exposed to the MenuItem that can either return the Menu or nil

There is an isSubmenu method, but I am not too sure if that can be used

Etesam913 avatar May 19 '24 18:05 Etesam913

This worked for my usecase!

Thanks

Etesam913 avatar May 20 '24 00:05 Etesam913