aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjefvel2018-10-15 15:09:58 +0200
committerjefvel2018-10-15 15:09:58 +0200
commit369c5d2d63719b7eecc0c68e8d6c0e11d9b36697 (patch)
tree9094dca13a085735e02c16109b077e3686b775c2
parent3fd1443dac5c8297999189fe28d5836b2b075b66 (diff)
downloadsystray-369c5d2d63719b7eecc0c68e8d6c0e11d9b36697.tar.bz2
Made it possible to add icons to menu items on Mac
-rw-r--r--systray.h1
-rw-r--r--systray_darwin.m22
-rw-r--r--systray_linux.c3
-rw-r--r--systray_nonwindows.go7
-rw-r--r--systray_windows.go5
5 files changed, 38 insertions, 0 deletions
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..a489bc4 100644
--- a/systray_darwin.m
+++ b/systray_darwin.m
@@ -124,6 +124,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 +176,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 b56005b..a34c267 100644
--- a/systray_linux.c
+++ b/systray_linux.c
@@ -177,6 +177,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) {