Skip to content

Commit

Permalink
fix: reimplement Tray with StatusIconLinuxDbus on Linux (#36472)
Browse files Browse the repository at this point in the history
* fix: reimplement Tray with StatusIconLinuxDbus on Linux (#36333)

Co-authored-by: Cheng Zhao <zcbenz@gmail.com>

* chore: remove incorrectly added patches

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
Co-authored-by: Keeley Hammond <vertedinde@electronjs.org>
  • Loading branch information
3 people committed Nov 29, 2022
1 parent ccc5a7a commit 4989994
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 948 deletions.
8 changes: 0 additions & 8 deletions BUILD.gn
Expand Up @@ -631,16 +631,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
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.
* 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.
* 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.

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

Returns:
Expand Down
1 change: 1 addition & 0 deletions patches/chromium/.patches
Expand Up @@ -121,3 +121,4 @@ preconnect_manager.patch
fix_remove_caption-removing_style_call.patch
build_allow_electron_to_use_exec_script.patch
cherry-pick-7196a42b42ce.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
@@ -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

0 comments on commit 4989994

Please sign in to comment.