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/prefs/pref_service.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/status_icons/status_icon.h"
13 #include "chrome/browser/status_icons/status_icon_menu_model.h"
14 #include "chrome/browser/status_icons/status_tray.h"
15 #include "chrome/common/pref_names.h"
16 #include "content/public/browser/notification_service.h"
17 #include "grit/chromium_strings.h"
18 #include "grit/theme_resources.h"
19 #include "grit/ui_strings.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/image/image_skia_operations.h"
24 #include "ui/gfx/rect.h"
25 #include "ui/gfx/screen.h"
26 #include "ui/gfx/size.h"
27 #include "ui/message_center/message_center_tray.h"
28 #include "ui/message_center/message_center_tray_delegate.h"
29 #include "ui/message_center/views/message_popup_collection.h"
30 #include "ui/views/widget/widget.h"
35 const int kScreenEdgePadding
= 2;
37 // Number of pixels the message center is offset from the mouse.
38 const int kMouseOffset
= 5;
41 const int kToggleQuietMode
= 0;
42 const int kEnableQuietModeHour
= 1;
43 const int kEnableQuietModeDay
= 2;
45 gfx::ImageSkia
* GetIcon(int unread_count
, bool is_quiet_mode
) {
46 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
47 int resource_id
= IDR_NOTIFICATION_TRAY_EMPTY
;
51 resource_id
= IDR_NOTIFICATION_TRAY_DO_NOT_DISTURB_ATTENTION
;
53 resource_id
= IDR_NOTIFICATION_TRAY_ATTENTION
;
54 } else if (is_quiet_mode
) {
55 resource_id
= IDR_NOTIFICATION_TRAY_DO_NOT_DISTURB_EMPTY
;
58 return rb
.GetImageSkiaNamed(resource_id
);
63 namespace message_center
{
67 // Gets the position of the taskbar from the work area bounds. Returns
68 // ALIGNMENT_NONE if position cannot be found.
69 Alignment
GetTaskbarAlignment() {
70 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
71 // TODO(dewittj): It's possible GetPrimaryDisplay is wrong.
72 gfx::Rect screen_bounds
= screen
->GetPrimaryDisplay().bounds();
73 gfx::Rect work_area
= screen
->GetPrimaryDisplay().work_area();
74 work_area
.Inset(kScreenEdgePadding
, kScreenEdgePadding
);
76 // Comparing the work area to the screen bounds gives us the location of the
77 // taskbar. If the work area is exactly the same as the screen bounds,
78 // we are unable to locate the taskbar so we say we don't know it's alignment.
79 if (work_area
.height() < screen_bounds
.height()) {
80 if (work_area
.y() > screen_bounds
.y())
82 return ALIGNMENT_BOTTOM
;
84 if (work_area
.width() < screen_bounds
.width()) {
85 if (work_area
.x() > screen_bounds
.x())
86 return ALIGNMENT_LEFT
;
87 return ALIGNMENT_RIGHT
;
90 return ALIGNMENT_NONE
;
93 gfx::Point
GetClosestCorner(const gfx::Rect
& rect
, const gfx::Point
& query
) {
94 gfx::Point center_point
= rect
.CenterPoint();
97 if (query
.x() > center_point
.x())
98 rv
.set_x(rect
.right());
102 if (query
.y() > center_point
.y())
103 rv
.set_y(rect
.bottom());
110 // Gets the corner of the screen where the message center should pop up.
111 Alignment
GetAnchorAlignment(const gfx::Rect
& work_area
, gfx::Point corner
) {
112 gfx::Point center
= work_area
.CenterPoint();
114 Alignment anchor_alignment
=
115 center
.y() > corner
.y() ? ALIGNMENT_TOP
: ALIGNMENT_BOTTOM
;
117 (Alignment
)(anchor_alignment
|
118 (center
.x() > corner
.x() ? ALIGNMENT_LEFT
: ALIGNMENT_RIGHT
));
120 return anchor_alignment
;
123 } // namespace internal
125 MessageCenterTrayDelegate
* CreateMessageCenterTray() {
126 return new WebNotificationTray(g_browser_process
->local_state());
129 WebNotificationTray::WebNotificationTray(PrefService
* local_state
)
130 : message_center_delegate_(NULL
),
132 status_icon_menu_(NULL
),
133 should_update_tray_content_(true) {
134 message_center_tray_
.reset(
135 new MessageCenterTray(this, g_browser_process
->message_center()));
136 last_quiet_mode_state_
= message_center()->IsQuietMode();
137 popup_collection_
.reset(new message_center::MessagePopupCollection(
138 NULL
, message_center(), message_center_tray_
.get(), false));
141 // |local_state| can be NULL in tests.
143 did_force_tray_visible_
.reset(new BooleanPrefMember());
144 did_force_tray_visible_
->Init(prefs::kMessageCenterForcedOnTaskbar
,
150 WebNotificationTray::~WebNotificationTray() {
151 // Reset this early so that delegated events during destruction don't cause
153 popup_collection_
.reset();
154 message_center_tray_
.reset();
158 message_center::MessageCenter
* WebNotificationTray::message_center() {
159 return message_center_tray_
->message_center();
162 bool WebNotificationTray::ShowPopups() {
163 popup_collection_
->DoUpdateIfPossible();
167 void WebNotificationTray::HidePopups() {
168 DCHECK(popup_collection_
.get());
169 popup_collection_
->MarkAllPopupsShown();
172 bool WebNotificationTray::ShowMessageCenter() {
173 message_center_delegate_
=
174 new MessageCenterWidgetDelegate(this,
175 message_center_tray_
.get(),
176 false, // settings initally invisible
182 void WebNotificationTray::HideMessageCenter() {
183 if (message_center_delegate_
) {
184 views::Widget
* widget
= message_center_delegate_
->GetWidget();
190 bool WebNotificationTray::ShowNotifierSettings() {
191 if (message_center_delegate_
) {
192 message_center_delegate_
->SetSettingsVisible(true);
195 message_center_delegate_
=
196 new MessageCenterWidgetDelegate(this,
197 message_center_tray_
.get(),
198 true, // settings initally visible
204 bool WebNotificationTray::IsContextMenuEnabled() const {
205 // It can always return true because the notifications are invisible if
206 // the context menu shouldn't be enabled, such as in the lock screen.
210 void WebNotificationTray::OnMessageCenterTrayChanged() {
212 bool quiet_mode_state
= message_center()->IsQuietMode();
213 if (last_quiet_mode_state_
!= quiet_mode_state
) {
214 last_quiet_mode_state_
= quiet_mode_state
;
216 // Quiet mode has changed, update the quiet mode menu.
217 status_icon_menu_
->SetCommandIdChecked(kToggleQuietMode
,
222 // See the comments in ash/system/web_notification/web_notification_tray.cc
224 should_update_tray_content_
= true;
225 base::MessageLoop::current()->PostTask(
227 base::Bind(&WebNotificationTray::UpdateStatusIcon
, AsWeakPtr()));
230 void WebNotificationTray::OnStatusIconClicked() {
231 // TODO(dewittj): It's possible GetNativeScreen is wrong for win-aura.
232 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
233 mouse_click_point_
= screen
->GetCursorScreenPoint();
234 message_center_tray_
->ToggleMessageCenterBubble();
237 void WebNotificationTray::ExecuteCommand(int command_id
, int event_flags
) {
238 if (command_id
== kToggleQuietMode
) {
239 bool in_quiet_mode
= message_center()->IsQuietMode();
240 message_center()->SetQuietMode(!in_quiet_mode
);
243 base::TimeDelta expires_in
= command_id
== kEnableQuietModeDay
244 ? base::TimeDelta::FromDays(1)
245 : base::TimeDelta::FromHours(1);
246 message_center()->EnterQuietModeWithExpire(expires_in
);
249 void WebNotificationTray::UpdateStatusIcon() {
250 if (!should_update_tray_content_
)
252 should_update_tray_content_
= false;
254 int unread_notifications
= message_center()->UnreadNotificationCount();
256 base::string16 tool_tip
;
257 if (unread_notifications
> 0) {
258 base::string16 str_unread_count
= base::FormatNumber(unread_notifications
);
259 tool_tip
= l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_TOOLTIP_UNREAD
,
262 tool_tip
= l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_TOOLTIP
);
265 gfx::ImageSkia
* icon_image
= GetIcon(
266 unread_notifications
,
267 message_center()->IsQuietMode());
270 status_icon_
->SetImage(*icon_image
);
271 status_icon_
->SetToolTip(tool_tip
);
275 CreateStatusIcon(*icon_image
, tool_tip
);
278 void WebNotificationTray::SendHideMessageCenter() {
279 message_center_tray_
->HideMessageCenterBubble();
282 void WebNotificationTray::MarkMessageCenterHidden() {
283 if (message_center_delegate_
) {
284 message_center_tray_
->MarkMessageCenterHidden();
285 message_center_delegate_
= NULL
;
289 PositionInfo
WebNotificationTray::GetPositionInfo() {
290 PositionInfo pos_info
;
292 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
293 gfx::Rect work_area
= screen
->GetPrimaryDisplay().work_area();
294 work_area
.Inset(kScreenEdgePadding
, kScreenEdgePadding
);
296 gfx::Point corner
= internal::GetClosestCorner(work_area
, mouse_click_point_
);
298 pos_info
.taskbar_alignment
= internal::GetTaskbarAlignment();
300 // We assume the taskbar is either at the top or at the bottom if we are not
302 if (pos_info
.taskbar_alignment
== ALIGNMENT_NONE
) {
303 if (mouse_click_point_
.y() > corner
.y())
304 pos_info
.taskbar_alignment
= ALIGNMENT_TOP
;
306 pos_info
.taskbar_alignment
= ALIGNMENT_BOTTOM
;
309 pos_info
.message_center_alignment
=
310 internal::GetAnchorAlignment(work_area
, corner
);
312 pos_info
.inital_anchor_point
= corner
;
313 pos_info
.max_height
= work_area
.height();
315 if (work_area
.Contains(mouse_click_point_
)) {
316 // Message center is in the work area. So position it few pixels above the
317 // mouse click point if alignemnt is towards bottom and few pixels below if
318 // alignment is towards top.
319 pos_info
.inital_anchor_point
.set_y(
320 mouse_click_point_
.y() +
321 (pos_info
.message_center_alignment
& ALIGNMENT_BOTTOM
? -kMouseOffset
324 // Subtract the distance between mouse click point and the closest
325 // (insetted) edge from the max height to show the message center within the
326 // (insetted) work area bounds. Also subtract the offset from the mouse
327 // click point we added earlier.
328 pos_info
.max_height
-=
329 std::abs(mouse_click_point_
.y() - corner
.y()) + kMouseOffset
;
334 MessageCenterTray
* WebNotificationTray::GetMessageCenterTray() {
335 return message_center_tray_
.get();
338 void WebNotificationTray::CreateStatusIcon(const gfx::ImageSkia
& image
,
339 const base::string16
& tool_tip
) {
343 StatusTray
* status_tray
= g_browser_process
->status_tray();
347 status_icon_
= status_tray
->CreateStatusIcon(
348 StatusTray::NOTIFICATION_TRAY_ICON
, image
, tool_tip
);
352 status_icon_
->AddObserver(this);
353 AddQuietModeMenu(status_icon_
);
356 void WebNotificationTray::DestroyStatusIcon() {
360 status_icon_
->RemoveObserver(this);
361 StatusTray
* status_tray
= g_browser_process
->status_tray();
363 status_tray
->RemoveStatusIcon(status_icon_
);
364 status_icon_menu_
= NULL
;
368 void WebNotificationTray::AddQuietModeMenu(StatusIcon
* status_icon
) {
371 scoped_ptr
<StatusIconMenuModel
> menu(new StatusIconMenuModel(this));
372 menu
->AddCheckItem(kToggleQuietMode
,
373 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_QUIET_MODE
));
374 menu
->SetCommandIdChecked(kToggleQuietMode
, message_center()->IsQuietMode());
375 menu
->AddItem(kEnableQuietModeHour
,
376 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_QUIET_MODE_1HOUR
));
377 menu
->AddItem(kEnableQuietModeDay
,
378 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_QUIET_MODE_1DAY
));
380 status_icon_menu_
= menu
.get();
381 status_icon
->SetContextMenu(menu
.Pass());
384 MessageCenterWidgetDelegate
*
385 WebNotificationTray::GetMessageCenterWidgetDelegateForTest() {
386 return message_center_delegate_
;
389 } // namespace message_center