1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/touch/frame/touch_browser_frame_view.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_host/render_widget_host_view_views.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/ui/touch/frame/keyboard_container_view.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/tab_contents/tab_contents_view_touch.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/navigation_controller.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/browser/tab_contents/tab_contents_view.h"
#include "content/common/notification_service.h"
#include "content/common/notification_type.h"
#include "content/common/view_messages.h"
#include "ui/base/animation/slide_animation.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/transform.h"
#include "views/controls/button/image_button.h"
#include "views/controls/textfield/textfield.h"
#include "views/focus/focus_manager.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/input_method/virtual_keyboard_selector.h"
#endif
namespace {
const int kDefaultKeyboardHeight = 300;
const int kKeyboardSlideDuration = 300; // In milliseconds
PropertyAccessor<bool>* GetFocusedStateAccessor() {
static PropertyAccessor<bool> state;
return &state;
}
bool TabContentsHasFocus(const TabContents* contents) {
views::View* view = static_cast<TabContentsViewTouch*>(contents->view());
return view->Contains(view->GetFocusManager()->GetFocusedView());
}
} // namespace
// static
const char TouchBrowserFrameView::kViewClassName[] =
"browser/ui/touch/frame/TouchBrowserFrameView";
///////////////////////////////////////////////////////////////////////////////
// TouchBrowserFrameView, public:
TouchBrowserFrameView::TouchBrowserFrameView(BrowserFrame* frame,
BrowserView* browser_view)
: OpaqueBrowserFrameView(frame, browser_view),
keyboard_showing_(false),
keyboard_height_(kDefaultKeyboardHeight),
focus_listener_added_(false),
keyboard_(NULL) {
registrar_.Add(this,
NotificationType::NAV_ENTRY_COMMITTED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::FOCUS_CHANGED_IN_PAGE,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::TAB_CONTENTS_DESTROYED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::HIDE_KEYBOARD_INVOKED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::SET_KEYBOARD_HEIGHT_INVOKED,
NotificationService::AllSources());
browser_view->browser()->tabstrip_model()->AddObserver(this);
animation_.reset(new ui::SlideAnimation(this));
animation_->SetTweenType(ui::Tween::LINEAR);
animation_->SetSlideDuration(kKeyboardSlideDuration);
#if defined(OS_CHROMEOS)
chromeos::InputMethodLibrary* library =
chromeos::CrosLibrary::Get()->GetInputMethodLibrary();
library->AddVirtualKeyboardObserver(this);
#endif
}
TouchBrowserFrameView::~TouchBrowserFrameView() {
browser_view()->browser()->tabstrip_model()->RemoveObserver(this);
}
std::string TouchBrowserFrameView::GetClassName() const {
return kViewClassName;
}
void TouchBrowserFrameView::Layout() {
OpaqueBrowserFrameView::Layout();
if (!keyboard_)
return;
keyboard_->SetVisible(keyboard_showing_ || animation_->is_animating());
gfx::Rect bounds = GetBoundsForReservedArea();
if (animation_->is_animating() && !keyboard_showing_) {
// The keyboard is in the process of hiding. So pretend it still has the
// same bounds as when the keyboard is visible. But
// |GetBoundsForReservedArea| should not take this into account so that the
// render view gets the entire area to relayout itself.
bounds.set_y(bounds.y() - keyboard_height_);
bounds.set_height(keyboard_height_);
}
keyboard_->SetBoundsRect(bounds);
}
void TouchBrowserFrameView::FocusWillChange(views::View* focused_before,
views::View* focused_now) {
VirtualKeyboardType before = DecideKeyboardStateForView(focused_before);
VirtualKeyboardType now = DecideKeyboardStateForView(focused_now);
if (before != now) {
// TODO(varunjain): support other types of keyboard.
UpdateKeyboardAndLayout(now == GENERIC);
}
}
///////////////////////////////////////////////////////////////////////////////
// TouchBrowserFrameView, protected:
int TouchBrowserFrameView::GetReservedHeight() const {
return keyboard_showing_ ? keyboard_height_ : 0;
}
void TouchBrowserFrameView::ViewHierarchyChanged(bool is_add,
View* parent,
View* child) {
OpaqueBrowserFrameView::ViewHierarchyChanged(is_add, parent, child);
if (!GetFocusManager())
return;
if (is_add && !focus_listener_added_) {
// Add focus listener when this view is added to the hierarchy.
GetFocusManager()->AddFocusChangeListener(this);
focus_listener_added_ = true;
} else if (!is_add && focus_listener_added_) {
// Remove focus listener when this view is removed from the hierarchy.
GetFocusManager()->RemoveFocusChangeListener(this);
focus_listener_added_ = false;
}
}
///////////////////////////////////////////////////////////////////////////////
// TouchBrowserFrameView, private:
void TouchBrowserFrameView::InitVirtualKeyboard() {
if (keyboard_)
return;
Profile* keyboard_profile = browser_view()->browser()->profile();
DCHECK(keyboard_profile) << "Profile required for virtual keyboard.";
keyboard_ = new KeyboardContainerView(keyboard_profile,
browser_view()->browser());
keyboard_->SetVisible(false);
AddChildView(keyboard_);
}
void TouchBrowserFrameView::UpdateKeyboardAndLayout(bool should_show_keyboard) {
if (should_show_keyboard)
InitVirtualKeyboard();
if (should_show_keyboard == keyboard_showing_)
return;
DCHECK(keyboard_);
keyboard_showing_ = should_show_keyboard;
if (keyboard_showing_) {
// We don't re-layout the client view until the animation ends (see
// AnimationEnded below) because we want the client view to occupy the
// entire height during the animation. It is necessary to reset the
// transform for the keyboard first so that the contents are sized properly
// when layout, and then start the animation.
ui::Transform reset;
keyboard_->SetTransform(reset);
Layout();
animation_->Show();
} else {
animation_->Hide();
browser_view()->set_clip_y(ui::Tween::ValueBetween(
animation_->GetCurrentValue(), 0, keyboard_height_));
parent()->Layout();
}
}
TouchBrowserFrameView::VirtualKeyboardType
TouchBrowserFrameView::DecideKeyboardStateForView(views::View* view) {
if (!view)
return NONE;
std::string cname = view->GetClassName();
if (cname == views::Textfield::kViewClassName) {
return GENERIC;
} else if (cname == RenderWidgetHostViewViews::kViewClassName) {
TabContents* contents = browser_view()->browser()->GetSelectedTabContents();
bool* editable = contents ? GetFocusedStateAccessor()->GetProperty(
contents->property_bag()) : NULL;
if (editable && *editable)
return GENERIC;
}
return NONE;
}
bool TouchBrowserFrameView::HitTest(const gfx::Point& point) const {
if (OpaqueBrowserFrameView::HitTest(point))
return true;
if (close_button()->IsVisible() &&
close_button()->GetMirroredBounds().Contains(point))
return true;
if (restore_button()->IsVisible() &&
restore_button()->GetMirroredBounds().Contains(point))
return true;
if (maximize_button()->IsVisible() &&
maximize_button()->GetMirroredBounds().Contains(point))
return true;
if (minimize_button()->IsVisible() &&
minimize_button()->GetMirroredBounds().Contains(point))
return true;
return false;
}
void TouchBrowserFrameView::ActiveTabChanged(TabContentsWrapper* old_contents,
TabContentsWrapper* new_contents,
int index,
bool user_gesture) {
if (new_contents == old_contents)
return;
TabContents* contents = new_contents->tab_contents();
if (!TabContentsHasFocus(contents))
return;
bool* editable = GetFocusedStateAccessor()->GetProperty(
contents->property_bag());
UpdateKeyboardAndLayout(editable ? *editable : false);
}
void TouchBrowserFrameView::TabStripEmpty() {
if (animation_->is_animating()) {
// Reset the delegate so the AnimationEnded callback doesn't trigger.
animation_->set_delegate(NULL);
animation_->Stop();
}
}
void TouchBrowserFrameView::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
Browser* browser = browser_view()->browser();
if (type == NotificationType::FOCUS_CHANGED_IN_PAGE) {
// Only modify the keyboard state if the currently active tab sent the
// notification.
const TabContents* current_tab = browser->GetSelectedTabContents();
TabContents* source_tab = Source<TabContents>(source).ptr();
const bool editable = *Details<const bool>(details).ptr();
if (current_tab == source_tab && TabContentsHasFocus(source_tab))
UpdateKeyboardAndLayout(editable);
// Save the state of the focused field so that the keyboard visibility
// can be determined after tab switching.
GetFocusedStateAccessor()->SetProperty(
source_tab->property_bag(), editable);
} else if (type == NotificationType::NAV_ENTRY_COMMITTED) {
NavigationController* controller =
Source<NavigationController>(source).ptr();
Browser* source_browser = Browser::GetBrowserForController(
controller, NULL);
// If the Browser for the keyboard has navigated, re-evaluate the visibility
// of the keyboard.
TouchBrowserFrameView::VirtualKeyboardType keyboard_type = NONE;
views::View* view = GetFocusManager()->GetFocusedView();
if (view) {
if (view->GetClassName() == views::Textfield::kViewClassName)
keyboard_type = GENERIC;
if (view->GetClassName() == RenderWidgetHostViewViews::kViewClassName) {
// Reset the state of the focused field in the current tab.
GetFocusedStateAccessor()->SetProperty(
controller->tab_contents()->property_bag(), false);
}
}
if (source_browser == browser)
UpdateKeyboardAndLayout(keyboard_type == GENERIC);
} else if (type == NotificationType::TAB_CONTENTS_DESTROYED) {
GetFocusedStateAccessor()->DeleteProperty(
Source<TabContents>(source).ptr()->property_bag());
} else if (type == NotificationType::PREF_CHANGED) {
OpaqueBrowserFrameView::Observe(type, source, details);
} else if (type == NotificationType::HIDE_KEYBOARD_INVOKED) {
TabContents* tab_contents =
browser_view()->browser()->GetSelectedTabContents();
if (tab_contents) {
GetFocusedStateAccessor()->SetProperty(tab_contents->property_bag(),
false);
}
UpdateKeyboardAndLayout(false);
} else if (type == NotificationType::SET_KEYBOARD_HEIGHT_INVOKED) {
// TODO(penghuang) Allow extension conrtol the virtual keyboard directly
// instead of using Notification.
int height = *reinterpret_cast<int*>(details.map_key());
if (height != keyboard_height_) {
DCHECK_GE(height, 0) << "Height of the keyboard is less than 0.";
DCHECK_LE(height, View::height()) << "Height of the keyboard is greater "
"than the height of frame view.";
keyboard_height_ = height;
parent()->Layout();
}
}
}
///////////////////////////////////////////////////////////////////////////////
// ui::AnimationDelegate implementation
void TouchBrowserFrameView::AnimationProgressed(const ui::Animation* anim) {
ui::Transform transform;
transform.SetTranslateY(
ui::Tween::ValueBetween(anim->GetCurrentValue(), keyboard_height_, 0));
keyboard_->SetTransform(transform);
browser_view()->set_clip_y(
ui::Tween::ValueBetween(anim->GetCurrentValue(), 0, keyboard_height_));
SchedulePaint();
}
void TouchBrowserFrameView::AnimationEnded(const ui::Animation* animation) {
browser_view()->set_clip_y(0);
if (keyboard_showing_) {
// Because the NonClientFrameView is a sibling of the ClientView, we rely on
// the parent to resize the ClientView instead of resizing it directly.
parent()->Layout();
// The keyboard that pops up may end up hiding the text entry. So make sure
// the renderer scrolls when necessary to keep the textfield visible.
RenderViewHost* host =
browser_view()->browser()->GetSelectedTabContents()->render_view_host();
host->Send(new ViewMsg_ScrollFocusedEditableNodeIntoView(
host->routing_id()));
} else {
// Notify the keyboard that it is hidden now.
keyboard_->SetVisible(false);
}
SchedulePaint();
}
#if defined(OS_CHROMEOS)
void TouchBrowserFrameView::VirtualKeyboardChanged(
chromeos::InputMethodLibrary* obj,
const chromeos::input_method::VirtualKeyboard& virtual_keyboard,
const std::string& virtual_keyboard_layout) {
if (!keyboard_)
return;
const GURL& url = virtual_keyboard.GetURLForLayout(virtual_keyboard_layout);
keyboard_->LoadURL(url);
VLOG(1) << "VirtualKeyboardChanged: Switched to " << url.spec();
}
#endif
|