Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / tray_accessibility.cc
blobeba3bf07b07ee28e4510a9ea8618c1b0a77ada49
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_accessibility.h"
7 #include "ash/accessibility_delegate.h"
8 #include "ash/metrics/user_metrics_recorder.h"
9 #include "ash/session/session_state_delegate.h"
10 #include "ash/shell.h"
11 #include "ash/system/tray/hover_highlight_view.h"
12 #include "ash/system/tray/system_tray.h"
13 #include "ash/system/tray/system_tray_delegate.h"
14 #include "ash/system/tray/system_tray_notifier.h"
15 #include "ash/system/tray/tray_constants.h"
16 #include "ash/system/tray/tray_details_view.h"
17 #include "ash/system/tray/tray_item_more.h"
18 #include "ash/system/tray/tray_popup_label_button.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "grit/ash_resources.h"
21 #include "grit/ash_strings.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/views/controls/image_view.h"
26 #include "ui/views/controls/label.h"
27 #include "ui/views/layout/box_layout.h"
28 #include "ui/views/widget/widget.h"
30 namespace ash {
31 namespace {
33 enum AccessibilityState {
34 A11Y_NONE = 0,
35 A11Y_SPOKEN_FEEDBACK = 1 << 0,
36 A11Y_HIGH_CONTRAST = 1 << 1,
37 A11Y_SCREEN_MAGNIFIER = 1 << 2,
38 A11Y_LARGE_CURSOR = 1 << 3,
39 A11Y_AUTOCLICK = 1 << 4,
40 A11Y_VIRTUAL_KEYBOARD = 1 << 5,
41 A11Y_BRAILLE_DISPLAY_CONNECTED = 1 << 6,
44 uint32 GetAccessibilityState() {
45 AccessibilityDelegate* delegate =
46 Shell::GetInstance()->accessibility_delegate();
47 uint32 state = A11Y_NONE;
48 if (delegate->IsSpokenFeedbackEnabled())
49 state |= A11Y_SPOKEN_FEEDBACK;
50 if (delegate->IsHighContrastEnabled())
51 state |= A11Y_HIGH_CONTRAST;
52 if (delegate->IsMagnifierEnabled())
53 state |= A11Y_SCREEN_MAGNIFIER;
54 if (delegate->IsLargeCursorEnabled())
55 state |= A11Y_LARGE_CURSOR;
56 if (delegate->IsAutoclickEnabled())
57 state |= A11Y_AUTOCLICK;
58 if (delegate->IsVirtualKeyboardEnabled())
59 state |= A11Y_VIRTUAL_KEYBOARD;
60 if (delegate->IsBrailleDisplayConnected())
61 state |= A11Y_BRAILLE_DISPLAY_CONNECTED;
62 return state;
65 user::LoginStatus GetCurrentLoginStatus() {
66 return Shell::GetInstance()->system_tray_delegate()->GetUserLoginStatus();
69 } // namespace
71 namespace tray {
73 class DefaultAccessibilityView : public TrayItemMore {
74 public:
75 explicit DefaultAccessibilityView(SystemTrayItem* owner)
76 : TrayItemMore(owner, true) {
77 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
78 SetImage(bundle.GetImageNamed(IDR_AURA_UBER_TRAY_ACCESSIBILITY_DARK).
79 ToImageSkia());
80 base::string16 label = bundle.GetLocalizedString(
81 IDS_ASH_STATUS_TRAY_ACCESSIBILITY);
82 SetLabel(label);
83 SetAccessibleName(label);
84 set_id(test::kAccessibilityTrayItemViewId);
87 ~DefaultAccessibilityView() override {}
89 private:
90 DISALLOW_COPY_AND_ASSIGN(DefaultAccessibilityView);
93 ////////////////////////////////////////////////////////////////////////////////
94 // ash::tray::AccessibilityPopupView
96 AccessibilityPopupView::AccessibilityPopupView(SystemTrayItem* owner,
97 uint32 enabled_state_bits)
98 : TrayNotificationView(owner, IDR_AURA_UBER_TRAY_ACCESSIBILITY_DARK),
99 label_(CreateLabel(enabled_state_bits)) {
100 InitView(label_);
103 views::Label* AccessibilityPopupView::CreateLabel(uint32 enabled_state_bits) {
104 DCHECK((enabled_state_bits &
105 (A11Y_SPOKEN_FEEDBACK | A11Y_BRAILLE_DISPLAY_CONNECTED)) != 0);
106 base::string16 text;
107 if (enabled_state_bits & A11Y_BRAILLE_DISPLAY_CONNECTED) {
108 text.append(l10n_util::GetStringUTF16(
109 IDS_ASH_STATUS_TRAY_BRAILLE_DISPLAY_CONNECTED_BUBBLE));
111 if (enabled_state_bits & A11Y_SPOKEN_FEEDBACK) {
112 if (!text.empty())
113 text.append(base::ASCIIToUTF16(" "));
114 text.append(l10n_util::GetStringUTF16(
115 IDS_ASH_STATUS_TRAY_SPOKEN_FEEDBACK_ENABLED_BUBBLE));
117 views::Label* label = new views::Label(text);
118 label->SetMultiLine(true);
119 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
120 return label;
123 ////////////////////////////////////////////////////////////////////////////////
124 // ash::tray::AccessibilityDetailedView
126 AccessibilityDetailedView::AccessibilityDetailedView(
127 SystemTrayItem* owner, user::LoginStatus login) :
128 TrayDetailsView(owner),
129 spoken_feedback_view_(NULL),
130 high_contrast_view_(NULL),
131 screen_magnifier_view_(NULL),
132 large_cursor_view_(NULL),
133 help_view_(NULL),
134 settings_view_(NULL),
135 autoclick_view_(NULL),
136 virtual_keyboard_view_(NULL),
137 spoken_feedback_enabled_(false),
138 high_contrast_enabled_(false),
139 screen_magnifier_enabled_(false),
140 large_cursor_enabled_(false),
141 autoclick_enabled_(false),
142 virtual_keyboard_enabled_(false),
143 login_(login) {
145 Reset();
147 AppendAccessibilityList();
148 AppendHelpEntries();
149 CreateSpecialRow(IDS_ASH_STATUS_TRAY_ACCESSIBILITY_TITLE, this);
151 Layout();
154 void AccessibilityDetailedView::AppendAccessibilityList() {
155 CreateScrollableList();
156 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
158 AccessibilityDelegate* delegate =
159 Shell::GetInstance()->accessibility_delegate();
160 spoken_feedback_enabled_ = delegate->IsSpokenFeedbackEnabled();
161 spoken_feedback_view_ =
162 AddScrollListItem(bundle.GetLocalizedString(
163 IDS_ASH_STATUS_TRAY_ACCESSIBILITY_SPOKEN_FEEDBACK),
164 spoken_feedback_enabled_, spoken_feedback_enabled_);
166 // Large Cursor item is shown only in Login screen.
167 if (login_ == user::LOGGED_IN_NONE) {
168 large_cursor_enabled_ = delegate->IsLargeCursorEnabled();
169 large_cursor_view_ =
170 AddScrollListItem(bundle.GetLocalizedString(
171 IDS_ASH_STATUS_TRAY_ACCESSIBILITY_LARGE_CURSOR),
172 large_cursor_enabled_, large_cursor_enabled_);
175 high_contrast_enabled_ = delegate->IsHighContrastEnabled();
176 high_contrast_view_ = AddScrollListItem(
177 bundle.GetLocalizedString(
178 IDS_ASH_STATUS_TRAY_ACCESSIBILITY_HIGH_CONTRAST_MODE),
179 high_contrast_enabled_, high_contrast_enabled_);
180 screen_magnifier_enabled_ = delegate->IsMagnifierEnabled();
181 screen_magnifier_view_ =
182 AddScrollListItem(bundle.GetLocalizedString(
183 IDS_ASH_STATUS_TRAY_ACCESSIBILITY_SCREEN_MAGNIFIER),
184 screen_magnifier_enabled_, screen_magnifier_enabled_);
186 // Don't show autoclick option at login screen.
187 if (login_ != user::LOGGED_IN_NONE) {
188 autoclick_enabled_ = delegate->IsAutoclickEnabled();
189 autoclick_view_ = AddScrollListItem(
190 bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_ACCESSIBILITY_AUTOCLICK),
191 autoclick_enabled_, autoclick_enabled_);
194 virtual_keyboard_enabled_ = delegate->IsVirtualKeyboardEnabled();
195 virtual_keyboard_view_ =
196 AddScrollListItem(bundle.GetLocalizedString(
197 IDS_ASH_STATUS_TRAY_ACCESSIBILITY_VIRTUAL_KEYBOARD),
198 virtual_keyboard_enabled_, virtual_keyboard_enabled_);
201 void AccessibilityDetailedView::AppendHelpEntries() {
202 // Currently the help page requires a browser window.
203 // TODO(yoshiki): show this even on login/lock screen. crbug.com/158286
204 bool userAddingRunning = ash::Shell::GetInstance()
205 ->session_state_delegate()
206 ->IsInSecondaryLoginScreen();
208 if (login_ == user::LOGGED_IN_NONE ||
209 login_ == user::LOGGED_IN_LOCKED || userAddingRunning)
210 return;
212 views::View* bottom_row = new View();
213 views::BoxLayout* layout = new
214 views::BoxLayout(views::BoxLayout::kHorizontal,
215 kTrayMenuBottomRowPadding,
216 kTrayMenuBottomRowPadding,
217 kTrayMenuBottomRowPaddingBetweenItems);
218 layout->SetDefaultFlex(1);
219 bottom_row->SetLayoutManager(layout);
221 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
223 TrayPopupLabelButton* help = new TrayPopupLabelButton(
224 this,
225 bundle.GetLocalizedString(
226 IDS_ASH_STATUS_TRAY_ACCESSIBILITY_LEARN_MORE));
227 bottom_row->AddChildView(help);
228 help_view_ = help;
230 TrayPopupLabelButton* settings = new TrayPopupLabelButton(
231 this,
232 bundle.GetLocalizedString(
233 IDS_ASH_STATUS_TRAY_ACCESSIBILITY_SETTINGS));
234 bottom_row->AddChildView(settings);
235 settings_view_ = settings;
237 AddChildView(bottom_row);
240 HoverHighlightView* AccessibilityDetailedView::AddScrollListItem(
241 const base::string16& text,
242 bool highlight,
243 bool checked) {
244 HoverHighlightView* container = new HoverHighlightView(this);
245 container->AddCheckableLabel(text, highlight, checked);
246 scroll_content()->AddChildView(container);
247 return container;
250 void AccessibilityDetailedView::OnViewClicked(views::View* sender) {
251 AccessibilityDelegate* delegate =
252 Shell::GetInstance()->accessibility_delegate();
253 if (sender == footer()->content()) {
254 TransitionToDefaultView();
255 } else if (sender == spoken_feedback_view_) {
256 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
257 delegate->IsSpokenFeedbackEnabled() ?
258 ash::UMA_STATUS_AREA_DISABLE_SPOKEN_FEEDBACK :
259 ash::UMA_STATUS_AREA_ENABLE_SPOKEN_FEEDBACK);
260 delegate->ToggleSpokenFeedback(ui::A11Y_NOTIFICATION_NONE);
261 } else if (sender == high_contrast_view_) {
262 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
263 delegate->IsHighContrastEnabled() ?
264 ash::UMA_STATUS_AREA_DISABLE_HIGH_CONTRAST :
265 ash::UMA_STATUS_AREA_ENABLE_HIGH_CONTRAST);
266 delegate->ToggleHighContrast();
267 } else if (sender == screen_magnifier_view_) {
268 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
269 delegate->IsMagnifierEnabled() ?
270 ash::UMA_STATUS_AREA_DISABLE_MAGNIFIER :
271 ash::UMA_STATUS_AREA_ENABLE_MAGNIFIER);
272 delegate->SetMagnifierEnabled(!delegate->IsMagnifierEnabled());
273 } else if (large_cursor_view_ && sender == large_cursor_view_) {
274 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
275 delegate->IsLargeCursorEnabled() ?
276 ash::UMA_STATUS_AREA_DISABLE_LARGE_CURSOR :
277 ash::UMA_STATUS_AREA_ENABLE_LARGE_CURSOR);
278 delegate->SetLargeCursorEnabled(!delegate->IsLargeCursorEnabled());
279 } else if (autoclick_view_ && sender == autoclick_view_) {
280 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
281 delegate->IsAutoclickEnabled() ?
282 ash::UMA_STATUS_AREA_DISABLE_AUTO_CLICK :
283 ash::UMA_STATUS_AREA_ENABLE_AUTO_CLICK);
284 delegate->SetAutoclickEnabled(!delegate->IsAutoclickEnabled());
285 } else if (virtual_keyboard_view_ && sender == virtual_keyboard_view_) {
286 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
287 delegate->IsVirtualKeyboardEnabled() ?
288 ash::UMA_STATUS_AREA_DISABLE_VIRTUAL_KEYBOARD :
289 ash::UMA_STATUS_AREA_ENABLE_VIRTUAL_KEYBOARD);
290 delegate->SetVirtualKeyboardEnabled(!delegate->IsVirtualKeyboardEnabled());
294 void AccessibilityDetailedView::ButtonPressed(views::Button* sender,
295 const ui::Event& event) {
296 SystemTrayDelegate* tray_delegate =
297 Shell::GetInstance()->system_tray_delegate();
298 if (sender == help_view_)
299 tray_delegate->ShowAccessibilityHelp();
300 else if (sender == settings_view_)
301 tray_delegate->ShowAccessibilitySettings();
304 } // namespace tray
306 ////////////////////////////////////////////////////////////////////////////////
307 // ash::TrayAccessibility
309 TrayAccessibility::TrayAccessibility(SystemTray* system_tray)
310 : TrayImageItem(system_tray, IDR_AURA_UBER_TRAY_ACCESSIBILITY),
311 default_(NULL),
312 detailed_popup_(NULL),
313 detailed_menu_(NULL),
314 request_popup_view_state_(A11Y_NONE),
315 tray_icon_visible_(false),
316 login_(GetCurrentLoginStatus()),
317 previous_accessibility_state_(GetAccessibilityState()),
318 show_a11y_menu_on_lock_screen_(true) {
319 DCHECK(Shell::GetInstance()->delegate());
320 DCHECK(system_tray);
321 Shell::GetInstance()->system_tray_notifier()->AddAccessibilityObserver(this);
324 TrayAccessibility::~TrayAccessibility() {
325 Shell::GetInstance()->system_tray_notifier()->
326 RemoveAccessibilityObserver(this);
329 void TrayAccessibility::SetTrayIconVisible(bool visible) {
330 if (tray_view())
331 tray_view()->SetVisible(visible);
332 tray_icon_visible_ = visible;
335 tray::AccessibilityDetailedView* TrayAccessibility::CreateDetailedMenu() {
336 return new tray::AccessibilityDetailedView(this, login_);
339 bool TrayAccessibility::GetInitialVisibility() {
340 // Shows accessibility icon if any accessibility feature is enabled.
341 // Otherwise, doen't show it.
342 return GetAccessibilityState() != A11Y_NONE;
345 views::View* TrayAccessibility::CreateDefaultView(user::LoginStatus status) {
346 CHECK(default_ == NULL);
348 // Shows accessibility menu if:
349 // - on login screen (not logged in);
350 // - "Enable accessibility menu" on chrome://settings is checked;
351 // - or any of accessibility features is enabled
352 // Otherwise, not shows it.
353 AccessibilityDelegate* delegate =
354 Shell::GetInstance()->accessibility_delegate();
355 if (login_ != user::LOGGED_IN_NONE &&
356 !delegate->ShouldShowAccessibilityMenu() &&
357 // On login screen, keeps the initial visibility of the menu.
358 (status != user::LOGGED_IN_LOCKED || !show_a11y_menu_on_lock_screen_))
359 return NULL;
361 CHECK(default_ == NULL);
362 default_ = new tray::DefaultAccessibilityView(this);
364 return default_;
367 views::View* TrayAccessibility::CreateDetailedView(user::LoginStatus status) {
368 CHECK(detailed_popup_ == NULL);
369 CHECK(detailed_menu_ == NULL);
371 if (request_popup_view_state_) {
372 detailed_popup_ =
373 new tray::AccessibilityPopupView(this, request_popup_view_state_);
374 request_popup_view_state_ = A11Y_NONE;
375 return detailed_popup_;
376 } else {
377 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
378 ash::UMA_STATUS_AREA_DETAILED_ACCESSABILITY);
379 detailed_menu_ = CreateDetailedMenu();
380 return detailed_menu_;
384 void TrayAccessibility::DestroyDefaultView() {
385 default_ = NULL;
388 void TrayAccessibility::DestroyDetailedView() {
389 detailed_popup_ = NULL;
390 detailed_menu_ = NULL;
393 void TrayAccessibility::UpdateAfterLoginStatusChange(user::LoginStatus status) {
394 // Stores the a11y feature status on just entering the lock screen.
395 if (login_ != user::LOGGED_IN_LOCKED && status == user::LOGGED_IN_LOCKED)
396 show_a11y_menu_on_lock_screen_ = (GetAccessibilityState() != A11Y_NONE);
398 login_ = status;
399 SetTrayIconVisible(GetInitialVisibility());
402 void TrayAccessibility::OnAccessibilityModeChanged(
403 ui::AccessibilityNotificationVisibility notify) {
404 SetTrayIconVisible(GetInitialVisibility());
406 uint32 accessibility_state = GetAccessibilityState();
407 // We'll get an extra notification if a braille display is connected when
408 // spoken feedback wasn't already enabled. This is because the braille
409 // connection state is already updated when spoken feedback is enabled so
410 // that the notifications can be consolidated into one. Therefore, we
411 // return early if there's no change in the state that we keep track of.
412 if (accessibility_state == previous_accessibility_state_)
413 return;
414 // Contains bits for spoken feedback and braille display connected currently
415 // being enabled.
416 uint32 being_enabled =
417 (accessibility_state & ~previous_accessibility_state_) &
418 (A11Y_SPOKEN_FEEDBACK | A11Y_BRAILLE_DISPLAY_CONNECTED);
419 if ((notify == ui::A11Y_NOTIFICATION_SHOW) && being_enabled != A11Y_NONE) {
420 // Shows popup if |notify| is true and the spoken feedback is being enabled.
421 request_popup_view_state_ = being_enabled;
422 PopupDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds, false);
423 } else {
424 if (detailed_popup_)
425 detailed_popup_->GetWidget()->Close();
426 if (detailed_menu_)
427 detailed_menu_->GetWidget()->Close();
430 previous_accessibility_state_ = accessibility_state;
433 } // namespace ash