tray_icon.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_UI_TRAY_ICON_H_
  5. #define ELECTRON_SHELL_BROWSER_UI_TRAY_ICON_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/observer_list.h"
  9. #include "shell/browser/ui/electron_menu_model.h"
  10. #include "shell/browser/ui/tray_icon_observer.h"
  11. #include "shell/common/gin_converters/guid_converter.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. namespace electron {
  14. class TrayIcon {
  15. public:
  16. static TrayIcon* Create(std::optional<UUID> guid);
  17. #if BUILDFLAG(IS_WIN)
  18. using ImageType = HICON;
  19. #else
  20. using ImageType = const gfx::Image&;
  21. #endif
  22. virtual ~TrayIcon();
  23. // disable copy
  24. TrayIcon(const TrayIcon&) = delete;
  25. TrayIcon& operator=(const TrayIcon&) = delete;
  26. // Sets the image associated with this status icon.
  27. virtual void SetImage(ImageType image) = 0;
  28. // Sets the image associated with this status icon when pressed.
  29. virtual void SetPressedImage(ImageType image);
  30. // Sets the hover text for this status icon. This is also used as the label
  31. // for the menu item which is created as a replacement for the status icon
  32. // click action on platforms that do not support custom click actions for the
  33. // status icon (e.g. Ubuntu Unity).
  34. virtual void SetToolTip(const std::string& tool_tip) = 0;
  35. #if BUILDFLAG(IS_MAC)
  36. // Set/Get flag determining whether to ignore double click events.
  37. virtual void SetIgnoreDoubleClickEvents(bool ignore) = 0;
  38. virtual bool GetIgnoreDoubleClickEvents() = 0;
  39. struct TitleOptions {
  40. std::string font_type;
  41. };
  42. // Set/Get title displayed next to status icon in the status bar.
  43. virtual void SetTitle(const std::string& title,
  44. const TitleOptions& options) = 0;
  45. virtual std::string GetTitle() = 0;
  46. #endif
  47. enum class IconType { kNone, kInfo, kWarning, kError, kCustom };
  48. struct BalloonOptions {
  49. IconType icon_type = IconType::kCustom;
  50. #if BUILDFLAG(IS_WIN)
  51. HICON icon = nullptr;
  52. #else
  53. gfx::Image icon;
  54. #endif
  55. std::u16string title;
  56. std::u16string content;
  57. bool large_icon = true;
  58. bool no_sound = false;
  59. bool respect_quiet_time = false;
  60. BalloonOptions();
  61. };
  62. // Displays a notification balloon with the specified contents.
  63. // Depending on the platform it might not appear by the icon tray.
  64. virtual void DisplayBalloon(const BalloonOptions& options);
  65. // Removes the notification balloon.
  66. virtual void RemoveBalloon();
  67. // Returns focus to the taskbar notification area.
  68. virtual void Focus();
  69. // Popups the menu.
  70. virtual void PopUpContextMenu(const gfx::Point& pos,
  71. base::WeakPtr<ElectronMenuModel> menu_model);
  72. virtual void CloseContextMenu();
  73. // Set the context menu for this icon.
  74. virtual void SetContextMenu(raw_ptr<ElectronMenuModel> menu_model) = 0;
  75. // Returns the bounds of tray icon.
  76. virtual gfx::Rect GetBounds();
  77. void AddObserver(TrayIconObserver* obs) { observers_.AddObserver(obs); }
  78. void RemoveObserver(TrayIconObserver* obs) { observers_.RemoveObserver(obs); }
  79. void NotifyClicked(const gfx::Rect& = gfx::Rect(),
  80. const gfx::Point& location = gfx::Point(),
  81. int modifiers = 0);
  82. void NotifyDoubleClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0);
  83. void NotifyMiddleClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0);
  84. void NotifyBalloonShow();
  85. void NotifyBalloonClicked();
  86. void NotifyBalloonClosed();
  87. void NotifyRightClicked(const gfx::Rect& bounds = gfx::Rect(),
  88. int modifiers = 0);
  89. void NotifyDrop();
  90. void NotifyDropFiles(const std::vector<std::string>& files);
  91. void NotifyDropText(const std::string& text);
  92. void NotifyDragEntered();
  93. void NotifyDragExited();
  94. void NotifyDragEnded();
  95. void NotifyMouseUp(const gfx::Point& location = gfx::Point(),
  96. int modifiers = 0);
  97. void NotifyMouseDown(const gfx::Point& location = gfx::Point(),
  98. int modifiers = 0);
  99. void NotifyMouseEntered(const gfx::Point& location = gfx::Point(),
  100. int modifiers = 0);
  101. void NotifyMouseExited(const gfx::Point& location = gfx::Point(),
  102. int modifiers = 0);
  103. void NotifyMouseMoved(const gfx::Point& location = gfx::Point(),
  104. int modifiers = 0);
  105. protected:
  106. TrayIcon();
  107. private:
  108. base::ObserverList<TrayIconObserver> observers_;
  109. };
  110. } // namespace electron
  111. #endif // ELECTRON_SHELL_BROWSER_UI_TRAY_ICON_H_