From 40a64fb67ea67bb68cefbd7b222eeef7d4513d87 Mon Sep 17 00:00:00 2001 From: DeedleFake Date: Tue, 11 Apr 2023 16:02:01 -0400 Subject: [PATCH] instance a few more things --- default.go | 3 +-- menuitem.go | 15 +++++++++------ systray.go | 5 +++++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/default.go b/default.go index 8200d31a..93ac495d 100644 --- a/default.go +++ b/default.go @@ -2,7 +2,6 @@ package systray import ( "sync" - "sync/atomic" ) var ( @@ -111,5 +110,5 @@ func AddMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { func AddSeparator() { initDefaultIcon() - defaultIcon.addSeparator(atomic.AddUint32(¤tID, 1), 0) + defaultIcon.addSeparator(0) } diff --git a/menuitem.go b/menuitem.go index 83c865bd..909537b1 100644 --- a/menuitem.go +++ b/menuitem.go @@ -2,7 +2,6 @@ package systray import ( "fmt" - "sync/atomic" ) // MenuItem is used to keep track each menu item of systray. @@ -23,6 +22,8 @@ type MenuItem struct { checked bool // has the menu item a checkbox (Linux) isCheckable bool + // icon is the icon that the item was created from + icon *Icon // parent item, for sub menus parent *MenuItem } @@ -35,29 +36,30 @@ func (item *MenuItem) String() string { } // newMenuItem returns a populated MenuItem object -func newMenuItem(title string, tooltip string, parent *MenuItem) *MenuItem { +func (icon *Icon) newMenuItem(title string, tooltip string, parent *MenuItem) *MenuItem { return &MenuItem{ ClickedCh: make(chan struct{}), - id: atomic.AddUint32(¤tID, 1), + id: icon.nextID(), title: title, tooltip: tooltip, disabled: false, checked: false, isCheckable: false, + icon: icon, parent: parent, } } // AddSeparator adds a separator bar to the submenu func (item *MenuItem) AddSeparator() { - addSeparator(atomic.AddUint32(¤tID, 1), item.id) + item.icon.addSeparator(item.id) } // AddSubMenuItem adds a nested sub-menu item with the designated title and tooltip. // It can be safely invoked from different goroutines. // Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddSubMenuItemCheckbox func (item *MenuItem) AddSubMenuItem(title string, tooltip string) *MenuItem { - child := newMenuItem(title, tooltip, item) + child := item.icon.newMenuItem(title, tooltip, item) child.update() return child } @@ -66,7 +68,7 @@ func (item *MenuItem) AddSubMenuItem(title string, tooltip string) *MenuItem { // It can be safely invoked from different goroutines. // On Windows and OSX this is the same as calling AddSubMenuItem func (item *MenuItem) AddSubMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem { - child := newMenuItem(title, tooltip, item) + child := item.icon.newMenuItem(title, tooltip, item) child.isCheckable = true child.checked = checked child.update() @@ -110,6 +112,7 @@ func (item *MenuItem) Hide() { // Remove removes a menu item func (item *MenuItem) Remove() { removeMenuItem(item) + menuItemsLock.Lock() delete(menuItems, item.id) menuItemsLock.Unlock() diff --git a/systray.go b/systray.go index 861e6dfa..18d84711 100644 --- a/systray.go +++ b/systray.go @@ -4,6 +4,7 @@ package systray import ( "log" "sync" + "sync/atomic" ) type Icon struct { @@ -24,6 +25,10 @@ func NewIcon() (*Icon, error) { }, nil } +func (icon *Icon) nextID() uint32 { + return atomic.AddUint32(&icon.id, 1) +} + // This helper function allows us to call systrayExit only once, // without accidentally calling it twice in the same lifetime. func runSystrayExit() {