Skip to content

Commit

Permalink
[Mac] Fix MenuItem.setImage and Tray.setImage.
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Sep 20, 2012
1 parent 143aef5 commit cd6f81e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/api/menuitem/menuitem_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@
}

void MenuItem::SetIcon(const std::string& icon) {
NSImage* image = [[NSImage alloc]
initWithContentsOfFile:[NSString stringWithUTF8String:icon.c_str()]];
[menu_item_ setImage:image];
if (!icon.empty()) {
NSImage* image = [[NSImage alloc]
initWithContentsOfFile:[NSString stringWithUTF8String:icon.c_str()]];
[menu_item_ setImage:image];
[image release];
} else {
[menu_item_ setImage:nil];
}
}

void MenuItem::SetTooltip(const std::string& tooltip) {
Expand Down
13 changes: 9 additions & 4 deletions src/api/tray/tray_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@
return [[status_item_ title] UTF8String];
}

void Tray::SetIcon(const std::string& path) {
NSString* icon_path = [NSString stringWithUTF8String:path.c_str()];
NSImage* icon = [[NSImage alloc] initWithContentsOfFile:icon_path];
[status_item_ setImage:icon];
void Tray::SetIcon(const std::string& icon) {
if (!icon.empty()) {
NSImage* image = [[NSImage alloc]
initWithContentsOfFile:[NSString stringWithUTF8String:icon.c_str()]];
[status_item_ setImage:image];
[image release];
} else {
[status_item_ setImage:nil];
}
}

void Tray::SetTooltip(const std::string& tooltip) {
Expand Down
12 changes: 11 additions & 1 deletion tests/menu/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,17 @@
}

var tray = new gui.Tray({ title: 'Test Menu', menu: menu });
menu[0].icon = tray.icon = 'icon1.png';
menu[0].icon = 'icon1.png';
assert(
menu[0].icon == 'icon1.png',
'Set Menu Icon');

menu[0].icon = '';
assert(
menu[0].icon == '',
'Clear Menu Icon');

menu[1].icon = tray.icon = 'icon1.png';
</script>
</body>
</html>
26 changes: 23 additions & 3 deletions tests/tray/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,42 @@

var tray = new gui.Tray({
title: 'Test Tray',
icon: 'icon.png',
menu: menu
});
menu[0].icon = tray.icon = 'icon.png';

assert(
tray.icon == 'icon.png',
'Initial Tray Icon');

tray.icon = '';
assert(
tray.icon == '',
'Clear Tray Icon');

assert(
tray.title == 'Test Tray',
tray.tooltip == '',
tray.menu == menu,
'Init tray');
'Init Tray');

tray.title = 'Love Tray';
tray.tooltip = 'Love Tooltip';
assert(
tray.title == 'Love Tray',
tray.tooltip == 'Love Tooltip',
'Change tray');
'Change Tray');

var tray2 = new gui.Tray({
title: 'Test Tray 2',
icon: 'icon.png',
menu: menu
});

assert(
tray.menu == tray2.menu,
tray2.title == 'Test Tray 2',
'Second Tray');

</script>
</body>
Expand Down

0 comments on commit cd6f81e

Please sign in to comment.