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/ime/mode_indicator_view.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/wm/window_util.h"
11 #include "base/command_line.h"
12 #include "base/logging.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/chromeos/input_method/input_method_util.h"
15 #include "chromeos/chromeos_switches.h"
18 namespace input_method
{
21 ModeIndicatorObserverInterface
* g_mode_indicator_observer_for_testing_
= NULL
;
24 class ModeIndicatorObserver
: public ModeIndicatorObserverInterface
{
26 ModeIndicatorObserver()
27 : active_widget_(NULL
) {}
29 virtual ~ModeIndicatorObserver() {
31 active_widget_
->RemoveObserver(this);
34 // If other active mode indicator widget is shown, close it immedicately
35 // without fading animation. Then store this widget as the active widget.
36 virtual void AddModeIndicatorWidget(views::Widget
* widget
) OVERRIDE
{
39 active_widget_
->Close();
40 active_widget_
= widget
;
41 widget
->AddObserver(this);
44 // views::WidgetObserver override:
45 virtual void OnWidgetDestroying(views::Widget
* widget
) OVERRIDE
{
46 if (widget
== active_widget_
)
47 active_widget_
= NULL
;
51 views::Widget
* active_widget_
;
55 ModeIndicatorController::ModeIndicatorController(InputMethodManager
* imm
)
58 mi_observer_(new ModeIndicatorObserver
) {
60 imm_
->AddObserver(this);
63 ModeIndicatorController::~ModeIndicatorController() {
64 imm_
->RemoveObserver(this);
67 void ModeIndicatorController::SetCursorBounds(
68 const gfx::Rect
& cursor_bounds
) {
69 cursor_bounds_
= cursor_bounds
;
72 void ModeIndicatorController::FocusStateChanged(bool is_focused
) {
73 is_focused_
= is_focused
;
77 void ModeIndicatorController::SetModeIndicatorObserverForTesting(
78 ModeIndicatorObserverInterface
* observer
) {
79 g_mode_indicator_observer_for_testing_
= observer
;
83 ModeIndicatorObserverInterface
*
84 ModeIndicatorController::GetModeIndicatorObserverForTesting() {
85 return g_mode_indicator_observer_for_testing_
;
88 void ModeIndicatorController::InputMethodChanged(InputMethodManager
* manager
,
95 void ModeIndicatorController::InputMethodPropertyChanged(
96 InputMethodManager
* manager
) {
100 void ModeIndicatorController::ShowModeIndicator() {
101 // TODO(komatsu): When this is permanently enabled by defalut,
102 // delete command_line.h and chromeos_switches.h from the header
104 if (CommandLine::ForCurrentProcess()->HasSwitch(
105 switches::kDisableIMEModeIndicator
))
108 // TODO(komatsu): Show the mode indicator in the right bottom of the
109 // display when the launch bar is hidden and the focus is out. To
110 // implement it, we should consider to use message center or system
111 // notification. Note, launch bar can be vertical and can be placed
112 // right/left side of display.
116 // Get the short name of the changed input method (e.g. US, JA, etc.)
117 const InputMethodDescriptor descriptor
= imm_
->GetCurrentInputMethod();
118 const base::string16 short_name
=
119 imm_
->GetInputMethodUtil()->GetInputMethodShortName(descriptor
);
121 aura::Window
* parent
= ash::Shell::GetContainer(
122 ash::wm::GetActiveWindow()->GetRootWindow(),
123 ash::internal::kShellWindowId_InputMethodContainer
);
124 ash::ime::ModeIndicatorView
* mi_view
= new ash::ime::ModeIndicatorView(
125 parent
, cursor_bounds_
, short_name
);
126 views::BubbleDelegateView::CreateBubble(mi_view
);
128 views::Widget
* mi_widget
= mi_view
->GetWidget();
129 if (GetModeIndicatorObserverForTesting())
130 GetModeIndicatorObserverForTesting()->AddModeIndicatorWidget(mi_widget
);
132 mi_observer_
->AddModeIndicatorWidget(mi_widget
);
133 mi_view
->ShowAndFadeOut();
136 } // namespace input_method
137 } // namespace chromeos