Skip to content

Commit

Permalink
[OSX] Add iconsAreTemplates property to Tray API objects
Browse files Browse the repository at this point in the history
Add boolean property (defaults to `true`) `iconsAreTemplates` to `Tray` objects
to allow for proper display of icons in Mac OS X (Yosemite) Dark Menus.

When `iconsAreTemplates` is set to true, both `icon` and `altIcon` are treated
as "templates" and the system automatically ensures proper styling according to
the various states of the status item (e.g. dark menu, light menu, etc.).
Template images should consist only of black and clear colours and can use the
alpha channel in the image to adjust the opacity of black content.

See [Dark Menus in AppKit Release Notes for OS X v10.10](https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKit/#10_10DarkMenus)
and [`NSImage setTemplate:`](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/index.html#//apple_ref/occ/instm/NSImage/setTemplate:).

On Linux and Windows setting the property has no effect.

FIX nwjs#2476
  • Loading branch information
mrfabbri committed Dec 9, 2014
1 parent 2fc97ad commit 92ec44c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/api/tray/tray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Tray::Tray(int id,
if (option.GetString("title", &title))
SetTitle(title);

bool areTemplates;
if (option.GetBoolean("iconsAreTemplates", &areTemplates))
SetIconsAreTemplates(areTemplates);

std::string icon;
if (option.GetString("icon", &icon) && !icon.empty())
SetIcon(icon);
Expand Down Expand Up @@ -74,6 +78,10 @@ void Tray::Call(const std::string& method,
std::string alticon;
arguments.GetString(0, &alticon);
SetAltIcon(alticon);
} else if (method == "SetIconsAreTemplates") {
bool areTemplates;
arguments.GetBoolean(0, &areTemplates);
SetIconsAreTemplates(areTemplates);
} else if (method == "SetTooltip") {
std::string tooltip;
arguments.GetString(0, &tooltip);
Expand Down
3 changes: 3 additions & 0 deletions src/api/tray/tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ class Tray : public Base {
void Remove();
// Alternate icons only work with Macs
void SetAltIcon(const std::string& alticon_path);
// Template icons only work with Macs
void SetIconsAreTemplates(bool areTemplates);

#if defined(OS_MACOSX)
__block NSStatusItem* status_item_;
MacTrayObserver* status_observer_;
bool iconsAreTemplates;
#elif 0
GtkStatusIcon* status_item_;

Expand Down
13 changes: 13 additions & 0 deletions src/api/tray/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function Tray(option) {
option.alticon = nw.getAbsolutePath(option.alticon);
}

if (option.hasOwnProperty('iconsAreTemplates'))
option.iconsAreTemplates = Boolean(option.iconsAreTemplates);
else
option.iconsAreTemplates = true;

if (option.hasOwnProperty('tooltip'))
option.tooltip = String(option.tooltip);

Expand Down Expand Up @@ -103,6 +108,14 @@ Tray.prototype.__defineSetter__('alticon', function(val) {
this.handleSetter('alticon', 'SetAlticon', String, real_path);
});

Tray.prototype.__defineGetter__('iconsAreTemplates', function() {
return this.handleGetter('iconsAreTemplates');
});

Tray.prototype.__defineSetter__('iconsAreTemplates', function(val) {
this.handleSetter('iconsAreTemplates', 'SetIconsAreTemplates', Boolean, val);
});

Tray.prototype.__defineGetter__('tooltip', function() {
return this.handleGetter('tooltip');
});
Expand Down
3 changes: 3 additions & 0 deletions src/api/tray/tray_aura.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,7 @@ void Tray::Remove() {
void Tray::SetAltIcon(const std::string& alticon_path) {
}

void Tray::SetIconsAreTemplates(bool areTemplates) {
}

} // namespace nwapi
3 changes: 3 additions & 0 deletions src/api/tray/tray_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,7 @@ void Tray::OnPopupMenu(GtkWidget* widget, guint button, guint time) {
void Tray::SetAltIcon(const std::string& alticon_path) {
}

void Tray::SetIconsAreTemplates(bool areTemplates) {
}

} // namespace nwapi
12 changes: 12 additions & 0 deletions src/api/tray/tray_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ - (void)onClick:(id)sender {
if (!icon.empty()) {
NSImage* image = [[NSImage alloc]
initWithContentsOfFile:[NSString stringWithUTF8String:icon.c_str()]];
[image setTemplate:iconsAreTemplates];
[status_item_ setImage:image];
[image release];
} else {
Expand All @@ -83,13 +84,24 @@ - (void)onClick:(id)sender {
if (!alticon.empty()) {
NSImage* image = [[NSImage alloc]
initWithContentsOfFile:[NSString stringWithUTF8String:alticon.c_str()]];
[image setTemplate:iconsAreTemplates];
[status_item_ setAlternateImage:image];
[image release];
} else {
[status_item_ setAlternateImage:nil];
}
}

void Tray::SetIconsAreTemplates(bool areTemplates) {
iconsAreTemplates = areTemplates;
if ([status_item_ image] != nil) {
[[status_item_ image] setTemplate:areTemplates];
}
if ([status_item_ alternateImage] != nil) {
[[status_item_ alternateImage] setTemplate:areTemplates];
}
}

void Tray::SetTooltip(const std::string& tooltip) {
[status_item_ setToolTip:[NSString stringWithUTF8String:tooltip.c_str()]];
}
Expand Down

0 comments on commit 92ec44c

Please sign in to comment.