Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/system/tray_caps_lock.h" | 5 #include "ash/system/tray_caps_lock.h" |
| 6 | 6 |
| 7 #include "ash/accessibility_delegate.h" | 7 #include "ash/accessibility_delegate.h" |
| 8 #include "ash/resources/grit/ash_resources.h" | 8 #include "ash/resources/grit/ash_resources.h" |
| 9 #include "ash/resources/vector_icons/vector_icons.h" | 9 #include "ash/resources/vector_icons/vector_icons.h" |
| 10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| 11 #include "ash/shell_port.h" | 11 #include "ash/shell_port.h" |
| 12 #include "ash/strings/grit/ash_strings.h" | 12 #include "ash/strings/grit/ash_strings.h" |
| 13 #include "ash/system/system_notifier.h" | |
| 13 #include "ash/system/tray/actionable_view.h" | 14 #include "ash/system/tray/actionable_view.h" |
| 14 #include "ash/system/tray/system_tray_delegate.h" | 15 #include "ash/system/tray/system_tray_delegate.h" |
| 15 #include "ash/system/tray/tray_constants.h" | 16 #include "ash/system/tray/tray_constants.h" |
| 16 #include "ash/system/tray/tray_popup_item_style.h" | 17 #include "ash/system/tray/tray_popup_item_style.h" |
| 17 #include "ash/system/tray/tray_popup_utils.h" | 18 #include "ash/system/tray/tray_popup_utils.h" |
| 18 #include "ash/system/tray/tri_view.h" | 19 #include "ash/system/tray/tri_view.h" |
| 19 #include "base/sys_info.h" | 20 #include "base/sys_info.h" |
| 20 #include "ui/accessibility/ax_node_data.h" | 21 #include "ui/accessibility/ax_node_data.h" |
| 21 #include "ui/base/ime/chromeos/ime_keyboard.h" | 22 #include "ui/base/ime/chromeos/ime_keyboard.h" |
| 22 #include "ui/base/ime/chromeos/input_method_manager.h" | 23 #include "ui/base/ime/chromeos/input_method_manager.h" |
| 23 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
| 25 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 26 #include "ui/gfx/paint_vector_icon.h" | 27 #include "ui/gfx/paint_vector_icon.h" |
| 28 #include "ui/message_center/message_center.h" | |
| 29 #include "ui/message_center/notification.h" | |
| 27 #include "ui/views/border.h" | 30 #include "ui/views/border.h" |
| 28 #include "ui/views/controls/image_view.h" | 31 #include "ui/views/controls/image_view.h" |
| 29 #include "ui/views/controls/label.h" | 32 #include "ui/views/controls/label.h" |
| 30 #include "ui/views/layout/box_layout.h" | 33 #include "ui/views/layout/box_layout.h" |
| 31 #include "ui/views/layout/fill_layout.h" | 34 #include "ui/views/layout/fill_layout.h" |
| 32 #include "ui/views/widget/widget.h" | 35 #include "ui/views/widget/widget.h" |
| 33 | 36 |
| 37 using message_center::Notification; | |
| 38 | |
| 34 namespace ash { | 39 namespace ash { |
| 35 namespace { | 40 namespace { |
| 36 | 41 |
| 37 // Padding used to position the caption in the caps lock default view row. | 42 // Padding used to position the caption in the caps lock default view row. |
| 38 const int kCaptionRightPadding = 6; | 43 const int kCaptionRightPadding = 6; |
| 39 | 44 |
| 45 const char kCapsLockNotificationId[] = "capslock"; | |
| 46 | |
| 40 bool CapsLockIsEnabled() { | 47 bool CapsLockIsEnabled() { |
| 41 chromeos::input_method::InputMethodManager* ime = | 48 chromeos::input_method::InputMethodManager* ime = |
| 42 chromeos::input_method::InputMethodManager::Get(); | 49 chromeos::input_method::InputMethodManager::Get(); |
| 43 return (ime && ime->GetImeKeyboard()) | 50 return (ime && ime->GetImeKeyboard()) |
| 44 ? ime->GetImeKeyboard()->CapsLockIsEnabled() | 51 ? ime->GetImeKeyboard()->CapsLockIsEnabled() |
| 45 : false; | 52 : false; |
| 46 } | 53 } |
| 54 | |
| 55 std::unique_ptr<Notification> CreateNotification() { | |
| 56 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 57 TrayPopupItemStyle default_view_style( | |
| 58 TrayPopupItemStyle::FontStyle::DEFAULT_VIEW_LABEL); | |
|
tdanderson
2017/04/13 17:01:37
Thanks for making use of TrayPopupItemStyle. Howev
tetsui2
2017/04/14 01:54:53
Done.
| |
| 59 const int string_id = | |
| 60 Shell::Get()->system_tray_delegate()->IsSearchKeyMappedToCapsLock() | |
| 61 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_SEARCH | |
| 62 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_ALT_SEARCH; | |
| 63 std::unique_ptr<Notification> notification(new Notification( | |
| 64 message_center::NOTIFICATION_TYPE_SIMPLE, kCapsLockNotificationId, | |
| 65 base::string16(), bundle.GetLocalizedString(string_id), | |
| 66 gfx::Image(gfx::CreateVectorIcon(kSystemMenuCapsLockIcon, | |
| 67 default_view_style.GetIconColor())), | |
| 68 base::string16() /* display_source */, GURL(), | |
| 69 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
| 70 system_notifier::kNotifierCapsLock), | |
| 71 message_center::RichNotificationData(), NULL)); | |
|
tdanderson
2017/04/13 17:01:37
nit: nullptr
tetsui2
2017/04/14 01:54:53
Done.
| |
| 72 return notification; | |
| 47 } | 73 } |
| 48 | 74 |
| 75 } // namespace | |
| 76 | |
| 49 class CapsLockDefaultView : public ActionableView { | 77 class CapsLockDefaultView : public ActionableView { |
| 50 public: | 78 public: |
| 51 CapsLockDefaultView() | 79 CapsLockDefaultView() |
| 52 : ActionableView(nullptr, TrayPopupInkDropStyle::FILL_BOUNDS), | 80 : ActionableView(nullptr, TrayPopupInkDropStyle::FILL_BOUNDS), |
| 53 text_label_(TrayPopupUtils::CreateDefaultLabel()), | 81 text_label_(TrayPopupUtils::CreateDefaultLabel()), |
| 54 shortcut_label_(TrayPopupUtils::CreateDefaultLabel()) { | 82 shortcut_label_(TrayPopupUtils::CreateDefaultLabel()) { |
| 55 shortcut_label_->SetEnabled(false); | 83 shortcut_label_->SetEnabled(false); |
| 56 | 84 |
| 57 TriView* tri_view(TrayPopupUtils::CreateDefaultRowView()); | 85 TriView* tri_view(TrayPopupUtils::CreateDefaultRowView()); |
| 58 SetLayoutManager(new views::FillLayout); | 86 SetLayoutManager(new views::FillLayout); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 | 160 |
| 133 // It indicates the shortcut can be used to turn on or turn off Caps Lock. | 161 // It indicates the shortcut can be used to turn on or turn off Caps Lock. |
| 134 views::Label* shortcut_label_; | 162 views::Label* shortcut_label_; |
| 135 | 163 |
| 136 DISALLOW_COPY_AND_ASSIGN(CapsLockDefaultView); | 164 DISALLOW_COPY_AND_ASSIGN(CapsLockDefaultView); |
| 137 }; | 165 }; |
| 138 | 166 |
| 139 TrayCapsLock::TrayCapsLock(SystemTray* system_tray) | 167 TrayCapsLock::TrayCapsLock(SystemTray* system_tray) |
| 140 : TrayImageItem(system_tray, kSystemTrayCapsLockIcon, UMA_CAPS_LOCK), | 168 : TrayImageItem(system_tray, kSystemTrayCapsLockIcon, UMA_CAPS_LOCK), |
| 141 default_(nullptr), | 169 default_(nullptr), |
| 142 detailed_(nullptr), | 170 // detailed_(nullptr), |
|
tdanderson
2017/04/13 17:01:37
I think you meant to delete this :)
tetsui2
2017/04/14 01:54:53
Done.
| |
| 143 caps_lock_enabled_(CapsLockIsEnabled()), | 171 caps_lock_enabled_(CapsLockIsEnabled()), |
| 144 message_shown_(false) { | 172 message_shown_(false) { |
| 145 chromeos::input_method::InputMethodManager* ime = | 173 chromeos::input_method::InputMethodManager* ime = |
| 146 chromeos::input_method::InputMethodManager::Get(); | 174 chromeos::input_method::InputMethodManager::Get(); |
| 147 if (ime && ime->GetImeKeyboard()) | 175 if (ime && ime->GetImeKeyboard()) |
| 148 ime->GetImeKeyboard()->AddObserver(this); | 176 ime->GetImeKeyboard()->AddObserver(this); |
| 149 } | 177 } |
| 150 | 178 |
| 151 TrayCapsLock::~TrayCapsLock() { | 179 TrayCapsLock::~TrayCapsLock() { |
| 152 chromeos::input_method::InputMethodManager* ime = | 180 chromeos::input_method::InputMethodManager* ime = |
| 153 chromeos::input_method::InputMethodManager::Get(); | 181 chromeos::input_method::InputMethodManager::Get(); |
| 154 if (ime && ime->GetImeKeyboard()) | 182 if (ime && ime->GetImeKeyboard()) |
| 155 ime->GetImeKeyboard()->RemoveObserver(this); | 183 ime->GetImeKeyboard()->RemoveObserver(this); |
| 156 } | 184 } |
| 157 | 185 |
| 158 void TrayCapsLock::OnCapsLockChanged(bool enabled) { | 186 void TrayCapsLock::OnCapsLockChanged(bool enabled) { |
| 159 caps_lock_enabled_ = enabled; | 187 caps_lock_enabled_ = enabled; |
| 160 | 188 |
| 161 // Send an a11y alert. | 189 // Send an a11y alert. |
| 162 Shell::Get()->accessibility_delegate()->TriggerAccessibilityAlert( | 190 Shell::Get()->accessibility_delegate()->TriggerAccessibilityAlert( |
| 163 enabled ? A11Y_ALERT_CAPS_ON : A11Y_ALERT_CAPS_OFF); | 191 enabled ? A11Y_ALERT_CAPS_ON : A11Y_ALERT_CAPS_OFF); |
| 164 | 192 |
| 165 if (tray_view()) | 193 if (tray_view()) |
| 166 tray_view()->SetVisible(caps_lock_enabled_); | 194 tray_view()->SetVisible(caps_lock_enabled_); |
| 167 | 195 |
| 168 if (default_) { | 196 if (default_) { |
| 169 default_->Update(caps_lock_enabled_); | 197 default_->Update(caps_lock_enabled_); |
| 170 } else { | 198 } else { |
| 199 message_center::MessageCenter* message_center = | |
| 200 message_center::MessageCenter::Get(); | |
| 171 if (caps_lock_enabled_) { | 201 if (caps_lock_enabled_) { |
| 172 if (!message_shown_) { | 202 if (!message_shown_) { |
| 173 ShellPort::Get()->RecordUserMetricsAction( | 203 ShellPort::Get()->RecordUserMetricsAction( |
| 174 UMA_STATUS_AREA_CAPS_LOCK_POPUP); | 204 UMA_STATUS_AREA_CAPS_LOCK_POPUP); |
| 175 ShowDetailedView(kTrayPopupAutoCloseDelayForTextInSeconds, false); | 205 |
| 206 message_center->AddNotification(CreateNotification()); | |
| 176 message_shown_ = true; | 207 message_shown_ = true; |
| 177 } | 208 } |
| 178 } else if (detailed_) { | 209 } else { |
|
tdanderson
2017/04/13 17:01:37
nit: can you just make this an 'else if' here?
tetsui2
2017/04/14 01:54:52
Done.
| |
| 179 detailed_->GetWidget()->Close(); | 210 if (message_center->FindVisibleNotificationById( |
| 211 kCapsLockNotificationId)) { | |
| 212 message_center->RemoveNotification(kCapsLockNotificationId, false); | |
| 213 } | |
| 180 } | 214 } |
| 181 } | 215 } |
| 182 } | 216 } |
| 183 | 217 |
| 184 bool TrayCapsLock::GetInitialVisibility() { | 218 bool TrayCapsLock::GetInitialVisibility() { |
| 185 return CapsLockIsEnabled(); | 219 return CapsLockIsEnabled(); |
| 186 } | 220 } |
| 187 | 221 |
| 188 views::View* TrayCapsLock::CreateDefaultView(LoginStatus status) { | 222 views::View* TrayCapsLock::CreateDefaultView(LoginStatus status) { |
| 189 if (!caps_lock_enabled_) | 223 if (!caps_lock_enabled_) |
| 190 return nullptr; | 224 return nullptr; |
| 191 DCHECK(!default_); | 225 DCHECK(!default_); |
| 192 default_ = new CapsLockDefaultView; | 226 default_ = new CapsLockDefaultView; |
| 193 default_->Update(caps_lock_enabled_); | 227 default_->Update(caps_lock_enabled_); |
| 194 return default_; | 228 return default_; |
| 195 } | 229 } |
| 196 | 230 |
| 197 views::View* TrayCapsLock::CreateDetailedView(LoginStatus status) { | |
| 198 DCHECK(!detailed_); | |
| 199 detailed_ = new views::View; | |
| 200 | |
| 201 detailed_->SetLayoutManager(new views::BoxLayout( | |
| 202 views::BoxLayout::kHorizontal, kTrayPopupPaddingHorizontal, 10, | |
| 203 kTrayPopupPaddingBetweenItems)); | |
| 204 | |
| 205 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 206 views::ImageView* image = new views::ImageView; | |
| 207 image->SetImage( | |
| 208 CreateVectorIcon(kSystemMenuCapsLockIcon, kMenuIconSize, kMenuIconColor)); | |
| 209 detailed_->AddChildView(image); | |
| 210 | |
| 211 const int string_id = | |
| 212 Shell::Get()->system_tray_delegate()->IsSearchKeyMappedToCapsLock() | |
| 213 ? IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_SEARCH | |
| 214 : IDS_ASH_STATUS_TRAY_CAPS_LOCK_CANCEL_BY_ALT_SEARCH; | |
| 215 views::Label* label = TrayPopupUtils::CreateDefaultLabel(); | |
| 216 label->SetText(bundle.GetLocalizedString(string_id)); | |
| 217 label->SetMultiLine(true); | |
| 218 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 219 detailed_->AddChildView(label); | |
| 220 ShellPort::Get()->RecordUserMetricsAction(UMA_STATUS_AREA_CAPS_LOCK_DETAILED); | |
| 221 | |
| 222 return detailed_; | |
| 223 } | |
| 224 | |
| 225 void TrayCapsLock::DestroyDefaultView() { | 231 void TrayCapsLock::DestroyDefaultView() { |
| 226 default_ = nullptr; | 232 default_ = nullptr; |
| 227 } | 233 } |
| 228 | 234 |
| 229 void TrayCapsLock::DestroyDetailedView() { | |
| 230 detailed_ = nullptr; | |
| 231 } | |
| 232 | |
| 233 } // namespace ash | 235 } // namespace ash |
| OLD | NEW |