OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/common/system/chromeos/tray_caps_lock.h" | |
6 | |
7 #include "ash/common/accessibility_delegate.h" | |
8 #include "ash/common/system/tray/actionable_view.h" | |
9 #include "ash/common/system/tray/system_tray_delegate.h" | |
10 #include "ash/common/system/tray/tray_constants.h" | |
11 #include "ash/common/system/tray/tray_popup_item_style.h" | |
12 #include "ash/common/system/tray/tray_popup_utils.h" | |
13 #include "ash/common/system/tray/tri_view.h" | |
14 #include "ash/common/wm_shell.h" | |
15 #include "ash/resources/grit/ash_resources.h" | |
16 #include "ash/resources/vector_icons/vector_icons.h" | |
17 #include "ash/strings/grit/ash_strings.h" | |
18 #include "base/sys_info.h" | |
19 #include "ui/accessibility/ax_node_data.h" | |
20 #include "ui/base/ime/chromeos/ime_keyboard.h" | |
21 #include "ui/base/ime/chromeos/input_method_manager.h" | |
22 #include "ui/base/l10n/l10n_util.h" | |
23 #include "ui/base/resource/resource_bundle.h" | |
24 #include "ui/gfx/image/image.h" | |
25 #include "ui/gfx/paint_vector_icon.h" | |
26 #include "ui/views/border.h" | |
27 #include "ui/views/controls/image_view.h" | |
28 #include "ui/views/controls/label.h" | |
29 #include "ui/views/layout/box_layout.h" | |
30 #include "ui/views/layout/fill_layout.h" | |
31 #include "ui/views/widget/widget.h" | |
32 | |
33 namespace ash { | |
34 namespace { | |
35 | |
36 // Padding used to position the caption in the caps lock default view row. | |
37 const int kCaptionRightPadding = 6; | |
38 | |
39 bool CapsLockIsEnabled() { | |
40 chromeos::input_method::InputMethodManager* ime = | |
41 chromeos::input_method::InputMethodManager::Get(); | |
42 return (ime && ime->GetImeKeyboard()) | |
43 ? ime->GetImeKeyboard()->CapsLockIsEnabled() | |
44 : false; | |
45 } | |
46 } | |
47 | |
48 class CapsLockDefaultView : public ActionableView { | |
49 public: | |
50 CapsLockDefaultView() | |
51 : ActionableView(nullptr, TrayPopupInkDropStyle::FILL_BOUNDS), | |
52 text_label_(TrayPopupUtils::CreateDefaultLabel()), | |
53 shortcut_label_(TrayPopupUtils::CreateDefaultLabel()) { | |
54 shortcut_label_->SetEnabled(false); | |
55 | |
56 TriView* tri_view(TrayPopupUtils::CreateDefaultRowView()); | |
57 SetLayoutManager(new views::FillLayout); | |
58 AddChildView(tri_view); | |
59 | |
60 auto* image = TrayPopupUtils::CreateMainImageView(); | |
61 image->SetEnabled(enabled()); | |
62 TrayPopupItemStyle default_view_style( | |
63 TrayPopupItemStyle::FontStyle::DEFAULT_VIEW_LABEL); | |
64 image->SetImage(gfx::CreateVectorIcon(kSystemMenuCapsLockIcon, | |
65 default_view_style.GetIconColor())); | |
66 default_view_style.SetupLabel(text_label_); | |
67 | |
68 TrayPopupItemStyle caption_style(TrayPopupItemStyle::FontStyle::CAPTION); | |
69 caption_style.SetupLabel(shortcut_label_); | |
70 | |
71 SetInkDropMode(InkDropHostView::InkDropMode::ON); | |
72 | |
73 tri_view->AddView(TriView::Container::START, image); | |
74 tri_view->AddView(TriView::Container::CENTER, text_label_); | |
75 tri_view->AddView(TriView::Container::END, shortcut_label_); | |
76 tri_view->SetContainerBorder( | |
77 TriView::Container::END, | |
78 views::CreateEmptyBorder(0, 0, 0, kCaptionRightPadding)); | |
79 } | |
80 | |
81 ~CapsLockDefaultView() override {} | |
82 | |
83 // Updates the label text and the shortcut text. | |
84 void Update(bool caps_lock_enabled) { | |
85 const int text_string_id = caps_lock_enabled | |
86 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_ENABLED | |
87 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_DISABLED; | |
88 text_label_->SetText(l10n_util::GetStringUTF16(text_string_id)); | |
89 | |
90 int shortcut_string_id = 0; | |
91 bool search_mapped_to_caps_lock = | |
92 WmShell::Get()->system_tray_delegate()->IsSearchKeyMappedToCapsLock(); | |
93 if (caps_lock_enabled) { | |
94 shortcut_string_id = | |
95 search_mapped_to_caps_lock | |
96 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_SEARCH_OR_SHIFT | |
97 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_ALT_SEARCH_OR_SHIFT; | |
98 } else { | |
99 shortcut_string_id = | |
100 search_mapped_to_caps_lock | |
101 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_SEARCH | |
102 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_ALT_SEARCH; | |
103 } | |
104 shortcut_label_->SetText(l10n_util::GetStringUTF16(shortcut_string_id)); | |
105 | |
106 Layout(); | |
107 } | |
108 | |
109 private: | |
110 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { | |
111 node_data->role = ui::AX_ROLE_BUTTON; | |
112 node_data->SetName(text_label_->text()); | |
113 } | |
114 | |
115 // ActionableView: | |
116 bool PerformAction(const ui::Event& event) override { | |
117 chromeos::input_method::ImeKeyboard* keyboard = | |
118 chromeos::input_method::InputMethodManager::Get()->GetImeKeyboard(); | |
119 if (keyboard) { | |
120 WmShell::Get()->RecordUserMetricsAction( | |
121 keyboard->CapsLockIsEnabled() | |
122 ? UMA_STATUS_AREA_CAPS_LOCK_DISABLED_BY_CLICK | |
123 : UMA_STATUS_AREA_CAPS_LOCK_ENABLED_BY_CLICK); | |
124 keyboard->SetCapsLockEnabled(!keyboard->CapsLockIsEnabled()); | |
125 } | |
126 return true; | |
127 } | |
128 | |
129 // It indicates whether the Caps Lock is on or off. | |
130 views::Label* text_label_; | |
131 | |
132 // It indicates the shortcut can be used to turn on or turn off Caps Lock. | |
133 views::Label* shortcut_label_; | |
134 | |
135 DISALLOW_COPY_AND_ASSIGN(CapsLockDefaultView); | |
136 }; | |
137 | |
138 TrayCapsLock::TrayCapsLock(SystemTray* system_tray) | |
139 : TrayImageItem(system_tray, kSystemTrayCapsLockIcon, UMA_CAPS_LOCK), | |
140 default_(nullptr), | |
141 detailed_(nullptr), | |
142 caps_lock_enabled_(CapsLockIsEnabled()), | |
143 message_shown_(false) { | |
144 chromeos::input_method::InputMethodManager* ime = | |
145 chromeos::input_method::InputMethodManager::Get(); | |
146 if (ime && ime->GetImeKeyboard()) | |
147 ime->GetImeKeyboard()->AddObserver(this); | |
148 } | |
149 | |
150 TrayCapsLock::~TrayCapsLock() { | |
151 chromeos::input_method::InputMethodManager* ime = | |
152 chromeos::input_method::InputMethodManager::Get(); | |
153 if (ime && ime->GetImeKeyboard()) | |
154 ime->GetImeKeyboard()->RemoveObserver(this); | |
155 } | |
156 | |
157 void TrayCapsLock::OnCapsLockChanged(bool enabled) { | |
158 caps_lock_enabled_ = enabled; | |
159 | |
160 // Send an a11y alert. | |
161 WmShell::Get()->accessibility_delegate()->TriggerAccessibilityAlert( | |
162 enabled ? A11Y_ALERT_CAPS_ON : A11Y_ALERT_CAPS_OFF); | |
163 | |
164 if (tray_view()) | |
165 tray_view()->SetVisible(caps_lock_enabled_); | |
166 | |
167 if (default_) { | |
168 default_->Update(caps_lock_enabled_); | |
169 } else { | |
170 if (caps_lock_enabled_) { | |
171 if (!message_shown_) { | |
172 WmShell::Get()->RecordUserMetricsAction( | |
173 UMA_STATUS_AREA_CAPS_LOCK_POPUP); | |
174 PopupDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds, false); | |
175 message_shown_ = true; | |
176 } | |
177 } else if (detailed_) { | |
178 detailed_->GetWidget()->Close(); | |
179 } | |
180 } | |
181 } | |
182 | |
183 bool TrayCapsLock::GetInitialVisibility() { | |
184 return CapsLockIsEnabled(); | |
185 } | |
186 | |
187 views::View* TrayCapsLock::CreateDefaultView(LoginStatus status) { | |
188 if (!caps_lock_enabled_) | |
189 return nullptr; | |
190 DCHECK(!default_); | |
191 default_ = new CapsLockDefaultView; | |
192 default_->Update(caps_lock_enabled_); | |
193 return default_; | |
194 } | |
195 | |
196 views::View* TrayCapsLock::CreateDetailedView(LoginStatus status) { | |
197 DCHECK(!detailed_); | |
198 detailed_ = new views::View; | |
199 | |
200 detailed_->SetLayoutManager(new views::BoxLayout( | |
201 views::BoxLayout::kHorizontal, kTrayPopupPaddingHorizontal, 10, | |
202 kTrayPopupPaddingBetweenItems)); | |
203 | |
204 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
205 views::ImageView* image = new views::ImageView; | |
206 image->SetImage( | |
207 CreateVectorIcon(kSystemMenuCapsLockIcon, kMenuIconSize, kMenuIconColor)); | |
208 detailed_->AddChildView(image); | |
209 | |
210 const int string_id = | |
211 WmShell::Get()->system_tray_delegate()->IsSearchKeyMappedToCapsLock() | |
212 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_SEARCH | |
213 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_ALT_SEARCH; | |
214 views::Label* label = TrayPopupUtils::CreateDefaultLabel(); | |
215 label->SetText(bundle.GetLocalizedString(string_id)); | |
216 label->SetMultiLine(true); | |
217 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
218 detailed_->AddChildView(label); | |
219 WmShell::Get()->RecordUserMetricsAction(UMA_STATUS_AREA_CAPS_LOCK_DETAILED); | |
220 | |
221 return detailed_; | |
222 } | |
223 | |
224 void TrayCapsLock::DestroyDefaultView() { | |
225 default_ = nullptr; | |
226 } | |
227 | |
228 void TrayCapsLock::DestroyDetailedView() { | |
229 detailed_ = nullptr; | |
230 } | |
231 | |
232 } // namespace ash | |
OLD | NEW |