Commit 5e529ae 1 parent 03c0d4c commit 5e529ae Copy full SHA for 5e529ae
File tree 7 files changed +78
-14
lines changed
docs/src/content/docs/learn
7 files changed +78
-14
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,43 @@ Create a new application menu using the `NewMenu` method:
16
16
menu := app.NewMenu ()
17
17
```
18
18
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
+
19
56
## Menu Roles
20
57
21
58
Wails provides predefined menu roles that automatically create platform-appropriate menu structures:
Original file line number Diff line number Diff line change @@ -27,16 +27,7 @@ func main() {
27
27
if runtime .GOOS == "darwin" {
28
28
menu .AddRole (application .AppMenu )
29
29
}
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 )
40
31
menu .AddRole (application .EditMenu )
41
32
menu .AddRole (application .WindowMenu )
42
33
menu .AddRole (application .HelpMenu )
@@ -124,7 +115,8 @@ func main() {
124
115
})
125
116
app .SetMenu (menu )
126
117
127
- app .NewWebviewWindow ().SetBackgroundColour (application .NewRGB (33 , 37 , 41 ))
118
+ window := app .NewWebviewWindow ().SetBackgroundColour (application .NewRGB (33 , 37 , 41 ))
119
+ window .SetMenu (menu )
128
120
129
121
err := app .Run ()
130
122
Original file line number Diff line number Diff line change @@ -108,6 +108,7 @@ type (
108
108
showMenuBar ()
109
109
hideMenuBar ()
110
110
toggleMenuBar ()
111
+ setMenu (menu * Menu )
111
112
}
112
113
)
113
114
@@ -168,6 +169,22 @@ type WebviewWindow struct {
168
169
unconditionallyClose bool
169
170
}
170
171
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
+
171
188
// EmitEvent emits an event from the window
172
189
func (w * WebviewWindow ) EmitEvent (name string , data ... any ) {
173
190
globalApplication .emitEvent (& CustomEvent {
Original file line number Diff line number Diff line change @@ -1427,6 +1427,7 @@ func (w *macosWebviewWindow) delete() {
1427
1427
func (w * macosWebviewWindow ) redo () {
1428
1428
}
1429
1429
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 ) {}
Original file line number Diff line number Diff line change @@ -235,6 +235,15 @@ func (w *linuxWebviewWindow) setPhysicalBounds(physicalBounds Rect) {
235
235
w .setBounds (physicalBounds )
236
236
}
237
237
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
+
238
247
func (w * linuxWebviewWindow ) run () {
239
248
for eventId := range w .parent .eventListeners {
240
249
w .on (eventId )
Original file line number Diff line number Diff line change @@ -73,6 +73,13 @@ type windowsWebviewWindow struct {
73
73
isMinimizing bool
74
74
}
75
75
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
+
76
83
func (w * windowsWebviewWindow ) cut () {
77
84
w .execJS ("document.execCommand('cut')" )
78
85
}
Original file line number Diff line number Diff line change @@ -84,4 +84,5 @@ type Window interface {
84
84
ZoomIn ()
85
85
ZoomOut ()
86
86
ZoomReset () Window
87
+ SetMenu (menu * Menu )
87
88
}
You can’t perform that action at this time.
0 commit comments