| 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/common/system/tray/system_tray.h" | 5 #include "ash/common/system/tray/system_tray.h" |
| 6 | 6 |
| 7 #include "ash/common/key_event_watcher.h" |
| 7 #include "ash/common/login_status.h" | 8 #include "ash/common/login_status.h" |
| 8 #include "ash/common/material_design/material_design_controller.h" | 9 #include "ash/common/material_design/material_design_controller.h" |
| 9 #include "ash/common/session/session_state_delegate.h" | 10 #include "ash/common/session/session_state_delegate.h" |
| 10 #include "ash/common/shelf/wm_shelf.h" | 11 #include "ash/common/shelf/wm_shelf.h" |
| 11 #include "ash/common/shelf/wm_shelf_util.h" | 12 #include "ash/common/shelf/wm_shelf_util.h" |
| 12 #include "ash/common/shell_window_ids.h" | 13 #include "ash/common/shell_window_ids.h" |
| 13 #include "ash/common/system/cast/tray_cast.h" | 14 #include "ash/common/system/cast/tray_cast.h" |
| 14 #include "ash/common/system/date/tray_date.h" | 15 #include "ash/common/system/date/tray_date.h" |
| 15 #include "ash/common/system/tray/system_tray_delegate.h" | 16 #include "ash/common/system/tray/system_tray_delegate.h" |
| 16 #include "ash/common/system/tray/system_tray_item.h" | 17 #include "ash/common/system/tray/system_tray_item.h" |
| 17 #include "ash/common/system/tray/tray_bubble_wrapper.h" | 18 #include "ash/common/system/tray/tray_bubble_wrapper.h" |
| 18 #include "ash/common/system/tray/tray_constants.h" | 19 #include "ash/common/system/tray/tray_constants.h" |
| 19 #include "ash/common/system/tray_accessibility.h" | 20 #include "ash/common/system/tray_accessibility.h" |
| 20 #include "ash/common/system/update/tray_update.h" | 21 #include "ash/common/system/update/tray_update.h" |
| 21 #include "ash/common/system/user/tray_user.h" | 22 #include "ash/common/system/user/tray_user.h" |
| 22 #include "ash/common/system/user/tray_user_separator.h" | 23 #include "ash/common/system/user/tray_user_separator.h" |
| 23 #include "ash/common/system/web_notification/web_notification_tray.h" | 24 #include "ash/common/system/web_notification/web_notification_tray.h" |
| 25 #include "ash/common/wm/container_finder.h" |
| 26 #include "ash/common/wm_activation_observer.h" |
| 24 #include "ash/common/wm_lookup.h" | 27 #include "ash/common/wm_lookup.h" |
| 25 #include "ash/common/wm_root_window_controller.h" | 28 #include "ash/common/wm_root_window_controller.h" |
| 26 #include "ash/common/wm_shell.h" | 29 #include "ash/common/wm_shell.h" |
| 27 #include "ash/common/wm_window.h" | 30 #include "ash/common/wm_window.h" |
| 28 #include "base/logging.h" | 31 #include "base/logging.h" |
| 29 #include "base/metrics/histogram.h" | 32 #include "base/metrics/histogram.h" |
| 30 #include "base/strings/utf_string_conversions.h" | 33 #include "base/strings/utf_string_conversions.h" |
| 31 #include "base/timer/timer.h" | 34 #include "base/timer/timer.h" |
| 32 #include "grit/ash_strings.h" | 35 #include "grit/ash_strings.h" |
| 36 #include "ui/base/accelerators/accelerator.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 34 #include "ui/compositor/layer.h" | 38 #include "ui/compositor/layer.h" |
| 35 #include "ui/display/display.h" | 39 #include "ui/display/display.h" |
| 36 #include "ui/display/screen.h" | 40 #include "ui/display/screen.h" |
| 37 #include "ui/events/event_constants.h" | 41 #include "ui/events/event_constants.h" |
| 38 #include "ui/gfx/canvas.h" | 42 #include "ui/gfx/canvas.h" |
| 39 #include "ui/gfx/skia_util.h" | 43 #include "ui/gfx/skia_util.h" |
| 40 #include "ui/views/border.h" | 44 #include "ui/views/border.h" |
| 41 #include "ui/views/controls/label.h" | 45 #include "ui/views/controls/label.h" |
| 42 #include "ui/views/view.h" | 46 #include "ui/views/view.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 bool is_persistent() const { return is_persistent_; } | 116 bool is_persistent() const { return is_persistent_; } |
| 113 | 117 |
| 114 private: | 118 private: |
| 115 std::unique_ptr<SystemTrayBubble> bubble_; | 119 std::unique_ptr<SystemTrayBubble> bubble_; |
| 116 std::unique_ptr<TrayBubbleWrapper> bubble_wrapper_; | 120 std::unique_ptr<TrayBubbleWrapper> bubble_wrapper_; |
| 117 bool is_persistent_; | 121 bool is_persistent_; |
| 118 | 122 |
| 119 DISALLOW_COPY_AND_ASSIGN(SystemBubbleWrapper); | 123 DISALLOW_COPY_AND_ASSIGN(SystemBubbleWrapper); |
| 120 }; | 124 }; |
| 121 | 125 |
| 126 // An activation observer to close the bubble if the window other |
| 127 // than system bubble nor popup notification is activated. |
| 128 class SystemTray::ActivationObserver : public WmActivationObserver { |
| 129 public: |
| 130 explicit ActivationObserver(SystemTray* tray) : tray_(tray) { |
| 131 DCHECK(tray_); |
| 132 WmShell::Get()->AddActivationObserver(this); |
| 133 } |
| 134 |
| 135 ~ActivationObserver() override { |
| 136 WmShell::Get()->RemoveActivationObserver(this); |
| 137 } |
| 138 |
| 139 // WmActivationObserver: |
| 140 void OnWindowActivated(WmWindow* gained_active, |
| 141 WmWindow* lost_active) override { |
| 142 if (!tray_->HasSystemBubble() || !gained_active) |
| 143 return; |
| 144 |
| 145 int container_id = |
| 146 wm::GetContainerForWindow(gained_active)->GetShellWindowId(); |
| 147 |
| 148 // Don't close the bubble if a popup notification is activated. |
| 149 if (container_id == kShellWindowId_StatusContainer) |
| 150 return; |
| 151 |
| 152 if (tray_->GetSystemBubble()->bubble_view()->GetWidget() != |
| 153 gained_active->GetInternalWidget()) { |
| 154 tray_->CloseSystemBubble(); |
| 155 } |
| 156 } |
| 157 void OnAttemptToReactivateWindow(WmWindow* request_active, |
| 158 WmWindow* actual_active) override {} |
| 159 |
| 160 private: |
| 161 SystemTray* tray_; |
| 162 |
| 163 DISALLOW_COPY_AND_ASSIGN(ActivationObserver); |
| 164 }; |
| 165 |
| 122 // SystemTray | 166 // SystemTray |
| 123 | 167 |
| 124 SystemTray::SystemTray(WmShelf* wm_shelf) | 168 SystemTray::SystemTray(WmShelf* wm_shelf) |
| 125 : TrayBackgroundView(wm_shelf), | 169 : TrayBackgroundView(wm_shelf), |
| 126 web_notification_tray_(nullptr), | 170 web_notification_tray_(nullptr), |
| 127 detailed_item_(nullptr), | 171 detailed_item_(nullptr), |
| 128 default_bubble_height_(0), | 172 default_bubble_height_(0), |
| 129 hide_notifications_(false), | 173 hide_notifications_(false), |
| 130 full_system_tray_menu_(false), | 174 full_system_tray_menu_(false), |
| 131 tray_accessibility_(nullptr), | 175 tray_accessibility_(nullptr), |
| 132 tray_audio_(nullptr), | 176 tray_audio_(nullptr), |
| 133 tray_cast_(nullptr), | 177 tray_cast_(nullptr), |
| 134 tray_date_(nullptr), | 178 tray_date_(nullptr), |
| 135 tray_update_(nullptr), | 179 tray_update_(nullptr), |
| 136 screen_capture_tray_item_(nullptr), | 180 screen_capture_tray_item_(nullptr), |
| 137 screen_share_tray_item_(nullptr) { | 181 screen_share_tray_item_(nullptr) { |
| 138 SetContentsBackground(); | 182 SetContentsBackground(); |
| 139 } | 183 } |
| 140 | 184 |
| 141 SystemTray::~SystemTray() { | 185 SystemTray::~SystemTray() { |
| 142 // Destroy any child views that might have back pointers before ~View(). | 186 // Destroy any child views that might have back pointers before ~View(). |
| 187 activation_observer_.reset(); |
| 188 key_event_watcher_.reset(); |
| 143 system_bubble_.reset(); | 189 system_bubble_.reset(); |
| 144 notification_bubble_.reset(); | 190 notification_bubble_.reset(); |
| 145 for (std::vector<SystemTrayItem*>::iterator it = items_.begin(); | 191 for (std::vector<SystemTrayItem*>::iterator it = items_.begin(); |
| 146 it != items_.end(); ++it) { | 192 it != items_.end(); ++it) { |
| 147 (*it)->DestroyTrayView(); | 193 (*it)->DestroyTrayView(); |
| 148 } | 194 } |
| 149 } | 195 } |
| 150 | 196 |
| 151 void SystemTray::InitializeTrayItems( | 197 void SystemTray::InitializeTrayItems( |
| 152 SystemTrayDelegate* delegate, | 198 SystemTrayDelegate* delegate, |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 // The menu width is fixed, and it is a per language setting. | 537 // The menu width is fixed, and it is a per language setting. |
| 492 int menu_width = std::max( | 538 int menu_width = std::max( |
| 493 MaterialDesignController::IsSystemTrayMenuMaterial() | 539 MaterialDesignController::IsSystemTrayMenuMaterial() |
| 494 ? kMinimumSystemTrayMenuWidthMd | 540 ? kMinimumSystemTrayMenuWidthMd |
| 495 : kMinimumSystemTrayMenuWidth, | 541 : kMinimumSystemTrayMenuWidth, |
| 496 WmShell::Get()->system_tray_delegate()->GetSystemTrayMenuWidth()); | 542 WmShell::Get()->system_tray_delegate()->GetSystemTrayMenuWidth()); |
| 497 | 543 |
| 498 TrayBubbleView::InitParams init_params(TrayBubbleView::ANCHOR_TYPE_TRAY, | 544 TrayBubbleView::InitParams init_params(TrayBubbleView::ANCHOR_TYPE_TRAY, |
| 499 GetAnchorAlignment(), menu_width, | 545 GetAnchorAlignment(), menu_width, |
| 500 kTrayPopupMaxWidth); | 546 kTrayPopupMaxWidth); |
| 501 init_params.can_activate = can_activate; | 547 // TODO(oshima): Change TrayBubbleView itself. |
| 548 init_params.can_activate = false; |
| 502 init_params.first_item_has_no_margin = true; | 549 init_params.first_item_has_no_margin = true; |
| 503 if (detailed) { | 550 if (detailed) { |
| 504 // This is the case where a volume control or brightness control bubble | 551 // This is the case where a volume control or brightness control bubble |
| 505 // is created. | 552 // is created. |
| 506 init_params.max_height = default_bubble_height_; | 553 init_params.max_height = default_bubble_height_; |
| 507 init_params.arrow_color = kBackgroundColor; | 554 init_params.arrow_color = kBackgroundColor; |
| 508 } else { | 555 } else { |
| 509 init_params.arrow_color = kHeaderBackgroundColor; | 556 init_params.arrow_color = kHeaderBackgroundColor; |
| 510 } | 557 } |
| 511 init_params.arrow_offset = arrow_offset; | 558 init_params.arrow_offset = arrow_offset; |
| 512 if (bubble_type == SystemTrayBubble::BUBBLE_TYPE_DEFAULT) | 559 if (bubble_type == SystemTrayBubble::BUBBLE_TYPE_DEFAULT) |
| 513 init_params.close_on_deactivate = !persistent; | 560 init_params.close_on_deactivate = !persistent; |
| 514 // For Volume and Brightness we don't want to show an arrow when | 561 // For Volume and Brightness we don't want to show an arrow when |
| 515 // they are shown in a bubble by themselves. | 562 // they are shown in a bubble by themselves. |
| 516 init_params.arrow_paint_type = views::BubbleBorder::PAINT_NORMAL; | 563 init_params.arrow_paint_type = views::BubbleBorder::PAINT_NORMAL; |
| 517 if (items.size() == 1 && items[0]->ShouldHideArrow()) | 564 if (items.size() == 1 && items[0]->ShouldHideArrow()) |
| 518 init_params.arrow_paint_type = views::BubbleBorder::PAINT_TRANSPARENT; | 565 init_params.arrow_paint_type = views::BubbleBorder::PAINT_TRANSPARENT; |
| 519 SystemTrayBubble* bubble = new SystemTrayBubble(this, items, bubble_type); | 566 SystemTrayBubble* bubble = new SystemTrayBubble(this, items, bubble_type); |
| 520 | 567 |
| 521 system_bubble_.reset(new SystemBubbleWrapper(bubble)); | 568 system_bubble_.reset(new SystemBubbleWrapper(bubble)); |
| 522 system_bubble_->InitView(this, tray_container(), &init_params, persistent); | 569 system_bubble_->InitView(this, tray_container(), &init_params, persistent); |
| 523 | 570 |
| 571 activation_observer_.reset(persistent ? nullptr |
| 572 : new ActivationObserver(this)); |
| 573 |
| 524 // Record metrics for the system menu when the default view is invoked. | 574 // Record metrics for the system menu when the default view is invoked. |
| 525 if (!detailed) | 575 if (!detailed) |
| 526 RecordSystemMenuMetrics(); | 576 RecordSystemMenuMetrics(); |
| 527 } | 577 } |
| 528 // Save height of default view for creating detailed views directly. | 578 // Save height of default view for creating detailed views directly. |
| 529 if (!detailed) | 579 if (!detailed) |
| 530 default_bubble_height_ = system_bubble_->bubble_view()->height(); | 580 default_bubble_height_ = system_bubble_->bubble_view()->height(); |
| 531 | 581 |
| 582 key_event_watcher_.reset(); |
| 583 if (can_activate) |
| 584 CreateKeyEventWatcher(); |
| 585 |
| 532 if (detailed && items.size() > 0) | 586 if (detailed && items.size() > 0) |
| 533 detailed_item_ = items[0]; | 587 detailed_item_ = items[0]; |
| 534 else | 588 else |
| 535 detailed_item_ = NULL; | 589 detailed_item_ = NULL; |
| 536 | 590 |
| 537 UpdateNotificationBubble(); // State changed, re-create notifications. | 591 UpdateNotificationBubble(); // State changed, re-create notifications. |
| 538 if (!notification_bubble_) | 592 if (!notification_bubble_) |
| 539 UpdateWebNotifications(); | 593 UpdateWebNotifications(); |
| 540 shelf()->UpdateAutoHideState(); | 594 shelf()->UpdateAutoHideState(); |
| 541 | 595 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 } | 778 } |
| 725 | 779 |
| 726 TrayDate* SystemTray::GetTrayDateForTesting() const { | 780 TrayDate* SystemTray::GetTrayDateForTesting() const { |
| 727 return tray_date_; | 781 return tray_date_; |
| 728 } | 782 } |
| 729 | 783 |
| 730 TrayUpdate* SystemTray::GetTrayUpdateForTesting() const { | 784 TrayUpdate* SystemTray::GetTrayUpdateForTesting() const { |
| 731 return tray_update_; | 785 return tray_update_; |
| 732 } | 786 } |
| 733 | 787 |
| 788 void SystemTray::CloseBubble(const ui::KeyEvent& key_event) { |
| 789 CloseSystemBubble(); |
| 790 } |
| 791 |
| 792 void SystemTray::ActivateAndStartNavigation(const ui::KeyEvent& key_event) { |
| 793 if (!system_bubble_) |
| 794 return; |
| 795 ActivateBubble(); |
| 796 views::Widget* widget = GetSystemBubble()->bubble_view()->GetWidget(); |
| 797 widget->GetFocusManager()->OnKeyEvent(key_event); |
| 798 } |
| 799 |
| 800 void SystemTray::CreateKeyEventWatcher() { |
| 801 key_event_watcher_ = WmShell::Get()->CreateKeyEventWatcher(); |
| 802 key_event_watcher_->AddKeyEventCallback( |
| 803 ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE), |
| 804 base::Bind(&SystemTray::CloseBubble, base::Unretained(this))); |
| 805 key_event_watcher_->AddKeyEventCallback( |
| 806 ui::Accelerator(ui::VKEY_TAB, ui::EF_NONE), |
| 807 base::Bind(&SystemTray::ActivateAndStartNavigation, |
| 808 base::Unretained(this))); |
| 809 key_event_watcher_->AddKeyEventCallback( |
| 810 ui::Accelerator(ui::VKEY_TAB, ui::EF_SHIFT_DOWN), |
| 811 base::Bind(&SystemTray::ActivateAndStartNavigation, |
| 812 base::Unretained(this))); |
| 813 } |
| 814 |
| 815 void SystemTray::ActivateBubble() { |
| 816 TrayBubbleView* bubble_view = GetSystemBubble()->bubble_view(); |
| 817 bubble_view->set_can_activate(true); |
| 818 bubble_view->GetWidget()->Activate(); |
| 819 } |
| 820 |
| 734 bool SystemTray::PerformAction(const ui::Event& event) { | 821 bool SystemTray::PerformAction(const ui::Event& event) { |
| 735 // If we're already showing the default view, hide it; otherwise, show it | 822 // If we're already showing the default view, hide it; otherwise, show it |
| 736 // (and hide any popup that's currently shown). | 823 // (and hide any popup that's currently shown). |
| 737 if (HasSystemBubbleType(SystemTrayBubble::BUBBLE_TYPE_DEFAULT)) { | 824 if (HasSystemBubbleType(SystemTrayBubble::BUBBLE_TYPE_DEFAULT)) { |
| 738 system_bubble_->bubble()->Close(); | 825 system_bubble_->bubble()->Close(); |
| 739 } else { | 826 } else { |
| 740 int arrow_offset = TrayBubbleView::InitParams::kArrowDefaultOffset; | 827 int arrow_offset = TrayBubbleView::InitParams::kArrowDefaultOffset; |
| 741 if (event.IsMouseEvent() || event.type() == ui::ET_GESTURE_TAP) { | 828 if (event.IsMouseEvent() || event.type() == ui::ET_GESTURE_TAP) { |
| 742 const ui::LocatedEvent& located_event = | 829 const ui::LocatedEvent& located_event = |
| 743 static_cast<const ui::LocatedEvent&>(event); | 830 static_cast<const ui::LocatedEvent&>(event); |
| 744 if (IsHorizontalAlignment(shelf_alignment())) { | 831 if (IsHorizontalAlignment(shelf_alignment())) { |
| 745 gfx::Point point(located_event.x(), 0); | 832 gfx::Point point(located_event.x(), 0); |
| 746 ConvertPointToWidget(this, &point); | 833 ConvertPointToWidget(this, &point); |
| 747 arrow_offset = point.x(); | 834 arrow_offset = point.x(); |
| 748 } | 835 } |
| 749 } | 836 } |
| 750 ShowDefaultViewWithOffset(BUBBLE_CREATE_NEW, arrow_offset, false); | 837 ShowDefaultViewWithOffset(BUBBLE_CREATE_NEW, arrow_offset, false); |
| 838 if (event.IsKeyEvent()) |
| 839 ActivateBubble(); |
| 751 } | 840 } |
| 752 return true; | 841 return true; |
| 753 } | 842 } |
| 754 | 843 |
| 755 void SystemTray::CloseSystemBubbleAndDeactivateSystemTray() { | 844 void SystemTray::CloseSystemBubbleAndDeactivateSystemTray() { |
| 845 activation_observer_.reset(); |
| 846 key_event_watcher_.reset(); |
| 756 system_bubble_.reset(); | 847 system_bubble_.reset(); |
| 757 // When closing a system bubble with the alternate shelf layout, we need to | 848 // When closing a system bubble with the alternate shelf layout, we need to |
| 758 // turn off the active tinting of the shelf. | 849 // turn off the active tinting of the shelf. |
| 759 if (full_system_tray_menu_) { | 850 if (full_system_tray_menu_) { |
| 760 SetDrawBackgroundAsActive(false); | 851 SetDrawBackgroundAsActive(false); |
| 761 full_system_tray_menu_ = false; | 852 full_system_tray_menu_ = false; |
| 762 } | 853 } |
| 763 } | 854 } |
| 764 | 855 |
| 765 void SystemTray::RecordSystemMenuMetrics() { | 856 void SystemTray::RecordSystemMenuMetrics() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 783 .work_area() | 874 .work_area() |
| 784 .height(); | 875 .height(); |
| 785 if (work_area_height > 0) { | 876 if (work_area_height > 0) { |
| 786 UMA_HISTOGRAM_CUSTOM_COUNTS( | 877 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 787 "Ash.SystemMenu.PercentageOfWorkAreaHeightCoveredByMenu", | 878 "Ash.SystemMenu.PercentageOfWorkAreaHeightCoveredByMenu", |
| 788 100 * bubble_view->height() / work_area_height, 1, 300, 100); | 879 100 * bubble_view->height() / work_area_height, 1, 300, 100); |
| 789 } | 880 } |
| 790 } | 881 } |
| 791 | 882 |
| 792 } // namespace ash | 883 } // namespace ash |
| OLD | NEW |