- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / input_method / mode_indicator_controller.cc
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.
4
5 #include "base/command_line.h"
6 #include "base/logging.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chromeos/input_method/input_method_util.h"
9 #include "chrome/browser/chromeos/input_method/mode_indicator_controller.h"
10 #include "chrome/browser/chromeos/input_method/mode_indicator_widget.h"
11 #include "chromeos/chromeos_switches.h"
12
13 namespace chromeos {
14 namespace input_method {
15
16 namespace {
17 // Show mode indicator with the current ime's short name.
18 void ShowModeIndicator(InputMethodManager* manager,
19                        ModeIndicatorWidget* mi_widget) {
20   // Need the flag, --enable-ime-mode-indicator at this moment.
21   // TODO(komatsu): When this is enabled by defalut, delete command_line.h
22   // and chromeos_switches.h from the header files.
23   if (!CommandLine::ForCurrentProcess()->HasSwitch(
24           switches::kEnableIMEModeIndicator))
25     return;
26
27   DCHECK(manager);
28   DCHECK(mi_widget);
29
30   // Get the short name of the changed input method (e.g. US, JA, etc.)
31   const InputMethodDescriptor descriptor = manager->GetCurrentInputMethod();
32   const std::string short_name = UTF16ToUTF8(
33       manager->GetInputMethodUtil()->GetInputMethodShortName(descriptor));
34   mi_widget->SetLabelTextUtf8(short_name);
35
36   // Show the widget and hide it after 750msec.
37   mi_widget->Show();
38   const int kDelayMSec = 750;
39   mi_widget->DelayHide(kDelayMSec);
40 }
41 }  // namespace
42
43 ModeIndicatorController::ModeIndicatorController(
44     ModeIndicatorWidget* mi_widget) {
45   mi_widget_.reset(mi_widget);
46
47   InputMethodManager* imm = InputMethodManager::Get();
48   DCHECK(imm);
49   imm->AddObserver(this);
50 }
51
52 ModeIndicatorController::~ModeIndicatorController() {
53   InputMethodManager* imm = InputMethodManager::Get();
54   DCHECK(imm);
55   imm->RemoveObserver(this);
56 }
57
58 void ModeIndicatorController::SetCursorLocation(
59     const gfx::Rect& cursor_location) {
60   mi_widget_->SetCursorLocation(cursor_location);
61 }
62
63 void ModeIndicatorController::InputMethodChanged(InputMethodManager* manager,
64                                                  bool show_message) {
65   if (!show_message)
66     return;
67
68   ShowModeIndicator(manager, mi_widget_.get());
69 }
70
71 void ModeIndicatorController::InputMethodPropertyChanged(
72     InputMethodManager* manager) {
73   // Do nothing.
74 }
75
76 }  // namespace input_method
77 }  // namespace chromeos