fix: restore hidden menuitem at correct position

This commit is contained in:
dingda.li 2025-04-27 12:00:13 +08:00
commit c922a7e685
3 changed files with 48 additions and 30 deletions

View file

@ -124,8 +124,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed transparency issue for frameless windows by [@leaanthony](https://github.com/leaanthony) based on work by @kron.
- Fixed focus calls when window is disabled or minimised by [@leaanthony](https://github.com/leaanthony) based on work by @kron.
- Fixed system trays not showing after taskbar restarts by [@leaanthony](https://github.com/leaanthony) based on work by @kron.
- Fixed fallbackResponseWriter not implementing Flush() in
[#4245](https://github.com/wailsapp/wails/pull/4245)
- Fixed fallbackResponseWriter not implementing Flush() in [#4245](https://github.com/wailsapp/wails/pull/4245)
- Fixed fallbackResponseWriter not implementing Flush() by [@superDingda] in [#4236](https://github.com/wailsapp/wails/issues/4236)
### Changed

View file

@ -113,6 +113,37 @@ func main() {
ctx.ClickedMenuItem().SetLabel("Unhide the beatles!")
}
})
myMenu.AddSeparator()
coffee := myMenu.Add("Request Coffee").OnClick(func(*application.Context) {
println("Coffee dispatched. Productivity +10!")
})
myMenu.Add("Toggle coffee availability").OnClick(func(*application.Context) {
if coffee.Enabled() {
coffee.SetEnabled(false)
coffee.SetLabel("Coffee Machine Broken")
println("Alert: Developer morale critically low.")
} else {
coffee.SetEnabled(true)
coffee.SetLabel("Request Coffee")
println("All systems nominal. Coffee restored.")
}
})
myMenu.Add("Hide the coffee option").OnClick(func(ctx *application.Context) {
if coffee.Hidden() {
ctx.ClickedMenuItem().SetLabel("Hide the coffee option")
coffee.SetHidden(false)
println("Coffee menu item has been resurrected!")
} else {
coffee.SetHidden(true)
ctx.ClickedMenuItem().SetLabel("Unhide the coffee option")
println("The coffee option has vanished into the void.")
}
})
app.SetMenu(menu)
window := app.NewWebviewWindow().SetBackgroundColour(application.NewRGB(33, 37, 41))

View file

@ -12,45 +12,32 @@ type windowsMenuItem struct {
parent *Menu
menuItem *MenuItem
hMenu w32.HMENU
id int
label string
disabled bool
checked bool
itemType menuItemType
hidden bool
submenu w32.HMENU
itemAfter *MenuItem
hMenu w32.HMENU
id int
label string
disabled bool
checked bool
itemType menuItemType
hidden bool
submenu w32.HMENU
}
func (m *windowsMenuItem) setHidden(hidden bool) {
if hidden && !m.hidden {
m.hidden = true
// iterate the parent items and find the menu item after us
for i, item := range m.parent.items {
if item == m.menuItem {
if i < len(m.parent.items)-1 {
m.itemAfter = m.parent.items[i+1]
} else {
m.itemAfter = nil
}
break
}
}
// Remove from parent menu
w32.RemoveMenu(m.hMenu, m.id, w32.MF_BYCOMMAND)
} else if !hidden && m.hidden {
m.hidden = false
// Add to parent menu before the "itemAfter"
// Reinsert into parent menu at correct visible position
var pos int
if m.itemAfter != nil {
for i, item := range m.parent.items {
if item == m.itemAfter {
pos = i - 1
break
}
for _, item := range m.parent.items {
if item == m.menuItem {
break
}
if item.hidden == false {
pos++
}
m.itemAfter = nil
}
w32.InsertMenuItem(m.hMenu, uint32(pos), true, m.getMenuInfo())
}