5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-19 18:39:30 +08:00

Add UseGlobalMenuByDefault flag. Update examples

This commit is contained in:
Lea Anthony 2025-04-19 13:45:38 +10:00
parent 3a7c084fa5
commit 0c81a37e3b
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
7 changed files with 51 additions and 10 deletions

View File

@ -284,11 +284,36 @@ fileMenu.AddRole(application.Quit)
Application menus are the menus that appear at the top of your application window (Windows/Linux) or at the top of the screen (macOS). Application menus are the menus that appear at the top of your application window (Windows/Linux) or at the top of the screen (macOS).
### Global Menu Inheritance: `UseGlobalMenuByDefault`
On Windows and Linux, menus are usually set per window. However, if you want all windows to use the global application menu by default (unless a window-specific menu is provided), you can enable this behaviour with the `UseGlobalMenuByDefault` option:
```go
app := application.New(application.Options{
Name: "MyApp",
UseGlobalMenuByDefault: true, // All windows will use the global menu unless overridden
})
menu := app.NewMenu()
// ... add items/roles ...
app.SetMenu(menu)
app.NewWebviewWindow().SetTitle("Window 1").Show()
app.NewWebviewWindow().SetTitle("Window 2").Show()
```
- When `UseGlobalMenuByDefault` is `true`, any window created without an explicit menu will inherit the global application menu.
- If you set a menu directly on a window (via `WebviewWindowOptions.Menu` or `window.SetMenu()`), that menu takes precedence for that window.
- This flag has no effect on macOS, where the application menu is always global.
**When to use:**
- Use this option if you want a consistent application menu across all windows on Windows/Linux, without having to set it for each window individually.
- Omit this option or set it to `false` if you want to control menus per window.
### Application Menu Behaviour ### Application Menu Behaviour
When you set an application menu using `app.SetMenu()`, it becomes the main menu on macOS. When you set an application menu using `app.SetMenu()`, it becomes the main menu on macOS.
Menus are set on a pre-window basis for Windows/Linux. Menus are set on a pre-window basis for Windows/Linux (unless `UseGlobalMenuByDefault` is enabled).
```go ```go
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{

View File

@ -14,6 +14,7 @@ var clickBitmap []byte
func main() { func main() {
app := application.New(application.Options{ app := application.New(application.Options{
UseGlobalMenuByDefault: true,
Name: "Menu Demo", Name: "Menu Demo",
Description: "A demo of the menu system", Description: "A demo of the menu system",
Assets: application.AlphaAssets, Assets: application.AlphaAssets,

View File

@ -14,6 +14,7 @@ var clickBitmap []byte
func main() { func main() {
app := application.New(application.Options{ app := application.New(application.Options{
UseGlobalMenuByDefault: true,
Name: "Menu Demo", Name: "Menu Demo",
Description: "A demo of the menu system", Description: "A demo of the menu system",
Assets: application.AlphaAssets, Assets: application.AlphaAssets,

View File

@ -84,12 +84,10 @@ func (r rnr) run() {
} }
func (a *linuxApp) setApplicationMenu(menu *Menu) { func (a *linuxApp) setApplicationMenu(menu *Menu) {
// FIXME: How do we avoid putting a menu?
if menu == nil { if menu == nil {
// Create a default menu
menu = DefaultApplicationMenu() menu = DefaultApplicationMenu()
globalApplication.ApplicationMenu = menu
} }
globalApplication.ApplicationMenu = menu
} }
func (a *linuxApp) run() error { func (a *linuxApp) run() error {

View File

@ -28,6 +28,10 @@ type Options struct {
// Linux is the Linux specific configuration for Linux builds // Linux is the Linux specific configuration for Linux builds
Linux LinuxOptions Linux LinuxOptions
// UseGlobalMenuByDefault determines if the global application menu set via SetMenu should be used by default for all windows on Windows and Linux.
// If false (default), windows do not inherit the global menu unless explicitly set. If true, all windows inherit the global menu unless they opt out.
UseGlobalMenuByDefault bool
// Services allows you to bind Go methods to the frontend. // Services allows you to bind Go methods to the frontend.
Services []Service Services []Service

View File

@ -275,6 +275,12 @@ func (w *linuxWebviewWindow) run() {
menu.Update() menu.Update()
}) })
w.gtkmenu = (menu.impl).(*linuxMenu).native w.gtkmenu = (menu.impl).(*linuxMenu).native
} else if !w.parent.options.Frameless && globalApplication.options.UseGlobalMenuByDefault && globalApplication.ApplicationMenu != nil {
// Use the global application menu if the flag is set and no explicit window menu is provided
InvokeSync(func() {
globalApplication.ApplicationMenu.Update()
})
w.gtkmenu = (globalApplication.ApplicationMenu.impl).(*linuxMenu).native
} }
w.window, w.webview, w.vbox = windowNew(app.application, w.gtkmenu, w.parent.id, w.parent.options.Linux.WebviewGpuPolicy) w.window, w.webview, w.vbox = windowNew(app.application, w.gtkmenu, w.parent.id, w.parent.options.Linux.WebviewGpuPolicy)

View File

@ -322,6 +322,12 @@ func (w *windowsWebviewWindow) run() {
w.menu = NewApplicationMenu(w, userMenu) w.menu = NewApplicationMenu(w, userMenu)
w.menu.parentWindow = w w.menu.parentWindow = w
appMenu = w.menu.menu appMenu = w.menu.menu
} else if globalApplication.options.UseGlobalMenuByDefault && globalApplication.ApplicationMenu != nil {
// Use the global application menu if the flag is set and no explicit window menu is provided
globalApplication.ApplicationMenu.Update()
w.menu = NewApplicationMenu(w, globalApplication.ApplicationMenu)
w.menu.parentWindow = w
appMenu = w.menu.menu
} }
} }