Skip to content
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

[V3] Fix initially-hidden menuItem [mac/win] #4116

Merged
merged 17 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions v3/examples/menu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func main() {
// Let's make a "Demo" menu
myMenu := menu.AddSubmenu("Demo")

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

// Disabled menu item
myMenu.Add("Not Enabled").SetEnabled(false)

Expand Down
6 changes: 3 additions & 3 deletions v3/pkg/application/menu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ func (m *macosMenu) update() {

func (m *macosMenu) processMenu(parent unsafe.Pointer, menu *Menu) {
for _, item := range menu.items {
if item.hidden {
continue
}
switch item.itemType {
case submenu:
submenu := item.submenu
Expand All @@ -102,6 +99,9 @@ func (m *macosMenu) processMenu(parent unsafe.Pointer, menu *Menu) {
case text, checkbox, radio:
menuItem := newMenuItemImpl(item)
item.impl = menuItem
if item.hidden {
menuItem.setHidden(true)
}
C.addMenuItem(parent, menuItem.nsMenuItem)
case separator:
C.addMenuSeparator(parent)
Expand Down
17 changes: 13 additions & 4 deletions v3/pkg/application/menu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func (w *windowsMenu) update() {

func (w *windowsMenu) processMenu(parentMenu w32.HMENU, inputMenu *Menu) {
for _, item := range inputMenu.items {
w.currentMenuID++
itemID := w.currentMenuID
w.menuMapping[itemID] = item

menuItemImpl := newMenuItemImpl(item, parentMenu, itemID)
menuItemImpl.parent = inputMenu
item.impl = menuItemImpl

if item.Hidden() {
if item.accelerator != nil && item.callback != nil {
if w.parentWindow != nil {
Expand All @@ -44,11 +52,7 @@ func (w *windowsMenu) processMenu(parentMenu w32.HMENU, inputMenu *Menu) {
globalApplication.removeKeyBinding(item.accelerator.String())
}
}
continue
}
w.currentMenuID++
itemID := w.currentMenuID
w.menuMapping[itemID] = item

flags := uint32(w32.MF_STRING)
if item.disabled {
Expand Down Expand Up @@ -84,6 +88,11 @@ func (w *windowsMenu) processMenu(parentMenu w32.HMENU, inputMenu *Menu) {
}
var menuText = w32.MustStringToUTF16Ptr(thisText)

// If the item is hidden, don't append
if item.Hidden() {
continue
}

w32.AppendMenu(parentMenu, flags, uintptr(itemID), menuText)
if item.bitmap != nil {
w32.SetMenuIcons(parentMenu, itemID, item.bitmap, nil)
Expand Down
14 changes: 6 additions & 8 deletions v3/pkg/application/menuitem_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type windowsMenuItem struct {
}

func (m *windowsMenuItem) setHidden(hidden bool) {
m.hidden = hidden
if m.hidden {
// iterate the parent items and find the menu item before us
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 {
Expand All @@ -37,13 +37,11 @@ func (m *windowsMenuItem) setHidden(hidden bool) {
break
}
}
// Get the position of this menu item in the parent menu
// m.pos = w32.GetMenuItemPosition(m.hMenu, uint32(m.id))
// Remove from parent menu
w32.RemoveMenu(m.hMenu, m.id, w32.MF_BYCOMMAND)
} else {
// Add to parent menu
// Get the position of the item before us
} else if !hidden && m.hidden {
m.hidden = false
// Add to parent menu before the "itemAfter"
var pos int
if m.itemAfter != nil {
for i, item := range m.parent.items {
Expand Down
23 changes: 14 additions & 9 deletions v3/pkg/application/popupmenu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ func (p *Win32Menu) newMenu() w32.HMENU {
func (p *Win32Menu) buildMenu(parentMenu w32.HMENU, inputMenu *Menu) {
currentRadioGroup := RadioGroup{}
for _, item := range inputMenu.items {
p.currentMenuID++
itemID := p.currentMenuID
p.menuMapping[itemID] = item

menuItemImpl := newMenuItemImpl(item, parentMenu, itemID)
menuItemImpl.parent = inputMenu
item.impl = menuItemImpl

if item.Hidden() {
if item.accelerator != nil {
if p.parentWindow != nil {
Expand All @@ -71,14 +79,7 @@ func (p *Win32Menu) buildMenu(parentMenu w32.HMENU, inputMenu *Menu) {
globalApplication.removeKeyBinding(item.accelerator.String())
}
}
continue
}
p.currentMenuID++
itemID := p.currentMenuID
p.menuMapping[itemID] = item

menuItemImpl := newMenuItemImpl(item, parentMenu, itemID)
menuItemImpl.parent = inputMenu

flags := uint32(w32.MF_STRING)
if item.disabled {
Expand Down Expand Up @@ -131,6 +132,12 @@ func (p *Win32Menu) buildMenu(parentMenu w32.HMENU, inputMenu *Menu) {
}
}
}

// If the item is hidden, don't append
if item.Hidden() {
continue
}

ok := w32.AppendMenu(parentMenu, flags, uintptr(itemID), w32.MustStringToUTF16Ptr(menuText))
if !ok {
globalApplication.fatal("error adding menu item '%s'", menuText)
Expand All @@ -141,8 +148,6 @@ func (p *Win32Menu) buildMenu(parentMenu w32.HMENU, inputMenu *Menu) {
globalApplication.fatal("error setting menu icons: %w", err)
}
}

item.impl = menuItemImpl
}
if len(currentRadioGroup) > 0 {
for _, radioMember := range currentRadioGroup {
Expand Down
Loading