status_icon_gtk.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2023 Microsoft, Inc.
  2. // Copyright (c) 2011 The Chromium Authors.
  3. // Use of this source code is governed by the MIT license that can be
  4. // found in the LICENSE file.
  5. #include "shell/browser/ui/status_icon_gtk.h"
  6. #include <gtk/gtk.h>
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "shell/browser/ui/gtk/menu_gtk.h"
  9. #include "shell/browser/ui/gtk_util.h"
  10. #include "ui/gfx/image/image_skia.h"
  11. namespace electron {
  12. StatusIconGtk::StatusIconGtk() : icon_(TakeGObject(gtk_status_icon_new())) {
  13. g_signal_connect(icon_, "activate", G_CALLBACK(OnClickThunk), this);
  14. g_signal_connect(icon_, "popup_menu", G_CALLBACK(OnContextMenuRequestedThunk),
  15. this);
  16. }
  17. StatusIconGtk::~StatusIconGtk() = default;
  18. void StatusIconGtk::SetIcon(const gfx::ImageSkia& image) {
  19. if (image.isNull())
  20. return;
  21. GdkPixbuf* pixbuf = gtk_util::GdkPixbufFromSkBitmap(*image.bitmap());
  22. gtk_status_icon_set_from_pixbuf(icon_, pixbuf);
  23. g_object_unref(pixbuf);
  24. }
  25. void StatusIconGtk::SetToolTip(const std::u16string& tool_tip) {
  26. gtk_status_icon_set_tooltip_text(icon_, base::UTF16ToUTF8(tool_tip).c_str());
  27. }
  28. void StatusIconGtk::UpdatePlatformContextMenu(ui::MenuModel* model) {
  29. if (model)
  30. menu_ = std::make_unique<gtkui::MenuGtk>(model);
  31. }
  32. void StatusIconGtk::RefreshPlatformContextMenu() {
  33. if (menu_)
  34. menu_->Refresh();
  35. }
  36. void StatusIconGtk::OnSetDelegate() {
  37. SetIcon(delegate_->GetImage());
  38. SetToolTip(delegate_->GetToolTip());
  39. UpdatePlatformContextMenu(delegate_->GetMenuModel());
  40. gtk_status_icon_set_visible(icon_, TRUE);
  41. }
  42. void StatusIconGtk::OnClick(GtkStatusIcon* status_icon) {
  43. delegate_->OnClick();
  44. }
  45. void StatusIconGtk::OnContextMenuRequested(GtkStatusIcon* status_icon,
  46. guint button,
  47. guint32 activate_time) {
  48. if (menu_.get()) {
  49. gtk_menu_popup(menu_->GetGtkMenu(), nullptr, nullptr,
  50. gtk_status_icon_position_menu, icon_, button, activate_time);
  51. }
  52. }
  53. } // namespace electron