2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-05-30 02:31:27 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/tray_icon_gtk.h"
|
2014-07-10 05:06:41 +00:00
|
|
|
|
2016-06-20 08:35:45 +00:00
|
|
|
#include "base/strings/stringprintf.h"
|
2014-07-10 05:06:41 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2019-10-18 19:57:34 +00:00
|
|
|
#include "chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser.h"
|
|
|
|
#include "shell/common/application_info.h"
|
2015-01-03 03:17:02 +00:00
|
|
|
#include "ui/gfx/image/image.h"
|
2020-01-28 15:17:45 +00:00
|
|
|
#include "ui/gfx/image/image_skia_operations.h"
|
2014-05-30 02:31:27 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2014-05-30 02:31:27 +00:00
|
|
|
|
2020-01-28 15:17:45 +00:00
|
|
|
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<double>(size.height());
|
|
|
|
size = gfx::Size(static_cast<int>(ratio * size.width()),
|
|
|
|
static_cast<int>(ratio * size.height()));
|
|
|
|
icon = gfx::ImageSkiaOperations::CreateResizedImage(
|
|
|
|
icon, skia::ImageOperations::RESIZE_BEST, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
TrayIconGtk::TrayIconGtk() = default;
|
2014-07-10 05:06:41 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
TrayIconGtk::~TrayIconGtk() = default;
|
2014-07-10 05:06:41 +00:00
|
|
|
|
2015-01-03 02:43:56 +00:00
|
|
|
void TrayIconGtk::SetImage(const gfx::Image& image) {
|
2020-01-28 15:17:45 +00:00
|
|
|
image_ = GetIconFromImage(image);
|
|
|
|
|
2014-07-10 05:06:41 +00:00
|
|
|
if (icon_) {
|
2019-10-18 19:57:34 +00:00
|
|
|
icon_->SetIcon(image_);
|
2014-07-10 05:06:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-18 19:57:34 +00:00
|
|
|
tool_tip_ = base::UTF8ToUTF16(GetApplicationName());
|
|
|
|
|
|
|
|
icon_ = base::MakeRefCounted<StatusIconLinuxDbus>();
|
|
|
|
icon_->SetIcon(image_);
|
|
|
|
icon_->SetToolTip(tool_tip_);
|
2019-07-03 01:22:09 +00:00
|
|
|
icon_->SetDelegate(this);
|
2014-07-10 05:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIconGtk::SetToolTip(const std::string& tool_tip) {
|
2019-10-18 19:57:34 +00:00
|
|
|
tool_tip_ = base::UTF8ToUTF16(tool_tip);
|
|
|
|
icon_->SetToolTip(tool_tip_);
|
2014-07-10 05:06:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
void TrayIconGtk::SetContextMenu(ElectronMenuModel* menu_model) {
|
2019-10-18 19:57:34 +00:00
|
|
|
menu_model_ = menu_model;
|
2020-01-26 21:02:33 +00:00
|
|
|
icon_->UpdatePlatformContextMenu(menu_model_);
|
2014-07-10 05:06:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 01:22:09 +00:00
|
|
|
const gfx::ImageSkia& TrayIconGtk::GetImage() const {
|
2019-10-18 19:57:34 +00:00
|
|
|
return image_;
|
2019-07-03 01:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const base::string16& TrayIconGtk::GetToolTip() const {
|
2019-10-18 19:57:34 +00:00
|
|
|
return tool_tip_;
|
2019-07-03 01:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ui::MenuModel* TrayIconGtk::GetMenuModel() const {
|
2019-10-18 19:57:34 +00:00
|
|
|
return menu_model_;
|
2019-07-03 01:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIconGtk::OnImplInitializationFailed() {}
|
|
|
|
|
2014-07-10 05:06:41 +00:00
|
|
|
void TrayIconGtk::OnClick() {
|
2015-05-21 07:22:52 +00:00
|
|
|
NotifyClicked();
|
2014-07-10 05:06:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TrayIconGtk::HasClickAction() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:57:54 +00:00
|
|
|
// static
|
2020-01-31 05:37:03 +00:00
|
|
|
TrayIcon* TrayIcon::Create(base::Optional<UUID> guid) {
|
2014-07-10 05:06:41 +00:00
|
|
|
return new TrayIconGtk;
|
2014-05-30 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|