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

Menu improvements #3492

Merged
merged 6 commits into from
May 20, 2024
Merged

Conversation

leaanthony
Copy link
Member

@leaanthony leaanthony commented May 18, 2024

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.

Add `FindByLabel` and `ItemAt` for finding menu items in a menu
Copy link

cloudflare-pages bot commented May 18, 2024

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

@Etesam913
Copy link

Etesam913 commented May 19, 2024

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
Copy link

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)
}

@leaanthony leaanthony marked this pull request as ready for review May 19, 2024 01:14
@Etesam913
Copy link

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)

@leaanthony
Copy link
Member Author

leaanthony commented May 19, 2024

Menu.Update() needs calling after any changes.

Try that ☝️

@Etesam913
Copy link

Still does not work

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

@leaanthony
Copy link
Member Author

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

@Etesam913
Copy link

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
Copy link

Etesam913 commented May 19, 2024

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
Copy link

This worked for my usecase!

Thanks

@leaanthony leaanthony requested a review from atterpac May 20, 2024 07:45
@leaanthony leaanthony merged commit e424a85 into v3-alpha May 20, 2024
5 of 6 checks passed
@leaanthony leaanthony deleted the v3-alpha-feature/improve-menu-features branch May 20, 2024 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants