aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--example/main.go4
-rw-r--r--systray.h1
-rw-r--r--systray_darwin.m51
-rw-r--r--systray_linux.c3
-rw-r--r--systray_nonwindows.go7
-rw-r--r--systray_windows.go5
7 files changed, 67 insertions, 7 deletions
diff --git a/README.md b/README.md
index 5a24b66..626c132 100644
--- a/README.md
+++ b/README.md
@@ -13,6 +13,9 @@ func onReady() {
systray.SetTitle("Awesome App")
systray.SetTooltip("Pretty awesome超级棒")
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
+
+ // Sets the icon of a menu item. Only available on Mac.
+ mQuit.SetIcon(icon.Data)
}
func onExit() {
diff --git a/example/main.go b/example/main.go
index f889b3c..5542b89 100644
--- a/example/main.go
+++ b/example/main.go
@@ -44,6 +44,10 @@ func onReady() {
systray.AddMenuItem("Ignored", "Ignored")
mUrl := systray.AddMenuItem("Open Lantern.org", "my home")
mQuit := systray.AddMenuItem("退出", "Quit the whole app")
+
+ // Sets the icon of a menu item. Only available on Mac.
+ mQuit.SetIcon(icon.Data)
+
systray.AddSeparator()
mToggle := systray.AddMenuItem("Toggle", "Toggle the Quit button")
shown := true
diff --git a/systray.h b/systray.h
index f9b1d3c..36bcf98 100644
--- a/systray.h
+++ b/systray.h
@@ -4,6 +4,7 @@ extern void systray_menu_item_selected(int menu_id);
int nativeLoop(void);
void setIcon(const char* iconBytes, int length);
+void setMenuItemIcon(const char* iconBytes, int length, int menuId);
void setTitle(char* title);
void setTooltip(char* tooltip);
void add_or_update_menu_item(int menuId, char* title, char* tooltip, short disabled, short checked);
diff --git a/systray_darwin.m b/systray_darwin.m
index f1dc7ac..e466948 100644
--- a/systray_darwin.m
+++ b/systray_darwin.m
@@ -64,15 +64,30 @@
}
- (void)setIcon:(NSImage *)image {
- [statusItem setImage:image];
+ statusItem.button.image = image;
+ [self updateTitleButtonStyle];
}
- (void)setTitle:(NSString *)title {
- [statusItem setTitle:title];
+ statusItem.button.title = title;
+ [self updateTitleButtonStyle];
}
+-(void)updateTitleButtonStyle {
+ if (statusItem.button.image != nil) {
+ if ([statusItem.button.title length] == 0) {
+ statusItem.button.imagePosition = NSImageOnly;
+ } else {
+ statusItem.button.imagePosition = NSImageLeft;
+ }
+ } else {
+ statusItem.button.imagePosition = NSNoImage;
+ }
+}
+
+
- (void)setTooltip:(NSString *)tooltip {
- [statusItem setToolTip:tooltip];
+ statusItem.button.toolTip = tooltip;
}
- (IBAction)menuHandler:(id)sender
@@ -97,14 +112,14 @@
}
[menuItem setToolTip:item->tooltip];
if (item->disabled == 1) {
- [menuItem setEnabled:FALSE];
+ menuItem.enabled = FALSE;
} else {
- [menuItem setEnabled:TRUE];
+ menuItem.enabled = TRUE;
}
if (item->checked == 1) {
- [menuItem setState:NSOnState];
+ menuItem.state = NSControlStateValueOn;
} else {
- [menuItem setState:NSOffState];
+ menuItem.state = NSControlStateValueOff;
}
}
@@ -124,6 +139,19 @@
[menuItem setHidden:TRUE];
}
+- (void)setMenuItemIcon:(NSArray*)imageAndMenuId {
+ NSImage* image = [imageAndMenuId objectAtIndex:0];
+ NSNumber* menuId = [imageAndMenuId objectAtIndex:1];
+
+ NSMenuItem* menuItem;
+ int existedMenuIndex = [menu indexOfItemWithRepresentedObject: menuId];
+ if (existedMenuIndex == -1) {
+ return;
+ }
+ menuItem = [menu itemAtIndex: existedMenuIndex];
+ menuItem.image = image;
+}
+
- (void) show_menu_item:(NSNumber*) menuId
{
NSMenuItem* menuItem;
@@ -163,6 +191,15 @@ void setIcon(const char* iconBytes, int length) {
runInMainThread(@selector(setIcon:), (id)image);
}
+void setMenuItemIcon(const char* iconBytes, int length, int menuId) {
+ NSData* buffer = [NSData dataWithBytes: iconBytes length:length];
+ NSImage *image = [[NSImage alloc] initWithData:buffer];
+ [image setSize:NSMakeSize(16, 16)];
+
+ NSNumber *mId = [NSNumber numberWithInt:menuId];
+ runInMainThread(@selector(setMenuItemIcon:), @[image, (id)mId]);
+}
+
void setTitle(char* ctitle) {
NSString* title = [[NSString alloc] initWithCString:ctitle
encoding:NSUTF8StringEncoding];
diff --git a/systray_linux.c b/systray_linux.c
index 7ed58cd..72cd614 100644
--- a/systray_linux.c
+++ b/systray_linux.c
@@ -181,6 +181,9 @@ void setTooltip(char* ctooltip) {
free(ctooltip);
}
+void setMenuItemIcon(const char* iconBytes, int length, int menuId) {
+}
+
void add_or_update_menu_item(int menu_id, char* title, char* tooltip, short disabled, short checked) {
MenuItemInfo *mii = malloc(sizeof(MenuItemInfo));
mii->menu_id = menu_id;
diff --git a/systray_nonwindows.go b/systray_nonwindows.go
index 0ed03b4..4868b55 100644
--- a/systray_nonwindows.go
+++ b/systray_nonwindows.go
@@ -60,6 +60,13 @@ func addOrUpdateMenuItem(item *MenuItem) {
)
}
+// SetIcon sets the icon of a menu item. Only available on Mac.
+// iconBytes should be the content of .ico/.jpg/.png
+func (item *MenuItem) SetIcon(iconBytes []byte) {
+ cstr := (*C.char)(unsafe.Pointer(&iconBytes[0]))
+ C.setMenuItemIcon(cstr, (C.int)(len(iconBytes)), C.int(item.id))
+}
+
func addSeparator(id int32) {
C.add_separator(C.int(id))
}
diff --git a/systray_windows.go b/systray_windows.go
index 68e20cf..7a9d7a1 100644
--- a/systray_windows.go
+++ b/systray_windows.go
@@ -647,6 +647,11 @@ func SetTitle(title string) {
// do nothing
}
+// SetIcon sets the icon of a menu item. Only available on Mac.
+func (item *MenuItem) SetIcon(iconBytes []byte) {
+ // do nothing
+}
+
// SetTooltip sets the systray tooltip to display on mouse hover of the tray icon,
// only available on Mac and Windows.
func SetTooltip(tooltip string) {