Updates for dynamic menus

Cleanup of logging
This commit is contained in:
Lea Anthony
2020-12-05 07:52:59 +11:00
parent 13dc0c78df
commit e8bb950e06
7 changed files with 182 additions and 20 deletions

View File

@@ -22,3 +22,30 @@ func NewMenuFromItems(first *MenuItem, rest ...*MenuItem) *Menu {
return result
}
func (m *Menu) GetByID(menuID string) *MenuItem {
// Loop over menu items
for _, item := range m.Items {
result := item.getByID(menuID)
if result != nil {
return result
}
}
return nil
}
func (m *Menu) RemoveByID(id string) bool {
// Loop over menu items
for index, item := range m.Items {
if item.ID == id {
m.Items = append(m.Items[:index], m.Items[index+1:]...)
return true
}
result := item.removeByID(id)
if result == true {
return result
}
}
return false
}

View File

@@ -43,6 +43,53 @@ func (m *MenuItem) Append(item *MenuItem) bool {
return true
}
// Prepend will attempt to prepend the given menu item to
// this item's submenu items. If this menu item is not a
// submenu, then this method will not add the item and
// simply return false.
func (m *MenuItem) Prepend(item *MenuItem) bool {
if m.Type != SubmenuType {
return false
}
m.SubMenu = append([]*MenuItem{item}, m.SubMenu...)
return true
}
func (m *MenuItem) getByID(id string) *MenuItem {
// If I have the ID return me!
if m.ID == id {
return m
}
// Check submenus
for _, submenu := range m.SubMenu {
result := submenu.getByID(id)
if result != nil {
return result
}
}
return nil
}
func (m *MenuItem) removeByID(id string) bool {
for index, item := range m.SubMenu {
if item.ID == id {
m.SubMenu = append(m.SubMenu[:index], m.SubMenu[index+1:]...)
return true
}
if item.Type == SubmenuType {
result := item.removeByID(id)
if result == true {
return result
}
}
}
return false
}
// Text is a helper to create basic Text menu items
func Text(label string, id string) *MenuItem {
return TextWithAccelerator(label, id, nil)