OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/keyboard/keyboard_controller.h" | 5 #include "ui/keyboard/keyboard_controller.h" |
6 | 6 |
7 #include "ui/aura/layout_manager.h" | 7 #include "ui/aura/layout_manager.h" |
8 #include "ui/aura/window.h" | 8 #include "ui/aura/window.h" |
9 #include "ui/aura/window_delegate.h" | |
10 #include "ui/base/hit_test.h" | |
11 #include "ui/gfx/path.h" | |
9 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
13 #include "ui/gfx/skia_util.h" | |
10 #include "ui/keyboard/keyboard_controller_proxy.h" | 14 #include "ui/keyboard/keyboard_controller_proxy.h" |
11 | 15 |
12 namespace { | 16 namespace { |
13 | 17 |
18 gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) { | |
19 const float kKeyboardHeightRatio = 0.3; | |
20 return gfx::Rect( | |
21 window_bounds.x(), | |
22 window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio), | |
23 window_bounds.width(), | |
24 window_bounds.height() * kKeyboardHeightRatio); | |
25 } | |
26 | |
14 // LayoutManager for the virtual keyboard container. Manages a single window | 27 // LayoutManager for the virtual keyboard container. Manages a single window |
15 // (the virtual keyboard) and keeps it positioned at the bottom of the | 28 // (the virtual keyboard) and keeps it positioned at the bottom of the |
16 // container window. | 29 // container window. |
17 class KeyboardLayoutManager : public aura::LayoutManager { | 30 class KeyboardLayoutManager : public aura::LayoutManager { |
18 public: | 31 public: |
19 KeyboardLayoutManager(aura::Window* owner, aura::Window* keyboard) | 32 KeyboardLayoutManager(aura::Window* owner, aura::Window* keyboard) |
20 : owner_(owner), keyboard_(keyboard) {} | 33 : owner_(owner), keyboard_(keyboard) {} |
21 | 34 |
22 // Overridden from aura::LayoutManager | 35 // Overridden from aura::LayoutManager |
23 virtual void OnWindowResized() OVERRIDE { | 36 virtual void OnWindowResized() OVERRIDE { |
24 gfx::Rect owner_bounds = owner_->bounds(); | 37 const gfx::Rect& owner_bounds = owner_->bounds(); |
25 gfx::Rect keyboard_bounds = gfx::Rect( | 38 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(owner_bounds); |
26 owner_bounds.x(), | |
27 owner_bounds.y() + owner_bounds.height() * 0.7, | |
28 owner_bounds.width(), | |
29 owner_bounds.height() * 0.3); | |
30 SetChildBoundsDirect(keyboard_, keyboard_bounds); | 39 SetChildBoundsDirect(keyboard_, keyboard_bounds); |
bryeung
2013/04/05 19:28:57
might as well:
SetChildBoundsDirect(keyboard_, Ke
sadrul
2013/04/08 04:09:48
Done.
| |
31 } | 40 } |
32 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { | 41 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { |
33 CHECK(child == keyboard_); | 42 CHECK(child == keyboard_); |
34 } | 43 } |
35 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} | 44 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} |
36 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} | 45 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} |
37 virtual void OnChildWindowVisibilityChanged(aura::Window* child, | 46 virtual void OnChildWindowVisibilityChanged(aura::Window* child, |
38 bool visible) OVERRIDE {} | 47 bool visible) OVERRIDE {} |
39 virtual void SetChildBounds(aura::Window* child, | 48 virtual void SetChildBounds(aura::Window* child, |
40 const gfx::Rect& requested_bounds) OVERRIDE { | 49 const gfx::Rect& requested_bounds) OVERRIDE { |
41 // Drop these: the size should only be set in OnWindowResized. | 50 // Drop these: the size should only be set in OnWindowResized. |
42 } | 51 } |
43 | 52 |
44 private: | 53 private: |
45 aura::Window* owner_; | 54 aura::Window* owner_; |
46 aura::Window* keyboard_; | 55 aura::Window* keyboard_; |
47 | 56 |
48 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); | 57 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager); |
49 }; | 58 }; |
50 | 59 |
60 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus. | |
61 // This is necessary to make sure that the synthetic key-events reach the target | |
62 // window. | |
63 // The delegate deletes itself when the window is destroyed. | |
64 class KeyboardWindowDelegate : public aura::WindowDelegate { | |
65 public: | |
66 KeyboardWindowDelegate() {} | |
67 virtual ~KeyboardWindowDelegate() {} | |
68 | |
69 private: | |
70 // Overridden from aura::WindowDelegate: | |
71 virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); } | |
72 virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); } | |
73 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, | |
74 const gfx::Rect& new_bounds) OVERRIDE { | |
75 bounds_ = new_bounds; | |
76 } | |
77 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { | |
78 return gfx::kNullCursor; | |
79 } | |
80 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { | |
81 return HTNOWHERE; | |
82 } | |
83 virtual bool ShouldDescendIntoChildForEventHandling( | |
84 aura::Window* child, | |
85 const gfx::Point& location) OVERRIDE { | |
86 return true; | |
87 } | |
88 virtual bool CanFocus() OVERRIDE { return false; } | |
89 virtual void OnCaptureLost() OVERRIDE {} | |
90 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {} | |
91 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {} | |
92 virtual void OnWindowDestroying() OVERRIDE {} | |
93 virtual void OnWindowDestroyed() OVERRIDE { delete this; } | |
94 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {} | |
95 virtual bool HasHitTestMask() const OVERRIDE { return true; } | |
96 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE { | |
97 gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(bounds_); | |
98 mask->addRect(RectToSkRect(keyboard_bounds)); | |
99 } | |
100 virtual scoped_refptr<ui::Texture> CopyTexture() OVERRIDE { return NULL; } | |
101 | |
102 gfx::Rect bounds_; | |
103 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate); | |
104 }; | |
105 | |
51 } // namespace | 106 } // namespace |
52 | 107 |
53 namespace keyboard { | 108 namespace keyboard { |
54 | 109 |
55 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) | 110 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) |
56 : proxy_(proxy), container_(NULL) { | 111 : proxy_(proxy), container_(NULL) { |
57 CHECK(proxy); | 112 CHECK(proxy); |
58 } | 113 } |
59 | 114 |
60 KeyboardController::~KeyboardController() {} | 115 KeyboardController::~KeyboardController() {} |
61 | 116 |
62 aura::Window* KeyboardController::GetContainerWindow() { | 117 aura::Window* KeyboardController::GetContainerWindow() { |
63 if (!container_) { | 118 if (!container_) { |
64 container_ = new aura::Window(NULL); | 119 container_ = new aura::Window(new KeyboardWindowDelegate()); |
65 container_->SetName("KeyboardContainer"); | 120 container_->SetName("KeyboardContainer"); |
66 container_->Init(ui::LAYER_NOT_DRAWN); | 121 container_->Init(ui::LAYER_NOT_DRAWN); |
67 | 122 |
68 aura::Window* keyboard = proxy_->GetKeyboardWindow(); | 123 aura::Window* keyboard = proxy_->GetKeyboardWindow(); |
69 keyboard->Show(); | 124 keyboard->Show(); |
70 | 125 |
71 container_->SetLayoutManager( | 126 container_->SetLayoutManager( |
72 new KeyboardLayoutManager(container_, keyboard)); | 127 new KeyboardLayoutManager(container_, keyboard)); |
73 container_->AddChild(keyboard); | 128 container_->AddChild(keyboard); |
74 } | 129 } |
75 return container_; | 130 return container_; |
76 } | 131 } |
77 | 132 |
78 } // namespace keyboard | 133 } // namespace keyboard |
OLD | NEW |