mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-04 06:22:16 +08:00
Add fix for application menu. Add docs
This commit is contained in:
parent
b733b1d3c4
commit
866fb36b67
@ -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:
|
||||
|
||||
<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
|
||||
|
||||
Wails provides predefined menu roles that automatically create platform-appropriate menu structures:
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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{
|
||||
|
@ -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) {}
|
||||
|
@ -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)
|
||||
|
@ -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')")
|
||||
}
|
||||
|
@ -84,4 +84,5 @@ type Window interface {
|
||||
ZoomIn()
|
||||
ZoomOut()
|
||||
ZoomReset() Window
|
||||
SetMenu(menu *Menu)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user