5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 05:50:54 +08:00

Add fix for application menu. Add docs

This commit is contained in:
Lea Anthony 2025-03-08 11:23:24 +11:00
parent b733b1d3c4
commit 866fb36b67
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
7 changed files with 78 additions and 14 deletions

View File

@ -16,6 +16,43 @@ Create a new application menu using the `NewMenu` method:
menu := app.NewMenu() menu := app.NewMenu()
``` ```
## Setting the Menu
The way to set the menu varies on the platform:
<Tabs>
<TabItem label="macOS" icon="fa-brands:apple">
On macOS, there is only one menu bar per application. Set the menu using the `SetMenu` method of the application:
```go
app.SetMenu(menu)
```
</TabItem>
<TabItem label="Windows" icon="fa-brands:windows">
On Windows, there is a menu bar per window. Set the menu using the `SetMenu` method of the window:
```go
window.SetMenu(menu)
```
</TabItem>
<TabItem label="Linux" icon="fa-brands:linux">
On Linux, the menu bar is typically per window. Set the menu using the `SetMenu` method of the window:
```go
window.SetMenu(menu)
```
</TabItem>
</Tabs>
## Menu Roles ## Menu Roles
Wails provides predefined menu roles that automatically create platform-appropriate menu structures: Wails provides predefined menu roles that automatically create platform-appropriate menu structures:

View File

@ -27,16 +27,7 @@ func main() {
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
menu.AddRole(application.AppMenu) menu.AddRole(application.AppMenu)
} }
fileMenu := menu.AddRole(application.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.EditMenu) menu.AddRole(application.EditMenu)
menu.AddRole(application.WindowMenu) menu.AddRole(application.WindowMenu)
menu.AddRole(application.HelpMenu) menu.AddRole(application.HelpMenu)
@ -124,7 +115,8 @@ func main() {
}) })
app.SetMenu(menu) 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() err := app.Run()

View File

@ -108,6 +108,7 @@ type (
showMenuBar() showMenuBar()
hideMenuBar() hideMenuBar()
toggleMenuBar() toggleMenuBar()
setMenu(menu *Menu)
} }
) )
@ -168,6 +169,22 @@ type WebviewWindow struct {
unconditionallyClose bool 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 // EmitEvent emits an event from the window
func (w *WebviewWindow) EmitEvent(name string, data ...any) { func (w *WebviewWindow) EmitEvent(name string, data ...any) {
globalApplication.emitEvent(&CustomEvent{ globalApplication.emitEvent(&CustomEvent{

View File

@ -1427,6 +1427,7 @@ func (w *macosWebviewWindow) delete() {
func (w *macosWebviewWindow) redo() { func (w *macosWebviewWindow) redo() {
} }
func (w *macosWebviewWindow) showMenuBar() {} func (w *macosWebviewWindow) showMenuBar() {}
func (w *macosWebviewWindow) hideMenuBar() {} func (w *macosWebviewWindow) hideMenuBar() {}
func (w *macosWebviewWindow) toggleMenuBar() {} func (w *macosWebviewWindow) toggleMenuBar() {}
func (w *macosWebviewWindow) setMenu(_ *Menu) {}

View File

@ -235,6 +235,15 @@ func (w *linuxWebviewWindow) setPhysicalBounds(physicalBounds Rect) {
w.setBounds(physicalBounds) 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() { func (w *linuxWebviewWindow) run() {
for eventId := range w.parent.eventListeners { for eventId := range w.parent.eventListeners {
w.on(eventId) w.on(eventId)

View File

@ -73,6 +73,13 @@ type windowsWebviewWindow struct {
isMinimizing bool 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() { func (w *windowsWebviewWindow) cut() {
w.execJS("document.execCommand('cut')") w.execJS("document.execCommand('cut')")
} }

View File

@ -84,4 +84,5 @@ type Window interface {
ZoomIn() ZoomIn()
ZoomOut() ZoomOut()
ZoomReset() Window ZoomReset() Window
SetMenu(menu *Menu)
} }