OLD | NEW |
---|---|
(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 "chrome/browser/browser_process.h" | |
8 #include "chrome/browser/status_icons/status_icon.h" | |
9 #include "chrome/browser/status_icons/status_tray.h" | |
10 #include "chrome/browser/ui/views/message_center/notification_bubble_wrapper_win .h" | |
11 #include "chrome/browser/ui/views/status_icons/status_icon_win.h" | |
12 #include "grit/theme_resources.h" | |
13 #include "ui/base/resource/resource_bundle.h" | |
14 #include "ui/base/win/hwnd_util.h" | |
15 #include "ui/gfx/image/image_skia_operations.h" | |
16 #include "ui/gfx/screen.h" | |
17 #include "ui/message_center/message_bubble_base.h" | |
18 #include "ui/message_center/message_center_bubble.h" | |
19 #include "ui/message_center/message_center_tray.h" | |
20 #include "ui/message_center/message_center_tray_delegate.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 | |
miket_OOO
2013/01/25 17:14:48
One space after period. http://www.slate.com/artic
dewittj
2013/01/25 19:38:46
I'll change it but note the paragraph in the middl
| |
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); | |
miket_OOO
2013/01/25 17:14:48
Consider a DCHECK(icon) here to isolate the failur
dewittj
2013/01/25 19:38:46
Done.
| |
74 if (has_unread_notifications) | |
75 return *icon; | |
76 return gfx::ImageSkiaOperations::CreateTransparentImage(*icon, .5); | |
77 } | |
78 | |
79 } // namespace | |
80 | |
81 namespace message_center { | |
82 | |
83 MessageCenterTrayDelegate* CreateMessageCenterTray() { | |
84 return new WebNotificationTrayWin(); | |
85 } | |
86 | |
87 WebNotificationTrayWin::WebNotificationTrayWin() | |
88 : status_icon_(NULL), | |
89 message_center_visible_(false) { | |
90 message_center_tray_.reset(new MessageCenterTray( | |
91 this, g_browser_process->message_center())); | |
92 StatusTray* status_tray = g_browser_process->status_tray(); | |
93 status_icon_ = status_tray->CreateStatusIcon(); | |
94 status_icon_->AddObserver(this); | |
95 status_icon_->SetImage( | |
96 GetIcon(message_center()->UnreadNotificationCount() > 0)); | |
97 } | |
98 | |
99 WebNotificationTrayWin::~WebNotificationTrayWin() { | |
100 // Reset this early so that delegated events during destruction don't cause | |
101 // problems. | |
102 message_center_tray_.reset(); | |
103 status_icon_->RemoveObserver(this); | |
104 StatusTray * status_tray = g_browser_process->status_tray(); | |
miket_OOO
2013/01/25 17:14:48
oops
dewittj
2013/01/25 19:38:46
Done.
| |
105 status_tray->RemoveStatusIcon(status_icon_); | |
106 status_icon_ = NULL; | |
107 } | |
108 | |
109 message_center::MessageCenter* WebNotificationTrayWin::message_center() { | |
110 return message_center_tray_->message_center(); | |
111 } | |
112 | |
113 bool WebNotificationTrayWin::ShowPopups() { | |
114 message_center::MessagePopupBubble* bubble = | |
115 new message_center::MessagePopupBubble(message_center()); | |
116 popup_bubble_.reset(new internal::NotificationBubbleWrapperWin( | |
117 this, bubble, views::TrayBubbleView::ANCHOR_TYPE_BUBBLE)); | |
118 return true; | |
119 } | |
miket_OOO
2013/01/25 17:14:48
vertical whitespace
dewittj
2013/01/25 19:38:46
Done.
| |
120 void WebNotificationTrayWin::HidePopups() { | |
121 popup_bubble_.reset(); | |
122 } | |
123 | |
124 bool WebNotificationTrayWin::ShowMessageCenter() { | |
125 // Calculate the maximum height of the message center, given its anchor. | |
126 message_center::MessageCenterBubble* bubble = | |
127 new message_center::MessageCenterBubble(message_center()); | |
128 gfx::Point anchor_center = message_center_anchor_rect_.CenterPoint(); | |
129 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | |
130 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area(); | |
131 gfx::Point work_area_center = work_area.CenterPoint(); | |
132 const int zMarginFromEdgeOfWorkArea = 10; | |
133 int max_height = 0; | |
134 if (work_area_center < anchor_center) | |
135 max_height = anchor_center.y() - work_area.origin().y(); | |
136 else | |
137 max_height = work_area.bottom() - message_center_anchor_rect_.bottom(); | |
138 bubble->SetMaxHeight(max_height - zMarginFromEdgeOfWorkArea); | |
139 | |
140 message_center_bubble_.reset(new internal::NotificationBubbleWrapperWin( | |
141 this, | |
142 bubble, | |
143 views::TrayBubbleView::ANCHOR_TYPE_TRAY)); | |
144 return true; | |
145 } | |
146 | |
147 void WebNotificationTrayWin::HideMessageCenter() { | |
148 message_center_bubble_.reset(); | |
149 } | |
150 | |
151 void WebNotificationTrayWin::UpdateMessageCenter() { | |
152 if (message_center_bubble_.get()) | |
153 message_center_bubble_->bubble()->ScheduleUpdate(); | |
154 } | |
155 | |
156 void WebNotificationTrayWin::UpdatePopups() { | |
157 if (popup_bubble_.get()) | |
158 popup_bubble_->bubble()->ScheduleUpdate(); | |
159 }; | |
160 | |
161 void WebNotificationTrayWin::OnMessageCenterTrayChanged() { | |
162 bool has_unread_notifications = | |
163 message_center()->UnreadNotificationCount() > 0; | |
164 status_icon_->SetImage(GetIcon(has_unread_notifications)); | |
165 } | |
166 | |
167 gfx::Rect WebNotificationTrayWin::GetAnchorRect( | |
168 gfx::Size preferred_size, | |
169 views::TrayBubbleView::AnchorType anchor_type, | |
170 views::TrayBubbleView::AnchorAlignment anchor_alignment) { | |
171 if (anchor_type == views::TrayBubbleView::ANCHOR_TYPE_TRAY) { | |
172 return message_center_anchor_rect_; | |
miket_OOO
2013/01/25 17:14:48
no braces needed
dewittj
2013/01/25 19:38:46
Done.
| |
173 } | |
174 return GetCornerAnchorRect(preferred_size); | |
175 } | |
176 | |
177 views::TrayBubbleView::AnchorAlignment | |
178 WebNotificationTrayWin::GetAnchorAlignment() { | |
179 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | |
180 // TODO(dewittj): It's possible GetPrimaryDisplay is wrong. | |
181 gfx::Rect screen_bounds = screen->GetPrimaryDisplay().bounds(); | |
182 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area(); | |
183 | |
184 if (work_area.height() < screen_bounds.height()) | |
185 return views::TrayBubbleView::ANCHOR_ALIGNMENT_BOTTOM; | |
186 if (work_area.x() > screen_bounds.x()) | |
187 return views::TrayBubbleView::ANCHOR_ALIGNMENT_LEFT; | |
188 return views::TrayBubbleView::ANCHOR_ALIGNMENT_RIGHT; | |
189 } | |
190 | |
191 gfx::NativeView WebNotificationTrayWin::GetBubbleWindowContainer() { | |
192 return NULL; | |
193 } | |
194 | |
195 void WebNotificationTrayWin::OnStatusIconClicked() { | |
196 UpdateAnchorRect(); | |
197 message_center_tray_->ToggleMessageCenterBubble(); | |
198 } | |
199 | |
200 void WebNotificationTrayWin::HideBubbleWithView( | |
201 const views::TrayBubbleView* bubble_view) { | |
202 if (message_center_bubble_.get() && | |
203 bubble_view == message_center_bubble_->bubble_view()) { | |
204 message_center_tray_->HideMessageCenterBubble(); | |
205 } else if (popup_bubble_.get() && | |
206 bubble_view == popup_bubble_->bubble_view()) { | |
207 message_center_tray_->HidePopupBubble(); | |
208 } | |
209 } | |
210 | |
211 void WebNotificationTrayWin::UpdateAnchorRect() { | |
212 message_center_anchor_rect_ = GetMouseAnchorRect(); | |
213 } | |
214 | |
215 message_center::MessageCenterBubble* | |
216 WebNotificationTrayWin::GetMessageCenterBubbleForTest() { | |
217 if (!message_center_bubble_.get()) | |
218 return NULL; | |
219 return static_cast<message_center::MessageCenterBubble*>( | |
220 message_center_bubble_->bubble()); | |
221 } | |
222 | |
223 message_center::MessagePopupBubble* | |
224 WebNotificationTrayWin::GetPopupBubbleForTest() { | |
225 if (!popup_bubble_.get()) | |
226 return NULL; | |
227 return static_cast<message_center::MessagePopupBubble*>( | |
228 popup_bubble_->bubble()); | |
229 } | |
230 | |
231 } // namespace message_center | |
OLD | NEW |