From b2f9cce2973bc396519d1203efae1fc27ae850ce Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 26 Jul 2016 13:51:43 -0700 Subject: [PATCH 1/4] Add option to always highlight the tray icon --- atom/browser/api/atom_api_tray.cc | 43 +++++++++++++++++++++++++++--- atom/browser/api/atom_api_tray.h | 3 ++- atom/browser/ui/tray_icon.cc | 2 +- atom/browser/ui/tray_icon.h | 10 ++++--- atom/browser/ui/tray_icon_cocoa.h | 2 +- atom/browser/ui/tray_icon_cocoa.mm | 26 +++++++++++------- 6 files changed, 67 insertions(+), 19 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index e576601449d6..3b804972cbf7 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -8,7 +8,6 @@ #include "atom/browser/api/atom_api_menu.h" #include "atom/browser/browser.h" -#include "atom/browser/ui/tray_icon.h" #include "atom/common/api/atom_api_native_image.h" #include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/image_converter.h" @@ -18,6 +17,44 @@ #include "native_mate/dictionary.h" #include "ui/gfx/image/image.h" +namespace mate { + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, v8::Local val, + atom::TrayIcon::HighlightMode* out) { + std::string mode; + if (ConvertFromV8(isolate, val, &mode)) { + if (mode == "always") { + *out = atom::TrayIcon::HighlightMode::ALWAYS; + return true; + } + if (mode == "selection") { + *out = atom::TrayIcon::HighlightMode::SELECTION; + return true; + } + if (mode == "never") { + *out = atom::TrayIcon::HighlightMode::NEVER; + return true; + } + } + + // Support old boolean parameter + bool hightlight; + if (ConvertFromV8(isolate, val, &hightlight)) { + if (hightlight) + *out = atom::TrayIcon::HighlightMode::SELECTION; + else + *out = atom::TrayIcon::HighlightMode::NEVER; + return true; + } + + return false; + } +}; +} // namespace mate + + namespace atom { namespace api { @@ -117,8 +154,8 @@ void Tray::SetTitle(const std::string& title) { tray_icon_->SetTitle(title); } -void Tray::SetHighlightMode(bool highlight) { - tray_icon_->SetHighlightMode(highlight); +void Tray::SetHighlightMode(TrayIcon::HighlightMode mode) { + tray_icon_->SetHighlightMode(mode); } void Tray::DisplayBalloon(mate::Arguments* args, diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 56d851d44e76..1e1bc1307543 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -10,6 +10,7 @@ #include #include "atom/browser/api/trackable_object.h" +#include "atom/browser/ui/tray_icon.h" #include "atom/browser/ui/tray_icon_observer.h" #include "native_mate/handle.h" @@ -62,7 +63,7 @@ class Tray : public mate::TrackableObject, void SetPressedImage(v8::Isolate* isolate, mate::Handle image); void SetToolTip(const std::string& tool_tip); void SetTitle(const std::string& title); - void SetHighlightMode(bool highlight); + void SetHighlightMode(TrayIcon::HighlightMode mode); void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options); void PopUpContextMenu(mate::Arguments* args); void SetContextMenu(v8::Isolate* isolate, mate::Handle menu); diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index 6e54af92757d..273f5c084391 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -18,7 +18,7 @@ void TrayIcon::SetPressedImage(ImageType image) { void TrayIcon::SetTitle(const std::string& title) { } -void TrayIcon::SetHighlightMode(bool highlight) { +void TrayIcon::SetHighlightMode(TrayIcon::HighlightMode mode) { } void TrayIcon::DisplayBalloon(ImageType icon, diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 78981d076c77..90e81657aae2 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -43,9 +43,13 @@ class TrayIcon { // only works on macOS. virtual void SetTitle(const std::string& title); - // Sets whether the status icon is highlighted when it is clicked. This only - // works on macOS. - virtual void SetHighlightMode(bool highlight); + // Sets the status icon highlight mode. This only works on macOS. + enum HighlightMode { + ALWAYS, // Always highlight the tray icon + NEVER, // Never highlight the tray icon + SELECTION // Highlight the tray icon when clicked or the menu is opened + }; + virtual void SetHighlightMode(HighlightMode mode); // Displays a notification balloon with the specified contents. // Depending on the platform it might not appear by the icon tray. diff --git a/atom/browser/ui/tray_icon_cocoa.h b/atom/browser/ui/tray_icon_cocoa.h index e9abaa5590e3..fb66a6b3f147 100644 --- a/atom/browser/ui/tray_icon_cocoa.h +++ b/atom/browser/ui/tray_icon_cocoa.h @@ -27,7 +27,7 @@ class TrayIconCocoa : public TrayIcon, void SetPressedImage(const gfx::Image& image) override; void SetToolTip(const std::string& tool_tip) override; void SetTitle(const std::string& title) override; - void SetHighlightMode(bool highlight) override; + void SetHighlightMode(TrayIcon::HighlightMode mode) override; void PopUpContextMenu(const gfx::Point& pos, AtomMenuModel* menu_model) override; void SetContextMenu(AtomMenuModel* menu_model) override; diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 231f1bfb1282..5ac9050b622c 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -23,7 +23,7 @@ const CGFloat kVerticalTitleMargin = 2; @interface StatusItemView : NSView { atom::TrayIconCocoa* trayIcon_; // weak AtomMenuController* menuController_; // weak - BOOL isHighlightEnable_; + atom::TrayIcon::HighlightMode highlight_mode_; BOOL forceHighlight_; BOOL inMouseEventSequence_; base::scoped_nsobject image_; @@ -39,7 +39,7 @@ const CGFloat kVerticalTitleMargin = 2; - (id)initWithImage:(NSImage*)image icon:(atom::TrayIconCocoa*)icon { image_.reset([image copy]); trayIcon_ = icon; - isHighlightEnable_ = YES; + highlight_mode_ = atom::TrayIcon::HighlightMode::SELECTION; forceHighlight_ = NO; inMouseEventSequence_ = NO; @@ -192,8 +192,9 @@ const CGFloat kVerticalTitleMargin = 2; alternateImage_.reset([image copy]); } -- (void)setHighlight:(BOOL)highlight { - isHighlightEnable_ = highlight; +- (void)setHighlight:(atom::TrayIcon::HighlightMode)mode { + highlight_mode_ = mode; + [self setNeedsDisplay:YES]; } - (void)setTitle:(NSString*)title { @@ -328,10 +329,15 @@ const CGFloat kVerticalTitleMargin = 2; } - (BOOL)shouldHighlight { - if (isHighlightEnable_ && forceHighlight_) - return true; - BOOL isMenuOpen = menuController_ && [menuController_ isMenuOpen]; - return isHighlightEnable_ && (inMouseEventSequence_ || isMenuOpen); + switch (highlight_mode_) { + case atom::TrayIcon::HighlightMode::ALWAYS: + return true; + case atom::TrayIcon::HighlightMode::NEVER: + return false; + case atom::TrayIcon::HighlightMode::SELECTION: + BOOL isMenuOpen = menuController_ && [menuController_ isMenuOpen]; + return forceHighlight_ || inMouseEventSequence_ || isMenuOpen; + } } @end @@ -369,8 +375,8 @@ void TrayIconCocoa::SetTitle(const std::string& title) { [status_item_view_ setTitle:base::SysUTF8ToNSString(title)]; } -void TrayIconCocoa::SetHighlightMode(bool highlight) { - [status_item_view_ setHighlight:highlight]; +void TrayIconCocoa::SetHighlightMode(TrayIcon::HighlightMode mode) { + [status_item_view_ setHighlight:mode]; } void TrayIconCocoa::PopUpContextMenu(const gfx::Point& pos, From 0ff6b87f8cbe6e6da76a112915b6eea810a0bef1 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 26 Jul 2016 13:59:19 -0700 Subject: [PATCH 2/4] Update tray.setHighlightMode docs --- docs/api/tray.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/api/tray.md b/docs/api/tray.md index 5ab68c5e9445..f8b5989b36e5 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -173,12 +173,15 @@ Sets the hover text for this tray icon. Sets the title displayed aside of the tray icon in the status bar. -#### `tray.setHighlightMode(highlight)` _macOS_ +#### `tray.setHighlightMode(mode)` _macOS_ -* `highlight` Boolean +* `mode` String highlight mode with one of the following values: + * `'selection'` - Highlight the tray icon when it is clicked and also when + its context menu is open. This is the default. + * `'always'` - Always highlight the tray icon. + * `'never'` - Never highlight the tray icon. -Sets whether the tray icon's background becomes highlighted (in blue) -when the tray icon is clicked. Defaults to true. +Sets when the tray's icon background becomes highlighted (in blue). #### `tray.displayBalloon(options)` _Windows_ From 8e1de8851290ec6f9857a8a79917a4d03e2f1863 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 26 Jul 2016 14:17:41 -0700 Subject: [PATCH 3/4] Correct typo in variable name --- atom/browser/api/atom_api_tray.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 3b804972cbf7..aefe340a3b4e 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -40,9 +40,9 @@ struct Converter { } // Support old boolean parameter - bool hightlight; - if (ConvertFromV8(isolate, val, &hightlight)) { - if (hightlight) + bool highlight; + if (ConvertFromV8(isolate, val, &highlight)) { + if (highlight) *out = atom::TrayIcon::HighlightMode::SELECTION; else *out = atom::TrayIcon::HighlightMode::NEVER; From c4e743d20799a74eb7e475582ef0bde9ff3ccc9b Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 26 Jul 2016 14:18:15 -0700 Subject: [PATCH 4/4] Add TODO to deprecate boolean param --- atom/browser/api/atom_api_tray.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index aefe340a3b4e..213ddbfd89a0 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -40,6 +40,7 @@ struct Converter { } // Support old boolean parameter + // TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings bool highlight; if (ConvertFromV8(isolate, val, &highlight)) { if (highlight)