Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1028)

Side by Side Diff: chrome/browser/ui/views/message_center/web_notification_tray_win.cc

Issue 11819048: Implement message center on Windows (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Use notification manager instead of balloon view, remove singleton-ness from MessageCenterTray. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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_win.h"
6
7 #include "base/memory/singleton.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/status_icons/status_icon.h"
10 #include "chrome/browser/status_icons/status_tray.h"
11 #include "chrome/browser/ui/views/message_center/notification_bubble_wrapper_win .h"
12 #include "chrome/browser/ui/views/status_icons/status_icon_win.h"
13 #include "grit/theme_resources.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/base/win/hwnd_util.h"
16 #include "ui/gfx/image/image_skia_operations.h"
17 #include "ui/gfx/screen.h"
18 #include "ui/message_center/message_bubble_base.h"
19 #include "ui/message_center/message_center_bubble.h"
20 #include "ui/message_center/message_center_tray.h"
21 #include "ui/message_center/message_popup_bubble.h"
22 #include "ui/views/widget/widget.h"
23
24 namespace {
25
26 // Tray constants
27 const int kPaddingFromLeftEdgeOfSystemTrayBottomAlignment = 8;
28
29 gfx::Rect GetCornerAnchorRect(gfx::Size preferred_size) {
30 // TODO(dewittj): Use the preference to determine which corner to anchor from.
31 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
32 gfx::Rect rect = screen->GetPrimaryDisplay().work_area();
33 rect.Inset(10, 5);
34 gfx::Point bottom_right(
35 rect.bottom_right().x() - preferred_size.width() / 2,
36 rect.bottom_right().y());
37 return gfx::Rect(bottom_right, gfx::Size());
38 }
39
40 // GetMouseAnchorRect returns a rectangle that is near the cursor point, but
41 // whose behavior depends on where the Windows taskbar is. If it is on the
42 // top or bottom of the screen, we want the arrow to touch the edge of the
43 // taskbar directly above or below the mouse pointer and within the work area.
44 // Otherwise, position the anchor on the mouse cursor directly.
45 gfx::Rect GetMouseAnchorRect() {
46 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
47 gfx::Rect usable_area = screen->GetPrimaryDisplay().bounds();
48 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area();
49
50 // Inset the rectangle by the taskbar width if it is on top or bottom.
51 usable_area.set_y(work_area.y());
52 usable_area.set_height(work_area.height());
53
54 // Keep the anchor from being too close to the edge of the screen.
55 usable_area.Inset(kPaddingFromLeftEdgeOfSystemTrayBottomAlignment, 0);
56
57 // Use a mouse point that is on the mouse cursor, unless the mouse is over the
58 // start menu and the start menu is on the top or bottom.
59 gfx::Point cursor = screen->GetCursorScreenPoint();
60 gfx::Rect mouse_anchor_rect(
61 gfx::BoundingRect(cursor, usable_area.bottom_right()));
62 mouse_anchor_rect.set_height(0);
63 if (!usable_area.Contains(cursor))
64 mouse_anchor_rect.AdjustToFit(usable_area);
65 mouse_anchor_rect.set_width(0);
66 return mouse_anchor_rect;
67 }
68
69 gfx::ImageSkia GetIcon(bool has_unread_notifications) {
70 // TODO(dewittj): Use an icon resource for both unread and read notifications.
71 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
72 gfx::ImageSkia* icon =
73 rb.GetImageSkiaNamed(IDR_ALLOWED_NOTIFICATION);
74 if (has_unread_notifications)
75 return *icon;
76 return gfx::ImageSkiaOperations::CreateTransparentImage(*icon, .5);
77 }
78
79 } // namespace
80
81 namespace ui {
82
83 // Ash defines this function elsewhere.
84 #if !defined(USE_ASH)
85
86 // static
87 MessageCenterTrayDelegate*
88 MessageCenterTrayDelegate::CreateForPlatform() {
89 return new WebNotificationTrayWin();
90 }
91
92 #endif
93
94 WebNotificationTrayWin::WebNotificationTrayWin()
95 : status_icon_(NULL),
96 message_center_visible_(false) {
97 message_center_tray_ = new MessageCenterTray(
98 this, g_browser_process->message_center());
Jun Mukai 2013/01/22 18:55:34 BrowserProcess doesn't have 'message_center' field
dewittj 2013/01/22 20:49:21 This CL depends on crrev.com/11958025 which makes
99 message_center_tray_->AddObserver(this);
100 StatusTray* status_tray = g_browser_process->status_tray();
101 status_icon_ = status_tray->CreateStatusIcon();
102 status_icon_->AddObserver(this);
103 status_icon_->SetImage(
104 GetIcon(message_center()->UnreadNotificationCount() > 0));
105 }
106
107 WebNotificationTrayWin::~WebNotificationTrayWin() {
108 message_center_tray_->RemoveObserver(this);
109 status_icon_->RemoveObserver(this);
110 StatusTray * status_tray = g_browser_process->status_tray();
111 status_tray->RemoveStatusIcon(status_icon_);
112 status_icon_ = NULL;
113 }
114
115 message_center::MessageCenter* WebNotificationTrayWin::message_center() {
116 return message_center_tray_->message_center();
117 }
118
119 bool WebNotificationTrayWin::ShowPopups(
120 message_center::MessageBubbleBase* bubble) {
121 popup_bubble_.reset(new internal::NotificationBubbleWrapperWin(
122 this, bubble, views::TrayBubbleView::ANCHOR_TYPE_BUBBLE));
123 return true;
124 }
125 void WebNotificationTrayWin::HidePopups() {
126 popup_bubble_.reset();
127 }
128
129 bool WebNotificationTrayWin::ShowMessageCenter(
130 message_center::MessageBubbleBase* bubble) {
131 // Calculate the maximum height of the message center, given its anchor.
132 gfx::Point anchor_center = message_center_anchor_rect_.CenterPoint();
133 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
134 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area();
135 gfx::Point work_area_center = work_area.CenterPoint();
136 const int zMarginFromEdgeOfWorkArea = 10;
137 int max_height = 0;
138 if (work_area_center < anchor_center)
139 max_height = anchor_center.y() - work_area.origin().y();
140 else
141 max_height = work_area.bottom() - message_center_anchor_rect_.bottom();
142 bubble->SetMaxHeight(max_height - zMarginFromEdgeOfWorkArea);
143
144 message_center_bubble_.reset(new internal::NotificationBubbleWrapperWin(
145 this,
146 bubble,
147 views::TrayBubbleView::ANCHOR_TYPE_TRAY));
148 return true;
149 }
150
151 void WebNotificationTrayWin::HideMessageCenter() {
152 message_center_bubble_.reset();
153 }
154
155 void WebNotificationTrayWin::UpdateMessageCenter() {
156 if (message_center_bubble_.get())
157 message_center_bubble_->bubble()->ScheduleUpdate();
158 }
159
160 void WebNotificationTrayWin::UpdatePopups() {
161 if (popup_bubble_.get())
162 popup_bubble_->bubble()->ScheduleUpdate();
163 };
164
165 void WebNotificationTrayWin::OnMessageCenterTrayChanged() {
166 bool has_unread_notifications =
167 message_center()->UnreadNotificationCount() > 0;
168 status_icon_->SetImage(GetIcon(has_unread_notifications));
169 }
170
171 gfx::Rect WebNotificationTrayWin::GetAnchorRect(
172 gfx::Size preferred_size,
173 views::TrayBubbleView::AnchorType anchor_type,
174 views::TrayBubbleView::AnchorAlignment anchor_alignment) {
175 if (anchor_type == views::TrayBubbleView::ANCHOR_TYPE_TRAY) {
176 return message_center_anchor_rect_;
177 }
178 return GetCornerAnchorRect(preferred_size);
179 }
180
181 views::TrayBubbleView::AnchorAlignment
182 WebNotificationTrayWin::GetAnchorAlignment() {
183 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
184 // TODO(dewittj): It's possible GetPrimaryDisplay is wrong.
185 gfx::Rect screen_bounds = screen->GetPrimaryDisplay().bounds();
186 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area();
187
188 if (work_area.height() < screen_bounds.height())
189 return views::TrayBubbleView::ANCHOR_ALIGNMENT_BOTTOM;
190 if (work_area.x() > screen_bounds.x())
191 return views::TrayBubbleView::ANCHOR_ALIGNMENT_LEFT;
192 return views::TrayBubbleView::ANCHOR_ALIGNMENT_RIGHT;
193 }
194
195 gfx::NativeView WebNotificationTrayWin::GetBubbleWindowContainer() {
196 return NULL;
197 }
198
199 void WebNotificationTrayWin::OnStatusIconClicked() {
200 UpdateAnchorRect();
201 message_center_tray_->ToggleMessageCenterBubble();
202 }
203
204 void WebNotificationTrayWin::HideBubbleWithView(
205 const views::TrayBubbleView* bubble_view) {
206 if (message_center_bubble_.get() &&
207 bubble_view == message_center_bubble_->bubble_view()) {
208 message_center_tray_->HideMessageCenterBubble();
209 } else if (popup_bubble_.get() &&
210 bubble_view == popup_bubble_->bubble_view()) {
211 message_center_tray_->HidePopupBubble();
212 }
213 }
214
215 void WebNotificationTrayWin::UpdateAnchorRect() {
216 message_center_anchor_rect_ = GetMouseAnchorRect();
217 }
218
219 message_center::MessageCenterBubble*
220 WebNotificationTrayWin::GetMessageCenterBubbleForTest() {
221 if (!message_center_bubble_.get())
222 return NULL;
223 return static_cast<message_center::MessageCenterBubble*>(
224 message_center_bubble_->bubble());
225 }
226
227 message_center::MessagePopupBubble*
228 WebNotificationTrayWin::GetPopupBubbleForTest() {
229 if (!popup_bubble_.get())
230 return NULL;
231 return static_cast<message_center::MessagePopupBubble*>(
232 popup_bubble_->bubble());
233 }
234
235 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698