Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ash / system / chromeos / rotation / tray_rotation_lock.cc
blobe860976379c88e74b2e2dd1a1e1fa1f9e5573490
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 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);
113 return NULL;
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() &&
133 Shell::GetInstance()
134 ->maximize_mode_controller()
135 ->IsMaximizeModeWindowManagerEnabled() &&
136 Shell::GetInstance()
137 ->screen_orientation_controller()
138 ->rotation_locked();
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();
148 } // namespace ash