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_popup_bubble.h" | |
21 #include "ui/views/widget/widget.h" | |
22 | |
23 namespace { | |
24 | |
25 // Tray constants | |
26 const int kPaddingFromLeftEdgeOfSystemTrayBottomAlignment = 8; | |
27 | |
28 gfx::Rect GetCornerAnchorRect(gfx::Size preferred_size) { | |
29 // TODO(dewittj): Use the preference to determine which corner to anchor from. | |
30 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | |
31 gfx::Rect rect = screen->GetPrimaryDisplay().work_area(); | |
32 rect.Inset(10, 5); | |
33 gfx::Point bottom_right( | |
34 rect.bottom_right().x() - preferred_size.width() / 2, | |
35 rect.bottom_right().y()); | |
36 return gfx::Rect(bottom_right, gfx::Size()); | |
37 } | |
38 | |
39 // GetMouseAnchorRect returns a rectangle that is near the cursor point, but | |
40 // whose behavior depends on where the Windows taskbar is. If it is on the | |
41 // top or bottom of the screen, we want the arrow to touch the edge of the | |
42 // taskbar directly above or below the mouse pointer and within the work area. | |
43 // Otherwise, position the anchor on the mouse cursor directly. | |
44 gfx::Rect GetMouseAnchorRect() { | |
45 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | |
46 gfx::Rect usable_area = screen->GetPrimaryDisplay().bounds(); | |
47 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area(); | |
48 | |
49 // Inset the rectangle by the taskbar width if it is on top or bottom. | |
50 usable_area.set_y(work_area.y()); | |
51 usable_area.set_height(work_area.height()); | |
52 | |
53 // Keep the anchor from being too close to the edge of the screen. | |
54 usable_area.Inset(kPaddingFromLeftEdgeOfSystemTrayBottomAlignment, 0); | |
55 | |
56 // Use a mouse point that is on the mouse cursor, unless the mouse is over the | |
57 // start menu and the start menu is on the top or bottom. | |
58 gfx::Point cursor = screen->GetCursorScreenPoint(); | |
59 gfx::Rect mouse_anchor_rect( | |
60 gfx::BoundingRect(cursor, usable_area.bottom_right())); | |
61 mouse_anchor_rect.set_height(0); | |
62 if (!usable_area.Contains(cursor)) | |
63 mouse_anchor_rect.AdjustToFit(usable_area); | |
64 mouse_anchor_rect.set_width(0); | |
65 return mouse_anchor_rect; | |
66 } | |
67 | |
68 gfx::ImageSkia GetIcon(bool has_unread_notifications) { | |
69 // TODO(dewittj): Use an icon resource for both unread and read notifications. | |
70 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
71 gfx::ImageSkia* icon = | |
72 rb.GetImageSkiaNamed(IDR_ALLOWED_NOTIFICATION); | |
73 if (has_unread_notifications) | |
74 return *icon; | |
75 return gfx::ImageSkiaOperations::CreateTransparentImage(*icon, .5); | |
76 } | |
77 | |
78 } // namespace | |
79 | |
80 namespace ui { | |
81 | |
82 // Ash defines this function elsewhere. | |
83 #if !defined(USE_ASH) | |
84 | |
85 // static | |
86 MessageCenterTrayDelegate* | |
87 MessageCenterTrayDelegate::CreateForPlatform() { | |
88 return new WebNotificationTrayWin(); | |
89 } | |
stevenjb
2013/01/23 23:14:18
Same comment about using the ui:: namespace here.
dewittj
2013/01/25 00:49:04
Non-ash non-windows platforms don't get message ce
| |
90 | |
91 #endif | |
92 | |
93 WebNotificationTrayWin::WebNotificationTrayWin() | |
94 : status_icon_(NULL), | |
95 message_center_visible_(false) { | |
96 message_center_tray_.reset(new MessageCenterTray( | |
97 this, g_browser_process->message_center())); | |
98 StatusTray* status_tray = g_browser_process->status_tray(); | |
99 status_icon_ = status_tray->CreateStatusIcon(); | |
100 status_icon_->AddObserver(this); | |
101 status_icon_->SetImage( | |
102 GetIcon(message_center()->UnreadNotificationCount() > 0)); | |
103 } | |
104 | |
105 WebNotificationTrayWin::~WebNotificationTrayWin() { | |
106 // Reset this early so that delegated events during destruction don't cause | |
107 // problems. | |
108 message_center_tray_.reset(); | |
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 | |
OLD | NEW |