updated Tray API to ignore double click events on macOS (#8952)

This commit is contained in:
mikeykhalil 2018-04-01 18:07:26 -07:00
parent a08b4f780c
commit 1f29124d11
6 changed files with 36 additions and 0 deletions

View file

@ -176,6 +176,10 @@ void Tray::SetHighlightMode(TrayIcon::HighlightMode mode) {
tray_icon_->SetHighlightMode(mode);
}
void Tray::SetIgnoreDoubleClickEvents(bool ignore) {
tray_icon_->SetIgnoreDoubleClickEvents(ignore);
}
void Tray::DisplayBalloon(mate::Arguments* args,
const mate::Dictionary& options) {
mate::Handle<NativeImage> icon;
@ -224,6 +228,8 @@ void Tray::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setToolTip", &Tray::SetToolTip)
.SetMethod("setTitle", &Tray::SetTitle)
.SetMethod("setHighlightMode", &Tray::SetHighlightMode)
.SetMethod("setIgnoreDoubleClickEvents",
&Tray::SetIgnoreDoubleClickEvents)
.SetMethod("displayBalloon", &Tray::DisplayBalloon)
.SetMethod("popUpContextMenu", &Tray::PopUpContextMenu)
.SetMethod("setContextMenu", &Tray::SetContextMenu)

View file

@ -70,6 +70,7 @@ class Tray : public mate::TrackableObject<Tray>, public TrayIconObserver {
void SetToolTip(const std::string& tool_tip);
void SetTitle(const std::string& title);
void SetHighlightMode(TrayIcon::HighlightMode mode);
void SetIgnoreDoubleClickEvents(bool ignore);
void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options);
void PopUpContextMenu(mate::Arguments* args);
void SetContextMenu(v8::Isolate* isolate, mate::Handle<Menu> menu);

View file

@ -16,6 +16,9 @@ void TrayIcon::SetTitle(const std::string& title) {}
void TrayIcon::SetHighlightMode(TrayIcon::HighlightMode mode) {}
void TrayIcon::SetIgnoreDoubleClickEvents(bool ignore) {
}
void TrayIcon::DisplayBalloon(ImageType icon,
const base::string16& title,
const base::string16& contents) {}

View file

@ -51,6 +51,10 @@ class TrayIcon {
};
virtual void SetHighlightMode(HighlightMode mode);
// Sets the flag which determines whether to ignore double click events. This
// only works on macOS.
virtual void SetIgnoreDoubleClickEvents(bool ignore);
// Displays a notification balloon with the specified contents.
// Depending on the platform it might not appear by the icon tray.
virtual void DisplayBalloon(ImageType icon,

View file

@ -27,6 +27,7 @@ class TrayIconCocoa : public TrayIcon, public AtomMenuModel::Observer {
void SetToolTip(const std::string& tool_tip) override;
void SetTitle(const std::string& title) override;
void SetHighlightMode(TrayIcon::HighlightMode mode) override;
void SetIgnoreDoubleClickEvents(bool ignore) override;
void PopUpContextMenu(const gfx::Point& pos,
AtomMenuModel* menu_model) override;
void SetContextMenu(AtomMenuModel* menu_model) override;

View file

@ -25,6 +25,7 @@ const CGFloat kVerticalTitleMargin = 2;
atom::TrayIconCocoa* trayIcon_; // weak
AtomMenuController* menuController_; // weak
atom::TrayIcon::HighlightMode highlight_mode_;
BOOL ignoreDoubleClickEvents_;
BOOL forceHighlight_;
BOOL inMouseEventSequence_;
BOOL ANSI_;
@ -44,6 +45,7 @@ const CGFloat kVerticalTitleMargin = 2;
image_.reset([image copy]);
trayIcon_ = icon;
highlight_mode_ = atom::TrayIcon::HighlightMode::SELECTION;
ignoreDoubleClickEvents_ = NO;
forceHighlight_ = NO;
inMouseEventSequence_ = NO;
@ -204,6 +206,10 @@ const CGFloat kVerticalTitleMargin = 2;
[self setNeedsDisplay:YES];
}
- (void) setIgnoreDoubleClickEvents:(BOOL)ignore {
ignoreDoubleClickEvents_ = ignore;
}
- (void)setTitle:(NSString*)title {
if (title.length > 0) {
title_.reset([title copy]);
@ -279,6 +285,17 @@ const CGFloat kVerticalTitleMargin = 2;
if (menuController_)
return;
// If we are ignoring double click events, we should ignore the `clickCount`
// value and immediately emit a click event.
if (ignoreDoubleClickEvents_ == YES) {
trayIcon_->NotifyClicked(
gfx::ScreenRectFromNSRect(event.window.frame),
gfx::ScreenPointFromNSPoint([event locationInWindow]),
ui::EventFlagsFromModifiers([event modifierFlags]));
[self setNeedsDisplay:YES];
return;
}
// Single click event.
if (event.clickCount == 1)
trayIcon_->NotifyClicked(
@ -437,6 +454,10 @@ void TrayIconCocoa::SetHighlightMode(TrayIcon::HighlightMode mode) {
[status_item_view_ setHighlight:mode];
}
void TrayIconCocoa::SetIgnoreDoubleClickEvents(bool ignore) {
[status_item_view_ setIgnoreDoubleClickEvents:ignore];
}
void TrayIconCocoa::PopUpContextMenu(const gfx::Point& pos,
AtomMenuModel* menu_model) {
[status_item_view_ popUpContextMenu:menu_model];