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

fix: reimplement Tray with StatusIconLinuxDbus on Linux #36333

Merged
merged 4 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 0 additions & 8 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -632,16 +632,8 @@ source_set("electron_lib") {
sources += [
"shell/browser/certificate_manager_model.cc",
"shell/browser/certificate_manager_model.h",
"shell/browser/ui/gtk/app_indicator_icon.cc",
"shell/browser/ui/gtk/app_indicator_icon.h",
"shell/browser/ui/gtk/app_indicator_icon_menu.cc",
"shell/browser/ui/gtk/app_indicator_icon_menu.h",
"shell/browser/ui/gtk/gtk_status_icon.cc",
"shell/browser/ui/gtk/gtk_status_icon.h",
"shell/browser/ui/gtk/menu_util.cc",
"shell/browser/ui/gtk/menu_util.h",
"shell/browser/ui/gtk/status_icon.cc",
"shell/browser/ui/gtk/status_icon.h",
"shell/browser/ui/gtk_util.cc",
"shell/browser/ui/gtk_util.h",
]
Expand Down
18 changes: 9 additions & 9 deletions docs/api/tray.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@ app.whenReady().then(() => {

__Platform Considerations__

If you want to keep exact same behaviors on all platforms, you should not
rely on the `click` event; instead, always attach a context menu to the tray icon.

__Linux__

* On Linux distributions that only have app indicator support, you have to
install `libappindicator1` to make the tray icon work.
* The app indicator will be used if it is supported, otherwise
`GtkStatusIcon` will be used instead.
* App indicator will only be shown when it has a context menu.
* The `click` event is ignored when using the app indicator.
* Tray icon requires support of [StatusNotifierItem](https://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/)
in user's desktop environment.
Comment on lines +32 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Tray icon requires support of [StatusNotifierItem](https://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/)
in user's desktop environment.
* The user's desktop environment must support [StatusNotifierItem](https://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/)
in order to display a tray icon.

* The `click` event is emitted when the tray icon receives activation from
user, however the StatusNotifierItem spec does not specify which action would
cause an activation, for some environments it is left mouse click, but for
some it might be double left mouse click.
Comment on lines +34 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The `click` event is emitted when the tray icon receives activation from
user, however the StatusNotifierItem spec does not specify which action would
cause an activation, for some environments it is left mouse click, but for
some it might be double left mouse click.
* The `click` event is emitted when the tray icon receives activation from the
user. However, the StatusNotifierItem spec does not specify which interactions
cause an activation. For some environments, a left mouse click causes an
activation, but for others it might be double left mouse click, or some other
interaction.

* In order for changes made to individual `MenuItem`s to take effect,
you have to call `setContextMenu` again. For example:

Expand Down Expand Up @@ -92,6 +89,9 @@ Returns:

Emitted when the tray icon is clicked.

Note that on Linux this event is emitted when the tray icon receives an
activation, which might not necessarily be left mouse click.
Comment on lines +92 to +93
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Note that on Linux this event is emitted when the tray icon receives an
activation, which might not necessarily be left mouse click.
Note that on Linux this event is emitted when the tray icon receives an
activation, which might not necessarily be a left mouse click.


#### Event: 'right-click' _macOS_ _Windows_

Returns:
Expand Down
1 change: 1 addition & 0 deletions patches/chromium/.patches
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ build_allow_electron_to_use_exec_script.patch
revert_use_accessibility_pkey_when_setting_page_access.patch
roll_clang_llvmorg-16-init-8189-g97196a2d-2.patch
build_only_use_the_mas_build_config_in_the_required_components.patch
fix_tray_icon_gone_on_lock_screen.patch
61 changes: 61 additions & 0 deletions patches/chromium/fix_tray_icon_gone_on_lock_screen.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Tue, 15 Nov 2022 09:38:25 +0900
Subject: Re-register status item when owner of status watcher is changed

https://chromium-review.googlesource.com/c/chromium/src/+/4022621

diff --git a/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.cc b/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.cc
index f3c9dfa9ca33496a9c45cd0c780d3d629aeb4663..387b59a1015b51690810b90a4ac65df862b337f3 100644
--- a/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.cc
+++ b/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.cc
@@ -381,6 +381,13 @@ void StatusIconLinuxDbus::OnInitialized(bool success) {
return;
}

+ watcher_->SetNameOwnerChangedCallback(
+ base::BindRepeating(&StatusIconLinuxDbus::NameOwnerChangedReceived,
+ weak_factory_.GetWeakPtr()));
+ RegisterStatusNotifierItem();
+}
+
+void StatusIconLinuxDbus::RegisterStatusNotifierItem() {
dbus::MethodCall method_call(kInterfaceStatusNotifierWatcher,
kMethodRegisterStatusNotifierItem);
dbus::MessageWriter writer(&method_call);
@@ -396,6 +403,14 @@ void StatusIconLinuxDbus::OnRegistered(dbus::Response* response) {
delegate_->OnImplInitializationFailed();
}

+void StatusIconLinuxDbus::NameOwnerChangedReceived(
+ const std::string& old_owner,
+ const std::string& new_owner) {
+ // Re-register the item when the StatusNotifierWatcher has a new owner.
+ if (!new_owner.empty())
+ RegisterStatusNotifierItem();
+}
+
void StatusIconLinuxDbus::OnActivate(
dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender sender) {
diff --git a/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h b/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h
index e7628de42f980fa3535cab9dfffd0deab30f8812..eae1c332a0972aefb8843cac947aeb2f4c48d360 100644
--- a/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h
+++ b/chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h
@@ -74,10 +74,16 @@ class StatusIconLinuxDbus : public ui::StatusIconLinux,
const std::string& method_name,
bool success);
void OnInitialized(bool success);
+ void RegisterStatusNotifierItem();

// Step 5: register the StatusNotifierItem with the StatusNotifierWatcher.
void OnRegistered(dbus::Response* response);

+ // Called when the name owner of StatusNotifierWatcher has changed, which
+ // can happen when lock/unlock screen.
+ void NameOwnerChangedReceived(const std::string& old_owner,
+ const std::string& new_owner);
+
// DBus methods.
// Action -> KDE behavior:
// Left-click -> Activate