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"
9 #include "ash/system/tray/actionable_view.h"
10 #include "ash/system/tray/fixed_sized_image_view.h"
11 #include "ash/system/tray/system_tray_notifier.h"
12 #include "ash/system/tray/tray_constants.h"
13 #include "grit/ash_resources.h"
14 #include "grit/ash_strings.h"
15 #include "ui/base/accessibility/accessible_view_state.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/image/image.h"
18 #include "ui/views/controls/image_view.h"
19 #include "ui/views/controls/label.h"
20 #include "ui/views/layout/box_layout.h"
21 #include "ui/views/widget/widget.h"
26 class CapsLockDefaultView
: public ActionableView
{
29 : text_label_(new views::Label
),
30 shortcut_label_(new views::Label
) {
31 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal
,
32 kTrayPopupPaddingHorizontal
,
34 kTrayPopupPaddingBetweenItems
));
36 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
37 FixedSizedImageView
* image
=
38 new FixedSizedImageView(0, kTrayPopupItemHeight
);
39 image
->SetImage(bundle
.GetImageNamed(IDR_AURA_UBER_TRAY_CAPS_LOCK_DARK
).
43 text_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
44 AddChildView(text_label_
);
46 shortcut_label_
->SetEnabled(false);
47 AddChildView(shortcut_label_
);
50 virtual ~CapsLockDefaultView() {}
52 // Updates the label text and the shortcut text.
53 void Update(bool caps_lock_enabled
, bool search_mapped_to_caps_lock
) {
54 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
55 const int text_string_id
= caps_lock_enabled
?
56 IDS_ASH_STATUS_TRAY_CAPS_LOCK_ENABLED
:
57 IDS_ASH_STATUS_TRAY_CAPS_LOCK_DISABLED
;
58 text_label_
->SetText(bundle
.GetLocalizedString(text_string_id
));
60 int shortcut_string_id
= 0;
61 if (caps_lock_enabled
) {
62 shortcut_string_id
= search_mapped_to_caps_lock
?
63 IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_SEARCH_OR_SHIFT
:
64 IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_ALT_SEARCH_OR_SHIFT
;
66 shortcut_string_id
= search_mapped_to_caps_lock
?
67 IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_SEARCH
:
68 IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_ALT_SEARCH
;
70 shortcut_label_
->SetText(bundle
.GetLocalizedString(shortcut_string_id
));
76 // Overridden from views::View:
77 virtual void Layout() OVERRIDE
{
78 views::View::Layout();
80 // Align the shortcut text with the right end
81 const int old_x
= shortcut_label_
->x();
83 width() - shortcut_label_
->width() - kTrayPopupPaddingHorizontal
;
84 shortcut_label_
->SetX(new_x
);
85 const gfx::Size text_size
= text_label_
->size();
86 text_label_
->SetSize(gfx::Size(text_size
.width() + new_x
- old_x
,
90 virtual void GetAccessibleState(ui::AccessibleViewState
* state
) OVERRIDE
{
91 state
->role
= ui::AccessibilityTypes::ROLE_PUSHBUTTON
;
92 state
->name
= text_label_
->text();
95 // Overridden from ActionableView:
96 virtual bool PerformAction(const ui::Event
& event
) OVERRIDE
{
97 Shell::GetInstance()->caps_lock_delegate()->ToggleCapsLock();
101 views::Label
* text_label_
;
102 views::Label
* shortcut_label_
;
104 DISALLOW_COPY_AND_ASSIGN(CapsLockDefaultView
);
107 TrayCapsLock::TrayCapsLock(SystemTray
* system_tray
)
108 : TrayImageItem(system_tray
, IDR_AURA_UBER_TRAY_CAPS_LOCK
),
111 search_mapped_to_caps_lock_(false),
113 Shell::GetInstance()->caps_lock_delegate()->IsCapsLockEnabled()),
114 message_shown_(false) {
115 Shell::GetInstance()->system_tray_notifier()->AddCapsLockObserver(this);
118 TrayCapsLock::~TrayCapsLock() {
119 Shell::GetInstance()->system_tray_notifier()->RemoveCapsLockObserver(this);
122 bool TrayCapsLock::GetInitialVisibility() {
123 return Shell::GetInstance()->caps_lock_delegate()->IsCapsLockEnabled();
126 views::View
* TrayCapsLock::CreateDefaultView(user::LoginStatus status
) {
127 if (!caps_lock_enabled_
)
129 DCHECK(default_
== NULL
);
130 default_
= new CapsLockDefaultView
;
131 default_
->Update(caps_lock_enabled_
, search_mapped_to_caps_lock_
);
135 views::View
* TrayCapsLock::CreateDetailedView(user::LoginStatus status
) {
136 DCHECK(detailed_
== NULL
);
137 detailed_
= new views::View
;
139 detailed_
->SetLayoutManager(new
140 views::BoxLayout(views::BoxLayout::kHorizontal
,
141 kTrayPopupPaddingHorizontal
, 10, kTrayPopupPaddingBetweenItems
));
143 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
144 views::ImageView
* image
= new views::ImageView
;
145 image
->SetImage(bundle
.GetImageNamed(IDR_AURA_UBER_TRAY_CAPS_LOCK_DARK
).
148 detailed_
->AddChildView(image
);
150 const int string_id
= search_mapped_to_caps_lock_
?
151 IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_SEARCH
:
152 IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_ALT_SEARCH
;
153 views::Label
* label
= new views::Label(bundle
.GetLocalizedString(string_id
));
154 label
->SetMultiLine(true);
155 label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
156 detailed_
->AddChildView(label
);
161 void TrayCapsLock::DestroyDefaultView() {
165 void TrayCapsLock::DestroyDetailedView() {
169 void TrayCapsLock::OnCapsLockChanged(bool enabled
,
170 bool search_mapped_to_caps_lock
) {
172 tray_view()->SetVisible(enabled
);
174 caps_lock_enabled_
= enabled
;
175 search_mapped_to_caps_lock_
= search_mapped_to_caps_lock
;
178 default_
->Update(enabled
, search_mapped_to_caps_lock
);
181 if (!message_shown_
) {
182 PopupDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds
, false);
183 message_shown_
= true;
185 } else if (detailed_
) {
186 detailed_
->GetWidget()->Close();
191 } // namespace internal