| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/status/input_method_menu_button.h" | 5 #include "chrome/browser/chromeos/status/input_method_menu_button.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/chromeos/input_method/input_method_manager.h" | 10 #include "chrome/browser/chromeos/input_method/input_method_manager.h" |
| 11 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 11 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
| 12 #include "chrome/browser/chromeos/status/status_area_host.h" | |
| 13 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
| 17 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
| 18 #include "views/widget/widget.h" | 17 #include "views/widget/widget.h" |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 // Returns PrefService object associated with |host|. Returns NULL if we are NOT | 21 PrefService* GetPrefService() { |
| 23 // within a browser. | 22 Profile* profile = ProfileManager::GetDefaultProfile(); |
| 24 PrefService* GetPrefService(chromeos::StatusAreaHost* host) { | 23 if (profile) |
| 25 if (host->GetProfile()) { | 24 return profile->GetPrefs(); |
| 26 return host->GetProfile()->GetPrefs(); | |
| 27 } | |
| 28 return NULL; | 25 return NULL; |
| 29 } | 26 } |
| 30 | 27 |
| 31 // A class which implements interfaces of chromeos::InputMethodMenu. This class | 28 // A class which implements interfaces of chromeos::InputMethodMenu. This class |
| 32 // is just for avoiding multiple inheritance. | 29 // is just for avoiding multiple inheritance. |
| 33 class MenuImpl : public chromeos::InputMethodMenu { | 30 class MenuImpl : public chromeos::InputMethodMenu { |
| 34 public: | 31 public: |
| 35 MenuImpl(chromeos::InputMethodMenuButton* button, | 32 MenuImpl(chromeos::InputMethodMenuButton* button, |
| 36 PrefService* pref_service, | 33 PrefService* pref_service, |
| 37 chromeos::StatusAreaHost::ScreenMode screen_mode) | 34 chromeos::StatusAreaViewChromeos::ScreenMode screen_mode) |
| 38 : InputMethodMenu(pref_service, screen_mode, false), button_(button) {} | 35 : InputMethodMenu(pref_service, screen_mode, false), button_(button) {} |
| 39 | 36 |
| 40 private: | 37 private: |
| 41 // InputMethodMenu implementation. | 38 // InputMethodMenu implementation. |
| 42 virtual void UpdateUI(const std::string& input_method_id, | 39 virtual void UpdateUI(const std::string& input_method_id, |
| 43 const string16& name, | 40 const string16& name, |
| 44 const string16& tooltip, | 41 const string16& tooltip, |
| 45 size_t num_active_input_methods) { | 42 size_t num_active_input_methods) { |
| 46 button_->UpdateUI(input_method_id, name, tooltip, num_active_input_methods); | 43 button_->UpdateUI(input_method_id, name, tooltip, num_active_input_methods); |
| 47 } | 44 } |
| 48 virtual bool ShouldSupportConfigUI() { | 45 virtual bool ShouldSupportConfigUI() { |
| 49 return button_->ShouldSupportConfigUI(); | 46 return button_->ShouldSupportConfigUI(); |
| 50 } | 47 } |
| 51 virtual void OpenConfigUI() { | 48 virtual void OpenConfigUI() { |
| 52 button_->OpenConfigUI(); | 49 button_->OpenConfigUI(); |
| 53 } | 50 } |
| 54 // The UI (views button) to which this class delegates all requests. | 51 // The UI (views button) to which this class delegates all requests. |
| 55 chromeos::InputMethodMenuButton* button_; | 52 chromeos::InputMethodMenuButton* button_; |
| 56 | 53 |
| 57 DISALLOW_COPY_AND_ASSIGN(MenuImpl); | 54 DISALLOW_COPY_AND_ASSIGN(MenuImpl); |
| 58 }; | 55 }; |
| 59 | 56 |
| 60 } // namespace | 57 } // namespace |
| 61 | 58 |
| 62 namespace chromeos { | 59 namespace chromeos { |
| 63 | 60 |
| 64 //////////////////////////////////////////////////////////////////////////////// | 61 //////////////////////////////////////////////////////////////////////////////// |
| 65 // InputMethodMenuButton | 62 // InputMethodMenuButton |
| 66 | 63 |
| 67 InputMethodMenuButton::InputMethodMenuButton(StatusAreaHost* host) | 64 InputMethodMenuButton::InputMethodMenuButton( |
| 68 : StatusAreaButton(host, this), | 65 StatusAreaButton::Delegate* delegate, |
| 69 menu_(new MenuImpl(this, GetPrefService(host), host->GetScreenMode())) { | 66 StatusAreaViewChromeos::ScreenMode screen_mode) |
| 67 : StatusAreaButton(delegate, this), |
| 68 menu_(new MenuImpl(this, GetPrefService(), screen_mode)), |
| 69 screen_mode_(screen_mode) { |
| 70 set_id(VIEW_ID_STATUS_BUTTON_INPUT_METHOD); |
| 70 UpdateUIFromCurrentInputMethod(); | 71 UpdateUIFromCurrentInputMethod(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 InputMethodMenuButton::~InputMethodMenuButton() {} | 74 InputMethodMenuButton::~InputMethodMenuButton() {} |
| 74 | 75 |
| 75 //////////////////////////////////////////////////////////////////////////////// | 76 //////////////////////////////////////////////////////////////////////////////// |
| 76 // views::View implementation: | 77 // views::View implementation: |
| 77 | 78 |
| 78 void InputMethodMenuButton::OnLocaleChanged() { | 79 void InputMethodMenuButton::OnLocaleChanged() { |
| 79 input_method::InputMethodManager* manager = | 80 input_method::InputMethodManager* manager = |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 const string16& name, | 112 const string16& name, |
| 112 const string16& tooltip, | 113 const string16& tooltip, |
| 113 size_t num_active_input_methods) { | 114 size_t num_active_input_methods) { |
| 114 // Hide the button only if there is only one input method, and the input | 115 // Hide the button only if there is only one input method, and the input |
| 115 // method is a XKB keyboard layout. We don't hide the button for other | 116 // method is a XKB keyboard layout. We don't hide the button for other |
| 116 // types of input methods as these might have intra input method modes, | 117 // types of input methods as these might have intra input method modes, |
| 117 // like Hiragana and Katakana modes in Japanese input methods. | 118 // like Hiragana and Katakana modes in Japanese input methods. |
| 118 const bool hide_button = | 119 const bool hide_button = |
| 119 num_active_input_methods == 1 && | 120 num_active_input_methods == 1 && |
| 120 input_method::InputMethodUtil::IsKeyboardLayout(input_method_id) && | 121 input_method::InputMethodUtil::IsKeyboardLayout(input_method_id) && |
| 121 host_->GetScreenMode() == StatusAreaHost::kBrowserMode; | 122 screen_mode_ == StatusAreaViewChromeos::BROWSER_MODE; |
| 122 SetVisible(!hide_button); | 123 SetVisible(!hide_button); |
| 123 SetText(name); | 124 SetText(name); |
| 124 SetTooltipText(tooltip); | 125 SetTooltipText(tooltip); |
| 125 SetAccessibleName(tooltip); | 126 SetAccessibleName(tooltip); |
| 126 | 127 |
| 127 if (WindowIsActive()) { | 128 if (WindowIsActive()) { |
| 128 // We don't call these functions if the |current_window| is not active since | 129 // We don't call these functions if the |current_window| is not active since |
| 129 // the calls are relatively expensive (crosbug.com/9206). Please note that | 130 // the calls are relatively expensive (crosbug.com/9206). Please note that |
| 130 // PrepareMenuModel() is necessary for fixing crosbug.com/7522 when the | 131 // PrepareMenuModel() is necessary for fixing crosbug.com/7522 when the |
| 131 // window is active. | 132 // window is active. |
| 132 menu_->PrepareMenuModel(); | 133 menu_->PrepareMenuModel(); |
| 133 SchedulePaint(); | 134 SchedulePaint(); |
| 134 } | 135 } |
| 135 | 136 |
| 136 // TODO(yusukes): For a window which isn't on top, probably it's better to | 137 // TODO(yusukes): For a window which isn't on top, probably it's better to |
| 137 // update the texts when the window gets activated because SetTooltipText() | 138 // update the texts when the window gets activated because SetTooltipText() |
| 138 // and SetText() are also expensive. | 139 // and SetText() are also expensive. |
| 139 } | 140 } |
| 140 | 141 |
| 141 void InputMethodMenuButton::OpenConfigUI() { | 142 void InputMethodMenuButton::OpenConfigUI() { |
| 142 host_->OpenButtonOptions(this); // ask browser to open the WebUI page. | 143 // Ask browser to open the WebUI page. |
| 144 delegate()->ExecuteStatusAreaCommand( |
| 145 this, StatusAreaViewChromeos::SHOW_LANGUAGE_OPTIONS); |
| 143 } | 146 } |
| 144 | 147 |
| 145 bool InputMethodMenuButton::ShouldSupportConfigUI() { | 148 bool InputMethodMenuButton::ShouldSupportConfigUI() { |
| 146 return host_->ShouldOpenButtonOptions(this); | 149 return delegate()->ShouldExecuteStatusAreaCommand( |
| 150 this, StatusAreaViewChromeos::SHOW_LANGUAGE_OPTIONS); |
| 147 } | 151 } |
| 148 | 152 |
| 149 void InputMethodMenuButton::UpdateUIFromCurrentInputMethod() { | 153 void InputMethodMenuButton::UpdateUIFromCurrentInputMethod() { |
| 150 input_method::InputMethodManager* input_method_manager = | 154 input_method::InputMethodManager* input_method_manager = |
| 151 input_method::InputMethodManager::GetInstance(); | 155 input_method::InputMethodManager::GetInstance(); |
| 152 const input_method::InputMethodDescriptor& input_method = | 156 const input_method::InputMethodDescriptor& input_method = |
| 153 input_method_manager->current_input_method(); | 157 input_method_manager->current_input_method(); |
| 154 const string16 name = InputMethodMenu::GetTextForIndicator(input_method); | 158 const string16 name = InputMethodMenu::GetTextForIndicator(input_method); |
| 155 const string16 tooltip = InputMethodMenu::GetTextForMenu(input_method); | 159 const string16 tooltip = InputMethodMenu::GetTextForMenu(input_method); |
| 156 const size_t num_active_input_methods = | 160 const size_t num_active_input_methods = |
| 157 input_method_manager->GetNumActiveInputMethods(); | 161 input_method_manager->GetNumActiveInputMethods(); |
| 158 UpdateUI(input_method.id(), name, tooltip, num_active_input_methods); | 162 UpdateUI(input_method.id(), name, tooltip, num_active_input_methods); |
| 159 } | 163 } |
| 160 | 164 |
| 161 } // namespace chromeos | 165 } // namespace chromeos |
| OLD | NEW |