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
|
|
|
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2022-11-28 19:36:25 +00:00
|
|
|
#include "chrome/browser/ui/views/status_icons/status_icon_linux_dbus.h"
|
|
|
|
#include "ui/gfx/image/image_skia_rep.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
|
|
|
|
2022-11-28 19:36:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-07-10 05:06:41 +00:00
|
|
|
|
2022-11-28 19:36:25 +00:00
|
|
|
} // namespace
|
2020-01-28 15:17:45 +00:00
|
|
|
|
2022-11-28 19:36:25 +00:00
|
|
|
TrayIconGtk::TrayIconGtk()
|
2022-12-05 08:59:20 +00:00
|
|
|
: status_icon_(new StatusIconLinuxDbus),
|
|
|
|
status_icon_type_(StatusIconType::kDbus) {
|
2022-11-28 19:36:25 +00:00
|
|
|
status_icon_->SetDelegate(this);
|
|
|
|
}
|
2014-07-10 05:06:41 +00:00
|
|
|
|
2022-11-28 19:36:25 +00:00
|
|
|
TrayIconGtk::~TrayIconGtk() = default;
|
2019-10-18 19:57:34 +00:00
|
|
|
|
2022-11-28 19:36:25 +00:00
|
|
|
void TrayIconGtk::SetImage(const gfx::Image& image) {
|
|
|
|
image_ = GetBestImageRep(image.AsImageSkia());
|
|
|
|
if (status_icon_)
|
|
|
|
status_icon_->SetIcon(image_);
|
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);
|
2022-11-28 19:36:25 +00:00
|
|
|
if (status_icon_)
|
|
|
|
status_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;
|
2022-11-28 19:36:25 +00:00
|
|
|
if (status_icon_)
|
|
|
|
status_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
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& 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
|
|
|
}
|
|
|
|
|
2022-11-28 19:36:25 +00:00
|
|
|
void TrayIconGtk::OnImplInitializationFailed() {
|
|
|
|
switch (status_icon_type_) {
|
2022-12-05 08:59:20 +00:00
|
|
|
case StatusIconType::kDbus:
|
2022-11-28 19:36:25 +00:00
|
|
|
status_icon_ = nullptr;
|
2022-12-05 08:59:20 +00:00
|
|
|
status_icon_type_ = StatusIconType::kNone;
|
2022-11-28 19:36:25 +00:00
|
|
|
return;
|
2022-12-05 08:59:20 +00:00
|
|
|
case StatusIconType::kNone:
|
2022-11-28 19:36:25 +00:00
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 01:22:09 +00:00
|
|
|
|
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() {
|
2022-11-28 19:36:25 +00:00
|
|
|
// Returning true will make the tooltip show as an additional context menu
|
|
|
|
// item, which makes sense in Chrome but not in most Electron apps.
|
2014-07-10 05:06:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:57:54 +00:00
|
|
|
// static
|
2021-06-03 08:05:04 +00:00
|
|
|
TrayIcon* TrayIcon::Create(absl::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
|