Skip to content

Commit

Permalink
Merge pull request #65 from jefvel/menu-item-icons
Browse files Browse the repository at this point in the history
Made it possible to add icons to menu items on Mac
  • Loading branch information
joesis committed Oct 17, 2018
2 parents 3fd1443 + 1a665b2 commit 89b3d9c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions systray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions systray_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ - (void) hide_menu_item:(NSNumber*) menuId
[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;
Expand Down Expand Up @@ -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];
Expand Down
3 changes: 3 additions & 0 deletions systray_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions systray_nonwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
5 changes: 5 additions & 0 deletions systray_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 89b3d9c

Please sign in to comment.