Skip to content

Commit

Permalink
Merge pull request #4 from James-Pickett/james/menu-open-event
Browse files Browse the repository at this point in the history
adds menu opened event
  • Loading branch information
James-Pickett authored Apr 25, 2023
2 parents 61bda79 + d52b122 commit bab0910
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 141 deletions.
8 changes: 7 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
fmt.Println("Exit at", now.String())
}

systray.Run(onReady, onExit)
systray.Run(onReady, onExit, func(b bool) {})
}

func addQuitItem() {
Expand All @@ -39,6 +39,9 @@ func onReady() {
systray.SetTemplateIcon(icon.Data, icon.Data)
systray.SetTitle("Awesome App")
systray.SetTooltip("Pretty awesome棒棒嗒")
mOpened := systray.AddMenuItem("Should Change When Menu Opened", "Should Change When Menu Opened")
mOpened.Disable()

mChange := systray.AddMenuItem("Change Me", "Change Me")
mChecked := systray.AddMenuItemCheckbox("Checked", "Check Me", true)
mEnabled := systray.AddMenuItem("Enabled", "Enabled")
Expand Down Expand Up @@ -72,6 +75,9 @@ func onReady() {

for {
select {
case <-systray.SystrayMenuOpened:
mOpened.SetTitle("Menu Has Been Opened")
mOpened.SetTooltip("Menu Has Been Opened")
case <-mChange.ClickedCh:
mChange.SetTitle("I've Changed")
case <-mChecked.ClickedCh:
Expand Down
9 changes: 9 additions & 0 deletions systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

var (
SystrayMenuOpened = make(chan struct{})

systrayReady func()
systrayExit func()
systrayOnAppearanceChanged func(bool)
Expand Down Expand Up @@ -259,6 +261,13 @@ func (item *MenuItem) update() {
addOrUpdateMenuItem(item)
}

func systrayMenuOpened() {
select {
case SystrayMenuOpened <- struct{}{}:
default:
}
}

func systrayMenuItemSelected(id uint32) {
menuItemsLock.RLock()
item, ok := menuItems[id]
Expand Down
1 change: 1 addition & 0 deletions systray.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

extern void systray_ready();
extern void systray_on_exit();
extern void systray_menu_opened();
extern void systray_menu_item_selected(int menu_id);
extern void systray_appearance_changed(bool dark);
void registerSystray(void);
Expand Down
5 changes: 5 additions & 0 deletions systray_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ func systray_menu_item_selected(cID C.int) {
systrayMenuItemSelected(uint32(cID))
}

//export systray_menu_opened
func systray_menu_opened() {
systrayMenuOpened()
}

//export systray_appearance_changed
func systray_appearance_changed(dark C.bool) {
systrayAppearanceChanged(bool(dark))
Expand Down
15 changes: 10 additions & 5 deletions systray_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ -(id) initWithId: (int)theMenuId
}
@end

@interface AppDelegate: NSObject <NSApplicationDelegate>
@interface AppDelegate: NSObject <NSApplicationDelegate, NSMenuDelegate>
- (void) add_or_update_menu_item:(MenuItem*) item;
- (IBAction)menuHandler:(id)sender;
@property (assign) IBOutlet NSWindow *window;
@end
@end

@implementation AppDelegate
@implementation AppDelegate
{
NSStatusItem *statusItem;
NSMenu *menu;
Expand All @@ -70,11 +70,16 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
self->statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
self->menu = [[NSMenu alloc] init];
[self->menu setAutoenablesItems: FALSE];
[self->menu setDelegate:self]; // Set the delegate to AppDelegate
[self->statusItem setMenu:self->menu];
[self->statusItem addObserver:self forKeyPath:@"button.effectiveAppearance" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
systray_ready();
}

- (void)menuWillOpen:(NSMenu *)menu {
systray_menu_opened();
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:@"button.effectiveAppearance"]) {
Expand Down Expand Up @@ -140,7 +145,7 @@ - (void)add_or_update_menu_item:(MenuItem *)item {
[parentItem setSubmenu:theMenu];
}
}

NSMenuItem *menuItem;
menuItem = find_menu_item(theMenu, item->menuId);
if (menuItem == NULL) {
Expand Down Expand Up @@ -222,7 +227,7 @@ - (void) remove_menu_item:(NSNumber*) menuId
{
NSMenuItem* menuItem = find_menu_item(menu, menuId);
if (menuItem != NULL) {
[menuItem.menu removeItem:menuItem];
[menuItem.menu removeItem:menuItem];
}
}

Expand Down
5 changes: 5 additions & 0 deletions systray_menu_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (t *tray) GetProperty(id int32, name string) (value dbus.Variant, err *dbus
func (t *tray) Event(id int32, eventID string, data dbus.Variant, timestamp uint32) (err *dbus.Error) {
if eventID == "clicked" {
systrayMenuItemSelected(uint32(id))
return
}

if eventID == "opened" && id == 0 {
systrayMenuOpened()
}
return
}
Expand Down
18 changes: 12 additions & 6 deletions systray_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,20 @@ var wt = winTray{
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms633573(v=vs.85).aspx
func (t *winTray) wndProc(hWnd windows.Handle, message uint32, wParam, lParam uintptr) (lResult uintptr) {
const (
WM_RBUTTONUP = 0x0205
WM_LBUTTONUP = 0x0202
WM_COMMAND = 0x0111
WM_ENDSESSION = 0x0016
WM_CLOSE = 0x0010
WM_DESTROY = 0x0002
WM_RBUTTONUP = 0x0205
WM_LBUTTONUP = 0x0202
WM_COMMAND = 0x0111
WM_ENDSESSION = 0x0016
WM_CLOSE = 0x0010
WM_DESTROY = 0x0002
WM_INITMENUPOPUP = 0x0117
)
switch message {
case WM_INITMENUPOPUP:
isTopLevelMenu := lParam&0x0000FFFF == 0
if isTopLevelMenu {
systrayMenuOpened()
}
case WM_COMMAND:
menuItemId := int32(wParam)
// https://docs.microsoft.com/en-us/windows/win32/menurc/wm-command#menus
Expand Down
Loading

0 comments on commit bab0910

Please sign in to comment.