Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_H_ |
| 6 | #define CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_H_ |
| 7 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 8 | #include <memory> |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 9 | #include <string> |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 10 | |
[email protected] | 61ee6287 | 2010-03-31 16:08:56 | [diff] [blame] | 11 | #include "base/observer_list.h" |
avi | 664c07b | 2015-12-26 02:18:31 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | f528059f | 2013-08-28 08:23:59 | [diff] [blame] | 13 | #include "chrome/browser/status_icons/status_icon_menu_model.h" |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 14 | |
[email protected] | 7f1f176 | 2012-10-03 15:51:02 | [diff] [blame] | 15 | namespace gfx { |
| 16 | class ImageSkia; |
Tom Anderson | 513e016 | 2025-01-09 04:09:44 | [diff] [blame] | 17 | struct VectorIcon; |
[email protected] | 7f1f176 | 2012-10-03 15:51:02 | [diff] [blame] | 18 | } |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 19 | |
johnme | 9295617 | 2015-10-15 16:54:12 | [diff] [blame] | 20 | namespace message_center { |
| 21 | struct NotifierId; |
| 22 | } |
| 23 | |
[email protected] | 8a7923f | 2012-05-23 14:51:18 | [diff] [blame] | 24 | class StatusIconObserver; |
| 25 | |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 26 | class StatusIcon { |
| 27 | public: |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 28 | StatusIcon(); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 29 | |
| 30 | StatusIcon(const StatusIcon&) = delete; |
| 31 | StatusIcon& operator=(const StatusIcon&) = delete; |
| 32 | |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 33 | virtual ~StatusIcon(); |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 34 | |
| 35 | // Sets the image associated with this status icon. |
[email protected] | 7f1f176 | 2012-10-03 15:51:02 | [diff] [blame] | 36 | virtual void SetImage(const gfx::ImageSkia& image) = 0; |
Tom Anderson | 513e016 | 2025-01-09 04:09:44 | [diff] [blame] | 37 | virtual void SetIcon(const gfx::VectorIcon& icon); |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 38 | |
[email protected] | b6c510aa | 2013-07-31 23:36:14 | [diff] [blame] | 39 | // Sets the hover text for this status icon. This is also used as the label |
| 40 | // for the menu item which is created as a replacement for the status icon |
| 41 | // click action on platforms that do not support custom click actions for the |
| 42 | // status icon (e.g. Ubuntu Unity). |
Jan Wilken Dörrie | dec9912 | 2021-03-11 18:02:30 | [diff] [blame] | 43 | virtual void SetToolTip(const std::u16string& tool_tip) = 0; |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 44 | |
[email protected] | 6f9eac9 | 2011-03-06 17:49:02 | [diff] [blame] | 45 | // Displays a notification balloon with the specified contents. |
[email protected] | a25fd45 | 2011-10-26 10:45:08 | [diff] [blame] | 46 | // Depending on the platform it might not appear by the icon tray. |
johnme | 9295617 | 2015-10-15 16:54:12 | [diff] [blame] | 47 | virtual void DisplayBalloon( |
| 48 | const gfx::ImageSkia& icon, |
Jan Wilken Dörrie | dec9912 | 2021-03-11 18:02:30 | [diff] [blame] | 49 | const std::u16string& title, |
| 50 | const std::u16string& contents, |
johnme | 9295617 | 2015-10-15 16:54:12 | [diff] [blame] | 51 | const message_center::NotifierId& notifier_id) = 0; |
[email protected] | 6f9eac9 | 2011-03-06 17:49:02 | [diff] [blame] | 52 | |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 53 | // Set the context menu for this icon. The icon takes ownership of the passed |
| 54 | // context menu. Passing NULL results in no menu at all. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 55 | void SetContextMenu(std::unique_ptr<StatusIconMenuModel> menu); |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 56 | |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 57 | // Adds/Removes an observer for clicks on the status icon. If an observer is |
| 58 | // registered, then left clicks on the status icon will result in the observer |
| 59 | // being called, otherwise, both left and right clicks will display the |
| 60 | // context menu (if any). |
[email protected] | 8a7923f | 2012-05-23 14:51:18 | [diff] [blame] | 61 | void AddObserver(StatusIconObserver* observer); |
| 62 | void RemoveObserver(StatusIconObserver* observer); |
[email protected] | a24642a | 2010-03-24 21:29:05 | [diff] [blame] | 63 | |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 64 | // Returns true if there are registered click observers. |
[email protected] | 8a7923f | 2012-05-23 14:51:18 | [diff] [blame] | 65 | bool HasObservers() const; |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 66 | |
[email protected] | a24642a | 2010-03-24 21:29:05 | [diff] [blame] | 67 | // Dispatches a click event to the observers. |
| 68 | void DispatchClickEvent(); |
Xiaohan Wang | 1c69fe6 | 2022-01-16 01:35:51 | [diff] [blame] | 69 | #if BUILDFLAG(IS_WIN) |
[email protected] | 963b13c | 2013-06-22 14:10:41 | [diff] [blame] | 70 | void DispatchBalloonClickEvent(); |
| 71 | #endif |
[email protected] | a24642a | 2010-03-24 21:29:05 | [diff] [blame] | 72 | |
[email protected] | caea05f4 | 2014-04-25 02:34:58 | [diff] [blame] | 73 | // Attempts to make the status icon directly visible on system UI. Currently |
| 74 | // this only applies to Windows, where status icons are hidden by default |
| 75 | // inside an overflow window. |
| 76 | // WARNING: This currently uses undocumented Windows APIs and spawns a worker |
| 77 | // thread to do it. Use sparingly. |
| 78 | virtual void ForceVisible(); |
| 79 | |
Charles Meng | f071d066 | 2024-12-17 00:36:36 | [diff] [blame] | 80 | #if BUILDFLAG(IS_MAC) |
| 81 | // On mac, if there is a menu, by default primary click will open it and not |
| 82 | // call DispatchClickEvent(). Use this function to make the menu open on |
| 83 | // secondary click, and dispatch the click event on left click. |
| 84 | virtual void SetOpenMenuWithSecondaryClick( |
| 85 | bool open_menu_with_secondary_click); |
Charles Meng | 8af381b | 2025-01-13 21:06:58 | [diff] [blame] | 86 | |
| 87 | // Use template property on the status icon image so that it changes color |
| 88 | // based on contrast with the wallpaper. |
| 89 | virtual void SetImageTemplate(bool is_template); |
Charles Meng | f071d066 | 2024-12-17 00:36:36 | [diff] [blame] | 90 | #endif |
| 91 | |
Alison Gale | 5fd20b9a | 2025-01-28 22:29:24 | [diff] [blame] | 92 | StatusIconMenuModel* GetContextMenuForTesting(); |
| 93 | |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 94 | protected: |
| 95 | // Invoked after a call to SetContextMenu() to let the platform-specific |
| 96 | // subclass update the native context menu based on the new model. If NULL is |
| 97 | // passed, subclass should destroy the native context menu. |
[email protected] | f528059f | 2013-08-28 08:23:59 | [diff] [blame] | 98 | virtual void UpdatePlatformContextMenu(StatusIconMenuModel* model) = 0; |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 99 | |
[email protected] | a24642a | 2010-03-24 21:29:05 | [diff] [blame] | 100 | private: |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 101 | base::ObserverList<StatusIconObserver>::Unchecked observers_; |
[email protected] | 8a7923f | 2012-05-23 14:51:18 | [diff] [blame] | 102 | |
[email protected] | 9979f5d | 2010-08-20 04:52:44 | [diff] [blame] | 103 | // Context menu, if any. |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 104 | std::unique_ptr<StatusIconMenuModel> context_menu_contents_; |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 105 | }; |
| 106 | |
[email protected] | ccb55cf5 | 2010-03-06 22:02:04 | [diff] [blame] | 107 | #endif // CHROME_BROWSER_STATUS_ICONS_STATUS_ICON_H_ |