electron/shell/browser/ui/status_icon_gtk.cc
Cheng Zhao c303135b02
fix: fallback to GtkStatusIcon when app indicator is not supported (#36815)
* chore: get ready for multi backend tray

* fix: fallback to GtkStatusIcon when app indicator is not supported

* chore: use smart pointers
2023-01-26 19:15:55 +09:00

68 lines
2 KiB
C++

// Copyright (c) 2023 Microsoft, Inc.
// Copyright (c) 2011 The Chromium Authors.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/ui/status_icon_gtk.h"
#include <gtk/gtk.h>
#include "base/strings/utf_string_conversions.h"
#include "shell/browser/ui/gtk/menu_gtk.h"
#include "shell/browser/ui/gtk_util.h"
#include "ui/gfx/image/image_skia.h"
namespace electron {
StatusIconGtk::StatusIconGtk() : icon_(TakeGObject(gtk_status_icon_new())) {
g_signal_connect(icon_, "activate", G_CALLBACK(OnClickThunk), this);
g_signal_connect(icon_, "popup_menu", G_CALLBACK(OnContextMenuRequestedThunk),
this);
}
StatusIconGtk::~StatusIconGtk() = default;
void StatusIconGtk::SetIcon(const gfx::ImageSkia& image) {
if (image.isNull())
return;
GdkPixbuf* pixbuf = gtk_util::GdkPixbufFromSkBitmap(*image.bitmap());
gtk_status_icon_set_from_pixbuf(icon_, pixbuf);
g_object_unref(pixbuf);
}
void StatusIconGtk::SetToolTip(const std::u16string& tool_tip) {
gtk_status_icon_set_tooltip_text(icon_, base::UTF16ToUTF8(tool_tip).c_str());
}
void StatusIconGtk::UpdatePlatformContextMenu(ui::MenuModel* model) {
if (model)
menu_ = std::make_unique<gtkui::MenuGtk>(model);
}
void StatusIconGtk::RefreshPlatformContextMenu() {
if (menu_)
menu_->Refresh();
}
void StatusIconGtk::OnSetDelegate() {
SetIcon(delegate_->GetImage());
SetToolTip(delegate_->GetToolTip());
UpdatePlatformContextMenu(delegate_->GetMenuModel());
gtk_status_icon_set_visible(icon_, TRUE);
}
void StatusIconGtk::OnClick(GtkStatusIcon* status_icon) {
delegate_->OnClick();
}
void StatusIconGtk::OnContextMenuRequested(GtkStatusIcon* status_icon,
guint button,
guint32 activate_time) {
if (menu_.get()) {
gtk_menu_popup(menu_->GetGtkMenu(), nullptr, nullptr,
gtk_status_icon_position_menu, icon_, button, activate_time);
}
}
} // namespace electron