| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chromeos/rotation/tray_rotation_lock.h" | 5 #include "ash/system/chromeos/rotation/tray_rotation_lock.h" |
| 6 | 6 |
| 7 #include "ash/common/material_design/material_design_controller.h" | 7 #include "ash/common/material_design/material_design_controller.h" |
| 8 #include "ash/common/system/tray/actionable_view.h" |
| 8 #include "ash/common/system/tray/system_tray.h" | 9 #include "ash/common/system/tray/system_tray.h" |
| 9 #include "ash/common/system/tray/tray_constants.h" | 10 #include "ash/common/system/tray/tray_constants.h" |
| 10 #include "ash/common/system/tray/tray_item_more.h" | |
| 11 #include "ash/common/system/tray/tray_popup_item_style.h" | 11 #include "ash/common/system/tray/tray_popup_item_style.h" |
| 12 #include "ash/common/system/tray/tray_popup_utils.h" |
| 13 #include "ash/common/system/tray/tri_view.h" |
| 12 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" | 14 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" |
| 13 #include "ash/common/wm_shell.h" | 15 #include "ash/common/wm_shell.h" |
| 14 #include "ash/display/screen_orientation_controller_chromeos.h" | 16 #include "ash/display/screen_orientation_controller_chromeos.h" |
| 15 #include "ash/resources/vector_icons/vector_icons.h" | 17 #include "ash/resources/vector_icons/vector_icons.h" |
| 16 #include "ash/shell.h" | 18 #include "ash/shell.h" |
| 17 #include "grit/ash_resources.h" | 19 #include "grit/ash_resources.h" |
| 18 #include "grit/ash_strings.h" | 20 #include "grit/ash_strings.h" |
| 21 #include "ui/accessibility/ax_node_data.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 21 #include "ui/display/display.h" | 24 #include "ui/display/display.h" |
| 22 #include "ui/gfx/paint_vector_icon.h" | 25 #include "ui/gfx/paint_vector_icon.h" |
| 26 #include "ui/views/controls/image_view.h" |
| 27 #include "ui/views/controls/label.h" |
| 28 #include "ui/views/layout/fill_layout.h" |
| 23 | 29 |
| 24 namespace ash { | 30 namespace ash { |
| 25 | 31 |
| 32 namespace { |
| 33 |
| 34 bool IsMaximizeModeWindowManagerEnabled() { |
| 35 return WmShell::Get() |
| 36 ->maximize_mode_controller() |
| 37 ->IsMaximizeModeWindowManagerEnabled(); |
| 38 } |
| 39 |
| 40 bool IsRotationLocked() { |
| 41 return Shell::GetInstance() |
| 42 ->screen_orientation_controller() |
| 43 ->rotation_locked(); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 26 namespace tray { | 48 namespace tray { |
| 27 | 49 |
| 28 // Extends TrayItemMore, however does not make use of the chevron, nor of the | 50 class RotationLockDefaultView : public ActionableView, |
| 29 // DetailedView. This was chosen over ActionableView in order to reuse the | 51 public ShellObserver, |
| 30 // layout and styling of labels and images. This allows RotationLockDefaultView | 52 public ScreenOrientationController::Observer { |
| 31 // to maintain the look of other system tray items without code duplication. | |
| 32 class RotationLockDefaultView : public TrayItemMore, public ShellObserver { | |
| 33 public: | 53 public: |
| 34 explicit RotationLockDefaultView(SystemTrayItem* owner); | 54 explicit RotationLockDefaultView(SystemTrayItem* owner); |
| 35 ~RotationLockDefaultView() override; | 55 ~RotationLockDefaultView() override; |
| 36 | 56 |
| 57 private: |
| 58 // Updates icon and label according to current rotation lock status. |
| 59 void Update(); |
| 60 |
| 61 // Stop observing rotation lock status. |
| 62 void StopObservingRotation(); |
| 63 |
| 37 // ActionableView: | 64 // ActionableView: |
| 65 void GetAccessibleNodeData(ui::AXNodeData* node_data) override; |
| 38 bool PerformAction(const ui::Event& event) override; | 66 bool PerformAction(const ui::Event& event) override; |
| 39 | 67 |
| 40 // ShellObserver: | 68 // ShellObserver: |
| 41 void OnMaximizeModeStarted() override; | 69 void OnMaximizeModeStarted() override; |
| 42 void OnMaximizeModeEnded() override; | 70 void OnMaximizeModeEnded() override; |
| 43 | 71 |
| 44 protected: | 72 // ScreenOrientationController::Obsever: |
| 45 // TrayItemMore: | 73 void OnRotationLockChanged(bool rotation_locked) override; |
| 46 void UpdateStyle() override; | |
| 47 | 74 |
| 48 private: | 75 views::ImageView* icon_; |
| 49 void UpdateImage(); | 76 views::Label* label_; |
| 50 | 77 |
| 51 DISALLOW_COPY_AND_ASSIGN(RotationLockDefaultView); | 78 DISALLOW_COPY_AND_ASSIGN(RotationLockDefaultView); |
| 52 }; | 79 }; |
| 53 | 80 |
| 54 RotationLockDefaultView::RotationLockDefaultView(SystemTrayItem* owner) | 81 RotationLockDefaultView::RotationLockDefaultView(SystemTrayItem* owner) |
| 55 : TrayItemMore(owner, false) { | 82 : ActionableView(owner, TrayPopupInkDropStyle::FILL_BOUNDS), |
| 56 SetVisible(WmShell::Get() | 83 icon_(TrayPopupUtils::CreateMainImageView()), |
| 57 ->maximize_mode_controller() | 84 label_(TrayPopupUtils::CreateDefaultLabel()) { |
| 58 ->IsMaximizeModeWindowManagerEnabled()); | 85 SetLayoutManager(new views::FillLayout); |
| 86 |
| 87 TriView* tri_view = TrayPopupUtils::CreateDefaultRowView(); |
| 88 AddChildView(tri_view); |
| 89 |
| 90 tri_view->AddView(TriView::Container::START, icon_); |
| 91 tri_view->AddView(TriView::Container::CENTER, label_); |
| 92 tri_view->SetContainerVisible(TriView::Container::END, false); |
| 93 |
| 94 Update(); |
| 95 |
| 96 SetInkDropMode(InkDropHostView::InkDropMode::ON); |
| 97 |
| 98 SetVisible(IsMaximizeModeWindowManagerEnabled()); |
| 59 WmShell::Get()->AddShellObserver(this); | 99 WmShell::Get()->AddShellObserver(this); |
| 100 if (IsMaximizeModeWindowManagerEnabled()) |
| 101 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this); |
| 60 } | 102 } |
| 61 | 103 |
| 62 RotationLockDefaultView::~RotationLockDefaultView() { | 104 RotationLockDefaultView::~RotationLockDefaultView() { |
| 105 StopObservingRotation(); |
| 63 WmShell::Get()->RemoveShellObserver(this); | 106 WmShell::Get()->RemoveShellObserver(this); |
| 64 } | 107 } |
| 65 | 108 |
| 109 void RotationLockDefaultView::Update() { |
| 110 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::DEFAULT_VIEW_LABEL); |
| 111 icon_->SetImage(gfx::CreateVectorIcon(IsRotationLocked() |
| 112 ? kSystemMenuRotationLockLockedIcon |
| 113 : kSystemMenuRotationLockAutoIcon, |
| 114 kMenuIconSize, style.GetIconColor())); |
| 115 |
| 116 base::string16 label = l10n_util::GetStringUTF16( |
| 117 IsRotationLocked() ? IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED |
| 118 : IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO); |
| 119 label_->SetText(label); |
| 120 style.SetupLabel(label_); |
| 121 |
| 122 Layout(); |
| 123 SchedulePaint(); |
| 124 } |
| 125 |
| 126 void RotationLockDefaultView::StopObservingRotation() { |
| 127 ScreenOrientationController* controller = |
| 128 Shell::GetInstance()->screen_orientation_controller(); |
| 129 if (controller) |
| 130 controller->RemoveObserver(this); |
| 131 } |
| 132 |
| 133 void RotationLockDefaultView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 134 ActionableView::GetAccessibleNodeData(node_data); |
| 135 if (!label_->text().empty()) |
| 136 node_data->SetName(label_->text()); |
| 137 } |
| 138 |
| 66 bool RotationLockDefaultView::PerformAction(const ui::Event& event) { | 139 bool RotationLockDefaultView::PerformAction(const ui::Event& event) { |
| 67 ScreenOrientationController* screen_orientation_controller = | 140 Shell::GetInstance()->screen_orientation_controller()->SetRotationLocked( |
| 68 Shell::GetInstance()->screen_orientation_controller(); | 141 !IsRotationLocked()); |
| 69 screen_orientation_controller->SetRotationLocked( | |
| 70 !screen_orientation_controller->rotation_locked()); | |
| 71 UpdateImage(); | |
| 72 return true; | 142 return true; |
| 73 } | 143 } |
| 74 | 144 |
| 75 void RotationLockDefaultView::OnMaximizeModeStarted() { | 145 void RotationLockDefaultView::OnMaximizeModeStarted() { |
| 76 UpdateImage(); | 146 Update(); |
| 77 SetVisible(true); | 147 SetVisible(true); |
| 148 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this); |
| 78 } | 149 } |
| 79 | 150 |
| 80 void RotationLockDefaultView::OnMaximizeModeEnded() { | 151 void RotationLockDefaultView::OnMaximizeModeEnded() { |
| 81 SetVisible(false); | 152 SetVisible(false); |
| 153 StopObservingRotation(); |
| 82 } | 154 } |
| 83 | 155 |
| 84 void RotationLockDefaultView::UpdateStyle() { | 156 void RotationLockDefaultView::OnRotationLockChanged(bool rotation_locked) { |
| 85 TrayItemMore::UpdateStyle(); | 157 Update(); |
| 86 UpdateImage(); | |
| 87 } | |
| 88 | |
| 89 void RotationLockDefaultView::UpdateImage() { | |
| 90 const bool rotation_locked = | |
| 91 Shell::GetInstance()->screen_orientation_controller()->rotation_locked(); | |
| 92 if (MaterialDesignController::UseMaterialDesignSystemIcons()) { | |
| 93 std::unique_ptr<TrayPopupItemStyle> style = CreateStyle(); | |
| 94 SetImage(gfx::CreateVectorIcon(rotation_locked | |
| 95 ? kSystemMenuRotationLockLockedIcon | |
| 96 : kSystemMenuRotationLockAutoIcon, | |
| 97 kMenuIconSize, style->GetIconColor())); | |
| 98 } else { | |
| 99 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 100 const int resource_id = rotation_locked | |
| 101 ? IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED_DARK | |
| 102 : IDR_AURA_UBER_TRAY_AUTO_ROTATION_DARK; | |
| 103 SetImage(*bundle.GetImageNamed(resource_id).ToImageSkia()); | |
| 104 } | |
| 105 | |
| 106 base::string16 label = l10n_util::GetStringUTF16( | |
| 107 rotation_locked ? IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED | |
| 108 : IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO); | |
| 109 SetLabel(label); | |
| 110 SetAccessibleName(label); | |
| 111 } | 158 } |
| 112 | 159 |
| 113 } // namespace tray | 160 } // namespace tray |
| 114 | 161 |
| 115 TrayRotationLock::TrayRotationLock(SystemTray* system_tray) | 162 TrayRotationLock::TrayRotationLock(SystemTray* system_tray) |
| 116 : TrayImageItem(system_tray, | 163 : TrayImageItem(system_tray, |
| 117 IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED, | 164 IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED, |
| 118 UMA_ROTATION_LOCK), | 165 UMA_ROTATION_LOCK) { |
| 119 observing_rotation_(false), | |
| 120 observing_shell_(true) { | |
| 121 WmShell::Get()->AddShellObserver(this); | 166 WmShell::Get()->AddShellObserver(this); |
| 122 } | 167 } |
| 123 | 168 |
| 124 TrayRotationLock::~TrayRotationLock() { | 169 TrayRotationLock::~TrayRotationLock() { |
| 125 StopObservingShell(); | 170 WmShell::Get()->RemoveShellObserver(this); |
| 126 } | 171 } |
| 127 | 172 |
| 128 void TrayRotationLock::OnRotationLockChanged(bool rotation_locked) { | 173 void TrayRotationLock::OnRotationLockChanged(bool rotation_locked) { |
| 129 tray_view()->SetVisible(ShouldBeVisible()); | 174 tray_view()->SetVisible(ShouldBeVisible()); |
| 130 } | 175 } |
| 131 | 176 |
| 132 views::View* TrayRotationLock::CreateDefaultView(LoginStatus status) { | 177 views::View* TrayRotationLock::CreateDefaultView(LoginStatus status) { |
| 133 if (OnPrimaryDisplay()) | 178 if (OnPrimaryDisplay()) |
| 134 return new tray::RotationLockDefaultView(this); | 179 return new tray::RotationLockDefaultView(this); |
| 135 return NULL; | 180 return NULL; |
| 136 } | 181 } |
| 137 | 182 |
| 138 void TrayRotationLock::OnMaximizeModeStarted() { | 183 void TrayRotationLock::OnMaximizeModeStarted() { |
| 139 tray_view()->SetVisible( | 184 tray_view()->SetVisible(IsRotationLocked()); |
| 140 Shell::GetInstance()->screen_orientation_controller()->rotation_locked()); | |
| 141 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this); | 185 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this); |
| 142 observing_rotation_ = true; | |
| 143 } | 186 } |
| 144 | 187 |
| 145 void TrayRotationLock::OnMaximizeModeEnded() { | 188 void TrayRotationLock::OnMaximizeModeEnded() { |
| 146 tray_view()->SetVisible(false); | 189 tray_view()->SetVisible(false); |
| 147 StopObservingRotation(); | 190 StopObservingRotation(); |
| 148 } | 191 } |
| 149 | 192 |
| 150 void TrayRotationLock::DestroyTrayView() { | 193 void TrayRotationLock::DestroyTrayView() { |
| 151 StopObservingRotation(); | 194 StopObservingRotation(); |
| 152 StopObservingShell(); | 195 WmShell::Get()->RemoveShellObserver(this); |
| 153 TrayImageItem::DestroyTrayView(); | 196 TrayImageItem::DestroyTrayView(); |
| 154 } | 197 } |
| 155 | 198 |
| 156 bool TrayRotationLock::GetInitialVisibility() { | 199 bool TrayRotationLock::GetInitialVisibility() { |
| 157 return ShouldBeVisible(); | 200 return ShouldBeVisible(); |
| 158 } | 201 } |
| 159 | 202 |
| 160 bool TrayRotationLock::ShouldBeVisible() { | 203 bool TrayRotationLock::ShouldBeVisible() { |
| 161 return OnPrimaryDisplay() && | 204 return OnPrimaryDisplay() && IsMaximizeModeWindowManagerEnabled() && |
| 162 WmShell::Get() | 205 IsRotationLocked(); |
| 163 ->maximize_mode_controller() | |
| 164 ->IsMaximizeModeWindowManagerEnabled() && | |
| 165 Shell::GetInstance() | |
| 166 ->screen_orientation_controller() | |
| 167 ->rotation_locked(); | |
| 168 } | 206 } |
| 169 | 207 |
| 170 bool TrayRotationLock::OnPrimaryDisplay() const { | 208 bool TrayRotationLock::OnPrimaryDisplay() const { |
| 171 gfx::NativeView native_view = system_tray()->GetWidget()->GetNativeView(); | 209 gfx::NativeView native_view = system_tray()->GetWidget()->GetNativeView(); |
| 172 display::Display parent_display = | 210 display::Display parent_display = |
| 173 display::Screen::GetScreen()->GetDisplayNearestWindow(native_view); | 211 display::Screen::GetScreen()->GetDisplayNearestWindow(native_view); |
| 174 return parent_display.IsInternal(); | 212 return parent_display.IsInternal(); |
| 175 } | 213 } |
| 176 | 214 |
| 177 void TrayRotationLock::StopObservingRotation() { | 215 void TrayRotationLock::StopObservingRotation() { |
| 178 if (!observing_rotation_) | |
| 179 return; | |
| 180 ScreenOrientationController* controller = | 216 ScreenOrientationController* controller = |
| 181 Shell::GetInstance()->screen_orientation_controller(); | 217 Shell::GetInstance()->screen_orientation_controller(); |
| 182 if (controller) | 218 if (controller) |
| 183 controller->RemoveObserver(this); | 219 controller->RemoveObserver(this); |
| 184 observing_rotation_ = false; | |
| 185 } | |
| 186 | |
| 187 void TrayRotationLock::StopObservingShell() { | |
| 188 if (!observing_shell_) | |
| 189 return; | |
| 190 WmShell::Get()->RemoveShellObserver(this); | |
| 191 observing_shell_ = false; | |
| 192 } | 220 } |
| 193 | 221 |
| 194 } // namespace ash | 222 } // namespace ash |
| OLD | NEW |