Merge pull request #6620 from electron/tray-highlight-toggle
Add option to always highlight the tray icon
This commit is contained in:
commit
b8bafbc5a3
7 changed files with 75 additions and 23 deletions
|
@ -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,45 @@
|
|||
#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
|
||||
// TODO(kevinsawicki): Remove in 2.0, deprecate before then with warnings
|
||||
bool highlight;
|
||||
if (ConvertFromV8(isolate, val, &highlight)) {
|
||||
if (highlight)
|
||||
*out = atom::TrayIcon::HighlightMode::SELECTION;
|
||||
else
|
||||
*out = atom::TrayIcon::HighlightMode::NEVER;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
} // namespace mate
|
||||
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
@ -117,8 +155,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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -183,12 +183,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_
|
||||
|
||||
|
|
Loading…
Reference in a new issue