Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ash / system / chromeos / rotation / tray_rotation_lock.cc
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/shell.h"
8 #include "ash/system/tray/system_tray.h"
9 #include "ash/system/tray/tray_item_more.h"
10 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
11 #include "grit/ash_resources.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/display.h"
16
17 namespace ash {
18
19 namespace tray {
20
21 // Extends TrayItemMore, however does not make use of the chevron, nor of the
22 // DetailedView. This was chosen over ActionableView in order to reuse the
23 // layout and styling of labels and images. This allows RotationLockDefaultView
24 // to maintain the look of other system tray items without code duplication.
25 class RotationLockDefaultView : public TrayItemMore,
26                                 public ShellObserver {
27  public:
28   explicit RotationLockDefaultView(SystemTrayItem* owner);
29   virtual ~RotationLockDefaultView();
30
31   // ActionableView:
32   virtual bool PerformAction(const ui::Event& event) OVERRIDE;
33
34   // ShellObserver:
35   virtual void OnMaximizeModeStarted() OVERRIDE;
36   virtual void OnMaximizeModeEnded() OVERRIDE;
37
38  private:
39   void UpdateImage();
40
41   DISALLOW_COPY_AND_ASSIGN(RotationLockDefaultView);
42 };
43
44 RotationLockDefaultView::RotationLockDefaultView(SystemTrayItem* owner)
45     : TrayItemMore(owner, false) {
46   UpdateImage();
47   SetVisible(Shell::GetInstance()->IsMaximizeModeWindowManagerEnabled());
48   Shell::GetInstance()->AddShellObserver(this);
49 }
50
51 RotationLockDefaultView::~RotationLockDefaultView() {
52   Shell::GetInstance()->RemoveShellObserver(this);
53 }
54
55 bool RotationLockDefaultView::PerformAction(const ui::Event& event) {
56   MaximizeModeController* maximize_mode_controller = Shell::GetInstance()->
57       maximize_mode_controller();
58   bool rotation_locked = !maximize_mode_controller->rotation_locked();
59   maximize_mode_controller->set_rotation_locked(rotation_locked);
60
61   UpdateImage();
62
63   // RotationLockDefaultView can only be created by a TrayRotationLock. The
64   // owner needs to be told of the action so that it can update its visibility.
65   static_cast<TrayRotationLock*>(owner())->tray_view()->
66       SetVisible(rotation_locked);
67
68   return true;
69 }
70
71 void RotationLockDefaultView::OnMaximizeModeStarted() {
72   UpdateImage();
73   SetVisible(true);
74 }
75
76 void RotationLockDefaultView::OnMaximizeModeEnded() {
77   SetVisible(false);
78 }
79
80 void RotationLockDefaultView::UpdateImage() {
81   base::string16 label;
82   ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
83   if (Shell::GetInstance()->maximize_mode_controller()->rotation_locked()) {
84     SetImage(bundle.GetImageNamed(
85         IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED_DARK).ToImageSkia());
86     label = l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED);
87     SetLabel(label);
88     SetAccessibleName(label);
89   } else {
90     SetImage(bundle.GetImageNamed(IDR_AURA_UBER_TRAY_AUTO_ROTATION_DARK).
91         ToImageSkia());
92     label = l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO);
93     SetLabel(label);
94     SetAccessibleName(label);
95   }
96 }
97
98 }  // namespace tray
99
100 TrayRotationLock::TrayRotationLock(SystemTray* system_tray)
101     : TrayImageItem(system_tray, IDR_AURA_UBER_TRAY_AUTO_ROTATION_LOCKED),
102       on_primary_display_(false) {
103   gfx::NativeView native_view = system_tray->GetWidget()->GetNativeView();
104   gfx::Display parent_display = Shell::GetScreen()->
105       GetDisplayNearestWindow(native_view);
106
107   on_primary_display_ = parent_display.IsInternal();
108
109   if (on_primary_display_)
110     Shell::GetInstance()->AddShellObserver(this);
111 }
112
113 TrayRotationLock::~TrayRotationLock() {
114   if (on_primary_display_)
115     Shell::GetInstance()->RemoveShellObserver(this);
116 }
117
118 views::View* TrayRotationLock::CreateDefaultView(user::LoginStatus status) {
119   if (on_primary_display_)
120     return new tray::RotationLockDefaultView(this);
121   return NULL;
122 }
123
124 void TrayRotationLock::OnMaximizeModeStarted() {
125   tray_view()->SetVisible(
126       Shell::GetInstance()->maximize_mode_controller()->rotation_locked());
127 }
128
129 void TrayRotationLock::OnMaximizeModeEnded() {
130   tray_view()->SetVisible(false);
131 }
132
133 bool TrayRotationLock::GetInitialVisibility() {
134   return on_primary_display_ &&
135          Shell::GetInstance()->IsMaximizeModeWindowManagerEnabled() &&
136          Shell::GetInstance()->maximize_mode_controller()->rotation_locked();
137 }
138
139 }  // namespace ash