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

Avoid adding duplicate menuitems (windows)

Previously, when calling `.SetHidden(false)` on a menu item that was not hidden, a duplicate menu item was created.
This commit is contained in:
Ian VanSchooten 2025-03-06 08:49:22 -05:00 committed by Lea Anthony
parent 5fb1a14c36
commit 35cc7fd569
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
3 changed files with 14 additions and 6 deletions

View File

@ -46,7 +46,7 @@ func main() {
// Hidden menu item that can be unhidden
hidden := myMenu.Add("I was hidden").SetHidden(true)
myMenu.Add("Toggle hidden menu").OnClick(func(ctx *application.Context) {
myMenu.Add("Toggle the hidden menu").OnClick(func(ctx *application.Context) {
hidden.SetHidden(!hidden.Hidden())
})

View File

@ -2,11 +2,12 @@ package main
import (
_ "embed"
"log"
"runtime"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
"github.com/wailsapp/wails/v3/pkg/icons"
"log"
"runtime"
)
var windowShowing bool
@ -52,6 +53,12 @@ func main() {
systemTray := app.NewSystemTray()
menu := app.NewMenu()
// Hidden menu item that can be unhidden
hidden := menu.Add("I was hidden").SetHidden(true)
menu.Add("Toggle the hidden menu").OnClick(func(ctx *application.Context) {
hidden.SetHidden(!hidden.Hidden())
menu.Update()
})
menu.Add("Quit").OnClick(func(data *application.Context) {
app.Quit()
})

View File

@ -24,8 +24,8 @@ type windowsMenuItem struct {
}
func (m *windowsMenuItem) setHidden(hidden bool) {
m.hidden = hidden
if m.hidden {
if hidden && !m.hidden {
m.hidden = true
// iterate the parent items and find the menu item before us
for i, item := range m.parent.items {
if item == m.menuItem {
@ -41,7 +41,8 @@ func (m *windowsMenuItem) setHidden(hidden bool) {
// m.pos = w32.GetMenuItemPosition(m.hMenu, uint32(m.id))
// Remove from parent menu
w32.RemoveMenu(m.hMenu, m.id, w32.MF_BYCOMMAND)
} else {
} else if !hidden && m.hidden {
m.hidden = false
// Add to parent menu
// Get the position of the item before us
var pos int