tray_icon.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 SHELL_BROWSER_UI_TRAY_ICON_H_
  5. #define 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(base::Optional<UUID> guid);
  17. #if defined(OS_WIN)
  18. using ImageType = HICON;
  19. #else
  20. using ImageType = const gfx::Image&;
  21. #endif
  22. virtual ~TrayIcon();
  23. // Sets the image associated with this status icon.
  24. virtual void SetImage(ImageType image) = 0;
  25. // Sets the image associated with this status icon when pressed.
  26. virtual void SetPressedImage(ImageType image);
  27. // Sets the hover text for this status icon. This is also used as the label
  28. // for the menu item which is created as a replacement for the status icon
  29. // click action on platforms that do not support custom click actions for the
  30. // status icon (e.g. Ubuntu Unity).
  31. virtual void SetToolTip(const std::string& tool_tip) = 0;
  32. #if defined(OS_MAC)
  33. // Set/Get flag determining whether to ignore double click events.
  34. virtual void SetIgnoreDoubleClickEvents(bool ignore) = 0;
  35. virtual bool GetIgnoreDoubleClickEvents() = 0;
  36. struct TitleOptions {
  37. std::string font_type;
  38. };
  39. // Set/Get title displayed next to status icon in the status bar.
  40. virtual void SetTitle(const std::string& title,
  41. const TitleOptions& options) = 0;
  42. virtual std::string GetTitle() = 0;
  43. #endif
  44. enum class IconType { kNone, kInfo, kWarning, kError, kCustom };
  45. struct BalloonOptions {
  46. IconType icon_type = IconType::kCustom;
  47. #if defined(OS_WIN)
  48. HICON icon = nullptr;
  49. #else
  50. gfx::Image icon;
  51. #endif
  52. base::string16 title;
  53. base::string16 content;
  54. bool large_icon = true;
  55. bool no_sound = false;
  56. bool respect_quiet_time = false;
  57. BalloonOptions();
  58. };
  59. // Displays a notification balloon with the specified contents.
  60. // Depending on the platform it might not appear by the icon tray.
  61. virtual void DisplayBalloon(const BalloonOptions& options);
  62. // Removes the notification balloon.
  63. virtual void RemoveBalloon();
  64. // Returns focus to the taskbar notification area.
  65. virtual void Focus();
  66. // Popups the menu.
  67. virtual void PopUpContextMenu(const gfx::Point& pos,
  68. ElectronMenuModel* menu_model);
  69. virtual void CloseContextMenu();
  70. // Set the context menu for this icon.
  71. virtual void SetContextMenu(ElectronMenuModel* menu_model) = 0;
  72. // Returns the bounds of tray icon.
  73. virtual gfx::Rect GetBounds();
  74. void AddObserver(TrayIconObserver* obs) { observers_.AddObserver(obs); }
  75. void RemoveObserver(TrayIconObserver* obs) { observers_.RemoveObserver(obs); }
  76. void NotifyClicked(const gfx::Rect& = gfx::Rect(),
  77. const gfx::Point& location = gfx::Point(),
  78. int modifiers = 0);
  79. void NotifyDoubleClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0);
  80. void NotifyBalloonShow();
  81. void NotifyBalloonClicked();
  82. void NotifyBalloonClosed();
  83. void NotifyRightClicked(const gfx::Rect& bounds = gfx::Rect(),
  84. int modifiers = 0);
  85. void NotifyDrop();
  86. void NotifyDropFiles(const std::vector<std::string>& files);
  87. void NotifyDropText(const std::string& text);
  88. void NotifyDragEntered();
  89. void NotifyDragExited();
  90. void NotifyDragEnded();
  91. void NotifyMouseUp(const gfx::Point& location = gfx::Point(),
  92. int modifiers = 0);
  93. void NotifyMouseDown(const gfx::Point& location = gfx::Point(),
  94. int modifiers = 0);
  95. void NotifyMouseEntered(const gfx::Point& location = gfx::Point(),
  96. int modifiers = 0);
  97. void NotifyMouseExited(const gfx::Point& location = gfx::Point(),
  98. int modifiers = 0);
  99. void NotifyMouseMoved(const gfx::Point& location = gfx::Point(),
  100. int modifiers = 0);
  101. protected:
  102. TrayIcon();
  103. private:
  104. base::ObserverList<TrayIconObserver> observers_;
  105. DISALLOW_COPY_AND_ASSIGN(TrayIcon);
  106. };
  107. } // namespace electron
  108. #endif // SHELL_BROWSER_UI_TRAY_ICON_H_