fix: reimplement Tray with StatusIconLinuxDbus on Linux (#36333)

This commit is contained in:
Cheng Zhao 2022-11-29 04:36:25 +09:00 committed by GitHub
parent bbb590b777
commit 16a7bd7102
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 126 additions and 948 deletions

View file

@ -4,43 +4,54 @@
#include "shell/browser/ui/tray_icon_gtk.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "shell/browser/browser.h"
#include "shell/browser/ui/gtk/status_icon.h"
#include "shell/common/application_info.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h"
#include "ui/gfx/image/image_skia_rep.h"
namespace electron {
TrayIconGtk::TrayIconGtk() = default;
namespace {
gfx::ImageSkia GetBestImageRep(const gfx::ImageSkia& image) {
image.EnsureRepsForSupportedScales();
float best_scale = 0.0f;
SkBitmap best_rep;
for (const auto& rep : image.image_reps()) {
if (rep.scale() > best_scale) {
best_scale = rep.scale();
best_rep = rep.GetBitmap();
}
}
// All status icon implementations want the image in pixel coordinates, so use
// a scale factor of 1.
return gfx::ImageSkia::CreateFromBitmap(best_rep, 1.0f);
}
} // namespace
TrayIconGtk::TrayIconGtk()
: status_icon_(new StatusIconLinuxDbus), status_icon_type_(kTypeDbus) {
status_icon_->SetDelegate(this);
}
TrayIconGtk::~TrayIconGtk() = default;
void TrayIconGtk::SetImage(const gfx::Image& image) {
image_ = image.AsImageSkia();
if (icon_) {
icon_->SetIcon(image_);
return;
}
tool_tip_ = base::UTF8ToUTF16(GetApplicationName());
icon_ = gtkui::CreateLinuxStatusIcon(image_, tool_tip_,
Browser::Get()->GetName().c_str());
icon_->SetDelegate(this);
image_ = GetBestImageRep(image.AsImageSkia());
if (status_icon_)
status_icon_->SetIcon(image_);
}
void TrayIconGtk::SetToolTip(const std::string& tool_tip) {
tool_tip_ = base::UTF8ToUTF16(tool_tip);
icon_->SetToolTip(tool_tip_);
if (status_icon_)
status_icon_->SetToolTip(tool_tip_);
}
void TrayIconGtk::SetContextMenu(ElectronMenuModel* menu_model) {
menu_model_ = menu_model;
icon_->UpdatePlatformContextMenu(menu_model_);
if (status_icon_)
status_icon_->UpdatePlatformContextMenu(menu_model_);
}
const gfx::ImageSkia& TrayIconGtk::GetImage() const {
@ -55,13 +66,24 @@ ui::MenuModel* TrayIconGtk::GetMenuModel() const {
return menu_model_;
}
void TrayIconGtk::OnImplInitializationFailed() {}
void TrayIconGtk::OnImplInitializationFailed() {
switch (status_icon_type_) {
case kTypeDbus:
status_icon_ = nullptr;
status_icon_type_ = kTypeNone;
return;
case kTypeNone:
NOTREACHED();
}
}
void TrayIconGtk::OnClick() {
NotifyClicked();
}
bool TrayIconGtk::HasClickAction() {
// Returning true will make the tooltip show as an additional context menu
// item, which makes sense in Chrome but not in most Electron apps.
return false;
}