1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/message_center/web_notification_tray.h"
7 #include "base/i18n/number_formatting.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/status_icons/status_icon.h"
12 #include "chrome/browser/status_icons/status_icon_menu_model.h"
13 #include "chrome/browser/status_icons/status_tray.h"
14 #include "content/public/browser/notification_service.h"
15 #include "grit/chromium_strings.h"
16 #include "grit/theme_resources.h"
17 #include "grit/ui_strings.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/image/image_skia_operations.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/gfx/size.h"
25 #include "ui/message_center/message_center_tray.h"
26 #include "ui/message_center/message_center_tray_delegate.h"
27 #include "ui/message_center/views/message_popup_collection.h"
28 #include "ui/views/widget/widget.h"
33 const int kScreenEdgePadding = 2;
35 // Number of pixels the message center is offset from the mouse.
36 const int kMouseOffset = 5;
39 const int kToggleQuietMode = 0;
40 const int kEnableQuietModeHour = 1;
41 const int kEnableQuietModeDay = 2;
43 gfx::ImageSkia* GetIcon(int unread_count, bool is_quiet_mode) {
44 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
45 int resource_id = IDR_NOTIFICATION_TRAY_EMPTY;
49 resource_id = IDR_NOTIFICATION_TRAY_DO_NOT_DISTURB_ATTENTION;
51 resource_id = IDR_NOTIFICATION_TRAY_ATTENTION;
52 } else if (is_quiet_mode) {
53 resource_id = IDR_NOTIFICATION_TRAY_DO_NOT_DISTURB_EMPTY;
56 return rb.GetImageSkiaNamed(resource_id);
61 namespace message_center {
65 // Gets the position of the taskbar from the work area bounds. Returns
66 // ALIGNMENT_NONE if position cannot be found.
67 Alignment GetTaskbarAlignment() {
68 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
69 // TODO(dewittj): It's possible GetPrimaryDisplay is wrong.
70 gfx::Rect screen_bounds = screen->GetPrimaryDisplay().bounds();
71 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area();
72 work_area.Inset(kScreenEdgePadding, kScreenEdgePadding);
74 // Comparing the work area to the screen bounds gives us the location of the
75 // taskbar. If the work area is exactly the same as the screen bounds,
76 // we are unable to locate the taskbar so we say we don't know it's alignment.
77 if (work_area.height() < screen_bounds.height()) {
78 if (work_area.y() > screen_bounds.y())
80 return ALIGNMENT_BOTTOM;
82 if (work_area.width() < screen_bounds.width()) {
83 if (work_area.x() > screen_bounds.x())
84 return ALIGNMENT_LEFT;
85 return ALIGNMENT_RIGHT;
88 return ALIGNMENT_NONE;
91 gfx::Point GetClosestCorner(const gfx::Rect& rect, const gfx::Point& query) {
92 gfx::Point center_point = rect.CenterPoint();
95 if (query.x() > center_point.x())
96 rv.set_x(rect.right());
100 if (query.y() > center_point.y())
101 rv.set_y(rect.bottom());
108 // Gets the corner of the screen where the message center should pop up.
109 Alignment GetAnchorAlignment(const gfx::Rect& work_area, gfx::Point corner) {
110 gfx::Point center = work_area.CenterPoint();
112 Alignment anchor_alignment =
113 center.y() > corner.y() ? ALIGNMENT_TOP : ALIGNMENT_BOTTOM;
115 (Alignment)(anchor_alignment |
116 (center.x() > corner.x() ? ALIGNMENT_LEFT : ALIGNMENT_RIGHT));
118 return anchor_alignment;
121 } // namespace internal
123 MessageCenterTrayDelegate* CreateMessageCenterTray() {
124 return new WebNotificationTray();
127 WebNotificationTray::WebNotificationTray()
128 : message_center_delegate_(NULL),
130 status_icon_menu_(NULL),
131 message_center_visible_(false),
132 should_update_tray_content_(true) {
133 message_center_tray_.reset(
134 new MessageCenterTray(this, g_browser_process->message_center()));
135 last_quiet_mode_state_ = message_center()->IsQuietMode();
138 WebNotificationTray::~WebNotificationTray() {
139 // Reset this early so that delegated events during destruction don't cause
141 message_center_tray_.reset();
145 message_center::MessageCenter* WebNotificationTray::message_center() {
146 return message_center_tray_->message_center();
149 bool WebNotificationTray::ShowPopups() {
150 popup_collection_.reset(new message_center::MessagePopupCollection(
151 NULL, message_center(), message_center_tray_.get(), false));
155 void WebNotificationTray::HidePopups() { popup_collection_.reset(); }
157 bool WebNotificationTray::ShowMessageCenter() {
158 message_center_delegate_ =
159 new MessageCenterWidgetDelegate(this,
160 message_center_tray_.get(),
161 false, // settings initally invisible
167 void WebNotificationTray::HideMessageCenter() {
168 if (message_center_delegate_) {
169 views::Widget* widget = message_center_delegate_->GetWidget();
175 bool WebNotificationTray::ShowNotifierSettings() {
176 if (message_center_delegate_) {
177 message_center_delegate_->SetSettingsVisible(true);
180 message_center_delegate_ =
181 new MessageCenterWidgetDelegate(this,
182 message_center_tray_.get(),
183 true, // settings initally visible
189 void WebNotificationTray::OnMessageCenterTrayChanged() {
191 bool quiet_mode_state = message_center()->IsQuietMode();
192 if (last_quiet_mode_state_ != quiet_mode_state) {
193 last_quiet_mode_state_ = quiet_mode_state;
195 // Quiet mode has changed, update the quiet mode menu.
196 status_icon_menu_->SetCommandIdChecked(kToggleQuietMode,
201 // See the comments in ash/system/web_notification/web_notification_tray.cc
203 should_update_tray_content_ = true;
204 base::MessageLoop::current()->PostTask(
206 base::Bind(&WebNotificationTray::UpdateStatusIcon, AsWeakPtr()));
209 void WebNotificationTray::OnStatusIconClicked() {
210 // TODO(dewittj): It's possible GetNativeScreen is wrong for win-aura.
211 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
212 mouse_click_point_ = screen->GetCursorScreenPoint();
213 message_center_tray_->ToggleMessageCenterBubble();
216 void WebNotificationTray::ExecuteCommand(int command_id, int event_flags) {
217 if (command_id == kToggleQuietMode) {
218 bool in_quiet_mode = message_center()->IsQuietMode();
219 message_center()->SetQuietMode(!in_quiet_mode);
222 base::TimeDelta expires_in = command_id == kEnableQuietModeDay
223 ? base::TimeDelta::FromDays(1)
224 : base::TimeDelta::FromHours(1);
225 message_center()->EnterQuietModeWithExpire(expires_in);
228 void WebNotificationTray::UpdateStatusIcon() {
229 if (!should_update_tray_content_)
231 should_update_tray_content_ = false;
233 int unread_notifications = message_center()->UnreadNotificationCount();
236 if (unread_notifications > 0) {
237 string16 str_unread_count = base::FormatNumber(unread_notifications);
238 tool_tip = l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_TOOLTIP_UNREAD,
241 tool_tip = l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_TOOLTIP);
244 gfx::ImageSkia* icon_image = GetIcon(
245 unread_notifications,
246 message_center()->IsQuietMode());
249 status_icon_->SetImage(*icon_image);
250 status_icon_->SetToolTip(tool_tip);
254 CreateStatusIcon(*icon_image, tool_tip);
257 void WebNotificationTray::SendHideMessageCenter() {
258 message_center_tray_->HideMessageCenterBubble();
261 void WebNotificationTray::MarkMessageCenterHidden() {
262 if (message_center_delegate_) {
263 message_center_tray_->MarkMessageCenterHidden();
264 message_center_delegate_ = NULL;
268 PositionInfo WebNotificationTray::GetPositionInfo() {
269 PositionInfo pos_info;
271 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
272 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area();
273 work_area.Inset(kScreenEdgePadding, kScreenEdgePadding);
275 gfx::Point corner = internal::GetClosestCorner(work_area, mouse_click_point_);
277 pos_info.taskbar_alignment = internal::GetTaskbarAlignment();
279 // We assume the taskbar is either at the top or at the bottom if we are not
281 if (pos_info.taskbar_alignment == ALIGNMENT_NONE) {
282 if (mouse_click_point_.y() > corner.y())
283 pos_info.taskbar_alignment = ALIGNMENT_TOP;
285 pos_info.taskbar_alignment = ALIGNMENT_BOTTOM;
288 pos_info.message_center_alignment =
289 internal::GetAnchorAlignment(work_area, corner);
291 pos_info.inital_anchor_point = corner;
292 pos_info.max_height = work_area.height();
294 if (work_area.Contains(mouse_click_point_)) {
295 // Message center is in the work area. So position it few pixels above the
296 // mouse click point if alignemnt is towards bottom and few pixels below if
297 // alignment is towards top.
298 pos_info.inital_anchor_point.set_y(
299 mouse_click_point_.y() +
300 (pos_info.message_center_alignment & ALIGNMENT_BOTTOM ? -kMouseOffset
303 // Subtract the distance between mouse click point and the closest
304 // (insetted) edge from the max height to show the message center within the
305 // (insetted) work area bounds. Also subtract the offset from the mouse
306 // click point we added earlier.
307 pos_info.max_height -=
308 std::abs(mouse_click_point_.y() - corner.y()) + kMouseOffset;
313 MessageCenterTray* WebNotificationTray::GetMessageCenterTray() {
314 return message_center_tray_.get();
317 void WebNotificationTray::CreateStatusIcon(const gfx::ImageSkia& image,
318 const string16& tool_tip) {
322 StatusTray* status_tray = g_browser_process->status_tray();
326 status_icon_ = status_tray->CreateStatusIcon(
327 StatusTray::NOTIFICATION_TRAY_ICON, image, tool_tip);
331 status_icon_->AddObserver(this);
332 AddQuietModeMenu(status_icon_);
335 void WebNotificationTray::DestroyStatusIcon() {
339 status_icon_->RemoveObserver(this);
340 StatusTray* status_tray = g_browser_process->status_tray();
342 status_tray->RemoveStatusIcon(status_icon_);
343 status_icon_menu_ = NULL;
347 void WebNotificationTray::AddQuietModeMenu(StatusIcon* status_icon) {
350 scoped_ptr<StatusIconMenuModel> menu(new StatusIconMenuModel(this));
351 menu->AddCheckItem(kToggleQuietMode,
352 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_QUIET_MODE));
353 menu->SetCommandIdChecked(kToggleQuietMode, message_center()->IsQuietMode());
354 menu->AddItem(kEnableQuietModeHour,
355 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_QUIET_MODE_1HOUR));
356 menu->AddItem(kEnableQuietModeDay,
357 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_QUIET_MODE_1DAY));
359 status_icon_menu_ = menu.get();
360 status_icon->SetContextMenu(menu.Pass());
363 MessageCenterWidgetDelegate*
364 WebNotificationTray::GetMessageCenterWidgetDelegateForTest() {
365 return message_center_delegate_;
368 } // namespace message_center