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