Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / mode_indicator_controller.cc
blobcd8241e969c785e2c7a6500cc73a0a48ec616ca2
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"
8 #include "ash/shell.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"
17 namespace chromeos {
18 namespace input_method {
20 namespace {
21 ModeIndicatorObserverInterface* g_mode_indicator_observer_for_testing_ = NULL;
22 } // namespace
24 class ModeIndicatorObserver : public ModeIndicatorObserverInterface {
25 public:
26 ModeIndicatorObserver()
27 : active_widget_(NULL) {}
29 virtual ~ModeIndicatorObserver() {
30 if (active_widget_)
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 {
37 DCHECK(widget);
38 if (active_widget_)
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;
50 private:
51 views::Widget* active_widget_;
55 ModeIndicatorController::ModeIndicatorController(InputMethodManager* imm)
56 : imm_(imm),
57 is_focused_(false),
58 mi_observer_(new ModeIndicatorObserver) {
59 DCHECK(imm_);
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;
76 // static
77 void ModeIndicatorController::SetModeIndicatorObserverForTesting(
78 ModeIndicatorObserverInterface* observer) {
79 g_mode_indicator_observer_for_testing_ = observer;
82 // static
83 ModeIndicatorObserverInterface*
84 ModeIndicatorController::GetModeIndicatorObserverForTesting() {
85 return g_mode_indicator_observer_for_testing_;
88 void ModeIndicatorController::InputMethodChanged(InputMethodManager* manager,
89 bool show_message) {
90 if (!show_message)
91 return;
92 ShowModeIndicator();
95 void ModeIndicatorController::InputMethodPropertyChanged(
96 InputMethodManager* manager) {
97 // Do nothing.
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
103 // files.
104 if (CommandLine::ForCurrentProcess()->HasSwitch(
105 switches::kDisableIMEModeIndicator))
106 return;
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.
113 if (!is_focused_)
114 return;
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