Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add middle click event to tray #39926

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/api/tray.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ Returns:

Emitted when the tray icon is double clicked.

#### Event: 'middle-click' _Windows_

Returns:

* `event` [KeyboardEvent](structures/keyboard-event.md)
* `bounds` [Rectangle](structures/rectangle.md) - The bounds of tray icon.

Emitted when the tray icon is middle clicked.

#### Event: 'balloon-show' _Windows_

Emitted when the tray balloon shows.
Expand Down
6 changes: 6 additions & 0 deletions shell/browser/api/electron_api_tray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) {
EmitWithoutEvent("right-click", CreateEventFromFlags(modifiers), bounds);
}

void Tray::OnMiddleClicked(const gfx::Rect& bounds, int modifiers) {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
EmitWithoutEvent("middle-click", CreateEventFromFlags(modifiers), bounds);
}

void Tray::OnBalloonShow() {
Emit("balloon-show");
}
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Tray : public gin::Wrappable<Tray>,
int modifiers) override;
void OnDoubleClicked(const gfx::Rect& bounds, int modifiers) override;
void OnRightClicked(const gfx::Rect& bounds, int modifiers) override;
void OnMiddleClicked(const gfx::Rect& bounds, int modifiers) override;
void OnBalloonShow() override;
void OnBalloonClicked() override;
void OnBalloonClosed() override;
Expand Down
5 changes: 5 additions & 0 deletions shell/browser/ui/tray_icon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ void TrayIcon::NotifyDoubleClicked(const gfx::Rect& bounds, int modifiers) {
observer.OnDoubleClicked(bounds, modifiers);
}

void TrayIcon::NotifyMiddleClicked(const gfx::Rect& bounds, int modifiers) {
for (TrayIconObserver& observer : observers_)
observer.OnMiddleClicked(bounds, modifiers);
}

void TrayIcon::NotifyBalloonShow() {
for (TrayIconObserver& observer : observers_)
observer.OnBalloonShow();
Expand Down
1 change: 1 addition & 0 deletions shell/browser/ui/tray_icon.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class TrayIcon {
const gfx::Point& location = gfx::Point(),
int modifiers = 0);
void NotifyDoubleClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0);
void NotifyMiddleClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0);
void NotifyBalloonShow();
void NotifyBalloonClicked();
void NotifyBalloonClosed();
Expand Down
1 change: 1 addition & 0 deletions shell/browser/ui/tray_icon_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TrayIconObserver : public base::CheckedObserver {
const gfx::Point& location,
int modifiers) {}
virtual void OnDoubleClicked(const gfx::Rect& bounds, int modifiers) {}
virtual void OnMiddleClicked(const gfx::Rect& bounds, int modifiers) {}
virtual void OnBalloonShow() {}
virtual void OnBalloonClicked() {}
virtual void OnBalloonClosed() {}
Expand Down
5 changes: 4 additions & 1 deletion shell/browser/ui/win/notify_icon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ NotifyIcon::~NotifyIcon() {

void NotifyIcon::HandleClickEvent(int modifiers,
bool left_mouse_click,
bool double_button_click) {
bool double_button_click,
bool middle_button_click) {
gfx::Rect bounds = GetBounds();

if (left_mouse_click) {
Expand All @@ -84,6 +85,8 @@ void NotifyIcon::HandleClickEvent(int modifiers,
display::Screen::GetScreen()->GetCursorScreenPoint(),
modifiers);
return;
} else if (middle_button_click) { // single middle click
NotifyMiddleClicked(bounds, modifiers);
} else if (!double_button_click) { // single right click
if (menu_model_)
PopUpContextMenu(gfx::Point(), menu_model_->GetWeakPtr());
Expand Down
3 changes: 2 additions & 1 deletion shell/browser/ui/win/notify_icon.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class NotifyIcon : public TrayIcon {
// otherwise displays the context menu if there is one.
void HandleClickEvent(int modifiers,
bool left_button_click,
bool double_button_click);
bool double_button_click,
bool middle_button_click);

// Handles a mouse move event from the user.
void HandleMouseMoveEvent(int modifiers);
Expand Down
5 changes: 4 additions & 1 deletion shell/browser/ui/win/notify_icon_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ LRESULT CALLBACK NotifyIconHost::WndProc(HWND hwnd,

case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_CONTEXTMENU:
// Walk our icons, find which one was clicked on, and invoke its
// HandleClickEvent() method.
Expand All @@ -200,7 +202,8 @@ LRESULT CALLBACK NotifyIconHost::WndProc(HWND hwnd,
&NotifyIcon::HandleClickEvent, win_icon_weak,
GetKeyboardModifiers(),
(lparam == WM_LBUTTONDOWN || lparam == WM_LBUTTONDBLCLK),
(lparam == WM_LBUTTONDBLCLK || lparam == WM_RBUTTONDBLCLK)));
(lparam == WM_LBUTTONDBLCLK || lparam == WM_RBUTTONDBLCLK),
(lparam == WM_MBUTTONDOWN || lparam == WM_MBUTTONDBLCLK)));

return TRUE;

Expand Down