| 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/system/ime/tray_ime_chromeos.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "ash/common/session/session_state_delegate.h" | |
| 10 #include "ash/common/system/tray/hover_highlight_view.h" | |
| 11 #include "ash/common/system/tray/system_tray_delegate.h" | |
| 12 #include "ash/common/system/tray/tray_constants.h" | |
| 13 #include "ash/common/system/tray/tray_details_view.h" | |
| 14 #include "ash/common/system/tray/tray_item_more.h" | |
| 15 #include "ash/common/system/tray/tray_item_view.h" | |
| 16 #include "ash/common/system/tray/tray_utils.h" | |
| 17 #include "ash/common/system/tray/wm_system_tray_notifier.h" | |
| 18 #include "ash/common/system/tray_accessibility.h" | |
| 19 #include "ash/common/wm_shell.h" | |
| 20 #include "ash/metrics/user_metrics_recorder.h" | |
| 21 #include "ash/root_window_controller.h" | |
| 22 #include "ash/shell.h" | |
| 23 #include "ash/system/tray/system_tray.h" | |
| 24 #include "ash/system/tray/system_tray_notifier.h" | |
| 25 #include "ash/virtual_keyboard_controller.h" | |
| 26 #include "base/logging.h" | |
| 27 #include "base/strings/utf_string_conversions.h" | |
| 28 #include "grit/ash_resources.h" | |
| 29 #include "grit/ash_strings.h" | |
| 30 #include "ui/accessibility/ax_enums.h" | |
| 31 #include "ui/accessibility/ax_view_state.h" | |
| 32 #include "ui/base/resource/resource_bundle.h" | |
| 33 #include "ui/gfx/font.h" | |
| 34 #include "ui/gfx/image/image.h" | |
| 35 #include "ui/keyboard/keyboard_util.h" | |
| 36 #include "ui/views/controls/label.h" | |
| 37 #include "ui/views/layout/box_layout.h" | |
| 38 #include "ui/views/widget/widget.h" | |
| 39 | |
| 40 namespace ash { | |
| 41 namespace tray { | |
| 42 | |
| 43 // A |HoverHighlightView| that uses bold or normal font depending on whether | |
| 44 // it is selected. This view exposes itself as a checkbox to the accessibility | |
| 45 // framework. | |
| 46 class SelectableHoverHighlightView : public HoverHighlightView { | |
| 47 public: | |
| 48 SelectableHoverHighlightView(ViewClickListener* listener, | |
| 49 const base::string16& label, | |
| 50 bool selected) | |
| 51 : HoverHighlightView(listener), selected_(selected) { | |
| 52 AddLabel(label, gfx::ALIGN_LEFT, selected); | |
| 53 } | |
| 54 | |
| 55 ~SelectableHoverHighlightView() override {} | |
| 56 | |
| 57 protected: | |
| 58 // Overridden from views::View. | |
| 59 void GetAccessibleState(ui::AXViewState* state) override { | |
| 60 HoverHighlightView::GetAccessibleState(state); | |
| 61 state->role = ui::AX_ROLE_CHECK_BOX; | |
| 62 if (selected_) | |
| 63 state->AddStateFlag(ui::AX_STATE_CHECKED); | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 bool selected_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(SelectableHoverHighlightView); | |
| 70 }; | |
| 71 | |
| 72 class IMEDefaultView : public TrayItemMore { | |
| 73 public: | |
| 74 explicit IMEDefaultView(SystemTrayItem* owner, const base::string16& label) | |
| 75 : TrayItemMore(owner, true) { | |
| 76 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 77 SetImage(bundle.GetImageNamed(IDR_AURA_UBER_TRAY_IME).ToImageSkia()); | |
| 78 UpdateLabel(label); | |
| 79 } | |
| 80 | |
| 81 ~IMEDefaultView() override {} | |
| 82 | |
| 83 void UpdateLabel(const base::string16& label) { | |
| 84 SetLabel(label); | |
| 85 SetAccessibleName(label); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 DISALLOW_COPY_AND_ASSIGN(IMEDefaultView); | |
| 90 }; | |
| 91 | |
| 92 class IMEDetailedView : public TrayDetailsView, public ViewClickListener { | |
| 93 public: | |
| 94 IMEDetailedView(SystemTrayItem* owner, | |
| 95 LoginStatus login, | |
| 96 bool show_keyboard_toggle) | |
| 97 : TrayDetailsView(owner), login_(login) { | |
| 98 SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate(); | |
| 99 IMEInfoList list; | |
| 100 delegate->GetAvailableIMEList(&list); | |
| 101 IMEPropertyInfoList property_list; | |
| 102 delegate->GetCurrentIMEProperties(&property_list); | |
| 103 Update(list, property_list, show_keyboard_toggle); | |
| 104 } | |
| 105 | |
| 106 ~IMEDetailedView() override {} | |
| 107 | |
| 108 void Update(const IMEInfoList& list, | |
| 109 const IMEPropertyInfoList& property_list, | |
| 110 bool show_keyboard_toggle) { | |
| 111 Reset(); | |
| 112 ime_map_.clear(); | |
| 113 property_map_.clear(); | |
| 114 CreateScrollableList(); | |
| 115 | |
| 116 if (list.size() > 1) | |
| 117 AppendIMEList(list); | |
| 118 if (!property_list.empty()) | |
| 119 AppendIMEProperties(property_list); | |
| 120 | |
| 121 if (show_keyboard_toggle) { | |
| 122 if (list.size() > 1 || !property_list.empty()) | |
| 123 AddScrollSeparator(); | |
| 124 AppendKeyboardStatus(); | |
| 125 } | |
| 126 | |
| 127 bool userAddingRunning = ash::Shell::GetInstance() | |
| 128 ->session_state_delegate() | |
| 129 ->IsInSecondaryLoginScreen(); | |
| 130 if (login_ != LoginStatus::NOT_LOGGED_IN && login_ != LoginStatus::LOCKED && | |
| 131 !userAddingRunning) | |
| 132 AppendSettings(); | |
| 133 AppendHeaderEntry(); | |
| 134 | |
| 135 Layout(); | |
| 136 SchedulePaint(); | |
| 137 } | |
| 138 | |
| 139 private: | |
| 140 void AppendHeaderEntry() { CreateSpecialRow(IDS_ASH_STATUS_TRAY_IME, this); } | |
| 141 | |
| 142 // Appends the IMEs to the scrollable area of the detailed view. | |
| 143 void AppendIMEList(const IMEInfoList& list) { | |
| 144 DCHECK(ime_map_.empty()); | |
| 145 for (size_t i = 0; i < list.size(); i++) { | |
| 146 HoverHighlightView* container = new SelectableHoverHighlightView( | |
| 147 this, list[i].name, list[i].selected); | |
| 148 scroll_content()->AddChildView(container); | |
| 149 ime_map_[container] = list[i].id; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 // Appends the IME listed to the scrollable area of the detailed | |
| 154 // view. | |
| 155 void AppendIMEProperties(const IMEPropertyInfoList& property_list) { | |
| 156 DCHECK(property_map_.empty()); | |
| 157 for (size_t i = 0; i < property_list.size(); i++) { | |
| 158 HoverHighlightView* container = new SelectableHoverHighlightView( | |
| 159 this, property_list[i].name, property_list[i].selected); | |
| 160 if (i == 0) | |
| 161 container->SetBorder(views::Border::CreateSolidSidedBorder( | |
| 162 1, 0, 0, 0, kBorderLightColor)); | |
| 163 scroll_content()->AddChildView(container); | |
| 164 property_map_[container] = property_list[i].key; | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 void AppendKeyboardStatus() { | |
| 169 HoverHighlightView* container = new HoverHighlightView(this); | |
| 170 int id = keyboard::IsKeyboardEnabled() | |
| 171 ? IDS_ASH_STATUS_TRAY_DISABLE_KEYBOARD | |
| 172 : IDS_ASH_STATUS_TRAY_ENABLE_KEYBOARD; | |
| 173 container->AddLabel( | |
| 174 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(id), | |
| 175 gfx::ALIGN_LEFT, false /* highlight */); | |
| 176 scroll_content()->AddChildView(container); | |
| 177 keyboard_status_ = container; | |
| 178 } | |
| 179 | |
| 180 void AppendSettings() { | |
| 181 HoverHighlightView* container = new HoverHighlightView(this); | |
| 182 container->AddLabel( | |
| 183 ui::ResourceBundle::GetSharedInstance().GetLocalizedString( | |
| 184 IDS_ASH_STATUS_TRAY_IME_SETTINGS), | |
| 185 gfx::ALIGN_LEFT, false /* highlight */); | |
| 186 AddChildView(container); | |
| 187 settings_ = container; | |
| 188 } | |
| 189 | |
| 190 // Overridden from ViewClickListener. | |
| 191 void OnViewClicked(views::View* sender) override { | |
| 192 SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate(); | |
| 193 if (sender == footer()->content()) { | |
| 194 TransitionToDefaultView(); | |
| 195 } else if (sender == settings_) { | |
| 196 Shell::GetInstance()->metrics()->RecordUserMetricsAction( | |
| 197 ash::UMA_STATUS_AREA_IME_SHOW_DETAILED); | |
| 198 delegate->ShowIMESettings(); | |
| 199 } else if (sender == keyboard_status_) { | |
| 200 Shell::GetInstance()->virtual_keyboard_controller() | |
| 201 ->ToggleIgnoreExternalKeyboard(); | |
| 202 } else { | |
| 203 std::map<views::View*, std::string>::const_iterator ime_find; | |
| 204 ime_find = ime_map_.find(sender); | |
| 205 if (ime_find != ime_map_.end()) { | |
| 206 Shell::GetInstance()->metrics()->RecordUserMetricsAction( | |
| 207 ash::UMA_STATUS_AREA_IME_SWITCH_MODE); | |
| 208 std::string ime_id = ime_find->second; | |
| 209 delegate->SwitchIME(ime_id); | |
| 210 GetWidget()->Close(); | |
| 211 } else { | |
| 212 std::map<views::View*, std::string>::const_iterator prop_find; | |
| 213 prop_find = property_map_.find(sender); | |
| 214 if (prop_find != property_map_.end()) { | |
| 215 const std::string key = prop_find->second; | |
| 216 delegate->ActivateIMEProperty(key); | |
| 217 GetWidget()->Close(); | |
| 218 } | |
| 219 } | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 LoginStatus login_; | |
| 224 | |
| 225 std::map<views::View*, std::string> ime_map_; | |
| 226 std::map<views::View*, std::string> property_map_; | |
| 227 views::View* settings_; | |
| 228 views::View* keyboard_status_; | |
| 229 | |
| 230 DISALLOW_COPY_AND_ASSIGN(IMEDetailedView); | |
| 231 }; | |
| 232 | |
| 233 } // namespace tray | |
| 234 | |
| 235 TrayIME::TrayIME(SystemTray* system_tray) | |
| 236 : SystemTrayItem(system_tray), | |
| 237 tray_label_(NULL), | |
| 238 default_(NULL), | |
| 239 detailed_(NULL), | |
| 240 keyboard_suppressed_(false), | |
| 241 is_visible_(true) { | |
| 242 Shell::GetInstance()->system_tray_notifier()->AddIMEObserver(this); | |
| 243 Shell::GetInstance()->system_tray_notifier()->AddVirtualKeyboardObserver( | |
| 244 this); | |
| 245 WmShell::Get()->system_tray_notifier()->AddAccessibilityObserver(this); | |
| 246 } | |
| 247 | |
| 248 TrayIME::~TrayIME() { | |
| 249 Shell::GetInstance()->system_tray_notifier()->RemoveIMEObserver(this); | |
| 250 WmShell::Get()->system_tray_notifier()->RemoveAccessibilityObserver(this); | |
| 251 Shell::GetInstance()->system_tray_notifier()->RemoveVirtualKeyboardObserver( | |
| 252 this); | |
| 253 } | |
| 254 | |
| 255 void TrayIME::OnKeyboardSuppressionChanged(bool suppressed) { | |
| 256 keyboard_suppressed_ = suppressed; | |
| 257 Update(); | |
| 258 } | |
| 259 | |
| 260 void TrayIME::OnAccessibilityModeChanged( | |
| 261 AccessibilityNotificationVisibility notify) { | |
| 262 Update(); | |
| 263 } | |
| 264 | |
| 265 void TrayIME::Update() { | |
| 266 UpdateTrayLabel(current_ime_, ime_list_.size()); | |
| 267 if (default_) { | |
| 268 default_->SetVisible(ShouldDefaultViewBeVisible()); | |
| 269 default_->UpdateLabel(GetDefaultViewLabel(ime_list_.size() > 1)); | |
| 270 } | |
| 271 if (detailed_) | |
| 272 detailed_->Update(ime_list_, property_list_, ShouldShowKeyboardToggle()); | |
| 273 } | |
| 274 | |
| 275 void TrayIME::UpdateTrayLabel(const IMEInfo& current, size_t count) { | |
| 276 if (tray_label_) { | |
| 277 bool visible = count > 1 && is_visible_; | |
| 278 tray_label_->SetVisible(visible); | |
| 279 // Do not change label before hiding because this change is noticeable. | |
| 280 if (!visible) | |
| 281 return; | |
| 282 if (current.third_party) { | |
| 283 tray_label_->label()->SetText(current.short_name + | |
| 284 base::UTF8ToUTF16("*")); | |
| 285 } else { | |
| 286 tray_label_->label()->SetText(current.short_name); | |
| 287 } | |
| 288 SetTrayLabelItemBorder(tray_label_, system_tray()->shelf_alignment()); | |
| 289 tray_label_->Layout(); | |
| 290 } | |
| 291 } | |
| 292 | |
| 293 bool TrayIME::ShouldShowKeyboardToggle() { | |
| 294 return keyboard_suppressed_ && | |
| 295 !Shell::GetInstance() | |
| 296 ->accessibility_delegate() | |
| 297 ->IsVirtualKeyboardEnabled(); | |
| 298 } | |
| 299 | |
| 300 base::string16 TrayIME::GetDefaultViewLabel(bool show_ime_label) { | |
| 301 if (show_ime_label) { | |
| 302 IMEInfo current; | |
| 303 WmShell::Get()->system_tray_delegate()->GetCurrentIME(¤t); | |
| 304 return current.name; | |
| 305 } else { | |
| 306 // Display virtual keyboard status instead. | |
| 307 int id = keyboard::IsKeyboardEnabled() | |
| 308 ? IDS_ASH_STATUS_TRAY_KEYBOARD_ENABLED | |
| 309 : IDS_ASH_STATUS_TRAY_KEYBOARD_DISABLED; | |
| 310 return ui::ResourceBundle::GetSharedInstance().GetLocalizedString(id); | |
| 311 } | |
| 312 } | |
| 313 | |
| 314 views::View* TrayIME::CreateTrayView(LoginStatus status) { | |
| 315 CHECK(tray_label_ == NULL); | |
| 316 tray_label_ = new TrayItemView(this); | |
| 317 tray_label_->CreateLabel(); | |
| 318 SetupLabelForTray(tray_label_->label()); | |
| 319 // Hide IME tray when it is created, it will be updated when it is notified | |
| 320 // of the IME refresh event. | |
| 321 tray_label_->SetVisible(false); | |
| 322 return tray_label_; | |
| 323 } | |
| 324 | |
| 325 views::View* TrayIME::CreateDefaultView(LoginStatus status) { | |
| 326 CHECK(default_ == NULL); | |
| 327 default_ = | |
| 328 new tray::IMEDefaultView(this, GetDefaultViewLabel(ime_list_.size() > 1)); | |
| 329 default_->SetVisible(ShouldDefaultViewBeVisible()); | |
| 330 return default_; | |
| 331 } | |
| 332 | |
| 333 views::View* TrayIME::CreateDetailedView(LoginStatus status) { | |
| 334 CHECK(detailed_ == NULL); | |
| 335 detailed_ = | |
| 336 new tray::IMEDetailedView(this, status, ShouldShowKeyboardToggle()); | |
| 337 return detailed_; | |
| 338 } | |
| 339 | |
| 340 void TrayIME::DestroyTrayView() { | |
| 341 tray_label_ = NULL; | |
| 342 } | |
| 343 | |
| 344 void TrayIME::DestroyDefaultView() { | |
| 345 default_ = NULL; | |
| 346 } | |
| 347 | |
| 348 void TrayIME::DestroyDetailedView() { | |
| 349 detailed_ = NULL; | |
| 350 } | |
| 351 | |
| 352 void TrayIME::UpdateAfterLoginStatusChange(LoginStatus status) {} | |
| 353 | |
| 354 void TrayIME::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { | |
| 355 SetTrayLabelItemBorder(tray_label_, alignment); | |
| 356 tray_label_->Layout(); | |
| 357 } | |
| 358 | |
| 359 void TrayIME::OnIMERefresh() { | |
| 360 // Caches the current ime state. | |
| 361 SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate(); | |
| 362 ime_list_.clear(); | |
| 363 property_list_.clear(); | |
| 364 delegate->GetCurrentIME(¤t_ime_); | |
| 365 delegate->GetAvailableIMEList(&ime_list_); | |
| 366 delegate->GetCurrentIMEProperties(&property_list_); | |
| 367 | |
| 368 Update(); | |
| 369 } | |
| 370 | |
| 371 void TrayIME::OnIMEMenuActivationChanged(bool is_active) { | |
| 372 is_visible_ = !is_active; | |
| 373 if (is_visible_) | |
| 374 OnIMERefresh(); | |
| 375 else | |
| 376 Update(); | |
| 377 } | |
| 378 | |
| 379 bool TrayIME::ShouldDefaultViewBeVisible() { | |
| 380 return is_visible_ && (ime_list_.size() > 1 || property_list_.size() > 1 || | |
| 381 ShouldShowKeyboardToggle()); | |
| 382 } | |
| 383 | |
| 384 } // namespace ash | |
| OLD | NEW |