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 | |
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 DCHECK(icon); | |
75 if (has_unread_notifications) | |
76 return *icon; | |
77 return gfx::ImageSkiaOperations::CreateTransparentImage(*icon, .5); | |
78 } | |
79 | |
80 } // namespace | |
81 | |
82 namespace message_center { | |
83 | |
84 MessageCenterTrayDelegate* CreateMessageCenterTray() { | |
85 return new WebNotificationTrayWin(); | |
86 } | |
87 | |
88 WebNotificationTrayWin::WebNotificationTrayWin() | |
89 : status_icon_(NULL), | |
90 message_center_visible_(false) { | |
91 message_center_tray_.reset(new MessageCenterTray( | |
92 this, g_browser_process->message_center())); | |
93 StatusTray* status_tray = g_browser_process->status_tray(); | |
94 status_icon_ = status_tray->CreateStatusIcon(); | |
95 status_icon_->AddObserver(this); | |
96 status_icon_->SetImage( | |
97 GetIcon(message_center()->UnreadNotificationCount() > 0)); | |
98 } | |
99 | |
100 WebNotificationTrayWin::~WebNotificationTrayWin() { | |
101 // Reset this early so that delegated events during destruction don't cause | |
102 // problems. | |
103 message_center_tray_.reset(); | |
104 status_icon_->RemoveObserver(this); | |
105 StatusTray* status_tray = g_browser_process->status_tray(); | |
106 status_tray->RemoveStatusIcon(status_icon_); | |
107 status_icon_ = NULL; | |
108 } | |
109 | |
110 message_center::MessageCenter* WebNotificationTrayWin::message_center() { | |
111 return message_center_tray_->message_center(); | |
112 } | |
113 | |
114 bool WebNotificationTrayWin::ShowPopups() { | |
115 message_center::MessagePopupBubble* bubble = | |
116 new message_center::MessagePopupBubble(message_center()); | |
117 popup_bubble_.reset(new internal::NotificationBubbleWrapperWin( | |
118 this, bubble, views::TrayBubbleView::ANCHOR_TYPE_BUBBLE)); | |
119 return true; | |
120 } | |
121 | |
122 void WebNotificationTrayWin::HidePopups() { | |
123 popup_bubble_.reset(); | |
124 } | |
125 | |
126 bool WebNotificationTrayWin::ShowMessageCenter() { | |
127 // Calculate the maximum height of the message center, given its anchor. | |
128 message_center::MessageCenterBubble* bubble = | |
129 new message_center::MessageCenterBubble(message_center()); | |
130 gfx::Point anchor_center = message_center_anchor_rect_.CenterPoint(); | |
131 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | |
132 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area(); | |
133 gfx::Point work_area_center = work_area.CenterPoint(); | |
134 const int zMarginFromEdgeOfWorkArea = 10; | |
135 int max_height = 0; | |
136 if (work_area_center < anchor_center) | |
137 max_height = anchor_center.y() - work_area.origin().y(); | |
138 else | |
139 max_height = work_area.bottom() - message_center_anchor_rect_.bottom(); | |
140 bubble->SetMaxHeight(max_height - zMarginFromEdgeOfWorkArea); | |
141 | |
142 message_center_bubble_.reset(new internal::NotificationBubbleWrapperWin( | |
143 this, | |
144 bubble, | |
145 views::TrayBubbleView::ANCHOR_TYPE_TRAY)); | |
146 return true; | |
147 } | |
148 | |
149 void WebNotificationTrayWin::HideMessageCenter() { | |
150 message_center_bubble_.reset(); | |
151 } | |
152 | |
153 void WebNotificationTrayWin::UpdateMessageCenter() { | |
154 if (message_center_bubble_.get()) | |
155 message_center_bubble_->bubble()->ScheduleUpdate(); | |
156 } | |
157 | |
158 void WebNotificationTrayWin::UpdatePopups() { | |
159 if (popup_bubble_.get()) | |
160 popup_bubble_->bubble()->ScheduleUpdate(); | |
161 }; | |
162 | |
163 void WebNotificationTrayWin::OnMessageCenterTrayChanged() { | |
164 bool has_unread_notifications = | |
165 message_center()->UnreadNotificationCount() > 0; | |
166 status_icon_->SetImage(GetIcon(has_unread_notifications)); | |
167 } | |
168 | |
169 gfx::Rect WebNotificationTrayWin::GetAnchorRect( | |
170 gfx::Size preferred_size, | |
171 views::TrayBubbleView::AnchorType anchor_type, | |
172 views::TrayBubbleView::AnchorAlignment anchor_alignment) { | |
173 if (anchor_type == views::TrayBubbleView::ANCHOR_TYPE_TRAY) | |
174 return message_center_anchor_rect_; | |
175 return GetCornerAnchorRect(preferred_size); | |
176 } | |
177 | |
178 views::TrayBubbleView::AnchorAlignment | |
179 WebNotificationTrayWin::GetAnchorAlignment() { | |
180 gfx::Screen* screen = gfx::Screen::GetNativeScreen(); | |
181 // TODO(dewittj): It's possible GetPrimaryDisplay is wrong. | |
182 gfx::Rect screen_bounds = screen->GetPrimaryDisplay().bounds(); | |
183 gfx::Rect work_area = screen->GetPrimaryDisplay().work_area(); | |
184 | |
185 if (work_area.height() < screen_bounds.height()) | |
msw
2013/01/29 23:23:16
nit: Offhand, this is confusing, why are you compa
dewittj
2013/01/30 23:08:32
Done.
| |
186 return views::TrayBubbleView::ANCHOR_ALIGNMENT_BOTTOM; | |
187 if (work_area.x() > screen_bounds.x()) | |
188 return views::TrayBubbleView::ANCHOR_ALIGNMENT_LEFT; | |
189 return views::TrayBubbleView::ANCHOR_ALIGNMENT_RIGHT; | |
190 } | |
191 | |
192 gfx::NativeView WebNotificationTrayWin::GetBubbleWindowContainer() { | |
193 return NULL; | |
194 } | |
195 | |
196 void WebNotificationTrayWin::OnStatusIconClicked() { | |
197 UpdateAnchorRect(); | |
198 message_center_tray_->ToggleMessageCenterBubble(); | |
199 } | |
200 | |
201 void WebNotificationTrayWin::HideBubbleWithView( | |
202 const views::TrayBubbleView* bubble_view) { | |
203 if (message_center_bubble_.get() && | |
204 bubble_view == message_center_bubble_->bubble_view()) { | |
205 message_center_tray_->HideMessageCenterBubble(); | |
206 } else if (popup_bubble_.get() && | |
207 bubble_view == popup_bubble_->bubble_view()) { | |
208 message_center_tray_->HidePopupBubble(); | |
209 } | |
210 } | |
211 | |
212 void WebNotificationTrayWin::UpdateAnchorRect() { | |
213 message_center_anchor_rect_ = GetMouseAnchorRect(); | |
214 } | |
215 | |
216 message_center::MessageCenterBubble* | |
217 WebNotificationTrayWin::GetMessageCenterBubbleForTest() { | |
218 if (!message_center_bubble_.get()) | |
219 return NULL; | |
220 return static_cast<message_center::MessageCenterBubble*>( | |
221 message_center_bubble_->bubble()); | |
222 } | |
223 | |
224 message_center::MessagePopupBubble* | |
225 WebNotificationTrayWin::GetPopupBubbleForTest() { | |
226 if (!popup_bubble_.get()) | |
227 return NULL; | |
228 return static_cast<message_center::MessagePopupBubble*>( | |
229 popup_bubble_->bubble()); | |
230 } | |
231 | |
232 } // namespace message_center | |
OLD | NEW |