[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / ash / system / tray_caps_lock.cc
blob6985533bd7c0a5d2d782b406dd0c3344603e355f
1 // Copyright (c) 2012 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/tray_caps_lock.h"
7 #include "ash/caps_lock_delegate.h"
8 #include "ash/shell.h"
9 #include "ash/system/tray/system_tray_notifier.h"
10 #include "ash/system/tray/tray_constants.h"
11 #include "ash/system/tray/tray_views.h"
12 #include "grit/ash_resources.h"
13 #include "grit/ash_strings.h"
14 #include "ui/base/accessibility/accessible_view_state.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/views/controls/image_view.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/layout/box_layout.h"
20 #include "ui/views/widget/widget.h"
22 namespace ash {
23 namespace internal {
25 class CapsLockDefaultView : public ActionableView {
26 public:
27 CapsLockDefaultView()
28 : text_label_(new views::Label),
29 shortcut_label_(new views::Label) {
30 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
31 kTrayPopupPaddingHorizontal,
33 kTrayPopupPaddingBetweenItems));
35 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
36 FixedSizedImageView* image =
37 new FixedSizedImageView(0, kTrayPopupItemHeight);
38 image->SetImage(bundle.GetImageNamed(IDR_AURA_UBER_TRAY_CAPS_LOCK_DARK).
39 ToImageSkia());
40 AddChildView(image);
42 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
43 AddChildView(text_label_);
45 shortcut_label_->SetEnabled(false);
46 AddChildView(shortcut_label_);
49 virtual ~CapsLockDefaultView() {}
51 // Updates the label text and the shortcut text.
52 void Update(bool caps_lock_enabled, bool search_mapped_to_caps_lock) {
53 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
54 const int text_string_id = caps_lock_enabled ?
55 IDS_ASH_STATUS_TRAY_CAPS_LOCK_ENABLED :
56 IDS_ASH_STATUS_TRAY_CAPS_LOCK_DISABLED;
57 text_label_->SetText(bundle.GetLocalizedString(text_string_id));
59 int shortcut_string_id = 0;
60 if (caps_lock_enabled) {
61 shortcut_string_id = search_mapped_to_caps_lock ?
62 IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_SEARCH_OR_SHIFT :
63 IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_ALT_SEARCH_OR_SHIFT;
64 } else {
65 shortcut_string_id = search_mapped_to_caps_lock ?
66 IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_SEARCH :
67 IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_ALT_SEARCH;
69 shortcut_label_->SetText(bundle.GetLocalizedString(shortcut_string_id));
71 Layout();
74 private:
75 // Overridden from views::View:
76 virtual void Layout() OVERRIDE {
77 views::View::Layout();
79 // Align the shortcut text with the right end
80 const int old_x = shortcut_label_->x();
81 const int new_x =
82 width() - shortcut_label_->width() - kTrayPopupPaddingHorizontal;
83 shortcut_label_->SetX(new_x);
84 const gfx::Size text_size = text_label_->size();
85 text_label_->SetSize(gfx::Size(text_size.width() + new_x - old_x,
86 text_size.height()));
89 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE {
90 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
91 state->name = text_label_->text();
94 // Overridden from ActionableView:
95 virtual bool PerformAction(const ui::Event& event) OVERRIDE {
96 Shell::GetInstance()->caps_lock_delegate()->ToggleCapsLock();
97 return true;
100 views::Label* text_label_;
101 views::Label* shortcut_label_;
103 DISALLOW_COPY_AND_ASSIGN(CapsLockDefaultView);
106 TrayCapsLock::TrayCapsLock(SystemTray* system_tray)
107 : TrayImageItem(system_tray, IDR_AURA_UBER_TRAY_CAPS_LOCK),
108 default_(NULL),
109 detailed_(NULL),
110 search_mapped_to_caps_lock_(false),
111 caps_lock_enabled_(
112 Shell::GetInstance()->caps_lock_delegate()->IsCapsLockEnabled()),
113 message_shown_(false) {
114 Shell::GetInstance()->system_tray_notifier()->AddCapsLockObserver(this);
117 TrayCapsLock::~TrayCapsLock() {
118 Shell::GetInstance()->system_tray_notifier()->RemoveCapsLockObserver(this);
121 bool TrayCapsLock::GetInitialVisibility() {
122 return Shell::GetInstance()->caps_lock_delegate()->IsCapsLockEnabled();
125 views::View* TrayCapsLock::CreateDefaultView(user::LoginStatus status) {
126 if (!caps_lock_enabled_)
127 return NULL;
128 DCHECK(default_ == NULL);
129 default_ = new CapsLockDefaultView;
130 default_->Update(caps_lock_enabled_, search_mapped_to_caps_lock_);
131 return default_;
134 views::View* TrayCapsLock::CreateDetailedView(user::LoginStatus status) {
135 DCHECK(detailed_ == NULL);
136 detailed_ = new views::View;
138 detailed_->SetLayoutManager(new
139 views::BoxLayout(views::BoxLayout::kHorizontal,
140 kTrayPopupPaddingHorizontal, 10, kTrayPopupPaddingBetweenItems));
142 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
143 views::ImageView* image = new views::ImageView;
144 image->SetImage(bundle.GetImageNamed(IDR_AURA_UBER_TRAY_CAPS_LOCK_DARK).
145 ToImageSkia());
147 detailed_->AddChildView(image);
149 const int string_id = search_mapped_to_caps_lock_ ?
150 IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_SEARCH :
151 IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_ALT_SEARCH;
152 views::Label* label = new views::Label(bundle.GetLocalizedString(string_id));
153 label->SetMultiLine(true);
154 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
155 detailed_->AddChildView(label);
157 return detailed_;
160 void TrayCapsLock::DestroyDefaultView() {
161 default_ = NULL;
164 void TrayCapsLock::DestroyDetailedView() {
165 detailed_ = NULL;
168 void TrayCapsLock::OnCapsLockChanged(bool enabled,
169 bool search_mapped_to_caps_lock) {
170 if (tray_view())
171 tray_view()->SetVisible(enabled);
173 caps_lock_enabled_ = enabled;
174 search_mapped_to_caps_lock_ = search_mapped_to_caps_lock;
176 if (default_) {
177 default_->Update(enabled, search_mapped_to_caps_lock);
178 } else {
179 if (enabled) {
180 if (!message_shown_) {
181 PopupDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds, false);
182 message_shown_ = true;
184 } else if (detailed_) {
185 detailed_->GetWidget()->Close();
190 } // namespace internal
191 } // namespace ash