Skip to content

[V3] Restore hidden menuitem at correct position #4246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/content/docs/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions v3/examples/menu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
43 changes: 15 additions & 28 deletions v3/pkg/application/menuitem_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
Loading