From 620c9318eacea0df2ca983636d12a1969f983d23 Mon Sep 17 00:00:00 2001 From: Ox Cart Date: Mon, 11 Sep 2017 16:06:23 -0500 Subject: [PATCH] Added hide/show for darwin --- example/main.go | 14 ++++++++++++-- systray.go | 10 ++++++++++ systray.h | 2 ++ systray_darwin.m | 32 ++++++++++++++++++++++++++++++++ systray_nonwindows.go | 12 ++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/example/main.go b/example/main.go index 4b927da..90e8fb7 100644 --- a/example/main.go +++ b/example/main.go @@ -25,9 +25,9 @@ func onReady() { systray.SetIcon(icon.Data) systray.SetTitle("Awesome App") systray.SetTooltip("Lantern") - mQuit := systray.AddMenuItem("Quit", "Quit the whole app") + mQuitOrig := systray.AddMenuItem("Quit", "Quit the whole app") go func() { - <-mQuit.ClickedCh + <-mQuitOrig.ClickedCh fmt.Println("Requesting quit") systray.Quit() fmt.Println("Finished quitting") @@ -44,6 +44,8 @@ func onReady() { systray.AddMenuItem("Ignored", "Ignored") mUrl := systray.AddMenuItem("Open Lantern.org", "my home") mQuit := systray.AddMenuItem("退出", "Quit the whole app") + mToggle := systray.AddMenuItem("Toggle", "Toggle the Quit button") + shown := true for { select { case <-mChange.ClickedCh: @@ -61,6 +63,14 @@ func onReady() { mEnabled.Disable() case <-mUrl.ClickedCh: open.Run("https://www.getlantern.org") + case <-mToggle.ClickedCh: + if shown { + mQuitOrig.Hide() + shown = false + } else { + mQuitOrig.Show() + shown = true + } case <-mQuit.ClickedCh: systray.Quit() fmt.Println("Quit2 now...") diff --git a/systray.go b/systray.go index 33f6172..a55afb1 100644 --- a/systray.go +++ b/systray.go @@ -129,6 +129,16 @@ func (item *MenuItem) Disable() { item.update() } +// Hide hides a menu item +func (item *MenuItem) Hide() { + hideMenuItem(item) +} + +// Show shows a previously hidden menu item +func (item *MenuItem) Show() { + showMenuItem(item) +} + // Checked returns if the menu item has a check mark func (item *MenuItem) Checked() bool { return item.checked diff --git a/systray.h b/systray.h index f175ae4..ef91c07 100644 --- a/systray.h +++ b/systray.h @@ -7,4 +7,6 @@ void setIcon(const char* iconBytes, int length); void setTitle(char* title); void setTooltip(char* tooltip); void add_or_update_menu_item(int menuId, char* title, char* tooltip, short disabled, short checked); +void hide_menu_item(int menuId); +void show_menu_item(int menuId); void quit(); diff --git a/systray_darwin.m b/systray_darwin.m index 03a21ef..485256d 100644 --- a/systray_darwin.m +++ b/systray_darwin.m @@ -108,6 +108,28 @@ } } +- (void) hide_menu_item:(NSNumber*) menuId +{ + NSMenuItem* menuItem; + int existedMenuIndex = [menu indexOfItemWithRepresentedObject: menuId]; + if (existedMenuIndex == -1) { + return; + } + menuItem = [menu itemAtIndex: existedMenuIndex]; + [menuItem setHidden:TRUE]; +} + +- (void) show_menu_item:(NSNumber*) menuId +{ + NSMenuItem* menuItem; + int existedMenuIndex = [menu indexOfItemWithRepresentedObject: menuId]; + if (existedMenuIndex == -1) { + return; + } + menuItem = [menu itemAtIndex: existedMenuIndex]; + [menuItem setHidden:NO]; +} + - (void) quit { [NSApp terminate:self]; @@ -156,6 +178,16 @@ void add_or_update_menu_item(int menuId, char* title, char* tooltip, short disab runInMainThread(@selector(add_or_update_menu_item:), (id)item); } +void hide_menu_item(int menuId) { + NSNumber *mId = [NSNumber numberWithInt:menuId]; + runInMainThread(@selector(hide_menu_item:), (id)mId); +} + +void show_menu_item(int menuId) { + NSNumber *mId = [NSNumber numberWithInt:menuId]; + runInMainThread(@selector(show_menu_item:), (id)mId); +} + void quit() { runInMainThread(@selector(quit), nil); } diff --git a/systray_nonwindows.go b/systray_nonwindows.go index cb8bf80..d3d42f3 100644 --- a/systray_nonwindows.go +++ b/systray_nonwindows.go @@ -60,6 +60,18 @@ func addOrUpdateMenuItem(item *MenuItem) { ) } +func hideMenuItem(item *MenuItem) { + C.hide_menu_item( + C.int(item.id), + ) +} + +func showMenuItem(item *MenuItem) { + C.show_menu_item( + C.int(item.id), + ) +} + //export systray_ready func systray_ready() { systrayReady()