Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / mode_indicator_controller.cc
blob2540032115a4054500561782616e501af28c4903
1 // Copyright 2013 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 "chrome/browser/chromeos/input_method/mode_indicator_controller.h"
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "ash/wm/window_util.h"
10 #include "base/logging.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/chromeos/input_method/input_method_util.h"
13 #include "ui/chromeos/ime/mode_indicator_view.h"
15 namespace chromeos {
16 namespace input_method {
18 namespace {
19 ModeIndicatorObserverInterface* g_mode_indicator_observer_for_testing_ = NULL;
20 } // namespace
22 class ModeIndicatorObserver : public ModeIndicatorObserverInterface {
23 public:
24 ModeIndicatorObserver()
25 : active_widget_(NULL) {}
27 ~ModeIndicatorObserver() override {
28 if (active_widget_)
29 active_widget_->RemoveObserver(this);
32 // If other active mode indicator widget is shown, close it immedicately
33 // without fading animation. Then store this widget as the active widget.
34 void AddModeIndicatorWidget(views::Widget* widget) override {
35 DCHECK(widget);
36 if (active_widget_)
37 active_widget_->Close();
38 active_widget_ = widget;
39 widget->AddObserver(this);
42 // views::WidgetObserver override:
43 void OnWidgetDestroying(views::Widget* widget) override {
44 if (widget == active_widget_)
45 active_widget_ = NULL;
48 private:
49 views::Widget* active_widget_;
53 ModeIndicatorController::ModeIndicatorController(InputMethodManager* imm)
54 : imm_(imm),
55 is_focused_(false),
56 mi_observer_(new ModeIndicatorObserver) {
57 DCHECK(imm_);
58 imm_->AddObserver(this);
61 ModeIndicatorController::~ModeIndicatorController() {
62 imm_->RemoveObserver(this);
65 void ModeIndicatorController::SetCursorBounds(
66 const gfx::Rect& cursor_bounds) {
67 cursor_bounds_ = cursor_bounds;
70 void ModeIndicatorController::FocusStateChanged(bool is_focused) {
71 is_focused_ = is_focused;
74 // static
75 void ModeIndicatorController::SetModeIndicatorObserverForTesting(
76 ModeIndicatorObserverInterface* observer) {
77 g_mode_indicator_observer_for_testing_ = observer;
80 // static
81 ModeIndicatorObserverInterface*
82 ModeIndicatorController::GetModeIndicatorObserverForTesting() {
83 return g_mode_indicator_observer_for_testing_;
86 void ModeIndicatorController::InputMethodChanged(InputMethodManager* manager,
87 Profile* /* profile */,
88 bool show_message) {
89 if (!show_message)
90 return;
91 ShowModeIndicator();
94 void ModeIndicatorController::ShowModeIndicator() {
95 // TODO(komatsu): Show the mode indicator in the right bottom of the
96 // display when the launch bar is hidden and the focus is out. To
97 // implement it, we should consider to use message center or system
98 // notification. Note, launch bar can be vertical and can be placed
99 // right/left side of display.
100 if (!is_focused_)
101 return;
103 // Get the short name of the changed input method (e.g. US, JA, etc.)
104 const InputMethodDescriptor descriptor =
105 imm_->GetActiveIMEState()->GetCurrentInputMethod();
106 const base::string16 short_name =
107 imm_->GetInputMethodUtil()->GetInputMethodShortName(descriptor);
109 aura::Window* parent =
110 ash::Shell::GetContainer(ash::wm::GetActiveWindow()->GetRootWindow(),
111 ash::kShellWindowId_SettingBubbleContainer);
112 ui::ime::ModeIndicatorView* mi_view = new ui::ime::ModeIndicatorView(
113 parent, cursor_bounds_, short_name);
114 views::BubbleDelegateView::CreateBubble(mi_view);
116 views::Widget* mi_widget = mi_view->GetWidget();
117 if (GetModeIndicatorObserverForTesting())
118 GetModeIndicatorObserverForTesting()->AddModeIndicatorWidget(mi_widget);
120 mi_observer_->AddModeIndicatorWidget(mi_widget);
121 mi_view->ShowAndFadeOut();
124 } // namespace input_method
125 } // namespace chromeos