OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "chrome/browser/chromeos/input_method/mode_indicator_controller.h" |
| 6 |
5 #include "base/command_line.h" | 7 #include "base/command_line.h" |
6 #include "base/logging.h" | 8 #include "base/logging.h" |
7 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 10 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
9 #include "chrome/browser/chromeos/input_method/mode_indicator_controller.h" | 11 #include "chrome/browser/chromeos/input_method/mode_indicator_delegate_view.h" |
10 #include "chrome/browser/chromeos/input_method/mode_indicator_widget.h" | |
11 #include "chromeos/chromeos_switches.h" | 12 #include "chromeos/chromeos_switches.h" |
12 | 13 |
13 namespace chromeos { | 14 namespace chromeos { |
14 namespace input_method { | 15 namespace input_method { |
15 | 16 |
16 ModeIndicatorController::ModeIndicatorController( | 17 class ModeIndicatorObserver : public views::WidgetObserver { |
17 ModeIndicatorWidget* mi_widget) | 18 public: |
18 : is_focused_(false) { | 19 ModeIndicatorObserver() |
19 mi_widget_.reset(mi_widget); | 20 : active_widget_(NULL) {} |
20 | 21 |
21 InputMethodManager* imm = InputMethodManager::Get(); | 22 virtual ~ModeIndicatorObserver() {} |
22 DCHECK(imm); | 23 |
23 imm->AddObserver(this); | 24 // If other active mode indicator widget is shown, close it immedicately |
| 25 // without fading animation. Then store this widget as the active widget. |
| 26 void UpdateActiveModeIndicator(views::Widget* widget) { |
| 27 if (active_widget_) |
| 28 active_widget_->Close(); |
| 29 active_widget_ = widget; |
| 30 widget->AddObserver(this); |
| 31 } |
| 32 |
| 33 // views::WidgetObserver override: |
| 34 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE { |
| 35 if (widget == active_widget_) |
| 36 active_widget_ = NULL; |
| 37 } |
| 38 |
| 39 private: |
| 40 views::Widget* active_widget_; |
| 41 }; |
| 42 |
| 43 |
| 44 ModeIndicatorController::ModeIndicatorController(InputMethodManager* imm) |
| 45 : imm_(imm), |
| 46 is_focused_(false), |
| 47 mi_observer_(new ModeIndicatorObserver) { |
| 48 DCHECK(imm_); |
| 49 imm_->AddObserver(this); |
24 } | 50 } |
25 | 51 |
26 ModeIndicatorController::~ModeIndicatorController() { | 52 ModeIndicatorController::~ModeIndicatorController() { |
27 InputMethodManager* imm = InputMethodManager::Get(); | 53 imm_->RemoveObserver(this); |
28 DCHECK(imm); | |
29 imm->RemoveObserver(this); | |
30 } | 54 } |
31 | 55 |
32 void ModeIndicatorController::SetCursorBounds( | 56 void ModeIndicatorController::SetCursorBounds( |
33 const gfx::Rect& cursor_bounds) { | 57 const gfx::Rect& cursor_bounds) { |
34 mi_widget_->SetCursorBounds(cursor_bounds); | 58 cursor_bounds_ = cursor_bounds; |
35 } | 59 } |
36 | 60 |
37 void ModeIndicatorController::FocusStateChanged(bool is_focused) { | 61 void ModeIndicatorController::FocusStateChanged(bool is_focused) { |
38 is_focused_ = is_focused; | 62 is_focused_ = is_focused; |
39 } | 63 } |
40 | 64 |
41 void ModeIndicatorController::InputMethodChanged(InputMethodManager* manager, | 65 void ModeIndicatorController::InputMethodChanged(InputMethodManager* manager, |
42 bool show_message) { | 66 bool show_message) { |
43 if (!show_message) | 67 if (!show_message) |
44 return; | 68 return; |
45 | 69 |
46 ShowModeIndicator(manager); | 70 ShowModeIndicator(); |
47 } | 71 } |
48 | 72 |
49 void ModeIndicatorController::InputMethodPropertyChanged( | 73 void ModeIndicatorController::InputMethodPropertyChanged( |
50 InputMethodManager* manager) { | 74 InputMethodManager* manager) { |
51 // Do nothing. | 75 // Do nothing. |
52 } | 76 } |
53 | 77 |
54 void ModeIndicatorController::ShowModeIndicator(InputMethodManager* manager) { | 78 void ModeIndicatorController::ShowModeIndicator() { |
55 // TODO(komatsu): When this is permanently enabled by defalut, | 79 // TODO(komatsu): When this is permanently enabled by defalut, |
56 // delete command_line.h and chromeos_switches.h from the header | 80 // delete command_line.h and chromeos_switches.h from the header |
57 // files. | 81 // files. |
58 if (CommandLine::ForCurrentProcess()->HasSwitch( | 82 if (CommandLine::ForCurrentProcess()->HasSwitch( |
59 switches::kDisableIMEModeIndicator)) | 83 switches::kDisableIMEModeIndicator)) |
60 return; | 84 return; |
61 | 85 |
62 // TODO(komatsu): Show the mode indicator in the right bottom of the | 86 // TODO(komatsu): Show the mode indicator in the right bottom of the |
63 // display when the launch bar is hidden and the focus is out. To | 87 // display when the launch bar is hidden and the focus is out. To |
64 // implement it, we should consider to use message center or system | 88 // implement it, we should consider to use message center or system |
65 // notification. Note, launch bar can be vertical and can be placed | 89 // notification. Note, launch bar can be vertical and can be placed |
66 // right/left side of display. | 90 // right/left side of display. |
67 if (!is_focused_) | 91 if (!is_focused_) |
68 return; | 92 return; |
69 | 93 |
70 DCHECK(manager); | 94 // Get the short name of the changed input method (e.g. US, JA, etc.) |
71 DCHECK(mi_widget_.get()); | 95 const InputMethodDescriptor descriptor = imm_->GetCurrentInputMethod(); |
| 96 const string16 short_name = |
| 97 imm_->GetInputMethodUtil()->GetInputMethodShortName(descriptor); |
72 | 98 |
73 // Get the short name of the changed input method (e.g. US, JA, etc.) | 99 ModeIndicatorDelegateView* mi_delegate_view = |
74 const InputMethodDescriptor descriptor = manager->GetCurrentInputMethod(); | 100 new ModeIndicatorDelegateView(cursor_bounds_, short_name); |
75 const std::string short_name = UTF16ToUTF8( | 101 views::BubbleDelegateView::CreateBubble(mi_delegate_view); |
76 manager->GetInputMethodUtil()->GetInputMethodShortName(descriptor)); | 102 mi_observer_->UpdateActiveModeIndicator(mi_delegate_view->GetWidget()); |
77 mi_widget_->SetLabelTextUtf8(short_name); | 103 mi_delegate_view->ShowAndFadeOut(); |
78 | |
79 // Show the widget and hide it after 750msec. | |
80 mi_widget_->Show(); | |
81 const int kDelayMSec = 750; | |
82 mi_widget_->DelayHide(kDelayMSec); | |
83 } | 104 } |
84 | 105 |
85 } // namespace input_method | 106 } // namespace input_method |
86 } // namespace chromeos | 107 } // namespace chromeos |
OLD | NEW |