| 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/chromeos/tray_caps_lock.h" | |
| 6 | |
| 7 #include "ash/common/system/tray/actionable_view.h" | |
| 8 #include "ash/common/system/tray/fixed_sized_image_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/wm_shell.h" | |
| 12 #include "base/sys_info.h" | |
| 13 #include "grit/ash_resources.h" | |
| 14 #include "grit/ash_strings.h" | |
| 15 #include "ui/accessibility/ax_view_state.h" | |
| 16 #include "ui/base/ime/chromeos/ime_keyboard.h" | |
| 17 #include "ui/base/ime/chromeos/input_method_manager.h" | |
| 18 #include "ui/base/resource/resource_bundle.h" | |
| 19 #include "ui/gfx/image/image.h" | |
| 20 #include "ui/views/controls/image_view.h" | |
| 21 #include "ui/views/controls/label.h" | |
| 22 #include "ui/views/layout/box_layout.h" | |
| 23 #include "ui/views/widget/widget.h" | |
| 24 | |
| 25 namespace ash { | |
| 26 namespace { | |
| 27 | |
| 28 bool CapsLockIsEnabled() { | |
| 29 chromeos::input_method::InputMethodManager* ime = | |
| 30 chromeos::input_method::InputMethodManager::Get(); | |
| 31 return (ime && ime->GetImeKeyboard()) | |
| 32 ? ime->GetImeKeyboard()->CapsLockIsEnabled() | |
| 33 : false; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 class CapsLockDefaultView : public ActionableView { | |
| 38 public: | |
| 39 CapsLockDefaultView() | |
| 40 : text_label_(new views::Label), shortcut_label_(new views::Label) { | |
| 41 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, | |
| 42 kTrayPopupPaddingHorizontal, 0, | |
| 43 kTrayPopupPaddingBetweenItems)); | |
| 44 | |
| 45 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 46 FixedSizedImageView* image = | |
| 47 new FixedSizedImageView(0, kTrayPopupItemHeight); | |
| 48 image->SetImage( | |
| 49 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_CAPS_LOCK_DARK).ToImageSkia()); | |
| 50 AddChildView(image); | |
| 51 | |
| 52 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 53 AddChildView(text_label_); | |
| 54 | |
| 55 shortcut_label_->SetEnabled(false); | |
| 56 AddChildView(shortcut_label_); | |
| 57 } | |
| 58 | |
| 59 ~CapsLockDefaultView() override {} | |
| 60 | |
| 61 // Updates the label text and the shortcut text. | |
| 62 void Update(bool caps_lock_enabled) { | |
| 63 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 64 const int text_string_id = caps_lock_enabled | |
| 65 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_ENABLED | |
| 66 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_DISABLED; | |
| 67 text_label_->SetText(bundle.GetLocalizedString(text_string_id)); | |
| 68 | |
| 69 int shortcut_string_id = 0; | |
| 70 bool search_mapped_to_caps_lock = | |
| 71 WmShell::Get()->system_tray_delegate()->IsSearchKeyMappedToCapsLock(); | |
| 72 if (caps_lock_enabled) { | |
| 73 shortcut_string_id = | |
| 74 search_mapped_to_caps_lock | |
| 75 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_SEARCH_OR_SHIFT | |
| 76 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_ALT_SEARCH_OR_SHIFT; | |
| 77 } else { | |
| 78 shortcut_string_id = | |
| 79 search_mapped_to_caps_lock | |
| 80 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_SEARCH | |
| 81 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_SHORTCUT_ALT_SEARCH; | |
| 82 } | |
| 83 shortcut_label_->SetText(bundle.GetLocalizedString(shortcut_string_id)); | |
| 84 | |
| 85 Layout(); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 // Overridden from views::View: | |
| 90 void Layout() override { | |
| 91 views::View::Layout(); | |
| 92 | |
| 93 // Align the shortcut text with the right end | |
| 94 const int old_x = shortcut_label_->x(); | |
| 95 const int new_x = | |
| 96 width() - shortcut_label_->width() - kTrayPopupPaddingHorizontal; | |
| 97 shortcut_label_->SetX(new_x); | |
| 98 const gfx::Size text_size = text_label_->size(); | |
| 99 text_label_->SetSize( | |
| 100 gfx::Size(text_size.width() + new_x - old_x, text_size.height())); | |
| 101 } | |
| 102 | |
| 103 void GetAccessibleState(ui::AXViewState* state) override { | |
| 104 state->role = ui::AX_ROLE_BUTTON; | |
| 105 state->name = text_label_->text(); | |
| 106 } | |
| 107 | |
| 108 // Overridden from ActionableView: | |
| 109 bool PerformAction(const ui::Event& event) override { | |
| 110 chromeos::input_method::ImeKeyboard* keyboard = | |
| 111 chromeos::input_method::InputMethodManager::Get()->GetImeKeyboard(); | |
| 112 if (keyboard) { | |
| 113 WmShell::Get()->RecordUserMetricsAction( | |
| 114 keyboard->CapsLockIsEnabled() | |
| 115 ? UMA_STATUS_AREA_CAPS_LOCK_DISABLED_BY_CLICK | |
| 116 : UMA_STATUS_AREA_CAPS_LOCK_ENABLED_BY_CLICK); | |
| 117 keyboard->SetCapsLockEnabled(!keyboard->CapsLockIsEnabled()); | |
| 118 } | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 views::Label* text_label_; | |
| 123 views::Label* shortcut_label_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(CapsLockDefaultView); | |
| 126 }; | |
| 127 | |
| 128 TrayCapsLock::TrayCapsLock(SystemTray* system_tray) | |
| 129 : TrayImageItem(system_tray, IDR_AURA_UBER_TRAY_CAPS_LOCK), | |
| 130 default_(NULL), | |
| 131 detailed_(NULL), | |
| 132 caps_lock_enabled_(CapsLockIsEnabled()), | |
| 133 message_shown_(false) { | |
| 134 chromeos::input_method::InputMethodManager* ime = | |
| 135 chromeos::input_method::InputMethodManager::Get(); | |
| 136 if (ime && ime->GetImeKeyboard()) | |
| 137 ime->GetImeKeyboard()->AddObserver(this); | |
| 138 } | |
| 139 | |
| 140 TrayCapsLock::~TrayCapsLock() { | |
| 141 chromeos::input_method::InputMethodManager* ime = | |
| 142 chromeos::input_method::InputMethodManager::Get(); | |
| 143 if (ime && ime->GetImeKeyboard()) | |
| 144 ime->GetImeKeyboard()->RemoveObserver(this); | |
| 145 } | |
| 146 | |
| 147 void TrayCapsLock::OnCapsLockChanged(bool enabled) { | |
| 148 caps_lock_enabled_ = enabled; | |
| 149 | |
| 150 if (tray_view()) | |
| 151 tray_view()->SetVisible(caps_lock_enabled_); | |
| 152 | |
| 153 if (default_) { | |
| 154 default_->Update(caps_lock_enabled_); | |
| 155 } else { | |
| 156 if (caps_lock_enabled_) { | |
| 157 if (!message_shown_) { | |
| 158 WmShell::Get()->RecordUserMetricsAction( | |
| 159 UMA_STATUS_AREA_CAPS_LOCK_POPUP); | |
| 160 PopupDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds, false); | |
| 161 message_shown_ = true; | |
| 162 } | |
| 163 } else if (detailed_) { | |
| 164 detailed_->GetWidget()->Close(); | |
| 165 } | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 bool TrayCapsLock::GetInitialVisibility() { | |
| 170 return CapsLockIsEnabled(); | |
| 171 } | |
| 172 | |
| 173 views::View* TrayCapsLock::CreateDefaultView(LoginStatus status) { | |
| 174 if (!caps_lock_enabled_) | |
| 175 return NULL; | |
| 176 DCHECK(default_ == NULL); | |
| 177 default_ = new CapsLockDefaultView; | |
| 178 default_->Update(caps_lock_enabled_); | |
| 179 return default_; | |
| 180 } | |
| 181 | |
| 182 views::View* TrayCapsLock::CreateDetailedView(LoginStatus status) { | |
| 183 DCHECK(detailed_ == NULL); | |
| 184 detailed_ = new views::View; | |
| 185 | |
| 186 detailed_->SetLayoutManager(new views::BoxLayout( | |
| 187 views::BoxLayout::kHorizontal, kTrayPopupPaddingHorizontal, 10, | |
| 188 kTrayPopupPaddingBetweenItems)); | |
| 189 | |
| 190 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 191 views::ImageView* image = new views::ImageView; | |
| 192 image->SetImage( | |
| 193 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_CAPS_LOCK_DARK).ToImageSkia()); | |
| 194 | |
| 195 detailed_->AddChildView(image); | |
| 196 | |
| 197 const int string_id = | |
| 198 WmShell::Get()->system_tray_delegate()->IsSearchKeyMappedToCapsLock() | |
| 199 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_SEARCH | |
| 200 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_ALT_SEARCH; | |
| 201 views::Label* label = new views::Label(bundle.GetLocalizedString(string_id)); | |
| 202 label->SetMultiLine(true); | |
| 203 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 204 detailed_->AddChildView(label); | |
| 205 WmShell::Get()->RecordUserMetricsAction(UMA_STATUS_AREA_CAPS_LOCK_DETAILED); | |
| 206 | |
| 207 return detailed_; | |
| 208 } | |
| 209 | |
| 210 void TrayCapsLock::DestroyDefaultView() { | |
| 211 default_ = NULL; | |
| 212 } | |
| 213 | |
| 214 void TrayCapsLock::DestroyDetailedView() { | |
| 215 detailed_ = NULL; | |
| 216 } | |
| 217 | |
| 218 } // namespace ash | |
| OLD | NEW |