Browse Source

Add option to always highlight the tray icon

Kevin Sawicki 8 years ago
parent
commit
b2f9cce297

+ 40 - 3
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<atom::TrayIcon::HighlightMode> {
+  static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> 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,

+ 2 - 1
atom/browser/api/atom_api_tray.h

@@ -10,6 +10,7 @@
 #include <vector>
 
 #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<Tray>,
   void SetPressedImage(v8::Isolate* isolate, mate::Handle<NativeImage> 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> menu);

+ 1 - 1
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,

+ 7 - 3
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.

+ 1 - 1
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;

+ 16 - 10
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<NSImage> 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,