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. 4 5#include "chrome/browser/ui/views/message_center/web_notification_tray.h" 6 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_tray.h" 13#include "content/public/browser/notification_service.h" 14#include "content/public/browser/user_metrics.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" 29 30namespace { 31 32// Tray constants 33const int kScreenEdgePadding = 2; 34 35const int kSystemTrayWidth = 16; 36const int kSystemTrayHeight = 16; 37const int kNumberOfSystemTraySprites = 10; 38 39// Number of pixels the message center is offset from the mouse. 40const int kMouseOffset = 5; 41 42gfx::ImageSkia* GetIcon(int unread_count, bool is_quiet_mode) { 43 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 44 int resource_id = IDR_NOTIFICATION_TRAY_EMPTY; 45 46 if (unread_count) { 47 if (is_quiet_mode) 48 resource_id = IDR_NOTIFICATION_TRAY_DO_NOT_DISTURB_ATTENTION; 49 else 50 resource_id = IDR_NOTIFICATION_TRAY_ATTENTION; 51 } else if (is_quiet_mode) { 52 resource_id = IDR_NOTIFICATION_TRAY_DO_NOT_DISTURB_EMPTY; 53 } 54 55 return rb.GetImageSkiaNamed(resource_id); 56} 57 58} // namespace 59 60using content::UserMetricsAction; 61 62namespace message_center { 63 64namespace internal { 65 66// Gets the position of the taskbar from the work area bounds. Returns 67// ALIGNMENT_NONE if position cannot be found. 68Alignment GetTaskbarAlignment() { 69 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); 70 // TODO(dewittj): It's possible GetPrimaryDisplay is wrong. 71 gfx::Rect screen_bounds = screen->GetPrimaryDisplay().bounds(); 72 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area(); 73 work_area.Inset(kScreenEdgePadding, kScreenEdgePadding); 74 75 // Comparing the work area to the screen bounds gives us the location of the 76 // taskbar. If the work area is exactly the same as the screen bounds, 77 // we are unable to locate the taskbar so we say we don't know it's alignment. 78 if (work_area.height() < screen_bounds.height()) { 79 if (work_area.y() > screen_bounds.y()) 80 return ALIGNMENT_TOP; 81 return ALIGNMENT_BOTTOM; 82 } 83 if (work_area.width() < screen_bounds.width()) { 84 if (work_area.x() > screen_bounds.x()) 85 return ALIGNMENT_LEFT; 86 return ALIGNMENT_RIGHT; 87 } 88 89 return ALIGNMENT_NONE; 90} 91 92gfx::Point GetClosestCorner(const gfx::Rect& rect, const gfx::Point& query) { 93 gfx::Point center_point = rect.CenterPoint(); 94 gfx::Point rv; 95 96 if (query.x() > center_point.x()) 97 rv.set_x(rect.right()); 98 else 99 rv.set_x(rect.x()); 100 101 if (query.y() > center_point.y()) 102 rv.set_y(rect.bottom()); 103 else 104 rv.set_y(rect.y()); 105 106 return rv; 107} 108 109// Gets the corner of the screen where the message center should pop up. 110Alignment GetAnchorAlignment(const gfx::Rect& work_area, gfx::Point corner) { 111 gfx::Point center = work_area.CenterPoint(); 112 113 Alignment anchor_alignment = 114 center.y() > corner.y() ? ALIGNMENT_TOP : ALIGNMENT_BOTTOM; 115 anchor_alignment = 116 (Alignment)(anchor_alignment | 117 (center.x() > corner.x() ? ALIGNMENT_LEFT : ALIGNMENT_RIGHT)); 118 119 return anchor_alignment; 120} 121 122} // namespace internal 123 124MessageCenterTrayDelegate* CreateMessageCenterTray() { 125 return new WebNotificationTray(); 126} 127 128WebNotificationTray::WebNotificationTray() 129 : message_center_delegate_(NULL), 130 status_icon_(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} 136 137WebNotificationTray::~WebNotificationTray() { 138 // Reset this early so that delegated events during destruction don't cause 139 // problems. 140 message_center_tray_.reset(); 141 DestroyStatusIcon(); 142} 143 144message_center::MessageCenter* WebNotificationTray::message_center() { 145 return message_center_tray_->message_center(); 146} 147 148bool WebNotificationTray::ShowPopups() { 149 popup_collection_.reset(new message_center::MessagePopupCollection( 150 NULL, message_center(), message_center_tray_.get(), false)); 151 return true; 152} 153 154void WebNotificationTray::HidePopups() { popup_collection_.reset(); } 155 156bool WebNotificationTray::ShowMessageCenterInternal(bool show_settings) { 157 content::RecordAction(UserMetricsAction("Notifications.ShowMessageCenter")); 158 159 // Message center delegate will be set to NULL when the message center 160 // widget's Close method is called so we don't need to worry about 161 // use-after-free issues. 162 message_center_delegate_ = new MessageCenterWidgetDelegate( 163 this, 164 message_center_tray_.get(), 165 show_settings, // settings initally invisible 166 GetPositionInfo()); 167 168 return true; 169} 170 171bool WebNotificationTray::ShowMessageCenter() { 172 return ShowMessageCenterInternal(/*show_settings =*/false); 173} 174 175void WebNotificationTray::HideMessageCenter() { 176 if (message_center_delegate_) { 177 views::Widget* widget = message_center_delegate_->GetWidget(); 178 if (widget) 179 widget->Close(); 180 } 181} 182 183bool WebNotificationTray::ShowNotifierSettings() { 184 if (message_center_delegate_) { 185 message_center_delegate_->SetSettingsVisible(true); 186 return true; 187 } 188 return ShowMessageCenterInternal(/*show_settings =*/true); 189} 190 191void WebNotificationTray::OnMessageCenterTrayChanged() { 192 // See the comments in ash/system/web_notification/web_notification_tray.cc 193 // for why PostTask. 194 should_update_tray_content_ = true; 195 base::MessageLoop::current()->PostTask( 196 FROM_HERE, 197 base::Bind(&WebNotificationTray::UpdateStatusIcon, AsWeakPtr())); 198} 199 200void WebNotificationTray::OnStatusIconClicked() { 201 // TODO(dewittj): It's possible GetNativeScreen is wrong for win-aura. 202 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); 203 mouse_click_point_ = screen->GetCursorScreenPoint(); 204 message_center_tray_->ToggleMessageCenterBubble(); 205} 206 207void WebNotificationTray::UpdateStatusIcon() { 208 if (!should_update_tray_content_) 209 return; 210 should_update_tray_content_ = false; 211 212 int unread_notifications = message_center()->UnreadNotificationCount(); 213 214 string16 tool_tip; 215 if (unread_notifications > 0) { 216 string16 str_unread_count = base::FormatNumber(unread_notifications); 217 tool_tip = l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_TOOLTIP_UNREAD, 218 str_unread_count); 219 } else { 220 tool_tip = l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_TOOLTIP); 221 } 222 223 gfx::ImageSkia* icon_image = GetIcon( 224 unread_notifications, 225 message_center()->IsQuietMode()); 226 227 if (status_icon_) { 228 status_icon_->SetImage(*icon_image); 229 status_icon_->SetToolTip(tool_tip); 230 return; 231 } 232 233 CreateStatusIcon(*icon_image, tool_tip); 234} 235 236void WebNotificationTray::SendHideMessageCenter() { 237 message_center_tray_->HideMessageCenterBubble(); 238} 239 240void WebNotificationTray::MarkMessageCenterHidden() { 241 if (message_center_delegate_) { 242 message_center_tray_->MarkMessageCenterHidden(); 243 message_center_delegate_ = NULL; 244 } 245} 246 247PositionInfo WebNotificationTray::GetPositionInfo() { 248 PositionInfo pos_info; 249 250 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); 251 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area(); 252 work_area.Inset(kScreenEdgePadding, kScreenEdgePadding); 253 254 gfx::Point corner = internal::GetClosestCorner(work_area, mouse_click_point_); 255 256 pos_info.taskbar_alignment = internal::GetTaskbarAlignment(); 257 258 // We assume the taskbar is either at the top or at the bottom if we are not 259 // able to find it. 260 if (pos_info.taskbar_alignment == ALIGNMENT_NONE) { 261 if (mouse_click_point_.y() > corner.y()) 262 pos_info.taskbar_alignment = ALIGNMENT_TOP; 263 else 264 pos_info.taskbar_alignment = ALIGNMENT_BOTTOM; 265 } 266 267 pos_info.message_center_alignment = 268 internal::GetAnchorAlignment(work_area, corner); 269 270 pos_info.inital_anchor_point = corner; 271 pos_info.max_height = work_area.height(); 272 273 if (work_area.Contains(mouse_click_point_)) { 274 // Message center is in the work area. So position it few pixels above the 275 // mouse click point if alignemnt is towards bottom and few pixels below if 276 // alignment is towards top. 277 pos_info.inital_anchor_point.set_y( 278 mouse_click_point_.y() + 279 (pos_info.message_center_alignment & ALIGNMENT_BOTTOM ? -kMouseOffset 280 : kMouseOffset)); 281 282 // Subtract the distance between mouse click point and the closest 283 // (insetted) edge from the max height to show the message center within the 284 // (insetted) work area bounds. Also subtract the offset from the mouse 285 // click point we added earlier. 286 pos_info.max_height -= 287 std::abs(mouse_click_point_.y() - corner.y()) + kMouseOffset; 288 } 289 return pos_info; 290} 291 292MessageCenterTray* WebNotificationTray::GetMessageCenterTray() { 293 return message_center_tray_.get(); 294} 295 296void WebNotificationTray::CreateStatusIcon(const gfx::ImageSkia& image, 297 const string16& tool_tip) { 298 if (status_icon_) 299 return; 300 301 StatusTray* status_tray = g_browser_process->status_tray(); 302 if (!status_tray) 303 return; 304 305 status_icon_ = status_tray->CreateStatusIcon( 306 StatusTray::NOTIFICATION_TRAY_ICON, image, tool_tip); 307 if (!status_icon_) 308 return; 309 310 status_icon_->AddObserver(this); 311 AddQuietModeMenu(status_icon_); 312} 313 314void WebNotificationTray::DestroyStatusIcon() { 315 if (!status_icon_) 316 return; 317 318 status_icon_->RemoveObserver(this); 319 StatusTray* status_tray = g_browser_process->status_tray(); 320 if (status_tray) 321 status_tray->RemoveStatusIcon(status_icon_); 322 status_icon_ = NULL; 323} 324 325void WebNotificationTray::AddQuietModeMenu(StatusIcon* status_icon) { 326 DCHECK(status_icon); 327 status_icon->SetContextMenu(message_center_tray_->CreateQuietModeMenu()); 328} 329 330MessageCenterWidgetDelegate* 331WebNotificationTray::GetMessageCenterWidgetDelegateForTest() { 332 return message_center_delegate_; 333} 334 335} // namespace message_center 336