diff --git a/docs/src/content/docs/learn/application-menu.mdx b/docs/src/content/docs/learn/application-menu.mdx index df344a02c..01f2d271d 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 a9ae843b8..218f93a2d 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) @@ -124,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/webview_window.go b/v3/pkg/application/webview_window.go index b5903e33c..374c423b7 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 668d3c8e9..28e693a23 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 4bef34335..8c50c6117 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 89bea1cbd..3ff9aff9c 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 d1a4219ba..0d688126b 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) }