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.
5 #include "atom/browser/ui/win/notify_icon.h"
7 #include "atom/browser/ui/win/notify_icon_host.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/win/windows_version.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "ui/display/screen.h"
13 #include "ui/display/win/screen_win.h"
14 #include "ui/gfx/geometry/point.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/views/controls/menu/menu_runner.h"
21 NotifyIcon::NotifyIcon(NotifyIconHost* host,
30 NOTIFYICONDATA icon_data;
31 InitIconData(&icon_data);
32 icon_data.uFlags |= NIF_MESSAGE;
33 icon_data.uCallbackMessage = message_id_;
34 BOOL result = Shell_NotifyIcon(NIM_ADD, &icon_data);
35 // This can happen if the explorer process isn't running when we try to
36 // create the icon for some reason (for example, at startup).
38 LOG(WARNING) << "Unable to create status tray icon.";
41 NotifyIcon::~NotifyIcon() {
44 NOTIFYICONDATA icon_data;
45 InitIconData(&icon_data);
46 Shell_NotifyIcon(NIM_DELETE, &icon_data);
49 void NotifyIcon::HandleClickEvent(int modifiers,
50 bool left_mouse_click,
51 bool double_button_click) {
52 gfx::Rect bounds = GetBounds();
54 if (left_mouse_click) {
55 if (double_button_click) // double left click
56 NotifyDoubleClicked(bounds, modifiers);
57 else // single left click
58 NotifyClicked(bounds, modifiers);
60 } else if (!double_button_click) { // single right click
62 PopUpContextMenu(gfx::Point(), menu_model_);
64 NotifyRightClicked(bounds, modifiers);
68 void NotifyIcon::ResetIcon() {
69 NOTIFYICONDATA icon_data;
70 InitIconData(&icon_data);
71 // Delete any previously existing icon.
72 Shell_NotifyIcon(NIM_DELETE, &icon_data);
73 InitIconData(&icon_data);
74 icon_data.uFlags |= NIF_MESSAGE;
75 icon_data.uCallbackMessage = message_id_;
76 icon_data.hIcon = icon_.get();
77 // If we have an image, then set the NIF_ICON flag, which tells
78 // Shell_NotifyIcon() to set the image for the status icon it creates.
80 icon_data.uFlags |= NIF_ICON;
82 BOOL result = Shell_NotifyIcon(NIM_ADD, &icon_data);
84 LOG(WARNING) << "Unable to re-create status tray icon.";
87 void NotifyIcon::SetImage(HICON image) {
88 icon_ = base::win::ScopedHICON(CopyIcon(image));
91 NOTIFYICONDATA icon_data;
92 InitIconData(&icon_data);
93 icon_data.uFlags |= NIF_ICON;
94 icon_data.hIcon = image;
95 BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data);
97 LOG(WARNING) << "Error setting status tray icon image";
100 void NotifyIcon::SetPressedImage(HICON image) {
101 // Ignore pressed images, since the standard on Windows is to not highlight
102 // pressed status icons.
105 void NotifyIcon::SetToolTip(const std::string& tool_tip) {
107 NOTIFYICONDATA icon_data;
108 InitIconData(&icon_data);
109 icon_data.uFlags |= NIF_TIP;
110 wcsncpy_s(icon_data.szTip, base::UTF8ToUTF16(tool_tip).c_str(), _TRUNCATE);
111 BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data);
113 LOG(WARNING) << "Unable to set tooltip for status tray icon";
116 void NotifyIcon::DisplayBalloon(HICON icon,
117 const base::string16& title,
118 const base::string16& contents) {
119 NOTIFYICONDATA icon_data;
120 InitIconData(&icon_data);
121 icon_data.uFlags |= NIF_INFO;
122 icon_data.dwInfoFlags = NIIF_INFO;
123 wcsncpy_s(icon_data.szInfoTitle, title.c_str(), _TRUNCATE);
124 wcsncpy_s(icon_data.szInfo, contents.c_str(), _TRUNCATE);
125 icon_data.uTimeout = 0;
126 icon_data.hBalloonIcon = icon;
127 icon_data.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON;
129 BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data);
131 LOG(WARNING) << "Unable to create status tray balloon.";
134 void NotifyIcon::PopUpContextMenu(const gfx::Point& pos,
135 AtomMenuModel* menu_model) {
136 // Returns if context menu isn't set.
137 if (menu_model == nullptr && menu_model_ == nullptr)
140 // Set our window as the foreground window, so the context menu closes when
141 // we click away from it.
142 if (!SetForegroundWindow(window_))
145 // Show menu at mouse's position by default.
146 gfx::Rect rect(pos, gfx::Size());
148 rect.set_origin(display::Screen::GetScreen()->GetCursorScreenPoint());
150 views::MenuRunner menu_runner(
151 menu_model != nullptr ? menu_model : menu_model_,
152 views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS);
153 ignore_result(menu_runner.RunMenuAt(
154 NULL, NULL, rect, views::MENU_ANCHOR_TOPLEFT, ui::MENU_SOURCE_MOUSE));
157 void NotifyIcon::SetContextMenu(AtomMenuModel* menu_model) {
158 menu_model_ = menu_model;
161 gfx::Rect NotifyIcon::GetBounds() {
162 NOTIFYICONIDENTIFIER icon_id;
163 memset(&icon_id, 0, sizeof(NOTIFYICONIDENTIFIER));
164 icon_id.uID = icon_id_;
165 icon_id.hWnd = window_;
166 icon_id.cbSize = sizeof(NOTIFYICONIDENTIFIER);
169 Shell_NotifyIconGetRect(&icon_id, &rect);
170 return display::win::ScreenWin::ScreenToDIPRect(window_, gfx::Rect(rect));
173 void NotifyIcon::InitIconData(NOTIFYICONDATA* icon_data) {
174 memset(icon_data, 0, sizeof(NOTIFYICONDATA));
175 icon_data->cbSize = sizeof(NOTIFYICONDATA);
176 icon_data->hWnd = window_;
177 icon_data->uID = icon_id_;