Skip to content

Commit 40a64fb

Browse files
committed
instance a few more things
1 parent 486902f commit 40a64fb

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

default.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package systray
22

33
import (
44
"sync"
5-
"sync/atomic"
65
)
76

87
var (
@@ -111,5 +110,5 @@ func AddMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem {
111110
func AddSeparator() {
112111
initDefaultIcon()
113112

114-
defaultIcon.addSeparator(atomic.AddUint32(&currentID, 1), 0)
113+
defaultIcon.addSeparator(0)
115114
}

menuitem.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package systray
22

33
import (
44
"fmt"
5-
"sync/atomic"
65
)
76

87
// MenuItem is used to keep track each menu item of systray.
@@ -23,6 +22,8 @@ type MenuItem struct {
2322
checked bool
2423
// has the menu item a checkbox (Linux)
2524
isCheckable bool
25+
// icon is the icon that the item was created from
26+
icon *Icon
2627
// parent item, for sub menus
2728
parent *MenuItem
2829
}
@@ -35,29 +36,30 @@ func (item *MenuItem) String() string {
3536
}
3637

3738
// newMenuItem returns a populated MenuItem object
38-
func newMenuItem(title string, tooltip string, parent *MenuItem) *MenuItem {
39+
func (icon *Icon) newMenuItem(title string, tooltip string, parent *MenuItem) *MenuItem {
3940
return &MenuItem{
4041
ClickedCh: make(chan struct{}),
41-
id: atomic.AddUint32(&currentID, 1),
42+
id: icon.nextID(),
4243
title: title,
4344
tooltip: tooltip,
4445
disabled: false,
4546
checked: false,
4647
isCheckable: false,
48+
icon: icon,
4749
parent: parent,
4850
}
4951
}
5052

5153
// AddSeparator adds a separator bar to the submenu
5254
func (item *MenuItem) AddSeparator() {
53-
addSeparator(atomic.AddUint32(&currentID, 1), item.id)
55+
item.icon.addSeparator(item.id)
5456
}
5557

5658
// AddSubMenuItem adds a nested sub-menu item with the designated title and tooltip.
5759
// It can be safely invoked from different goroutines.
5860
// Created menu items are checkable on Windows and OSX by default. For Linux you have to use AddSubMenuItemCheckbox
5961
func (item *MenuItem) AddSubMenuItem(title string, tooltip string) *MenuItem {
60-
child := newMenuItem(title, tooltip, item)
62+
child := item.icon.newMenuItem(title, tooltip, item)
6163
child.update()
6264
return child
6365
}
@@ -66,7 +68,7 @@ func (item *MenuItem) AddSubMenuItem(title string, tooltip string) *MenuItem {
6668
// It can be safely invoked from different goroutines.
6769
// On Windows and OSX this is the same as calling AddSubMenuItem
6870
func (item *MenuItem) AddSubMenuItemCheckbox(title string, tooltip string, checked bool) *MenuItem {
69-
child := newMenuItem(title, tooltip, item)
71+
child := item.icon.newMenuItem(title, tooltip, item)
7072
child.isCheckable = true
7173
child.checked = checked
7274
child.update()
@@ -110,6 +112,7 @@ func (item *MenuItem) Hide() {
110112
// Remove removes a menu item
111113
func (item *MenuItem) Remove() {
112114
removeMenuItem(item)
115+
113116
menuItemsLock.Lock()
114117
delete(menuItems, item.id)
115118
menuItemsLock.Unlock()

systray.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package systray
44
import (
55
"log"
66
"sync"
7+
"sync/atomic"
78
)
89

910
type Icon struct {
@@ -24,6 +25,10 @@ func NewIcon() (*Icon, error) {
2425
}, nil
2526
}
2627

28+
func (icon *Icon) nextID() uint32 {
29+
return atomic.AddUint32(&icon.id, 1)
30+
}
31+
2732
// This helper function allows us to call systrayExit only once,
2833
// without accidentally calling it twice in the same lifetime.
2934
func runSystrayExit() {

0 commit comments

Comments
 (0)