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"
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"
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
{
29 explicit RotationLockDefaultView(SystemTrayItem
* owner
);
30 ~RotationLockDefaultView() override
;
33 bool PerformAction(const ui::Event
& event
) override
;
36 void OnMaximizeModeStarted() override
;
37 void OnMaximizeModeEnded() override
;
42 DISALLOW_COPY_AND_ASSIGN(RotationLockDefaultView
);
45 RotationLockDefaultView::RotationLockDefaultView(SystemTrayItem
* owner
)
46 : TrayItemMore(owner
, false) {
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());
66 void RotationLockDefaultView::OnMaximizeModeStarted() {
71 void RotationLockDefaultView::OnMaximizeModeEnded() {
75 void RotationLockDefaultView::UpdateImage() {
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
);
85 SetAccessibleName(label
);
87 SetImage(bundle
.GetImageNamed(IDR_AURA_UBER_TRAY_AUTO_ROTATION_DARK
).
89 label
= l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO
);
91 SetAccessibleName(label
);
97 TrayRotationLock::TrayRotationLock(SystemTray
* system_tray
)
98 : TrayImageItem(system_tray
, IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED
) {
99 Shell::GetInstance()->AddShellObserver(this);
102 TrayRotationLock::~TrayRotationLock() {
103 Shell::GetInstance()->RemoveShellObserver(this);
106 void TrayRotationLock::OnRotationLockChanged(bool rotation_locked
) {
107 tray_view()->SetVisible(ShouldBeVisible());
110 views::View
* TrayRotationLock::CreateDefaultView(user::LoginStatus status
) {
111 if (OnPrimaryDisplay())
112 return new tray::RotationLockDefaultView(this);
116 void TrayRotationLock::OnMaximizeModeStarted() {
117 tray_view()->SetVisible(
118 Shell::GetInstance()->screen_orientation_controller()->rotation_locked());
119 Shell::GetInstance()->screen_orientation_controller()->AddObserver(this);
122 void TrayRotationLock::OnMaximizeModeEnded() {
123 tray_view()->SetVisible(false);
124 Shell::GetInstance()->screen_orientation_controller()->RemoveObserver(this);
127 bool TrayRotationLock::GetInitialVisibility() {
128 return ShouldBeVisible();
131 bool TrayRotationLock::ShouldBeVisible() {
132 return OnPrimaryDisplay() &&
134 ->maximize_mode_controller()
135 ->IsMaximizeModeWindowManagerEnabled() &&
137 ->screen_orientation_controller()
141 bool TrayRotationLock::OnPrimaryDisplay() const {
142 gfx::NativeView native_view
= system_tray()->GetWidget()->GetNativeView();
143 gfx::Display parent_display
=
144 Shell::GetScreen()->GetDisplayNearestWindow(native_view
);
145 return parent_display
.IsInternal();