tray_icon.h 3.9 KB

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