Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Side by Side Diff: chrome/browser/ui/touch/keyboard/keyboard_manager.cc

Issue 7302015: A keyboard widget that manages itself (the animation and all that). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/touch/keyboard/keyboard_manager.h"
6
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/profiles/profile_manager.h"
9 #include "chrome/browser/tabs/tab_strip_model.h"
10 #include "chrome/browser/ui/browser_list.h"
11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
12 #include "chrome/browser/ui/views/dom_view.h"
13 #include "chrome/browser/ui/views/tab_contents/tab_contents_view_touch.h"
14 #include "chrome/common/extensions/extension_messages.h"
15 #include "chrome/common/url_constants.h"
16 #include "content/browser/site_instance.h"
17 #include "content/browser/tab_contents/tab_contents.h"
18 #include "content/common/notification_service.h"
19 #include "ui/base/animation/slide_animation.h"
20 #include "ui/base/ime/text_input_type.h"
21 #include "ui/gfx/interpolated_transform.h"
22 #include "views/ime/text_input_client.h"
23 #include "views/widget/widget.h"
24
25 #if defined(OS_CHROMEOS)
26 #include "chrome/browser/chromeos/input_method/virtual_keyboard_selector.h"
27 #endif
28
29 namespace {
30
31 const int kDefaultKeyboardHeight = 300;
32 const int kKeyboardSlideDuration = 300; // In milliseconds
33
34 PropertyAccessor<bool>* GetFocusedStateAccessor() {
35 static PropertyAccessor<bool> state;
36 return &state;
37 }
38
39 bool TabContentsCanAffectKeyboard(const TabContents* tab_contents) {
40 // There may not be a browser, e.g. for the login window. But if there is
41 // a browser, then |tab_contents| should be the active tab.
42 Browser* browser = Browser::GetBrowserForController(
43 &tab_contents->controller(), NULL);
44 return browser == NULL ||
45 (browser == BrowserList::GetLastActive() &&
46 browser->GetSelectedTabContents() == tab_contents);
47 }
48
49 } // namespace
50
51 // TODO(sad): Is the default profile always going to be the one we want?
52
53 KeyboardManager::KeyboardManager()
54 : views::Widget::Widget(),
55 dom_view_(new DOMView),
56 ALLOW_THIS_IN_INITIALIZER_LIST(
57 extension_dispatcher_(ProfileManager::GetDefaultProfile(), this)),
58 target_(NULL),
59 keyboard_height_(kDefaultKeyboardHeight) {
60
61 // Initialize the widget first.
62 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
63 params.transparent = true;
64 Init(params);
65
66 // Setup the DOM view to host the keyboard.
67 Profile* profile = ProfileManager::GetDefaultProfile();
68 GURL keyboard_url(chrome::kChromeUIKeyboardURL);
69 dom_view_->Init(profile,
70 SiteInstance::CreateSiteInstanceForURL(profile, keyboard_url));
71 dom_view_->LoadURL(keyboard_url);
72 dom_view_->SetVisible(true);
73 SetContentsView(dom_view_);
74
75 // Setup observer so the events from the keyboard can be handled.
76 TabContentsObserver::Observe(dom_view_->tab_contents());
77
78 // Initialize the animation.
79 animation_.reset(new ui::SlideAnimation(this));
80 animation_->SetTweenType(ui::Tween::LINEAR);
81 animation_->SetSlideDuration(kKeyboardSlideDuration);
82
83 // Start listening to notifications to maintain the keyboard visibility, size
84 // etc.
85 registrar_.Add(this,
86 NotificationType::NAV_ENTRY_COMMITTED,
87 NotificationService::AllSources());
88 registrar_.Add(this,
89 NotificationType::FOCUS_CHANGED_IN_PAGE,
90 NotificationService::AllSources());
91 registrar_.Add(this,
92 NotificationType::TAB_CONTENTS_DESTROYED,
93 NotificationService::AllSources());
94 registrar_.Add(this,
95 NotificationType::HIDE_KEYBOARD_INVOKED,
96 NotificationService::AllSources());
97 registrar_.Add(this,
98 NotificationType::SET_KEYBOARD_HEIGHT_INVOKED,
99 NotificationService::AllSources());
100 registrar_.Add(this,
101 NotificationType::EDITABLE_ELEMENT_TOUCHED,
102 NotificationService::AllSources());
103 registrar_.Add(this,
104 NotificationType::BROWSER_OPENED,
105 NotificationService::AllSources());
106 registrar_.Add(this,
107 NotificationType::BROWSER_CLOSING,
108 NotificationService::AllSources());
109 registrar_.Add(this,
110 NotificationType::APP_EXITING,
111 NotificationService::AllSources());
112
113 #if defined(OS_CHROMEOS)
114 chromeos::input_method::InputMethodManager* manager =
115 chromeos::input_method::InputMethodManager::GetInstance();
116 manager->AddVirtualKeyboardObserver(this);
117 #endif
118 }
119
120 KeyboardManager::~KeyboardManager() {
121 #if defined(OS_CHROMEOS)
122 chromeos::input_method::InputMethodManager* manager =
123 chromeos::input_method::InputMethodManager::GetInstance();
124 manager->RemoveVirtualKeyboardObserver(this);
125 #endif
126 // TODO(sad): Do anything else?
127 }
128
129 void KeyboardManager::ShowKeyboardForWidget(views::Widget* widget) {
130 // TODO(sad): There needs to be some way of resetting |target_| if/when it is
131 // destroyed.
132 target_ = widget;
133
134 gfx::Rect rect = target_->GetWindowScreenBounds();
135 rect.set_y(rect.height() - keyboard_height_);
136 rect.set_height(keyboard_height_);
137 SetBounds(rect);
138
139 transform_.reset(new ui::InterpolatedTranslation(
140 gfx::Point(0, keyboard_height_), gfx::Point()));
141
142 GetRootView()->SetTransform(
143 transform_->Interpolate(animation_->GetCurrentValue()));
144 animation_->Show();
145
146 MoveToTop();
147 Show();
148 }
149
150 void KeyboardManager::Hide() {
151 animation_->Hide();
152 }
153
154 bool KeyboardManager::OnKeyEvent(const views::KeyEvent& event) {
155 return target_ ? target_->OnKeyEvent(event) : false;
156 }
157
158 void KeyboardManager::AnimationProgressed(const ui::Animation* animation) {
159 GetRootView()->SetTransform(
160 transform_->Interpolate(animation_->GetCurrentValue()));
161 }
162
163 void KeyboardManager::AnimationEnded(const ui::Animation* animation) {
164 if (animation_->GetCurrentValue() < 0.01)
165 Widget::Hide();
166 }
167
168 bool KeyboardManager::OnMessageReceived(const IPC::Message& message) {
169 bool handled = true;
170 IPC_BEGIN_MESSAGE_MAP(KeyboardManager, message)
171 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
172 IPC_MESSAGE_UNHANDLED(handled = false)
173 IPC_END_MESSAGE_MAP()
174 return handled;
175 }
176
177 void KeyboardManager::OnRequest(
178 const ExtensionHostMsg_Request_Params& request) {
179 extension_dispatcher_.Dispatch(request,
180 dom_view_->tab_contents()->render_view_host());
181 }
182
183 void KeyboardManager::ActiveTabChanged(TabContentsWrapper* old_contents,
184 TabContentsWrapper* new_contents,
185 int index,
186 bool user_gesture) {
187 TabContents* contents = new_contents->tab_contents();
188 if (!TabContentsCanAffectKeyboard(contents))
189 return;
190
191 // If the tab contents does not have the focus, then it should not affect the
192 // keyboard visibility.
193 views::View* view = static_cast<TabContentsViewTouch*>(contents->view());
194 if (!view->Contains(view->GetFocusManager()->GetFocusedView()))
195 return;
196
197 bool* editable = GetFocusedStateAccessor()->GetProperty(
198 contents->property_bag());
199 if (editable && *editable)
200 ShowKeyboardForWidget(view->GetWidget());
201 else
202 Hide();
203 }
204
205 Browser* KeyboardManager::GetBrowser() {
206 // TODO(sad): Find a better way. Perhaps just return NULL, and fix
207 // SendKeyboardEventInputFunction::GetTopLevelWidget to somehow interact with
208 // the WM to find the top level widget?
209 return BrowserList::FindTabbedBrowser(
210 ProfileManager::GetDefaultProfile(), true);
211 }
212
213 gfx::NativeView KeyboardManager::GetNativeViewOfHost() {
214 return dom_view_->native_view();
215 }
216
217 TabContents* KeyboardManager::GetAssociatedTabContents() const {
218 return dom_view_->tab_contents();
219 }
220
221 #if defined(OS_CHROMEOS)
222 void KeyboardManager::VirtualKeyboardChanged(
223 chromeos::input_method::InputMethodManager* manager,
224 const chromeos::input_method::VirtualKeyboard& virtual_keyboard,
225 const std::string& virtual_keyboard_layout) {
226 const GURL& url = virtual_keyboard.GetURLForLayout(virtual_keyboard_layout);
227 dom_view_->LoadURL(url);
228 VLOG(1) << "VirtualKeyboardChanged: Switched to " << url.spec();
229 }
230 #endif
231
232 void KeyboardManager::Observe(NotificationType type,
233 const NotificationSource& source,
234 const NotificationDetails& details) {
235 switch (type.value) {
236 case NotificationType::NAV_ENTRY_COMMITTED: {
237 // When a navigation happens, we want to hide the keyboard if the focus is
238 // in the web-page. Otherwise, the keyboard visibility should not change.
239 NavigationController* controller =
240 Source<NavigationController>(source).ptr();
241 TabContents* tab_contents = controller->tab_contents();
242 GetFocusedStateAccessor()->SetProperty(tab_contents->property_bag(),
243 false);
244 if (!TabContentsCanAffectKeyboard(tab_contents))
245 break;
246
247 TabContentsViewTouch* view =
248 static_cast<TabContentsViewTouch*>(tab_contents->view());
249 views::View* focused = view->GetFocusManager()->GetFocusedView();
250 views::TextInputClient* input =
sadrul 2011/07/05 06:03:38 @bryeung: We used to look at the View::GetClassNam
bryeung 2011/07/05 14:23:13 Actually: all of this logic is going to change/go
251 focused ? focused->GetTextInputClient() : NULL;
252 // Show the keyboard if the focused view supports text-input.
253 if (input && input->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE)
254 ShowKeyboardForWidget(focused->GetWidget());
255 else
256 Hide();
257 break;
258 }
259
260 case NotificationType::FOCUS_CHANGED_IN_PAGE: {
261 // If the focus in the page moved to an editable field, then the keyboard
262 // should be visible, otherwise not.
263 TabContents* tab_contents = Source<TabContents>(source).ptr();
264 if (!TabContentsCanAffectKeyboard(tab_contents))
265 break;
266
267 const bool editable = *Details<const bool>(details).ptr();
268 if (editable) {
269 TabContentsViewTouch* view =
270 static_cast<TabContentsViewTouch*>(tab_contents->view());
271 ShowKeyboardForWidget(view->GetWidget());
272 } else {
273 Hide();
274 }
275
276 break;
277 }
278
279 case NotificationType::TAB_CONTENTS_DESTROYED: {
280 // Tab content was destroyed. Forget everything about it.
281 GetFocusedStateAccessor()->DeleteProperty(
282 Source<TabContents>(source).ptr()->property_bag());
283 break;
284 }
285
286 case NotificationType::HIDE_KEYBOARD_INVOKED: {
287 // The keyboard is hiding itself.
288 Browser* browser = BrowserList::GetLastActive();
289 if (browser) {
290 TabContents* tab_contents = browser->GetSelectedTabContents();
291 if (tab_contents)
292 GetFocusedStateAccessor()->SetProperty(tab_contents->property_bag(),
293 false);
294 }
295
296 Hide();
297 break;
298 }
299
300 case NotificationType::SET_KEYBOARD_HEIGHT_INVOKED: {
301 // The keyboard is resizing itself.
302
303 // TODO(penghuang) Allow extension conrtol the virtual keyboard directly
304 // instead of using Notification.
305 int height = *Details<int>(details).ptr();
306 if (height != keyboard_height_) {
307 DCHECK_GE(height, 0) << "Keyboard height should not be negative.";
308
309 keyboard_height_ = height;
310 gfx::Size size = GetWindowScreenBounds().size();
311 size.set_height(keyboard_height_);
312 SetSize(size);
313
314 // TODO(sad): Notify the target widget that the size has changed so it
315 // can update its display accordingly if it wanted to.
316 }
317 break;
318 }
319
320 case NotificationType::EDITABLE_ELEMENT_TOUCHED: {
321 // In case the keyboard hid itself and the focus is still in an editable
322 // field, and the user touches the field, then we want to show the
323 // keyboard again.
324 views::View* src = Source<views::View>(source).ptr();
325 ShowKeyboardForWidget(src->GetWidget());
326 break;
327 }
328
329 case NotificationType::BROWSER_OPENED: {
330 // A tabstrip observer is installed for the new Browser so that the
331 // keyboard can be updated when the selected tab changes in the Browser.
332 Browser* browser = Source<Browser>(source).ptr();
333 browser->tabstrip_model()->AddObserver(this);
334 break;
335 }
336
337 case NotificationType::BROWSER_CLOSING: {
338 // Remove the tabstrip observer.
339 Browser* browser = Source<Browser>(source).ptr();
340 browser->tabstrip_model()->RemoveObserver(this);
341 break;
342 }
343
344 case NotificationType::APP_EXITING: {
345 // Ideally KeyboardManager would Close itself, but that ends up destroying
346 // the singleton object, which causes a crash when all the singleton
347 // objects are destroyed from the AtExitManager. So just destroy the
348 // RootView here.
349 DestroyRootView();
350 break;
351 }
352
353 default:
354 NOTREACHED();
355 }
356 }
357
358 // static
359 KeyboardManager* KeyboardManager::GetInstance() {
360 return Singleton<KeyboardManager>::get();
361 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698