| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/status/input_method_menu_button.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/chromeos/input_method/input_method_manager.h" | |
| 11 #include "chrome/browser/chromeos/input_method/input_method_util.h" | |
| 12 #include "chrome/browser/chromeos/status/status_area_view_chromeos.h" | |
| 13 #include "chrome/browser/ui/browser.h" | |
| 14 #include "chrome/browser/ui/browser_list.h" | |
| 15 #include "chrome/browser/ui/browser_window.h" | |
| 16 #include "ui/views/widget/widget.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // A class which implements interfaces of chromeos::InputMethodMenu. This class | |
| 21 // is just for avoiding multiple inheritance. | |
| 22 class MenuImpl : public chromeos::InputMethodMenu { | |
| 23 public: | |
| 24 explicit MenuImpl(chromeos::InputMethodMenuButton* button) | |
| 25 : button_(button) {} | |
| 26 | |
| 27 private: | |
| 28 // InputMethodMenu implementation. | |
| 29 virtual void UpdateUI(const std::string& input_method_id, | |
| 30 const string16& name, | |
| 31 const string16& tooltip, | |
| 32 size_t num_active_input_methods) { | |
| 33 button_->UpdateUI(input_method_id, name, tooltip, num_active_input_methods); | |
| 34 } | |
| 35 virtual bool ShouldSupportConfigUI() { | |
| 36 return button_->ShouldSupportConfigUI(); | |
| 37 } | |
| 38 virtual void OpenConfigUI() { | |
| 39 button_->OpenConfigUI(); | |
| 40 } | |
| 41 // The UI (views button) to which this class delegates all requests. | |
| 42 chromeos::InputMethodMenuButton* button_; | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(MenuImpl); | |
| 45 }; | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 namespace chromeos { | |
| 50 | |
| 51 //////////////////////////////////////////////////////////////////////////////// | |
| 52 // InputMethodMenuButton | |
| 53 | |
| 54 InputMethodMenuButton::InputMethodMenuButton( | |
| 55 StatusAreaButton::Delegate* delegate) | |
| 56 : StatusAreaButton(delegate, this), | |
| 57 menu_(new MenuImpl(this)) { | |
| 58 set_id(VIEW_ID_STATUS_BUTTON_INPUT_METHOD); | |
| 59 UpdateUIFromCurrentInputMethod(); | |
| 60 } | |
| 61 | |
| 62 InputMethodMenuButton::~InputMethodMenuButton() {} | |
| 63 | |
| 64 //////////////////////////////////////////////////////////////////////////////// | |
| 65 // views::View implementation: | |
| 66 | |
| 67 void InputMethodMenuButton::OnLocaleChanged() { | |
| 68 input_method::InputMethodManager* manager = | |
| 69 input_method::InputMethodManager::GetInstance(); | |
| 70 manager->GetInputMethodUtil()->OnLocaleChanged(); | |
| 71 UpdateUIFromCurrentInputMethod(); | |
| 72 Layout(); | |
| 73 SchedulePaint(); | |
| 74 } | |
| 75 | |
| 76 //////////////////////////////////////////////////////////////////////////////// | |
| 77 // views::MenuButtonListener implementation: | |
| 78 | |
| 79 void InputMethodMenuButton::OnMenuButtonClicked(views::View* source, | |
| 80 const gfx::Point& point) { | |
| 81 menu_->OnMenuButtonClicked(source, point); | |
| 82 } | |
| 83 | |
| 84 bool InputMethodMenuButton::StatusAreaIsVisible() { | |
| 85 #if defined(USE_ASH) | |
| 86 return true; | |
| 87 #else | |
| 88 Browser* active_browser = BrowserList::GetLastActive(); | |
| 89 if (!active_browser) { | |
| 90 // Can't get an active browser. Just return true, which is safer. | |
| 91 return true; | |
| 92 } | |
| 93 BrowserWindow* active_window = active_browser->window(); | |
| 94 const views::Widget* current_window = GetWidget(); | |
| 95 if (!active_window || !current_window) { | |
| 96 // Can't get an active or current window. Just return true as well. | |
| 97 return true; | |
| 98 } | |
| 99 return active_window->GetNativeHandle() == current_window->GetNativeWindow(); | |
| 100 #endif | |
| 101 } | |
| 102 | |
| 103 void InputMethodMenuButton::UpdateUI(const std::string& input_method_id, | |
| 104 const string16& name, | |
| 105 const string16& tooltip, | |
| 106 size_t num_active_input_methods) { | |
| 107 // Hide the button only if there is only one input method, and the input | |
| 108 // method is a XKB keyboard layout. We don't hide the button for other | |
| 109 // types of input methods as these might have intra input method modes, | |
| 110 // like Hiragana and Katakana modes in Japanese input methods. | |
| 111 const bool hide_button = | |
| 112 num_active_input_methods == 1 && | |
| 113 input_method::InputMethodUtil::IsKeyboardLayout(input_method_id) && | |
| 114 StatusAreaViewChromeos::IsBrowserMode(); | |
| 115 SetVisible(!hide_button); | |
| 116 SetText(name); | |
| 117 SetTooltipText(tooltip); | |
| 118 SetAccessibleName(tooltip); | |
| 119 | |
| 120 if (StatusAreaIsVisible()) { | |
| 121 // We don't call these functions if the |current_window| is not active since | |
| 122 // the calls are relatively expensive (crosbug.com/9206). Please note that | |
| 123 // PrepareMenuModel() is necessary for fixing crosbug.com/7522 when the | |
| 124 // window is active. | |
| 125 menu_->PrepareMenuModel(); | |
| 126 SchedulePaint(); | |
| 127 } | |
| 128 | |
| 129 // TODO(yusukes): For a window which isn't on top, probably it's better to | |
| 130 // update the texts when the window gets activated because SetTooltipText() | |
| 131 // and SetText() are also expensive. | |
| 132 } | |
| 133 | |
| 134 void InputMethodMenuButton::OpenConfigUI() { | |
| 135 // Ask browser to open the WebUI page. | |
| 136 delegate()->ExecuteStatusAreaCommand( | |
| 137 this, StatusAreaButton::Delegate::SHOW_LANGUAGE_OPTIONS); | |
| 138 } | |
| 139 | |
| 140 bool InputMethodMenuButton::ShouldSupportConfigUI() { | |
| 141 return delegate()->ShouldExecuteStatusAreaCommand( | |
| 142 this, StatusAreaButton::Delegate::SHOW_LANGUAGE_OPTIONS); | |
| 143 } | |
| 144 | |
| 145 void InputMethodMenuButton::UpdateUIFromCurrentInputMethod() { | |
| 146 input_method::InputMethodManager* input_method_manager = | |
| 147 input_method::InputMethodManager::GetInstance(); | |
| 148 const input_method::InputMethodDescriptor& input_method = | |
| 149 input_method_manager->GetCurrentInputMethod(); | |
| 150 const string16 name = input_method_manager->GetInputMethodUtil()-> | |
| 151 GetInputMethodShortName(input_method); | |
| 152 const string16 tooltip = InputMethodMenu::GetTextForMenu(input_method); | |
| 153 const size_t num_active_input_methods = | |
| 154 input_method_manager->GetNumActiveInputMethods(); | |
| 155 UpdateUI(input_method.id(), name, tooltip, num_active_input_methods); | |
| 156 } | |
| 157 | |
| 158 } // namespace chromeos | |
| OLD | NEW |