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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
// Copyright (c) 2013 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 "ui/keyboard/keyboard_controller.h"
#include <set>
#include "base/bind.h"
#include "base/command_line.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/render_widget_host_view.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_observer.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/hit_test.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/path.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/skia_util.h"
#include "ui/keyboard/keyboard_controller_observer.h"
#include "ui/keyboard/keyboard_controller_proxy.h"
#include "ui/keyboard/keyboard_layout_manager.h"
#include "ui/keyboard/keyboard_util.h"
#include "ui/wm/core/masked_window_targeter.h"
#if defined(OS_CHROMEOS)
#include "base/process/launch.h"
#include "base/sys_info.h"
#endif
namespace {
const int kHideKeyboardDelayMs = 100;
// The virtual keyboard show/hide animation duration.
const int kShowAnimationDurationMs = 350;
const int kHideAnimationDurationMs = 100;
// The opacity of virtual keyboard container when show animation starts or
// hide animation finishes.
// TODO(rsadam@): Investigate why setting this to zero crashes.
const float kAnimationStartOrAfterHideOpacity = 0.01f;
// Event targeter for the keyboard container.
class KeyboardContainerTargeter : public wm::MaskedWindowTargeter {
public:
KeyboardContainerTargeter(aura::Window* container,
keyboard::KeyboardControllerProxy* proxy)
: wm::MaskedWindowTargeter(container),
proxy_(proxy) {
}
virtual ~KeyboardContainerTargeter() {}
private:
// wm::MaskedWindowTargeter:
virtual bool GetHitTestMask(aura::Window* window,
gfx::Path* mask) const OVERRIDE {
if (proxy_ && !proxy_->HasKeyboardWindow())
return true;
gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
keyboard::DefaultKeyboardBoundsFromWindowBounds(window->bounds());
mask->addRect(RectToSkRect(keyboard_bounds));
return true;
}
keyboard::KeyboardControllerProxy* proxy_;
DISALLOW_COPY_AND_ASSIGN(KeyboardContainerTargeter);
};
// The KeyboardWindowDelegate makes sure the keyboard-window does not get focus.
// This is necessary to make sure that the synthetic key-events reach the target
// window.
// The delegate deletes itself when the window is destroyed.
class KeyboardWindowDelegate : public aura::WindowDelegate {
public:
explicit KeyboardWindowDelegate(keyboard::KeyboardControllerProxy* proxy)
: proxy_(proxy) {}
virtual ~KeyboardWindowDelegate() {}
private:
// Overridden from aura::WindowDelegate:
virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); }
virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); }
virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds) OVERRIDE {
bounds_ = new_bounds;
}
virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
return gfx::kNullCursor;
}
virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
return HTNOWHERE;
}
virtual bool ShouldDescendIntoChildForEventHandling(
aura::Window* child,
const gfx::Point& location) OVERRIDE {
return true;
}
virtual bool CanFocus() OVERRIDE { return false; }
virtual void OnCaptureLost() OVERRIDE {}
virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {}
virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE { delete this; }
virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
virtual bool HasHitTestMask() const OVERRIDE {
return !proxy_ || proxy_->HasKeyboardWindow();
}
virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {
if (proxy_ && !proxy_->HasKeyboardWindow())
return;
gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
keyboard::DefaultKeyboardBoundsFromWindowBounds(bounds_);
mask->addRect(RectToSkRect(keyboard_bounds));
}
gfx::Rect bounds_;
keyboard::KeyboardControllerProxy* proxy_;
DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate);
};
void ToggleTouchEventLogging(bool enable) {
#if defined(OS_CHROMEOS)
if (!base::SysInfo::IsRunningOnChromeOS())
return;
CommandLine command(
base::FilePath("/opt/google/touchscreen/toggle_touch_event_logging"));
if (enable)
command.AppendArg("1");
else
command.AppendArg("0");
VLOG(1) << "Running " << command.GetCommandLineString();
base::LaunchOptions options;
options.wait = true;
base::LaunchProcess(command, options, NULL);
#endif
}
aura::Window *GetFrameWindow(aura::Window *window) {
// Each container window has a non-negative id. Stop traversing at the child
// of a container window.
if (!window)
return NULL;
while (window->parent() && window->parent()->id() < 0) {
window = window->parent();
}
return window;
}
} // namespace
namespace keyboard {
// Observer for both keyboard show and hide animations. It should be owned by
// KeyboardController.
class CallbackAnimationObserver : public ui::LayerAnimationObserver {
public:
CallbackAnimationObserver(ui::LayerAnimator* animator,
base::Callback<void(void)> callback);
virtual ~CallbackAnimationObserver();
private:
// Overridden from ui::LayerAnimationObserver:
virtual void OnLayerAnimationEnded(ui::LayerAnimationSequence* seq) OVERRIDE;
virtual void OnLayerAnimationAborted(
ui::LayerAnimationSequence* seq) OVERRIDE;
virtual void OnLayerAnimationScheduled(
ui::LayerAnimationSequence* seq) OVERRIDE {}
ui::LayerAnimator* animator_;
base::Callback<void(void)> callback_;
DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver);
};
CallbackAnimationObserver::CallbackAnimationObserver(
ui::LayerAnimator* animator, base::Callback<void(void)> callback)
: animator_(animator), callback_(callback) {
}
CallbackAnimationObserver::~CallbackAnimationObserver() {
animator_->RemoveObserver(this);
}
void CallbackAnimationObserver::OnLayerAnimationEnded(
ui::LayerAnimationSequence* seq) {
if (animator_->is_animating())
return;
animator_->RemoveObserver(this);
callback_.Run();
}
void CallbackAnimationObserver::OnLayerAnimationAborted(
ui::LayerAnimationSequence* seq) {
animator_->RemoveObserver(this);
}
class WindowBoundsChangeObserver : public aura::WindowObserver {
public:
virtual void OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds) OVERRIDE;
virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE;
void AddObservedWindow(aura::Window* window);
void RemoveAllObservedWindows();
private:
std::set<aura::Window*> observed_windows_;
};
void WindowBoundsChangeObserver::OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds, const gfx::Rect& new_bounds) {
KeyboardController* controller = KeyboardController::GetInstance();
if (controller)
controller->UpdateWindowInsets(window);
}
void WindowBoundsChangeObserver::OnWindowDestroyed(aura::Window* window) {
if (window->HasObserver(this))
window->RemoveObserver(this);
observed_windows_.erase(window);
}
void WindowBoundsChangeObserver::AddObservedWindow(aura::Window* window) {
if (!window->HasObserver(this)) {
window->AddObserver(this);
observed_windows_.insert(window);
}
}
void WindowBoundsChangeObserver::RemoveAllObservedWindows() {
for (std::set<aura::Window*>::iterator it = observed_windows_.begin();
it != observed_windows_.end(); ++it)
(*it)->RemoveObserver(this);
observed_windows_.clear();
}
// static
KeyboardController* KeyboardController::instance_ = NULL;
KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
: proxy_(proxy),
input_method_(NULL),
keyboard_visible_(false),
show_on_resize_(false),
lock_keyboard_(false),
type_(ui::TEXT_INPUT_TYPE_NONE),
weak_factory_(this) {
CHECK(proxy);
input_method_ = proxy_->GetInputMethod();
input_method_->AddObserver(this);
window_bounds_observer_.reset(new WindowBoundsChangeObserver());
}
KeyboardController::~KeyboardController() {
if (container_)
container_->RemoveObserver(this);
if (input_method_)
input_method_->RemoveObserver(this);
ResetWindowInsets();
}
// static
void KeyboardController::ResetInstance(KeyboardController* controller) {
if (instance_ && instance_ != controller)
delete instance_;
instance_ = controller;
}
// static
KeyboardController* KeyboardController::GetInstance() {
return instance_;
}
aura::Window* KeyboardController::GetContainerWindow() {
if (!container_.get()) {
container_.reset(new aura::Window(
new KeyboardWindowDelegate(proxy_.get())));
container_->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
new KeyboardContainerTargeter(container_.get(), proxy_.get())));
container_->SetName("KeyboardContainer");
container_->set_owned_by_parent(false);
container_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
container_->AddObserver(this);
container_->SetLayoutManager(new KeyboardLayoutManager(this));
}
return container_.get();
}
void KeyboardController::NotifyKeyboardBoundsChanging(
const gfx::Rect& new_bounds) {
current_keyboard_bounds_ = new_bounds;
if (proxy_->HasKeyboardWindow() && proxy_->GetKeyboardWindow()->IsVisible()) {
FOR_EACH_OBSERVER(KeyboardControllerObserver,
observer_list_,
OnKeyboardBoundsChanging(new_bounds));
if (keyboard::IsKeyboardOverscrollEnabled()) {
// Adjust the height of the viewport for visible windows on the primary
// display.
// TODO(kevers): Add EnvObserver to properly initialize insets if a
// window is created while the keyboard is visible.
scoped_ptr<content::RenderWidgetHostIterator> widgets(
content::RenderWidgetHost::GetRenderWidgetHosts());
aura::Window *keyboard_window = proxy_->GetKeyboardWindow();
aura::Window *root_window = keyboard_window->GetRootWindow();
while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
content::RenderWidgetHostView* view = widget->GetView();
// Can be NULL, e.g. if the RenderWidget is being destroyed or
// the render process crashed.
if (view) {
aura::Window *window = view->GetNativeView();
// If virtual keyboard failed to load, a widget that displays error
// message will be created and adds as a child of the virtual keyboard
// window. We want to avoid add BoundsChangedObserver to that window.
if (GetFrameWindow(window) != keyboard_window &&
window->GetRootWindow() == root_window) {
gfx::Rect window_bounds = window->GetBoundsInScreen();
gfx::Rect intersect = gfx::IntersectRects(window_bounds,
new_bounds);
int overlap = intersect.height();
if (overlap > 0 && overlap < window_bounds.height())
view->SetInsets(gfx::Insets(0, 0, overlap, 0));
else
view->SetInsets(gfx::Insets());
AddBoundsChangedObserver(window);
}
}
}
} else {
ResetWindowInsets();
}
} else {
current_keyboard_bounds_ = gfx::Rect();
}
}
void KeyboardController::HideKeyboard(HideReason reason) {
keyboard_visible_ = false;
ToggleTouchEventLogging(true);
keyboard::LogKeyboardControlEvent(
reason == HIDE_REASON_AUTOMATIC ?
keyboard::KEYBOARD_CONTROL_HIDE_AUTO :
keyboard::KEYBOARD_CONTROL_HIDE_USER);
NotifyKeyboardBoundsChanging(gfx::Rect());
set_lock_keyboard(false);
ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
animation_observer_.reset(new CallbackAnimationObserver(
container_animator,
base::Bind(&KeyboardController::HideAnimationFinished,
base::Unretained(this))));
container_animator->AddObserver(animation_observer_.get());
ui::ScopedLayerAnimationSettings settings(container_animator);
settings.SetTweenType(gfx::Tween::FAST_OUT_LINEAR_IN);
settings.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kHideAnimationDurationMs));
gfx::Transform transform;
transform.Translate(0, kAnimationDistance);
container_->SetTransform(transform);
container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
}
void KeyboardController::AddObserver(KeyboardControllerObserver* observer) {
observer_list_.AddObserver(observer);
}
void KeyboardController::RemoveObserver(KeyboardControllerObserver* observer) {
observer_list_.RemoveObserver(observer);
}
void KeyboardController::ShowKeyboard(bool lock) {
set_lock_keyboard(lock);
ShowKeyboardInternal();
}
void KeyboardController::OnWindowHierarchyChanged(
const HierarchyChangeParams& params) {
if (params.new_parent && params.target == container_.get())
OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient());
}
void KeyboardController::Reload() {
if (proxy_->HasKeyboardWindow()) {
// A reload should never try to show virtual keyboard. If keyboard is not
// visible before reload, it should keep invisible after reload.
show_on_resize_ = false;
proxy_->ReloadKeyboardIfNeeded();
}
}
void KeyboardController::OnTextInputStateChanged(
const ui::TextInputClient* client) {
if (!container_.get())
return;
type_ = client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE;
if (type_ == ui::TEXT_INPUT_TYPE_NONE && !lock_keyboard_) {
if (keyboard_visible_) {
// Set the visibility state here so that any queries for visibility
// before the timer fires returns the correct future value.
keyboard_visible_ = false;
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&KeyboardController::HideKeyboard,
weak_factory_.GetWeakPtr(), HIDE_REASON_AUTOMATIC),
base::TimeDelta::FromMilliseconds(kHideKeyboardDelayMs));
}
} else {
// Abort a pending keyboard hide.
if (WillHideKeyboard()) {
weak_factory_.InvalidateWeakPtrs();
keyboard_visible_ = true;
}
proxy_->SetUpdateInputType(type_);
// Do not explicitly show the Virtual keyboard unless it is in the process
// of hiding. Instead, the virtual keyboard is shown in response to a user
// gesture (mouse or touch) that is received while an element has input
// focus. Showing the keyboard requires an explicit call to
// OnShowImeIfNeeded.
}
}
void KeyboardController::OnInputMethodDestroyed(
const ui::InputMethod* input_method) {
DCHECK_EQ(input_method_, input_method);
input_method_ = NULL;
}
void KeyboardController::OnShowImeIfNeeded() {
ShowKeyboardInternal();
}
void KeyboardController::UpdateWindowInsets(aura::Window* window) {
aura::Window *keyboard_window = proxy_->GetKeyboardWindow();
if (window == keyboard_window)
return;
bool enableInsets = (keyboard_window->GetRootWindow() ==
window->GetRootWindow()) && keyboard::IsKeyboardOverscrollEnabled() &&
proxy_->GetKeyboardWindow()->IsVisible();
scoped_ptr<content::RenderWidgetHostIterator> widgets(
content::RenderWidgetHost::GetRenderWidgetHosts());
while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
content::RenderWidgetHostView* view = widget->GetView();
if (view && window->Contains(view->GetNativeView())) {
gfx::Rect window_bounds = view->GetNativeView()->GetBoundsInScreen();
gfx::Rect intersect = gfx::IntersectRects(window_bounds,
proxy_->GetKeyboardWindow()->bounds());
int overlap = enableInsets ? intersect.height() : 0;
if (overlap > 0 && overlap < window_bounds.height())
view->SetInsets(gfx::Insets(0, 0, overlap, 0));
else
view->SetInsets(gfx::Insets());
return;
}
}
}
void KeyboardController::ShowKeyboardInternal() {
if (!container_.get())
return;
if (container_->children().empty()) {
keyboard::MarkKeyboardLoadStarted();
aura::Window* keyboard = proxy_->GetKeyboardWindow();
keyboard->Show();
container_->AddChild(keyboard);
keyboard->set_owned_by_parent(false);
}
proxy_->ReloadKeyboardIfNeeded();
if (keyboard_visible_) {
return;
} else if (proxy_->GetKeyboardWindow()->bounds().height() == 0) {
show_on_resize_ = true;
return;
}
keyboard_visible_ = true;
// If the controller is in the process of hiding the keyboard, do not log
// the stat here since the keyboard will not actually be shown.
if (!WillHideKeyboard())
keyboard::LogKeyboardControlEvent(keyboard::KEYBOARD_CONTROL_SHOW);
weak_factory_.InvalidateWeakPtrs();
// If |container_| has hide animation, its visibility is set to false when
// hide animation finished. So even if the container is visible at this
// point, it may in the process of hiding. We still need to show keyboard
// container in this case.
if (container_->IsVisible() &&
!container_->layer()->GetAnimator()->is_animating())
return;
ToggleTouchEventLogging(false);
ui::LayerAnimator* container_animator = container_->layer()->GetAnimator();
// If the container is not animating, makes sure the position and opacity
// are at begin states for animation.
if (!container_animator->is_animating()) {
gfx::Transform transform;
transform.Translate(0, kAnimationDistance);
container_->SetTransform(transform);
container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
}
container_animator->set_preemption_strategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
animation_observer_.reset(new CallbackAnimationObserver(
container_animator,
base::Bind(&KeyboardController::ShowAnimationFinished,
base::Unretained(this))));
container_animator->AddObserver(animation_observer_.get());
proxy_->ShowKeyboardContainer(container_.get());
{
// Scope the following animation settings as we don't want to animate
// visibility change that triggered by a call to the base class function
// ShowKeyboardContainer with these settings. The container should become
// visible immediately.
ui::ScopedLayerAnimationSettings settings(container_animator);
settings.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
settings.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kShowAnimationDurationMs));
container_->SetTransform(gfx::Transform());
container_->layer()->SetOpacity(1.0);
}
}
void KeyboardController::ResetWindowInsets() {
const gfx::Insets insets;
scoped_ptr<content::RenderWidgetHostIterator> widgets(
content::RenderWidgetHost::GetRenderWidgetHosts());
while (content::RenderWidgetHost* widget = widgets->GetNextHost()) {
content::RenderWidgetHostView* view = widget->GetView();
if (view)
view->SetInsets(insets);
}
window_bounds_observer_->RemoveAllObservedWindows();
}
bool KeyboardController::WillHideKeyboard() const {
return weak_factory_.HasWeakPtrs();
}
void KeyboardController::ShowAnimationFinished() {
// Notify observers after animation finished to prevent reveal desktop
// background during animation.
NotifyKeyboardBoundsChanging(proxy_->GetKeyboardWindow()->bounds());
proxy_->EnsureCaretInWorkArea();
}
void KeyboardController::HideAnimationFinished() {
proxy_->HideKeyboardContainer(container_.get());
}
void KeyboardController::AddBoundsChangedObserver(aura::Window* window) {
aura::Window* target_window = GetFrameWindow(window);
if (target_window)
window_bounds_observer_->AddObservedWindow(target_window);
}
} // namespace keyboard
|