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

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

Powered by Google App Engine
This is Rietveld 408576698