Chromium Code Reviews| 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 { | |
|
tdanderson
2017/02/16 01:00:12
nit: new line after lines 32 and 43
mohsen
2017/02/16 04:15:17
Done.
| |
| 33 bool IsMaximizeModeWindowManagerEnabled() { | |
| 34 return WmShell::Get() | |
| 35 ->maximize_mode_controller() | |
| 36 ->IsMaximizeModeWindowManagerEnabled(); | |
| 37 } | |
| 38 | |
| 39 bool IsRotationLocked() { | |
| 40 return Shell::GetInstance() | |
| 41 ->screen_orientation_controller() | |
| 42 ->rotation_locked(); | |
| 43 } | |
| 44 } // namespce | |
|
tdanderson
2017/02/16 01:00:12
nit: 'namespace'
mohsen
2017/02/16 04:15:17
Done.
| |
| 45 | |
| 26 namespace tray { | 46 namespace tray { |
| 27 | 47 |
| 28 // Extends TrayItemMore, however does not make use of the chevron, nor of the | 48 class RotationLockDefaultView : public ActionableView, |
| 29 // DetailedView. This was chosen over ActionableView in order to reuse the | 49 public ShellObserver, |
| 30 // layout and styling of labels and images. This allows RotationLockDefaultView | 50 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: | 51 public: |
| 34 explicit RotationLockDefaultView(SystemTrayItem* owner); | 52 explicit RotationLockDefaultView(SystemTrayItem* owner); |
| 35 ~RotationLockDefaultView() override; | 53 ~RotationLockDefaultView() override; |
| 36 | 54 |
| 55 private: | |
| 56 // Updates icon and label according to current rotation lock status. | |
| 57 void Update(); | |
| 58 | |
| 59 // Stop observing rotation locak status. | |
|
tdanderson
2017/02/16 01:00:12
nit: 'lock'
mohsen
2017/02/16 04:15:17
Done.
| |
| 60 void StopObservingRotation(); | |
| 61 | |
| 37 // ActionableView: | 62 // ActionableView: |
| 63 void GetAccessibleNodeData(ui::AXNodeData* node_data) override; | |
| 38 bool PerformAction(const ui::Event& event) override; | 64 bool PerformAction(const ui::Event& event) override; |
| 39 | 65 |
| 40 // ShellObserver: | 66 // ShellObserver: |
| 41 void OnMaximizeModeStarted() override; | 67 void OnMaximizeModeStarted() override; |
| 42 void OnMaximizeModeEnded() override; | 68 void OnMaximizeModeEnded() override; |
| 43 | 69 |
| 44 protected: | 70 // ScreenOrientationController::Obsever: |
| 45 // TrayItemMore: | 71 void OnRotationLockChanged(bool rotation_locked) override; |
| 46 void UpdateStyle() override; | |
| 47 | 72 |
| 48 private: | 73 views::ImageView* icon_; |
| 49 void UpdateImage(); | 74 views::Label* label_; |
| 50 | 75 |
| 51 DISALLOW_COPY_AND_ASSIGN(RotationLockDefaultView); | 76 DISALLOW_COPY_AND_ASSIGN(RotationLockDefaultView); |
| 52 }; | 77 }; |
| 53 | 78 |
| 54 RotationLockDefaultView::RotationLockDefaultView(SystemTrayItem* owner) | 79 RotationLockDefaultView::RotationLockDefaultView(SystemTrayItem* owner) |
| 55 : TrayItemMore(owner, false) { | 80 : ActionableView(owner, TrayPopupInkDropStyle::FILL_BOUNDS), |
| 56 SetVisible(WmShell::Get() | 81 icon_(TrayPopupUtils::CreateMainImageView()), |
| 57 ->maximize_mode_controller() | 82 label_(TrayPopupUtils::CreateDefaultLabel()) { |
| 58 ->IsMaximizeModeWindowManagerEnabled()); | 83 SetLayoutManager(new views::FillLayout); |
| 84 | |
| 85 TriView* tri_view = TrayPopupUtils::CreateDefaultRowView(); | |
| 86 AddChildView(tri_view); | |
| 87 | |
| 88 tri_view->AddView(TriView::Container::START, icon_); | |
| 89 tri_view->AddView(TriView::Container::CENTER, label_); | |
| 90 tri_view->SetContainerVisible(TriView::Container::END, false); | |
| 91 | |
| 92 Update(); | |
| 93 | |
| 94 SetInkDropMode(InkDropHostView::InkDropMode::ON); | |
| 95 | |
| 96 SetVisible(IsMaximizeModeWindowManagerEnabled()); | |
| 59 WmShell::Get()->AddShellObserver(this); | 97 WmShell::Get()->AddShellObserver(this); |
| 98 if (IsMaximizeModeWindowManagerEnabled()) | |
| 99 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this); | |
| 60 } | 100 } |
| 61 | 101 |
| 62 RotationLockDefaultView::~RotationLockDefaultView() { | 102 RotationLockDefaultView::~RotationLockDefaultView() { |
| 103 StopObservingRotation(); | |
| 63 WmShell::Get()->RemoveShellObserver(this); | 104 WmShell::Get()->RemoveShellObserver(this); |
| 64 } | 105 } |
| 65 | 106 |
| 107 void RotationLockDefaultView::Update() { | |
| 108 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::DEFAULT_VIEW_LABEL); | |
| 109 icon_->SetImage(gfx::CreateVectorIcon(IsRotationLocked() | |
| 110 ? kSystemMenuRotationLockLockedIcon | |
| 111 : kSystemMenuRotationLockAutoIcon, | |
| 112 kMenuIconSize, style.GetIconColor())); | |
| 113 | |
| 114 base::string16 label = l10n_util::GetStringUTF16( | |
| 115 IsRotationLocked() ? IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED | |
| 116 : IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO); | |
| 117 label_->SetText(label); | |
| 118 style.SetupLabel(label_); | |
| 119 | |
| 120 Layout(); | |
| 121 SchedulePaint(); | |
| 122 } | |
| 123 | |
| 124 void RotationLockDefaultView::StopObservingRotation() { | |
| 125 ScreenOrientationController* controller = | |
| 126 Shell::GetInstance()->screen_orientation_controller(); | |
| 127 if (controller) | |
| 128 controller->RemoveObserver(this); | |
| 129 } | |
| 130 | |
| 131 void RotationLockDefaultView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | |
| 132 ActionableView::GetAccessibleNodeData(node_data); | |
| 133 if (!label_->text().empty()) | |
| 134 node_data->SetName(label_->text()); | |
| 135 } | |
| 136 | |
| 66 bool RotationLockDefaultView::PerformAction(const ui::Event& event) { | 137 bool RotationLockDefaultView::PerformAction(const ui::Event& event) { |
| 67 ScreenOrientationController* screen_orientation_controller = | 138 Shell::GetInstance()->screen_orientation_controller()->SetRotationLocked( |
| 68 Shell::GetInstance()->screen_orientation_controller(); | 139 !IsRotationLocked()); |
| 69 screen_orientation_controller->SetRotationLocked( | |
| 70 !screen_orientation_controller->rotation_locked()); | |
| 71 UpdateImage(); | |
| 72 return true; | 140 return true; |
| 73 } | 141 } |
| 74 | 142 |
| 75 void RotationLockDefaultView::OnMaximizeModeStarted() { | 143 void RotationLockDefaultView::OnMaximizeModeStarted() { |
| 76 UpdateImage(); | 144 Update(); |
| 77 SetVisible(true); | 145 SetVisible(true); |
| 146 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this); | |
| 78 } | 147 } |
| 79 | 148 |
| 80 void RotationLockDefaultView::OnMaximizeModeEnded() { | 149 void RotationLockDefaultView::OnMaximizeModeEnded() { |
| 81 SetVisible(false); | 150 SetVisible(false); |
| 151 StopObservingRotation(); | |
| 82 } | 152 } |
| 83 | 153 |
| 84 void RotationLockDefaultView::UpdateStyle() { | 154 void RotationLockDefaultView::OnRotationLockChanged(bool rotation_locked) { |
| 85 TrayItemMore::UpdateStyle(); | 155 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 } | 156 } |
| 112 | 157 |
| 113 } // namespace tray | 158 } // namespace tray |
| 114 | 159 |
| 115 TrayRotationLock::TrayRotationLock(SystemTray* system_tray) | 160 TrayRotationLock::TrayRotationLock(SystemTray* system_tray) |
| 116 : TrayImageItem(system_tray, | 161 : TrayImageItem(system_tray, |
| 117 IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED, | 162 IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED, |
| 118 UMA_ROTATION_LOCK), | 163 UMA_ROTATION_LOCK) { |
| 119 observing_rotation_(false), | |
| 120 observing_shell_(true) { | |
| 121 WmShell::Get()->AddShellObserver(this); | 164 WmShell::Get()->AddShellObserver(this); |
| 122 } | 165 } |
| 123 | 166 |
| 124 TrayRotationLock::~TrayRotationLock() { | 167 TrayRotationLock::~TrayRotationLock() { |
| 125 StopObservingShell(); | 168 WmShell::Get()->RemoveShellObserver(this); |
| 126 } | 169 } |
| 127 | 170 |
| 128 void TrayRotationLock::OnRotationLockChanged(bool rotation_locked) { | 171 void TrayRotationLock::OnRotationLockChanged(bool rotation_locked) { |
| 129 tray_view()->SetVisible(ShouldBeVisible()); | 172 tray_view()->SetVisible(ShouldBeVisible()); |
| 130 } | 173 } |
| 131 | 174 |
| 132 views::View* TrayRotationLock::CreateDefaultView(LoginStatus status) { | 175 views::View* TrayRotationLock::CreateDefaultView(LoginStatus status) { |
| 133 if (OnPrimaryDisplay()) | 176 if (OnPrimaryDisplay()) |
| 134 return new tray::RotationLockDefaultView(this); | 177 return new tray::RotationLockDefaultView(this); |
| 135 return NULL; | 178 return NULL; |
| 136 } | 179 } |
| 137 | 180 |
| 138 void TrayRotationLock::OnMaximizeModeStarted() { | 181 void TrayRotationLock::OnMaximizeModeStarted() { |
| 139 tray_view()->SetVisible( | 182 tray_view()->SetVisible(IsRotationLocked()); |
| 140 Shell::GetInstance()->screen_orientation_controller()->rotation_locked()); | |
| 141 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this); | 183 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this); |
| 142 observing_rotation_ = true; | |
| 143 } | 184 } |
| 144 | 185 |
| 145 void TrayRotationLock::OnMaximizeModeEnded() { | 186 void TrayRotationLock::OnMaximizeModeEnded() { |
| 146 tray_view()->SetVisible(false); | 187 tray_view()->SetVisible(false); |
| 147 StopObservingRotation(); | 188 StopObservingRotation(); |
| 148 } | 189 } |
| 149 | 190 |
| 150 void TrayRotationLock::DestroyTrayView() { | 191 void TrayRotationLock::DestroyTrayView() { |
| 151 StopObservingRotation(); | 192 StopObservingRotation(); |
| 152 StopObservingShell(); | 193 WmShell::Get()->RemoveShellObserver(this); |
| 153 TrayImageItem::DestroyTrayView(); | 194 TrayImageItem::DestroyTrayView(); |
| 154 } | 195 } |
| 155 | 196 |
| 156 bool TrayRotationLock::GetInitialVisibility() { | 197 bool TrayRotationLock::GetInitialVisibility() { |
| 157 return ShouldBeVisible(); | 198 return ShouldBeVisible(); |
| 158 } | 199 } |
| 159 | 200 |
| 160 bool TrayRotationLock::ShouldBeVisible() { | 201 bool TrayRotationLock::ShouldBeVisible() { |
| 161 return OnPrimaryDisplay() && | 202 return OnPrimaryDisplay() && IsMaximizeModeWindowManagerEnabled() && |
| 162 WmShell::Get() | 203 IsRotationLocked(); |
| 163 ->maximize_mode_controller() | |
| 164 ->IsMaximizeModeWindowManagerEnabled() && | |
| 165 Shell::GetInstance() | |
| 166 ->screen_orientation_controller() | |
| 167 ->rotation_locked(); | |
| 168 } | 204 } |
| 169 | 205 |
| 170 bool TrayRotationLock::OnPrimaryDisplay() const { | 206 bool TrayRotationLock::OnPrimaryDisplay() const { |
| 171 gfx::NativeView native_view = system_tray()->GetWidget()->GetNativeView(); | 207 gfx::NativeView native_view = system_tray()->GetWidget()->GetNativeView(); |
| 172 display::Display parent_display = | 208 display::Display parent_display = |
| 173 display::Screen::GetScreen()->GetDisplayNearestWindow(native_view); | 209 display::Screen::GetScreen()->GetDisplayNearestWindow(native_view); |
| 174 return parent_display.IsInternal(); | 210 return parent_display.IsInternal(); |
| 175 } | 211 } |
| 176 | 212 |
| 177 void TrayRotationLock::StopObservingRotation() { | 213 void TrayRotationLock::StopObservingRotation() { |
| 178 if (!observing_rotation_) | |
| 179 return; | |
| 180 ScreenOrientationController* controller = | 214 ScreenOrientationController* controller = |
| 181 Shell::GetInstance()->screen_orientation_controller(); | 215 Shell::GetInstance()->screen_orientation_controller(); |
| 182 if (controller) | 216 if (controller) |
| 183 controller->RemoveObserver(this); | 217 controller->RemoveObserver(this); |
| 184 observing_rotation_ = false; | |
| 185 } | |
| 186 | |
| 187 void TrayRotationLock::StopObservingShell() { | |
| 188 if (!observing_shell_) | |
|
tdanderson
2017/02/16 01:00:12
It seems that by removing these checks, your CL op
mohsen
2017/02/16 04:15:17
Yes, since the RemoveObserver() can handle multipl
tdanderson
2017/02/16 19:20:59
Sounds fine by me, thanks.
| |
| 189 return; | |
| 190 WmShell::Get()->RemoveShellObserver(this); | |
| 191 observing_shell_ = false; | |
| 192 } | 218 } |
| 193 | 219 |
| 194 } // namespace ash | 220 } // namespace ash |
| OLD | NEW |