Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / chromeos / rotation / tray_rotation_lock.cc
blob060bc520143a4d21e826032a6bac9903d0b4acaf
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.
5 #include "ash/system/chromeos/rotation/tray_rotation_lock.h"
7 #include "ash/content/display/screen_orientation_controller_chromeos.h"
8 #include "ash/shell.h"
9 #include "ash/system/tray/system_tray.h"
10 #include "ash/system/tray/tray_item_more.h"
11 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
12 #include "grit/ash_resources.h"
13 #include "grit/ash_strings.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/display.h"
18 namespace ash {
20 namespace tray {
22 // Extends TrayItemMore, however does not make use of the chevron, nor of the
23 // DetailedView. This was chosen over ActionableView in order to reuse the
24 // layout and styling of labels and images. This allows RotationLockDefaultView
25 // to maintain the look of other system tray items without code duplication.
26 class RotationLockDefaultView : public TrayItemMore,
27 public ShellObserver {
28 public:
29 explicit RotationLockDefaultView(SystemTrayItem* owner);
30 ~RotationLockDefaultView() override;
32 // ActionableView:
33 bool PerformAction(const ui::Event& event) override;
35 // ShellObserver:
36 void OnMaximizeModeStarted() override;
37 void OnMaximizeModeEnded() override;
39 private:
40 void UpdateImage();
42 DISALLOW_COPY_AND_ASSIGN(RotationLockDefaultView);
45 RotationLockDefaultView::RotationLockDefaultView(SystemTrayItem* owner)
46 : TrayItemMore(owner, false) {
47 UpdateImage();
48 SetVisible(Shell::GetInstance()->maximize_mode_controller()->
49 IsMaximizeModeWindowManagerEnabled());
50 Shell::GetInstance()->AddShellObserver(this);
53 RotationLockDefaultView::~RotationLockDefaultView() {
54 Shell::GetInstance()->RemoveShellObserver(this);
57 bool RotationLockDefaultView::PerformAction(const ui::Event& event) {
58 ScreenOrientationController* screen_orientation_controller =
59 Shell::GetInstance()->screen_orientation_controller();
60 screen_orientation_controller->SetRotationLocked(
61 !screen_orientation_controller->rotation_locked());
62 UpdateImage();
63 return true;
66 void RotationLockDefaultView::OnMaximizeModeStarted() {
67 UpdateImage();
68 SetVisible(true);
71 void RotationLockDefaultView::OnMaximizeModeEnded() {
72 SetVisible(false);
75 void RotationLockDefaultView::UpdateImage() {
76 base::string16 label;
77 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
78 if (Shell::GetInstance()
79 ->screen_orientation_controller()
80 ->rotation_locked()) {
81 SetImage(bundle.GetImageNamed(
82 IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED_DARK).ToImageSkia());
83 label = l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED);
84 SetLabel(label);
85 SetAccessibleName(label);
86 } else {
87 SetImage(bundle.GetImageNamed(IDR_AURA_UBER_TRAY_AUTO_ROTATION_DARK).
88 ToImageSkia());
89 label = l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO);
90 SetLabel(label);
91 SetAccessibleName(label);
95 } // namespace tray
97 TrayRotationLock::TrayRotationLock(SystemTray* system_tray)
98 : TrayImageItem(system_tray, IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED),
99 observing_rotation_(false),
100 observing_shell_(true) {
101 Shell::GetInstance()->AddShellObserver(this);
104 TrayRotationLock::~TrayRotationLock() {
105 StopObservingShell();
108 void TrayRotationLock::OnRotationLockChanged(bool rotation_locked) {
109 tray_view()->SetVisible(ShouldBeVisible());
112 views::View* TrayRotationLock::CreateDefaultView(user::LoginStatus status) {
113 if (OnPrimaryDisplay())
114 return new tray::RotationLockDefaultView(this);
115 return NULL;
118 void TrayRotationLock::OnMaximizeModeStarted() {
119 tray_view()->SetVisible(
120 Shell::GetInstance()->screen_orientation_controller()->rotation_locked());
121 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this);
122 observing_rotation_ = true;
125 void TrayRotationLock::OnMaximizeModeEnded() {
126 tray_view()->SetVisible(false);
127 StopObservingRotation();
130 void TrayRotationLock::DestroyTrayView() {
131 StopObservingRotation();
132 StopObservingShell();
133 TrayImageItem::DestroyTrayView();
136 bool TrayRotationLock::GetInitialVisibility() {
137 return ShouldBeVisible();
140 bool TrayRotationLock::ShouldBeVisible() {
141 return OnPrimaryDisplay() &&
142 Shell::GetInstance()
143 ->maximize_mode_controller()
144 ->IsMaximizeModeWindowManagerEnabled() &&
145 Shell::GetInstance()
146 ->screen_orientation_controller()
147 ->rotation_locked();
150 bool TrayRotationLock::OnPrimaryDisplay() const {
151 gfx::NativeView native_view = system_tray()->GetWidget()->GetNativeView();
152 gfx::Display parent_display =
153 Shell::GetScreen()->GetDisplayNearestWindow(native_view);
154 return parent_display.IsInternal();
157 void TrayRotationLock::StopObservingRotation() {
158 if (!observing_rotation_)
159 return;
160 ScreenOrientationController* controller =
161 Shell::GetInstance()->screen_orientation_controller();
162 if (controller)
163 controller->RemoveObserver(this);
164 observing_rotation_ = false;
167 void TrayRotationLock::StopObservingShell() {
168 if (!observing_shell_)
169 return;
170 Shell::GetInstance()->RemoveShellObserver(this);
171 observing_shell_ = false;
174 } // namespace ash