Skip to content

Commit 5e529ae

Browse files
committed
Add fix for application menu. Add docs
1 parent 03c0d4c commit 5e529ae

File tree

7 files changed

+78
-14
lines changed

7 files changed

+78
-14
lines changed

docs/src/content/docs/learn/application-menu.mdx

+37
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,43 @@ Create a new application menu using the `NewMenu` method:
1616
menu := app.NewMenu()
1717
```
1818

19+
## Setting the Menu
20+
21+
The way to set the menu varies on the platform:
22+
23+
<Tabs>
24+
<TabItem label="macOS" icon="fa-brands:apple">
25+
26+
On macOS, there is only one menu bar per application. Set the menu using the `SetMenu` method of the application:
27+
28+
```go
29+
app.SetMenu(menu)
30+
```
31+
32+
</TabItem>
33+
34+
<TabItem label="Windows" icon="fa-brands:windows">
35+
36+
On Windows, there is a menu bar per window. Set the menu using the `SetMenu` method of the window:
37+
38+
```go
39+
window.SetMenu(menu)
40+
```
41+
42+
</TabItem>
43+
44+
<TabItem label="Linux" icon="fa-brands:linux">
45+
46+
On Linux, the menu bar is typically per window. Set the menu using the `SetMenu` method of the window:
47+
48+
```go
49+
window.SetMenu(menu)
50+
```
51+
52+
</TabItem>
53+
</Tabs>
54+
55+
1956
## Menu Roles
2057

2158
Wails provides predefined menu roles that automatically create platform-appropriate menu structures:

v3/examples/menu/main.go

+3-11
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,7 @@ func main() {
2727
if runtime.GOOS == "darwin" {
2828
menu.AddRole(application.AppMenu)
2929
}
30-
fileMenu := menu.AddRole(application.FileMenu)
31-
_ = fileMenu
32-
//fileMenu.FindByRole(application.Open).OnClick(func(context *application.Context) {
33-
// selection, err := application.OpenFileDialog().PromptForSingleSelection()
34-
// if err != nil {
35-
// println("Error: " + err.Error())
36-
// return
37-
// }
38-
// println("You selected: " + selection)
39-
//})
30+
menu.AddRole(application.FileMenu)
4031
menu.AddRole(application.EditMenu)
4132
menu.AddRole(application.WindowMenu)
4233
menu.AddRole(application.HelpMenu)
@@ -124,7 +115,8 @@ func main() {
124115
})
125116
app.SetMenu(menu)
126117

127-
app.NewWebviewWindow().SetBackgroundColour(application.NewRGB(33, 37, 41))
118+
window := app.NewWebviewWindow().SetBackgroundColour(application.NewRGB(33, 37, 41))
119+
window.SetMenu(menu)
128120

129121
err := app.Run()
130122

v3/pkg/application/webview_window.go

+17
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type (
108108
showMenuBar()
109109
hideMenuBar()
110110
toggleMenuBar()
111+
setMenu(menu *Menu)
111112
}
112113
)
113114

@@ -168,6 +169,22 @@ type WebviewWindow struct {
168169
unconditionallyClose bool
169170
}
170171

172+
func (w *WebviewWindow) SetMenu(menu *Menu) {
173+
switch runtime.GOOS {
174+
case "darwin":
175+
return
176+
case "windows":
177+
w.options.Windows.Menu = menu
178+
case "linux":
179+
w.options.Linux.Menu = menu
180+
}
181+
if w.impl != nil {
182+
InvokeSync(func() {
183+
w.impl.setMenu(menu)
184+
})
185+
}
186+
}
187+
171188
// EmitEvent emits an event from the window
172189
func (w *WebviewWindow) EmitEvent(name string, data ...any) {
173190
globalApplication.emitEvent(&CustomEvent{

v3/pkg/application/webview_window_darwin.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,7 @@ func (w *macosWebviewWindow) delete() {
14271427
func (w *macosWebviewWindow) redo() {
14281428
}
14291429

1430-
func (w *macosWebviewWindow) showMenuBar() {}
1431-
func (w *macosWebviewWindow) hideMenuBar() {}
1432-
func (w *macosWebviewWindow) toggleMenuBar() {}
1430+
func (w *macosWebviewWindow) showMenuBar() {}
1431+
func (w *macosWebviewWindow) hideMenuBar() {}
1432+
func (w *macosWebviewWindow) toggleMenuBar() {}
1433+
func (w *macosWebviewWindow) setMenu(_ *Menu) {}

v3/pkg/application/webview_window_linux.go

+9
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ func (w *linuxWebviewWindow) setPhysicalBounds(physicalBounds Rect) {
235235
w.setBounds(physicalBounds)
236236
}
237237

238+
func (w *linuxWebviewWindow) setMenu(menu *Menu) {
239+
if menu == nil {
240+
w.gtkmenu = nil
241+
return
242+
}
243+
w.parent.options.Linux.Menu = menu
244+
w.gtkmenu = (menu.impl).(*linuxMenu).native
245+
}
246+
238247
func (w *linuxWebviewWindow) run() {
239248
for eventId := range w.parent.eventListeners {
240249
w.on(eventId)

v3/pkg/application/webview_window_windows.go

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ type windowsWebviewWindow struct {
7373
isMinimizing bool
7474
}
7575

76+
func (w *windowsWebviewWindow) setMenu(menu *Menu) {
77+
menu.Update()
78+
w.menu = NewApplicationMenu(w, menu)
79+
w.menu.parentWindow = w
80+
w32.SetMenu(w.hwnd, w.menu.menu)
81+
}
82+
7683
func (w *windowsWebviewWindow) cut() {
7784
w.execJS("document.execCommand('cut')")
7885
}

v3/pkg/application/window.go

+1
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,5 @@ type Window interface {
8484
ZoomIn()
8585
ZoomOut()
8686
ZoomReset() Window
87+
SetMenu(menu *Menu)
8788
}

0 commit comments

Comments
 (0)