notify_icon.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 ATOM_BROWSER_UI_WIN_NOTIFY_ICON_H_
  5. #define ATOM_BROWSER_UI_WIN_NOTIFY_ICON_H_
  6. #include <windows.h> // windows.h must be included first
  7. #include <shellapi.h>
  8. #include <memory>
  9. #include <string>
  10. #include "atom/browser/ui/tray_icon.h"
  11. #include "base/compiler_specific.h"
  12. #include "base/macros.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/win/scoped_gdi_object.h"
  15. namespace gfx {
  16. class Point;
  17. }
  18. namespace views {
  19. class MenuRunner;
  20. class Widget;
  21. } // namespace views
  22. namespace atom {
  23. class NotifyIconHost;
  24. class NotifyIcon : public TrayIcon {
  25. public:
  26. // Constructor which provides this icon's unique ID and messaging window.
  27. NotifyIcon(NotifyIconHost* host, UINT id, HWND window, UINT message);
  28. ~NotifyIcon() override;
  29. // Handles a click event from the user - if |left_button_click| is true and
  30. // there is a registered observer, passes the click event to the observer,
  31. // otherwise displays the context menu if there is one.
  32. void HandleClickEvent(int modifiers,
  33. bool left_button_click,
  34. bool double_button_click);
  35. // Re-creates the status tray icon now after the taskbar has been created.
  36. void ResetIcon();
  37. UINT icon_id() const { return icon_id_; }
  38. HWND window() const { return window_; }
  39. UINT message_id() const { return message_id_; }
  40. // Overridden from TrayIcon:
  41. void SetImage(HICON image) override;
  42. void SetPressedImage(HICON image) override;
  43. void SetToolTip(const std::string& tool_tip) override;
  44. void DisplayBalloon(HICON icon,
  45. const base::string16& title,
  46. const base::string16& contents) override;
  47. void PopUpContextMenu(const gfx::Point& pos,
  48. AtomMenuModel* menu_model) override;
  49. void SetContextMenu(AtomMenuModel* menu_model) override;
  50. gfx::Rect GetBounds() override;
  51. private:
  52. void InitIconData(NOTIFYICONDATA* icon_data);
  53. void OnContextMenuClosed();
  54. // The tray that owns us. Weak.
  55. NotifyIconHost* host_;
  56. // The unique ID corresponding to this icon.
  57. UINT icon_id_;
  58. // Window used for processing messages from this icon.
  59. HWND window_;
  60. // The message identifier used for status icon messages.
  61. UINT message_id_;
  62. // The currently-displayed icon for the window.
  63. base::win::ScopedHICON icon_;
  64. // The context menu.
  65. AtomMenuModel* menu_model_ = nullptr;
  66. // Context menu associated with this icon (if any).
  67. std::unique_ptr<views::MenuRunner> menu_runner_;
  68. // Temporary widget for the context menu, needed for keyboard event capture.
  69. std::unique_ptr<views::Widget> widget_;
  70. // WeakPtrFactory for CloseClosure safety.
  71. base::WeakPtrFactory<NotifyIcon> weak_factory_;
  72. DISALLOW_COPY_AND_ASSIGN(NotifyIcon);
  73. };
  74. } // namespace atom
  75. #endif // ATOM_BROWSER_UI_WIN_NOTIFY_ICON_H_