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/ime/tray_ime.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/shelf/shelf_widget.h"
11 #include "ash/shell.h"
12 #include "ash/system/system_notifier.h"
13 #include "ash/system/tray/hover_highlight_view.h"
14 #include "ash/system/tray/system_tray.h"
15 #include "ash/system/tray/system_tray_delegate.h"
16 #include "ash/system/tray/system_tray_notifier.h"
17 #include "ash/system/tray/tray_constants.h"
18 #include "ash/system/tray/tray_details_view.h"
19 #include "ash/system/tray/tray_item_more.h"
20 #include "ash/system/tray/tray_item_view.h"
21 #include "ash/system/tray/tray_utils.h"
22 #include "base/logging.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "grit/ash_resources.h"
25 #include "grit/ash_strings.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/font.h"
29 #include "ui/gfx/image/image.h"
30 #include "ui/message_center/message_center.h"
31 #include "ui/message_center/notification.h"
32 #include "ui/message_center/notification_delegate.h"
33 #include "ui/views/controls/label.h"
34 #include "ui/views/layout/box_layout.h"
35 #include "ui/views/widget/widget.h"
37 using message_center::Notification;
41 const char kIMENotificationId[] = "chrome://settings/ime";
49 class IMEDefaultView : public TrayItemMore {
51 explicit IMEDefaultView(SystemTrayItem* owner)
52 : TrayItemMore(owner, true) {
53 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
55 SetImage(bundle.GetImageNamed(
56 IDR_AURA_UBER_TRAY_IME).ToImageSkia());
59 Shell::GetInstance()->system_tray_delegate()->GetCurrentIME(&info);
63 virtual ~IMEDefaultView() {}
65 void UpdateLabel(const IMEInfo& info) {
67 SetAccessibleName(info.name);
71 DISALLOW_COPY_AND_ASSIGN(IMEDefaultView);
74 class IMEDetailedView : public TrayDetailsView,
75 public ViewClickListener {
77 IMEDetailedView(SystemTrayItem* owner, user::LoginStatus login)
78 : TrayDetailsView(owner),
80 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
82 delegate->GetAvailableIMEList(&list);
83 IMEPropertyInfoList property_list;
84 delegate->GetCurrentIMEProperties(&property_list);
85 Update(list, property_list);
88 virtual ~IMEDetailedView() {}
90 void Update(const IMEInfoList& list,
91 const IMEPropertyInfoList& property_list) {
95 if (!property_list.empty())
96 AppendIMEProperties(property_list);
97 if (login_ != user::LOGGED_IN_NONE && login_ != user::LOGGED_IN_LOCKED)
106 void AppendHeaderEntry() {
107 CreateSpecialRow(IDS_ASH_STATUS_TRAY_IME, this);
110 void AppendIMEList(const IMEInfoList& list) {
112 CreateScrollableList();
113 for (size_t i = 0; i < list.size(); i++) {
114 HoverHighlightView* container = new HoverHighlightView(this);
115 container->AddLabel(list[i].name,
116 list[i].selected ? gfx::Font::BOLD : gfx::Font::NORMAL);
117 scroll_content()->AddChildView(container);
118 ime_map_[container] = list[i].id;
122 void AppendIMEProperties(const IMEPropertyInfoList& property_list) {
123 property_map_.clear();
124 for (size_t i = 0; i < property_list.size(); i++) {
125 HoverHighlightView* container = new HoverHighlightView(this);
127 property_list[i].name,
128 property_list[i].selected ? gfx::Font::BOLD : gfx::Font::NORMAL);
130 container->set_border(views::Border::CreateSolidSidedBorder(1, 0, 0, 0,
132 scroll_content()->AddChildView(container);
133 property_map_[container] = property_list[i].key;
137 void AppendSettings() {
138 HoverHighlightView* container = new HoverHighlightView(this);
139 container->AddLabel(ui::ResourceBundle::GetSharedInstance().
140 GetLocalizedString(IDS_ASH_STATUS_TRAY_IME_SETTINGS),
142 AddChildView(container);
143 settings_ = container;
146 // Overridden from ViewClickListener.
147 virtual void OnViewClicked(views::View* sender) OVERRIDE {
148 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
149 if (sender == footer()->content()) {
150 owner()->system_tray()->ShowDefaultView(BUBBLE_USE_EXISTING);
151 } else if (sender == settings_) {
152 delegate->ShowIMESettings();
154 std::map<views::View*, std::string>::const_iterator ime_find;
155 ime_find = ime_map_.find(sender);
156 if (ime_find != ime_map_.end()) {
157 std::string ime_id = ime_find->second;
158 delegate->SwitchIME(ime_id);
159 GetWidget()->Close();
161 std::map<views::View*, std::string>::const_iterator prop_find;
162 prop_find = property_map_.find(sender);
163 if (prop_find != property_map_.end()) {
164 const std::string key = prop_find->second;
165 delegate->ActivateIMEProperty(key);
166 GetWidget()->Close();
172 user::LoginStatus login_;
174 std::map<views::View*, std::string> ime_map_;
175 std::map<views::View*, std::string> property_map_;
176 views::View* settings_;
178 DISALLOW_COPY_AND_ASSIGN(IMEDetailedView);
183 TrayIME::TrayIME(SystemTray* system_tray)
184 : SystemTrayItem(system_tray),
188 message_shown_(false) {
189 Shell::GetInstance()->system_tray_notifier()->AddIMEObserver(this);
192 TrayIME::~TrayIME() {
193 Shell::GetInstance()->system_tray_notifier()->RemoveIMEObserver(this);
194 message_center::MessageCenter::Get()->RemoveNotification(
195 kIMENotificationId, false /* by_user */);
198 void TrayIME::UpdateTrayLabel(const IMEInfo& current, size_t count) {
200 if (current.third_party) {
201 tray_label_->label()->SetText(current.short_name + UTF8ToUTF16("*"));
203 tray_label_->label()->SetText(current.short_name);
205 tray_label_->SetVisible(count > 1);
206 SetTrayLabelItemBorder(tray_label_, system_tray()->shelf_alignment());
207 tray_label_->Layout();
211 void TrayIME::UpdateOrCreateNotification() {
212 message_center::MessageCenter* message_center =
213 message_center::MessageCenter::Get();
215 if (!message_center->HasNotification(kIMENotificationId) && message_shown_)
218 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
220 delegate->GetCurrentIME(¤t);
222 ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
223 scoped_ptr<Notification> notification(new Notification(
224 message_center::NOTIFICATION_TYPE_SIMPLE,
226 // TODO(zork): Use IDS_ASH_STATUS_TRAY_THIRD_PARTY_IME_TURNED_ON_BUBBLE
227 // for third party IMEs
228 l10n_util::GetStringFUTF16(
229 IDS_ASH_STATUS_TRAY_IME_TURNED_ON_BUBBLE,
230 current.medium_name),
231 base::string16(), // message
232 bundle.GetImageNamed(IDR_AURA_UBER_TRAY_IME),
233 base::string16(), // display_source
234 message_center::NotifierId(system_notifier::NOTIFIER_INPUT_METHOD),
235 message_center::RichNotificationData(),
236 new message_center::HandleNotificationClickedDelegate(
237 base::Bind(&TrayIME::PopupDetailedView,
238 base::Unretained(this), 0, true))));
239 message_center->AddNotification(notification.Pass());
240 message_shown_ = true;
243 views::View* TrayIME::CreateTrayView(user::LoginStatus status) {
244 CHECK(tray_label_ == NULL);
245 tray_label_ = new TrayItemView(this);
246 tray_label_->CreateLabel();
247 SetupLabelForTray(tray_label_->label());
248 // Hide IME tray when it is created, it will be updated when it is notified
249 // for IME refresh event.
250 tray_label_->SetVisible(false);
254 views::View* TrayIME::CreateDefaultView(user::LoginStatus status) {
255 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
257 IMEPropertyInfoList property_list;
258 delegate->GetAvailableIMEList(&list);
259 delegate->GetCurrentIMEProperties(&property_list);
260 if (list.size() <= 1 && property_list.size() <= 1)
262 CHECK(default_ == NULL);
263 default_ = new tray::IMEDefaultView(this);
267 views::View* TrayIME::CreateDetailedView(user::LoginStatus status) {
268 CHECK(detailed_ == NULL);
269 detailed_ = new tray::IMEDetailedView(this, status);
273 void TrayIME::DestroyTrayView() {
277 void TrayIME::DestroyDefaultView() {
281 void TrayIME::DestroyDetailedView() {
285 void TrayIME::UpdateAfterLoginStatusChange(user::LoginStatus status) {
288 void TrayIME::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
289 SetTrayLabelItemBorder(tray_label_, alignment);
290 tray_label_->Layout();
293 void TrayIME::OnIMERefresh(bool show_message) {
294 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
297 IMEPropertyInfoList property_list;
298 delegate->GetCurrentIME(¤t);
299 delegate->GetAvailableIMEList(&list);
300 delegate->GetCurrentIMEProperties(&property_list);
302 UpdateTrayLabel(current, list.size());
305 default_->UpdateLabel(current);
307 detailed_->Update(list, property_list);
309 if (list.size() > 1 && show_message)
310 UpdateOrCreateNotification();
313 } // namespace internal