Skip to content

Commit 92ec44c

Browse files
committed
[OSX] Add iconsAreTemplates property to Tray API objects
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
1 parent 2fc97ad commit 92ec44c

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

src/api/tray/tray.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Tray::Tray(int id,
3737
if (option.GetString("title", &title))
3838
SetTitle(title);
3939

40+
bool areTemplates;
41+
if (option.GetBoolean("iconsAreTemplates", &areTemplates))
42+
SetIconsAreTemplates(areTemplates);
43+
4044
std::string icon;
4145
if (option.GetString("icon", &icon) && !icon.empty())
4246
SetIcon(icon);
@@ -74,6 +78,10 @@ void Tray::Call(const std::string& method,
7478
std::string alticon;
7579
arguments.GetString(0, &alticon);
7680
SetAltIcon(alticon);
81+
} else if (method == "SetIconsAreTemplates") {
82+
bool areTemplates;
83+
arguments.GetBoolean(0, &areTemplates);
84+
SetIconsAreTemplates(areTemplates);
7785
} else if (method == "SetTooltip") {
7886
std::string tooltip;
7987
arguments.GetString(0, &tooltip);

src/api/tray/tray.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ class Tray : public Base {
6969
void Remove();
7070
// Alternate icons only work with Macs
7171
void SetAltIcon(const std::string& alticon_path);
72+
// Template icons only work with Macs
73+
void SetIconsAreTemplates(bool areTemplates);
7274

7375
#if defined(OS_MACOSX)
7476
__block NSStatusItem* status_item_;
7577
MacTrayObserver* status_observer_;
78+
bool iconsAreTemplates;
7679
#elif 0
7780
GtkStatusIcon* status_item_;
7881

src/api/tray/tray.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ function Tray(option) {
4242
option.alticon = nw.getAbsolutePath(option.alticon);
4343
}
4444

45+
if (option.hasOwnProperty('iconsAreTemplates'))
46+
option.iconsAreTemplates = Boolean(option.iconsAreTemplates);
47+
else
48+
option.iconsAreTemplates = true;
49+
4550
if (option.hasOwnProperty('tooltip'))
4651
option.tooltip = String(option.tooltip);
4752

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

111+
Tray.prototype.__defineGetter__('iconsAreTemplates', function() {
112+
return this.handleGetter('iconsAreTemplates');
113+
});
114+
115+
Tray.prototype.__defineSetter__('iconsAreTemplates', function(val) {
116+
this.handleSetter('iconsAreTemplates', 'SetIconsAreTemplates', Boolean, val);
117+
});
118+
106119
Tray.prototype.__defineGetter__('tooltip', function() {
107120
return this.handleGetter('tooltip');
108121
});

src/api/tray/tray_aura.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,7 @@ void Tray::Remove() {
107107
void Tray::SetAltIcon(const std::string& alticon_path) {
108108
}
109109

110+
void Tray::SetIconsAreTemplates(bool areTemplates) {
111+
}
112+
110113
} // namespace nwapi

src/api/tray/tray_gtk.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,7 @@ void Tray::OnPopupMenu(GtkWidget* widget, guint button, guint time) {
8383
void Tray::SetAltIcon(const std::string& alticon_path) {
8484
}
8585

86+
void Tray::SetIconsAreTemplates(bool areTemplates) {
87+
}
88+
8689
} // namespace nwapi

src/api/tray/tray_mac.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ - (void)onClick:(id)sender {
7272
if (!icon.empty()) {
7373
NSImage* image = [[NSImage alloc]
7474
initWithContentsOfFile:[NSString stringWithUTF8String:icon.c_str()]];
75+
[image setTemplate:iconsAreTemplates];
7576
[status_item_ setImage:image];
7677
[image release];
7778
} else {
@@ -83,13 +84,24 @@ - (void)onClick:(id)sender {
8384
if (!alticon.empty()) {
8485
NSImage* image = [[NSImage alloc]
8586
initWithContentsOfFile:[NSString stringWithUTF8String:alticon.c_str()]];
87+
[image setTemplate:iconsAreTemplates];
8688
[status_item_ setAlternateImage:image];
8789
[image release];
8890
} else {
8991
[status_item_ setAlternateImage:nil];
9092
}
9193
}
9294

95+
void Tray::SetIconsAreTemplates(bool areTemplates) {
96+
iconsAreTemplates = areTemplates;
97+
if ([status_item_ image] != nil) {
98+
[[status_item_ image] setTemplate:areTemplates];
99+
}
100+
if ([status_item_ alternateImage] != nil) {
101+
[[status_item_ alternateImage] setTemplate:areTemplates];
102+
}
103+
}
104+
93105
void Tray::SetTooltip(const std::string& tooltip) {
94106
[status_item_ setToolTip:[NSString stringWithUTF8String:tooltip.c_str()]];
95107
}

0 commit comments

Comments
 (0)