diff --git a/docs/src/content/docs/changelog.mdx b/docs/src/content/docs/changelog.mdx index a64d2e71845..75dbf824bbc 100644 --- a/docs/src/content/docs/changelog.mdx +++ b/docs/src/content/docs/changelog.mdx @@ -75,6 +75,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add method `Close` on `sqlite` service to close the DB manually by [@fbbdev](https://github.com/fbbdev) in [#4067](https://github.com/wailsapp/wails/pull/4067) - Add cancellation support for query methods on `sqlite` service by [@fbbdev](https://github.com/fbbdev) in [#4067](https://github.com/wailsapp/wails/pull/4067) - Add prepared statement support to `sqlite` service with JS bindings by [@fbbdev](https://github.com/fbbdev) in [#4067](https://github.com/wailsapp/wails/pull/4067) +- Add `SetMenu()` on window to allow for setting a menu on a window by [@leaanthony](https://github.com/leaanthony) ### Fixed @@ -107,6 +108,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 -  Ensure menu updates occur on the main thread by [@leaanthony](https://github.com/leaanthony) - The dragging and resizing mechanism is now more robust and matches expected platform behaviour more closely by [@fbbdev](https://github.com/fbbdev) in [#4100](https://github.com/wailsapp/wails/pull/4100) - Fixed [#4097](https://github.com/wailsapp/wails/issues/4097) Webpack/angular discards runtime init code by [@fbbdev](https://github.com/fbbdev) in [#4100](https://github.com/wailsapp/wails/pull/4100) +- Fixed initially-hidden menu items by [@IanVS](https://github.com/IanVS) in [#4116](https://github.com/wailsapp/wails/pull/4116) - Fixed assetFileServer not serving `.html` files when non-extension request when `[request]` doesn't exist but `[request].html` does - Fixed icon generation paths by [@robin-samuel](https://github.com/robin-samuel) in [#4125](https://github.com/wailsapp/wails/pull/4125) diff --git a/docs/src/content/docs/learn/application-menu.mdx b/docs/src/content/docs/learn/application-menu.mdx index df344a02c07..01f2d271df8 100644 --- a/docs/src/content/docs/learn/application-menu.mdx +++ b/docs/src/content/docs/learn/application-menu.mdx @@ -16,6 +16,43 @@ Create a new application menu using the `NewMenu` method: menu := app.NewMenu() ``` +## Setting the Menu + +The way to set the menu varies on the platform: + + + + + On macOS, there is only one menu bar per application. Set the menu using the `SetMenu` method of the application: + + ```go + app.SetMenu(menu) + ``` + + + + + + On Windows, there is a menu bar per window. Set the menu using the `SetMenu` method of the window: + + ```go + window.SetMenu(menu) + ``` + + + + + + On Linux, the menu bar is typically per window. Set the menu using the `SetMenu` method of the window: + + ```go + window.SetMenu(menu) + ``` + + + + + ## Menu Roles Wails provides predefined menu roles that automatically create platform-appropriate menu structures: diff --git a/v3/examples/menu/main.go b/v3/examples/menu/main.go index 62bcb454b62..218f93a2d2f 100644 --- a/v3/examples/menu/main.go +++ b/v3/examples/menu/main.go @@ -27,16 +27,7 @@ func main() { if runtime.GOOS == "darwin" { menu.AddRole(application.AppMenu) } - fileMenu := menu.AddRole(application.FileMenu) - _ = fileMenu - //fileMenu.FindByRole(application.Open).OnClick(func(context *application.Context) { - // selection, err := application.OpenFileDialog().PromptForSingleSelection() - // if err != nil { - // println("Error: " + err.Error()) - // return - // } - // println("You selected: " + selection) - //}) + menu.AddRole(application.FileMenu) menu.AddRole(application.EditMenu) menu.AddRole(application.WindowMenu) menu.AddRole(application.HelpMenu) @@ -44,6 +35,12 @@ func main() { // Let's make a "Demo" menu myMenu := menu.AddSubmenu("Demo") + // Hidden menu item that can be unhidden + hidden := myMenu.Add("I was hidden").SetHidden(true) + myMenu.Add("Toggle the hidden menu").OnClick(func(ctx *application.Context) { + hidden.SetHidden(!hidden.Hidden()) + }) + // Disabled menu item myMenu.Add("Not Enabled").SetEnabled(false) @@ -118,7 +115,8 @@ func main() { }) app.SetMenu(menu) - app.NewWebviewWindow().SetBackgroundColour(application.NewRGB(33, 37, 41)) + window := app.NewWebviewWindow().SetBackgroundColour(application.NewRGB(33, 37, 41)) + window.SetMenu(menu) err := app.Run() diff --git a/v3/pkg/application/menu_darwin.go b/v3/pkg/application/menu_darwin.go index 1e3fc7197a4..a1703148904 100644 --- a/v3/pkg/application/menu_darwin.go +++ b/v3/pkg/application/menu_darwin.go @@ -84,9 +84,6 @@ func (m *macosMenu) update() { func (m *macosMenu) processMenu(parent unsafe.Pointer, menu *Menu) { for _, item := range menu.items { - if item.hidden { - continue - } switch item.itemType { case submenu: submenu := item.submenu @@ -102,6 +99,9 @@ func (m *macosMenu) processMenu(parent unsafe.Pointer, menu *Menu) { case text, checkbox, radio: menuItem := newMenuItemImpl(item) item.impl = menuItem + if item.hidden { + menuItem.setHidden(true) + } C.addMenuItem(parent, menuItem.nsMenuItem) case separator: C.addMenuSeparator(parent) diff --git a/v3/pkg/application/menu_windows.go b/v3/pkg/application/menu_windows.go index 04a08998256..b75ac74d12f 100644 --- a/v3/pkg/application/menu_windows.go +++ b/v3/pkg/application/menu_windows.go @@ -36,6 +36,14 @@ func (w *windowsMenu) update() { func (w *windowsMenu) processMenu(parentMenu w32.HMENU, inputMenu *Menu) { for _, item := range inputMenu.items { + w.currentMenuID++ + itemID := w.currentMenuID + w.menuMapping[itemID] = item + + menuItemImpl := newMenuItemImpl(item, parentMenu, itemID) + menuItemImpl.parent = inputMenu + item.impl = menuItemImpl + if item.Hidden() { if item.accelerator != nil && item.callback != nil { if w.parentWindow != nil { @@ -44,11 +52,7 @@ func (w *windowsMenu) processMenu(parentMenu w32.HMENU, inputMenu *Menu) { globalApplication.removeKeyBinding(item.accelerator.String()) } } - continue } - w.currentMenuID++ - itemID := w.currentMenuID - w.menuMapping[itemID] = item flags := uint32(w32.MF_STRING) if item.disabled { @@ -84,6 +88,11 @@ func (w *windowsMenu) processMenu(parentMenu w32.HMENU, inputMenu *Menu) { } var menuText = w32.MustStringToUTF16Ptr(thisText) + // If the item is hidden, don't append + if item.Hidden() { + continue + } + w32.AppendMenu(parentMenu, flags, uintptr(itemID), menuText) if item.bitmap != nil { w32.SetMenuIcons(parentMenu, itemID, item.bitmap, nil) diff --git a/v3/pkg/application/menuitem_windows.go b/v3/pkg/application/menuitem_windows.go index 44d825f1b1c..825b2f05c8c 100644 --- a/v3/pkg/application/menuitem_windows.go +++ b/v3/pkg/application/menuitem_windows.go @@ -24,9 +24,9 @@ type windowsMenuItem struct { } func (m *windowsMenuItem) setHidden(hidden bool) { - m.hidden = hidden - if m.hidden { - // iterate the parent items and find the menu item before us + if hidden && !m.hidden { + m.hidden = true + // iterate the parent items and find the menu item after us for i, item := range m.parent.items { if item == m.menuItem { if i < len(m.parent.items)-1 { @@ -37,13 +37,11 @@ func (m *windowsMenuItem) setHidden(hidden bool) { break } } - // Get the position of this menu item in the parent menu - // m.pos = w32.GetMenuItemPosition(m.hMenu, uint32(m.id)) // Remove from parent menu w32.RemoveMenu(m.hMenu, m.id, w32.MF_BYCOMMAND) - } else { - // Add to parent menu - // Get the position of the item before us + } else if !hidden && m.hidden { + m.hidden = false + // Add to parent menu before the "itemAfter" var pos int if m.itemAfter != nil { for i, item := range m.parent.items { diff --git a/v3/pkg/application/popupmenu_windows.go b/v3/pkg/application/popupmenu_windows.go index fbfd5eef9f3..bad9ad71e50 100644 --- a/v3/pkg/application/popupmenu_windows.go +++ b/v3/pkg/application/popupmenu_windows.go @@ -61,6 +61,14 @@ func (p *Win32Menu) newMenu() w32.HMENU { func (p *Win32Menu) buildMenu(parentMenu w32.HMENU, inputMenu *Menu) { currentRadioGroup := RadioGroup{} for _, item := range inputMenu.items { + p.currentMenuID++ + itemID := p.currentMenuID + p.menuMapping[itemID] = item + + menuItemImpl := newMenuItemImpl(item, parentMenu, itemID) + menuItemImpl.parent = inputMenu + item.impl = menuItemImpl + if item.Hidden() { if item.accelerator != nil { if p.parentWindow != nil { @@ -71,14 +79,7 @@ func (p *Win32Menu) buildMenu(parentMenu w32.HMENU, inputMenu *Menu) { globalApplication.removeKeyBinding(item.accelerator.String()) } } - continue } - p.currentMenuID++ - itemID := p.currentMenuID - p.menuMapping[itemID] = item - - menuItemImpl := newMenuItemImpl(item, parentMenu, itemID) - menuItemImpl.parent = inputMenu flags := uint32(w32.MF_STRING) if item.disabled { @@ -131,6 +132,12 @@ func (p *Win32Menu) buildMenu(parentMenu w32.HMENU, inputMenu *Menu) { } } } + + // If the item is hidden, don't append + if item.Hidden() { + continue + } + ok := w32.AppendMenu(parentMenu, flags, uintptr(itemID), w32.MustStringToUTF16Ptr(menuText)) if !ok { globalApplication.fatal("error adding menu item '%s'", menuText) @@ -141,8 +148,6 @@ func (p *Win32Menu) buildMenu(parentMenu w32.HMENU, inputMenu *Menu) { globalApplication.fatal("error setting menu icons: %w", err) } } - - item.impl = menuItemImpl } if len(currentRadioGroup) > 0 { for _, radioMember := range currentRadioGroup { diff --git a/v3/pkg/application/webview_window.go b/v3/pkg/application/webview_window.go index b5903e33c45..374c423b7ad 100644 --- a/v3/pkg/application/webview_window.go +++ b/v3/pkg/application/webview_window.go @@ -108,6 +108,7 @@ type ( showMenuBar() hideMenuBar() toggleMenuBar() + setMenu(menu *Menu) } ) @@ -168,6 +169,22 @@ type WebviewWindow struct { unconditionallyClose bool } +func (w *WebviewWindow) SetMenu(menu *Menu) { + switch runtime.GOOS { + case "darwin": + return + case "windows": + w.options.Windows.Menu = menu + case "linux": + w.options.Linux.Menu = menu + } + if w.impl != nil { + InvokeSync(func() { + w.impl.setMenu(menu) + }) + } +} + // EmitEvent emits an event from the window func (w *WebviewWindow) EmitEvent(name string, data ...any) { globalApplication.emitEvent(&CustomEvent{ diff --git a/v3/pkg/application/webview_window_darwin.go b/v3/pkg/application/webview_window_darwin.go index 668d3c8e92d..28e693a2372 100644 --- a/v3/pkg/application/webview_window_darwin.go +++ b/v3/pkg/application/webview_window_darwin.go @@ -1427,6 +1427,7 @@ func (w *macosWebviewWindow) delete() { func (w *macosWebviewWindow) redo() { } -func (w *macosWebviewWindow) showMenuBar() {} -func (w *macosWebviewWindow) hideMenuBar() {} -func (w *macosWebviewWindow) toggleMenuBar() {} +func (w *macosWebviewWindow) showMenuBar() {} +func (w *macosWebviewWindow) hideMenuBar() {} +func (w *macosWebviewWindow) toggleMenuBar() {} +func (w *macosWebviewWindow) setMenu(_ *Menu) {} diff --git a/v3/pkg/application/webview_window_linux.go b/v3/pkg/application/webview_window_linux.go index 4bef3433543..8c50c61175d 100644 --- a/v3/pkg/application/webview_window_linux.go +++ b/v3/pkg/application/webview_window_linux.go @@ -235,6 +235,15 @@ func (w *linuxWebviewWindow) setPhysicalBounds(physicalBounds Rect) { w.setBounds(physicalBounds) } +func (w *linuxWebviewWindow) setMenu(menu *Menu) { + if menu == nil { + w.gtkmenu = nil + return + } + w.parent.options.Linux.Menu = menu + w.gtkmenu = (menu.impl).(*linuxMenu).native +} + func (w *linuxWebviewWindow) run() { for eventId := range w.parent.eventListeners { w.on(eventId) diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index 89bea1cbd36..3ff9aff9c8d 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -73,6 +73,13 @@ type windowsWebviewWindow struct { isMinimizing bool } +func (w *windowsWebviewWindow) setMenu(menu *Menu) { + menu.Update() + w.menu = NewApplicationMenu(w, menu) + w.menu.parentWindow = w + w32.SetMenu(w.hwnd, w.menu.menu) +} + func (w *windowsWebviewWindow) cut() { w.execJS("document.execCommand('cut')") } diff --git a/v3/pkg/application/window.go b/v3/pkg/application/window.go index d1a4219baef..0d688126bd0 100644 --- a/v3/pkg/application/window.go +++ b/v3/pkg/application/window.go @@ -84,4 +84,5 @@ type Window interface { ZoomIn() ZoomOut() ZoomReset() Window + SetMenu(menu *Menu) }