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

Add menu parents to menu items. Update menu when menuitem methods are called directly.

This commit is contained in:
Lea Anthony 2025-03-24 11:34:51 +11:00
parent 044756a5cd
commit ade49bbdd3
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
3 changed files with 72 additions and 5 deletions

View File

@ -41,7 +41,41 @@ func main() {
hidden.SetHidden(!hidden.Hidden())
})
// Disabled menu item
disabledMenuItem := myMenu.Add("Show window").SetEnabled(false).OnClick(func(ctx *application.Context) {
// Create the window with HTML content
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "Success",
Width: 300,
Height: 300,
HTML: "It worked",
}).Show()
})
myMenu.Add("Enable show window").OnClick(func(ctx *application.Context) {
disabledMenuItem.SetEnabled(true)
})
// Add an example of URL-based window creation
myMenu.Add("Show window with URL").OnClick(func(ctx *application.Context) {
// Create a window with a URL
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "URL Example",
Width: 800,
Height: 600,
URL: "https://wails.io",
}).Show()
})
myMenu.Add("Show window with raw HTML").OnClick(func(ctx *application.Context) {
// Create a window with a URL
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "Raw HTML Example",
Width: 800,
Height: 600,
HTML: "<html><body><h1>Hello World</h1></body></html>",
}).Show()
})
// Not Enabled menu item (left for backwards compatibility)
myMenu.Add("Not Enabled").SetEnabled(false)
// Click callbacks

View File

@ -40,23 +40,27 @@ func NewMenu() *Menu {
func (m *Menu) Add(label string) *MenuItem {
result := NewMenuItem(label)
result.parent = m
m.items = append(m.items, result)
return result
}
func (m *Menu) AddSeparator() {
result := NewMenuItemSeparator()
result.parent = m
m.items = append(m.items, result)
}
func (m *Menu) AddCheckbox(label string, enabled bool) *MenuItem {
result := NewMenuItemCheckbox(label, enabled)
result.parent = m
m.items = append(m.items, result)
return result
}
func (m *Menu) AddRadio(label string, enabled bool) *MenuItem {
result := NewMenuItemRadio(label, enabled)
result.parent = m
m.items = append(m.items, result)
return result
}

View File

@ -62,6 +62,7 @@ type MenuItem struct {
accelerator *accelerator
role Role
contextMenuData *ContextMenuData
parent *Menu
impl menuItemImpl
radioGroupMembers []*MenuItem
@ -275,13 +276,16 @@ func (m *MenuItem) handleClick() {
func (m *MenuItem) SetAccelerator(shortcut string) *MenuItem {
accelerator, err := parseAccelerator(shortcut)
if err != nil {
globalApplication.error("invalid accelerator: %w", err)
return m
globalApplication.handleFatalError(err)
}
m.accelerator = accelerator
if m.impl != nil {
m.impl.setAccelerator(accelerator)
}
// If this menu item has a parent menu, update it
if m.parent != nil {
m.parent.Update()
}
return m
}
@ -301,6 +305,10 @@ func (m *MenuItem) SetTooltip(s string) *MenuItem {
if m.impl != nil {
m.impl.setTooltip(s)
}
// If this menu item has a parent menu, update it
if m.parent != nil {
m.parent.Update()
}
return m
}
@ -314,6 +322,10 @@ func (m *MenuItem) SetLabel(s string) *MenuItem {
if m.impl != nil {
m.impl.setLabel(s)
}
// If this menu item has a parent menu, update it
if m.parent != nil {
m.parent.Update()
}
return m
}
@ -322,6 +334,10 @@ func (m *MenuItem) SetEnabled(enabled bool) *MenuItem {
if m.impl != nil {
m.impl.setDisabled(m.disabled)
}
// If this menu item has a parent menu, update it
if m.parent != nil {
m.parent.Update()
}
return m
}
@ -330,13 +346,21 @@ func (m *MenuItem) SetBitmap(bitmap []byte) *MenuItem {
if m.impl != nil {
m.impl.setBitmap(bitmap)
}
// If this menu item has a parent menu, update it
if m.parent != nil {
m.parent.Update()
}
return m
}
func (m *MenuItem) SetChecked(checked bool) *MenuItem {
m.checked = checked
if m.impl != nil {
m.impl.setChecked(m.checked)
m.impl.setChecked(checked)
}
// If this menu item has a parent menu, update it
if m.parent != nil {
m.parent.Update()
}
return m
}
@ -344,7 +368,11 @@ func (m *MenuItem) SetChecked(checked bool) *MenuItem {
func (m *MenuItem) SetHidden(hidden bool) *MenuItem {
m.hidden = hidden
if m.impl != nil {
m.impl.setHidden(m.hidden)
m.impl.setHidden(hidden)
}
// If this menu item has a parent menu, update it
if m.parent != nil {
m.parent.Update()
}
return m
}
@ -416,6 +444,7 @@ func (m *MenuItem) Clone() *MenuItem {
callback: m.callback,
itemType: m.itemType,
role: m.role,
parent: m.parent,
}
if m.submenu != nil {
result.submenu = m.submenu.Clone()