OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/system/tray/system_tray.h" | 5 #include "ash/system/tray/system_tray.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/shell/panel_window.h" | 8 #include "ash/shell/panel_window.h" |
9 #include "ash/shell_window_ids.h" | 9 #include "ash/shell_window_ids.h" |
10 #include "ash/system/audio/tray_volume.h" | 10 #include "ash/system/audio/tray_volume.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "ash/system/status_area_widget.h" | 23 #include "ash/system/status_area_widget.h" |
24 #include "ash/system/tray/system_tray_bubble.h" | 24 #include "ash/system/tray/system_tray_bubble.h" |
25 #include "ash/system/tray/system_tray_delegate.h" | 25 #include "ash/system/tray/system_tray_delegate.h" |
26 #include "ash/system/tray/system_tray_item.h" | 26 #include "ash/system/tray/system_tray_item.h" |
27 #include "ash/system/tray/tray_constants.h" | 27 #include "ash/system/tray/tray_constants.h" |
28 #include "ash/system/tray_accessibility.h" | 28 #include "ash/system/tray_accessibility.h" |
29 #include "ash/system/tray_caps_lock.h" | 29 #include "ash/system/tray_caps_lock.h" |
30 #include "ash/system/tray_update.h" | 30 #include "ash/system/tray_update.h" |
31 #include "ash/system/user/login_status.h" | 31 #include "ash/system/user/login_status.h" |
32 #include "ash/system/user/tray_user.h" | 32 #include "ash/system/user/tray_user.h" |
33 #include "ash/wm/shelf_layout_manager.h" | |
34 #include "base/logging.h" | 33 #include "base/logging.h" |
35 #include "base/timer.h" | 34 #include "base/timer.h" |
36 #include "base/utf_string_conversions.h" | 35 #include "base/utf_string_conversions.h" |
37 #include "grit/ash_strings.h" | 36 #include "grit/ash_strings.h" |
38 #include "ui/aura/root_window.h" | 37 #include "ui/aura/root_window.h" |
39 #include "ui/base/accessibility/accessible_view_state.h" | |
40 #include "ui/base/events.h" | 38 #include "ui/base/events.h" |
41 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
42 #include "ui/compositor/layer.h" | 40 #include "ui/compositor/layer.h" |
43 #include "ui/gfx/canvas.h" | 41 #include "ui/gfx/canvas.h" |
44 #include "ui/gfx/screen.h" | 42 #include "ui/gfx/screen.h" |
45 #include "ui/gfx/skia_util.h" | 43 #include "ui/gfx/skia_util.h" |
46 #include "ui/views/border.h" | 44 #include "ui/views/border.h" |
47 #include "ui/views/controls/label.h" | 45 #include "ui/views/controls/label.h" |
48 #include "ui/views/layout/box_layout.h" | 46 #include "ui/views/layout/box_layout.h" |
49 #include "ui/views/layout/fill_layout.h" | 47 #include "ui/views/layout/fill_layout.h" |
50 #include "ui/views/view.h" | 48 #include "ui/views/view.h" |
51 | 49 |
52 namespace ash { | 50 namespace ash { |
53 | 51 |
54 namespace internal { | |
55 | |
56 // Observe the tray layer animation and update the anchor when it changes. | |
57 // TODO(stevenjb): Observe or mirror the actual animation, not just the start | |
58 // and end points. | |
59 class SystemTrayLayerAnimationObserver : public ui::LayerAnimationObserver { | |
60 public: | |
61 explicit SystemTrayLayerAnimationObserver(SystemTray* host) : host_(host) {} | |
62 | |
63 virtual void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) { | |
64 host_->UpdateNotificationAnchor(); | |
65 } | |
66 | |
67 virtual void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) { | |
68 host_->UpdateNotificationAnchor(); | |
69 } | |
70 | |
71 virtual void OnLayerAnimationScheduled(ui::LayerAnimationSequence* sequence) { | |
72 host_->UpdateNotificationAnchor(); | |
73 } | |
74 | |
75 private: | |
76 SystemTray* host_; | |
77 | |
78 DISALLOW_COPY_AND_ASSIGN(SystemTrayLayerAnimationObserver); | |
79 }; | |
80 | |
81 } // namespace internal | |
82 | |
83 // SystemTray | 52 // SystemTray |
84 | 53 |
85 using internal::SystemTrayBubble; | 54 using internal::SystemTrayBubble; |
86 using internal::SystemTrayLayerAnimationObserver; | |
87 using internal::TrayBubbleView; | 55 using internal::TrayBubbleView; |
88 | 56 |
89 SystemTray::SystemTray(internal::StatusAreaWidget* status_area_widget) | 57 SystemTray::SystemTray(internal::StatusAreaWidget* status_area_widget) |
90 : internal::TrayBackgroundView(status_area_widget), | 58 : internal::TrayBackgroundView(status_area_widget), |
91 items_(), | 59 items_(), |
92 accessibility_observer_(NULL), | 60 accessibility_observer_(NULL), |
93 audio_observer_(NULL), | 61 audio_observer_(NULL), |
94 bluetooth_observer_(NULL), | 62 bluetooth_observer_(NULL), |
95 brightness_observer_(NULL), | 63 brightness_observer_(NULL), |
96 caps_lock_observer_(NULL), | 64 caps_lock_observer_(NULL), |
97 clock_observer_(NULL), | 65 clock_observer_(NULL), |
98 drive_observer_(NULL), | 66 drive_observer_(NULL), |
99 ime_observer_(NULL), | 67 ime_observer_(NULL), |
100 locale_observer_(NULL), | 68 locale_observer_(NULL), |
101 network_observer_(NULL), | 69 network_observer_(NULL), |
102 update_observer_(NULL), | 70 update_observer_(NULL), |
103 user_observer_(NULL), | 71 user_observer_(NULL), |
104 should_show_launcher_(false), | |
105 default_bubble_height_(0), | 72 default_bubble_height_(0), |
106 hide_notifications_(false) { | 73 hide_notifications_(false) { |
107 } | 74 } |
108 | 75 |
109 SystemTray::~SystemTray() { | 76 SystemTray::~SystemTray() { |
110 bubble_.reset(); | 77 bubble_.reset(); |
111 for (std::vector<SystemTrayItem*>::iterator it = items_.begin(); | 78 for (std::vector<SystemTrayItem*>::iterator it = items_.begin(); |
112 it != items_.end(); | 79 it != items_.end(); |
113 ++it) { | 80 ++it) { |
114 (*it)->DestroyTrayView(); | 81 (*it)->DestroyTrayView(); |
115 } | 82 } |
116 } | 83 } |
117 | 84 |
118 void SystemTray::Initialize() { | |
119 layer_animation_observer_.reset(new SystemTrayLayerAnimationObserver(this)); | |
120 GetWidget()->GetNativeView()->layer()->GetAnimator()->AddObserver( | |
121 layer_animation_observer_.get()); | |
122 } | |
123 | |
124 void SystemTray::CreateItems() { | 85 void SystemTray::CreateItems() { |
125 internal::TrayVolume* tray_volume = new internal::TrayVolume(); | 86 internal::TrayVolume* tray_volume = new internal::TrayVolume(); |
126 internal::TrayBluetooth* tray_bluetooth = new internal::TrayBluetooth(); | 87 internal::TrayBluetooth* tray_bluetooth = new internal::TrayBluetooth(); |
127 internal::TrayBrightness* tray_brightness = new internal::TrayBrightness(); | 88 internal::TrayBrightness* tray_brightness = new internal::TrayBrightness(); |
128 internal::TrayDate* tray_date = new internal::TrayDate(); | 89 internal::TrayDate* tray_date = new internal::TrayDate(); |
129 internal::TrayPower* tray_power = new internal::TrayPower(); | 90 internal::TrayPower* tray_power = new internal::TrayPower(); |
130 internal::TrayNetwork* tray_network = new internal::TrayNetwork; | 91 internal::TrayNetwork* tray_network = new internal::TrayNetwork; |
131 internal::TraySms* tray_sms = new internal::TraySms(); | 92 internal::TraySms* tray_sms = new internal::TraySms(); |
132 internal::TrayUser* tray_user = new internal::TrayUser; | 93 internal::TrayUser* tray_user = new internal::TrayUser; |
133 internal::TrayAccessibility* tray_accessibility = | 94 internal::TrayAccessibility* tray_accessibility = |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 AddTrayItem(tray_date); | 133 AddTrayItem(tray_date); |
173 SetVisible(ash::Shell::GetInstance()->tray_delegate()-> | 134 SetVisible(ash::Shell::GetInstance()->tray_delegate()-> |
174 GetTrayVisibilityOnStartup()); | 135 GetTrayVisibilityOnStartup()); |
175 } | 136 } |
176 | 137 |
177 void SystemTray::AddTrayItem(SystemTrayItem* item) { | 138 void SystemTray::AddTrayItem(SystemTrayItem* item) { |
178 items_.push_back(item); | 139 items_.push_back(item); |
179 | 140 |
180 SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate(); | 141 SystemTrayDelegate* delegate = Shell::GetInstance()->tray_delegate(); |
181 views::View* tray_item = item->CreateTrayView(delegate->GetUserLoginStatus()); | 142 views::View* tray_item = item->CreateTrayView(delegate->GetUserLoginStatus()); |
182 item->UpdateAfterShelfAlignmentChange( | 143 item->UpdateAfterShelfAlignmentChange(shelf_alignment()); |
183 ash::Shell::GetInstance()->system_tray()->shelf_alignment()); | |
184 | 144 |
185 if (tray_item) { | 145 if (tray_item) { |
186 tray_container()->AddChildViewAt(tray_item, 0); | 146 tray_container()->AddChildViewAt(tray_item, 0); |
187 PreferredSizeChanged(); | 147 PreferredSizeChanged(); |
188 tray_item_map_[item] = tray_item; | 148 tray_item_map_[item] = tray_item; |
189 } | 149 } |
190 } | 150 } |
191 | 151 |
192 void SystemTray::RemoveTrayItem(SystemTrayItem* item) { | 152 void SystemTray::RemoveTrayItem(SystemTrayItem* item) { |
193 NOTIMPLEMENTED(); | 153 NOTIMPLEMENTED(); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 (*it)->UpdateAfterShelfAlignmentChange(alignment); | 220 (*it)->UpdateAfterShelfAlignmentChange(alignment); |
261 } | 221 } |
262 } | 222 } |
263 | 223 |
264 void SystemTray::SetHideNotifications(bool hide_notifications) { | 224 void SystemTray::SetHideNotifications(bool hide_notifications) { |
265 if (notification_bubble_.get()) | 225 if (notification_bubble_.get()) |
266 notification_bubble_->SetVisible(!hide_notifications); | 226 notification_bubble_->SetVisible(!hide_notifications); |
267 hide_notifications_ = hide_notifications; | 227 hide_notifications_ = hide_notifications; |
268 } | 228 } |
269 | 229 |
| 230 bool SystemTray::IsSystemBubbleVisible() const { |
| 231 return (bubble_.get() && bubble_->IsVisible()); |
| 232 } |
| 233 |
270 bool SystemTray::IsAnyBubbleVisible() const { | 234 bool SystemTray::IsAnyBubbleVisible() const { |
271 if (bubble_.get() && bubble_->IsVisible()) | 235 if (bubble_.get() && bubble_->IsVisible()) |
272 return true; | 236 return true; |
273 if (notification_bubble_.get() && notification_bubble_->IsVisible()) | 237 if (notification_bubble_.get() && notification_bubble_->IsVisible()) |
274 return true; | 238 return true; |
275 return false; | 239 return false; |
276 } | 240 } |
277 | 241 |
| 242 bool SystemTray::IsMouseInNotificationBubble() const { |
| 243 if (!notification_bubble_.get()) |
| 244 return false; |
| 245 return notification_bubble_->bubble_view()->GetBoundsInScreen().Contains( |
| 246 gfx::Screen::GetCursorScreenPoint()); |
| 247 } |
| 248 |
278 bool SystemTray::CloseBubbleForTest() const { | 249 bool SystemTray::CloseBubbleForTest() const { |
279 if (!bubble_.get()) | 250 if (!bubble_.get()) |
280 return false; | 251 return false; |
281 bubble_->Close(); | 252 bubble_->Close(); |
282 return true; | 253 return true; |
283 } | 254 } |
284 | 255 |
285 // Private methods. | 256 // Private methods. |
286 | 257 |
287 void SystemTray::DestroyBubble() { | 258 void SystemTray::DestroyBubble() { |
288 bubble_.reset(); | 259 bubble_.reset(); |
289 detailed_item_ = NULL; | 260 detailed_item_ = NULL; |
290 } | 261 } |
291 | 262 |
292 void SystemTray::RemoveBubble(SystemTrayBubble* bubble) { | 263 void SystemTray::RemoveBubble(SystemTrayBubble* bubble) { |
293 if (bubble == bubble_.get()) { | 264 if (bubble == bubble_.get()) { |
294 DestroyBubble(); | 265 DestroyBubble(); |
295 UpdateNotificationBubble(); // State changed, re-create notifications. | 266 UpdateNotificationBubble(); // State changed, re-create notifications. |
296 if (should_show_launcher_) { | 267 UpdateShouldShowLauncher(); |
297 // No need to show the launcher if the mouse isn't over the status area | |
298 // anymore. | |
299 should_show_launcher_ = GetWidget()->GetWindowBoundsInScreen().Contains( | |
300 gfx::Screen::GetCursorScreenPoint()); | |
301 if (!should_show_launcher_) | |
302 Shell::GetInstance()->shelf()->UpdateAutoHideState(); | |
303 } | |
304 } else if (bubble == notification_bubble_) { | 268 } else if (bubble == notification_bubble_) { |
305 notification_bubble_.reset(); | 269 notification_bubble_.reset(); |
306 } else { | 270 } else { |
307 NOTREACHED(); | 271 NOTREACHED(); |
308 } | 272 } |
309 } | 273 } |
310 | 274 |
311 int SystemTray::GetTrayXOffset(SystemTrayItem* item) const { | 275 int SystemTray::GetTrayXOffset(SystemTrayItem* item) const { |
312 // Don't attempt to align the arrow if the shelf is on the left or right. | 276 // Don't attempt to align the arrow if the shelf is on the left or right. |
313 if (shelf_alignment() != SHELF_ALIGNMENT_BOTTOM) | 277 if (shelf_alignment() != SHELF_ALIGNMENT_BOTTOM) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 } | 334 } |
371 // Save height of default view for creating detailed views directly. | 335 // Save height of default view for creating detailed views directly. |
372 if (!detailed) | 336 if (!detailed) |
373 default_bubble_height_ = bubble_->bubble_view()->height(); | 337 default_bubble_height_ = bubble_->bubble_view()->height(); |
374 | 338 |
375 if (detailed && items.size() > 0) | 339 if (detailed && items.size() > 0) |
376 detailed_item_ = items[0]; | 340 detailed_item_ = items[0]; |
377 else | 341 else |
378 detailed_item_ = NULL; | 342 detailed_item_ = NULL; |
379 | 343 |
380 // If we have focus the shelf should be visible and we need to continue | |
381 // showing the shelf when the popup is shown. | |
382 if (GetWidget()->IsActive()) | |
383 should_show_launcher_ = true; | |
384 | |
385 UpdateNotificationBubble(); // State changed, re-create notifications. | 344 UpdateNotificationBubble(); // State changed, re-create notifications. |
386 status_area_widget()->HideNonSystemNotifications(); | 345 status_area_widget()->HideNonSystemNotifications(); |
| 346 UpdateShouldShowLauncher(); |
387 } | 347 } |
388 | 348 |
389 void SystemTray::UpdateNotificationBubble() { | 349 void SystemTray::UpdateNotificationBubble() { |
390 // Only show the notification buble if we have notifications and we are not | 350 // Only show the notification buble if we have notifications and we are not |
391 // showing the default bubble. | 351 // showing the default bubble. |
392 if (notification_items_.empty() || | 352 if (notification_items_.empty() || |
393 (bubble_.get() && | 353 (bubble_.get() && |
394 bubble_->bubble_type() == SystemTrayBubble::BUBBLE_TYPE_DEFAULT)) { | 354 bubble_->bubble_type() == SystemTrayBubble::BUBBLE_TYPE_DEFAULT)) { |
395 notification_bubble_.reset(); | 355 notification_bubble_.reset(); |
396 return; | 356 return; |
(...skipping 29 matching lines...) Expand all Loading... |
426 init_params.arrow_color = kBackgroundColor; | 386 init_params.arrow_color = kBackgroundColor; |
427 user::LoginStatus login_status = | 387 user::LoginStatus login_status = |
428 Shell::GetInstance()->tray_delegate()->GetUserLoginStatus(); | 388 Shell::GetInstance()->tray_delegate()->GetUserLoginStatus(); |
429 notification_bubble_->InitView(anchor, init_params, login_status); | 389 notification_bubble_->InitView(anchor, init_params, login_status); |
430 if (hide_notifications_) | 390 if (hide_notifications_) |
431 notification_bubble_->SetVisible(false); | 391 notification_bubble_->SetVisible(false); |
432 else | 392 else |
433 status_area_widget()->HideNonSystemNotifications(); | 393 status_area_widget()->HideNonSystemNotifications(); |
434 } | 394 } |
435 | 395 |
436 void SystemTray::UpdateNotificationAnchor() { | |
437 if (!notification_bubble_.get()) | |
438 return; | |
439 notification_bubble_->bubble_view()->UpdateBubble(); | |
440 // Ensure that the notification buble is above the launcher/status area. | |
441 notification_bubble_->bubble_view()->GetWidget()->StackAtTop(); | |
442 } | |
443 | |
444 void SystemTray::SetShelfAlignment(ShelfAlignment alignment) { | 396 void SystemTray::SetShelfAlignment(ShelfAlignment alignment) { |
445 if (alignment == shelf_alignment()) | 397 if (alignment == shelf_alignment()) |
446 return; | 398 return; |
447 internal::TrayBackgroundView::SetShelfAlignment(alignment); | 399 internal::TrayBackgroundView::SetShelfAlignment(alignment); |
448 UpdateAfterShelfAlignmentChange(alignment); | 400 UpdateAfterShelfAlignmentChange(alignment); |
449 // Destroy any existing bubble so that it is rebuilt correctly. | 401 // Destroy any existing bubble so that it is rebuilt correctly. |
450 bubble_.reset(); | 402 bubble_.reset(); |
451 // Rebuild any notification bubble. | 403 // Rebuild any notification bubble. |
452 if (notification_bubble_.get()) { | 404 if (notification_bubble_.get()) { |
453 notification_bubble_.reset(); | 405 notification_bubble_.reset(); |
454 UpdateNotificationBubble(); | 406 UpdateNotificationBubble(); |
455 } | 407 } |
456 } | 408 } |
457 | 409 |
| 410 void SystemTray::AnchorUpdated() { |
| 411 if (notification_bubble_.get()) { |
| 412 notification_bubble_->bubble_view()->UpdateBubble(); |
| 413 // Ensure that the notification buble is above the launcher/status area. |
| 414 notification_bubble_->bubble_view()->GetWidget()->StackAtTop(); |
| 415 } |
| 416 if (bubble_.get()) |
| 417 bubble_->bubble_view()->UpdateBubble(); |
| 418 } |
| 419 |
| 420 string16 SystemTray::GetAccessibleName() { |
| 421 return l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ACCESSIBLE_NAME); |
| 422 } |
| 423 |
458 bool SystemTray::PerformAction(const ui::Event& event) { | 424 bool SystemTray::PerformAction(const ui::Event& event) { |
459 // If we're already showing the default view, hide it; otherwise, show it | 425 // If we're already showing the default view, hide it; otherwise, show it |
460 // (and hide any popup that's currently shown). | 426 // (and hide any popup that's currently shown). |
461 if (bubble_.get() && | 427 if (bubble_.get() && |
462 bubble_->bubble_type() == SystemTrayBubble::BUBBLE_TYPE_DEFAULT) { | 428 bubble_->bubble_type() == SystemTrayBubble::BUBBLE_TYPE_DEFAULT) { |
463 bubble_->Close(); | 429 bubble_->Close(); |
464 } else { | 430 } else { |
465 int arrow_offset = TrayBubbleView::InitParams::kArrowDefaultOffset; | 431 int arrow_offset = TrayBubbleView::InitParams::kArrowDefaultOffset; |
466 if (event.IsMouseEvent() || event.type() == ui::ET_GESTURE_TAP) { | 432 if (event.IsMouseEvent() || event.type() == ui::ET_GESTURE_TAP) { |
467 const views::LocatedEvent& located_event = | 433 const views::LocatedEvent& located_event = |
468 static_cast<const views::LocatedEvent&>(event); | 434 static_cast<const views::LocatedEvent&>(event); |
469 if (shelf_alignment() == SHELF_ALIGNMENT_BOTTOM) { | 435 if (shelf_alignment() == SHELF_ALIGNMENT_BOTTOM) { |
470 gfx::Point point(located_event.x(), 0); | 436 gfx::Point point(located_event.x(), 0); |
471 ConvertPointToWidget(this, &point); | 437 ConvertPointToWidget(this, &point); |
472 arrow_offset = point.x(); | 438 arrow_offset = point.x(); |
473 } | 439 } |
474 } | 440 } |
475 ShowDefaultViewWithOffset(BUBBLE_CREATE_NEW, arrow_offset); | 441 ShowDefaultViewWithOffset(BUBBLE_CREATE_NEW, arrow_offset); |
476 } | 442 } |
477 return true; | 443 return true; |
478 } | 444 } |
479 | 445 |
480 void SystemTray::OnMouseEntered(const views::MouseEvent& event) { | |
481 TrayBackgroundView::OnMouseEntered(event); | |
482 should_show_launcher_ = true; | |
483 } | |
484 | |
485 void SystemTray::OnMouseExited(const views::MouseEvent& event) { | |
486 TrayBackgroundView::OnMouseExited(event); | |
487 // When the popup closes we'll update |should_show_launcher_|. | |
488 if (!bubble_.get()) | |
489 should_show_launcher_ = false; | |
490 } | |
491 | |
492 void SystemTray::AboutToRequestFocusFromTabTraversal(bool reverse) { | |
493 views::View* v = GetNextFocusableView(); | |
494 if (v) | |
495 v->AboutToRequestFocusFromTabTraversal(reverse); | |
496 } | |
497 | |
498 void SystemTray::GetAccessibleState(ui::AccessibleViewState* state) { | |
499 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; | |
500 state->name = l10n_util::GetStringUTF16( | |
501 IDS_ASH_STATUS_TRAY_ACCESSIBLE_NAME); | |
502 } | |
503 | |
504 void SystemTray::OnPaintFocusBorder(gfx::Canvas* canvas) { | |
505 // The tray itself expands to the right and bottom edge of the screen to make | |
506 // sure clicking on the edges brings up the popup. However, the focus border | |
507 // should be only around the container. | |
508 if (GetWidget() && GetWidget()->IsActive()) | |
509 DrawBorder(canvas, GetContentsBounds()); | |
510 } | |
511 | |
512 } // namespace ash | 446 } // namespace ash |
OLD | NEW |