From 5f307381d2548a1567a16924cadd6cbac4145e33 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 28 Jan 2020 09:17:45 -0600 Subject: [PATCH] fix: ensure tray icon is the proper size on linux (#21904) * fix: ensure tray icon is the proper size on linux Fixes #21445. * chore: use correct include order * chore: use correct include order * chore: remove use of deprecated casting style * chore: be more explicit about the 22 pixel height * chore: remove now-unneeded #include statements * chore: fix typo and bugs.chromium.org link * refactor: put GetIconFromImage() in anon namespace --- shell/browser/ui/tray_icon_gtk.cc | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/shell/browser/ui/tray_icon_gtk.cc b/shell/browser/ui/tray_icon_gtk.cc index 6baa35e7932f..b66a6299c382 100644 --- a/shell/browser/ui/tray_icon_gtk.cc +++ b/shell/browser/ui/tray_icon_gtk.cc @@ -10,15 +10,41 @@ #include "shell/browser/browser.h" #include "shell/common/application_info.h" #include "ui/gfx/image/image.h" +#include "ui/gfx/image/image_skia_operations.h" namespace electron { +namespace { + +gfx::ImageSkia GetIconFromImage(const gfx::Image& image) { + auto icon = image.AsImageSkia(); + auto size = icon.size(); + + // Systray icons are historically 22 pixels tall, e.g. on Ubuntu GNOME, + // KDE, and xfce. Taller icons are causing incorrect sizing issues -- e.g. + // a 1x1 icon -- so for now, pin the height manually. Similar behavior to + // https://bugs.chromium.org/p/chromium/issues/detail?id=1042098 ? + static constexpr int DESIRED_HEIGHT = 22; + if ((size.height() != 0) && (size.height() != DESIRED_HEIGHT)) { + const double ratio = DESIRED_HEIGHT / static_cast(size.height()); + size = gfx::Size(static_cast(ratio * size.width()), + static_cast(ratio * size.height())); + icon = gfx::ImageSkiaOperations::CreateResizedImage( + icon, skia::ImageOperations::RESIZE_BEST, size); + } + + return icon; +} + +} // namespace + TrayIconGtk::TrayIconGtk() = default; TrayIconGtk::~TrayIconGtk() = default; void TrayIconGtk::SetImage(const gfx::Image& image) { - image_ = image.AsImageSkia(); + image_ = GetIconFromImage(image); + if (icon_) { icon_->SetIcon(image_); return;