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

Side by Side Diff: ui/keyboard/keyboard_controller.cc

Issue 13932030: Delayed loading of the virtual keyboard. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments Created 7 years, 8 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 | « ui/keyboard/keyboard_controller.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 9 #include "ui/aura/window_delegate.h"
10 #include "ui/base/hit_test.h" 10 #include "ui/base/hit_test.h"
11 #include "ui/base/ime/input_method.h" 11 #include "ui/base/ime/input_method.h"
12 #include "ui/base/ime/input_method_base.h"
13 #include "ui/base/ime/text_input_client.h" 12 #include "ui/base/ime/text_input_client.h"
14 #include "ui/base/ime/text_input_type.h" 13 #include "ui/base/ime/text_input_type.h"
15 #include "ui/gfx/path.h" 14 #include "ui/gfx/path.h"
16 #include "ui/gfx/rect.h" 15 #include "ui/gfx/rect.h"
17 #include "ui/gfx/skia_util.h" 16 #include "ui/gfx/skia_util.h"
18 #include "ui/keyboard/keyboard_controller_proxy.h" 17 #include "ui/keyboard/keyboard_controller_proxy.h"
19 18
20 namespace { 19 namespace {
21 20
22 gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) { 21 gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) {
23 const float kKeyboardHeightRatio = 0.3f; 22 const float kKeyboardHeightRatio = 0.3f;
24 return gfx::Rect( 23 return gfx::Rect(
25 window_bounds.x(), 24 window_bounds.x(),
26 window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio), 25 window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio),
27 window_bounds.width(), 26 window_bounds.width(),
28 window_bounds.height() * kKeyboardHeightRatio); 27 window_bounds.height() * kKeyboardHeightRatio);
29 } 28 }
30 29
31 // LayoutManager for the virtual keyboard container. Manages a single window
32 // (the virtual keyboard) and keeps it positioned at the bottom of the
33 // container window.
34 class KeyboardLayoutManager : public aura::LayoutManager {
35 public:
36 KeyboardLayoutManager(aura::Window* owner, aura::Window* keyboard)
37 : owner_(owner), keyboard_(keyboard) {}
38
39 // Overridden from aura::LayoutManager
40 virtual void OnWindowResized() OVERRIDE {
41 SetChildBoundsDirect(keyboard_,
42 KeyboardBoundsFromWindowBounds(owner_->bounds()));
43 }
44 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
45 CHECK(child == keyboard_);
46 }
47 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
48 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
49 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
50 bool visible) OVERRIDE {}
51 virtual void SetChildBounds(aura::Window* child,
52 const gfx::Rect& requested_bounds) OVERRIDE {
53 // Drop these: the size should only be set in OnWindowResized.
54 }
55
56 private:
57 aura::Window* owner_;
58 aura::Window* keyboard_;
59
60 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
61 };
62
63 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus. 30 // The KeyboardWindowDelegate makes sure the keyboard-window does not get focus.
64 // This is necessary to make sure that the synthetic key-events reach the target 31 // This is necessary to make sure that the synthetic key-events reach the target
65 // window. 32 // window.
66 // The delegate deletes itself when the window is destroyed. 33 // The delegate deletes itself when the window is destroyed.
67 class KeyboardWindowDelegate : public aura::WindowDelegate { 34 class KeyboardWindowDelegate : public aura::WindowDelegate {
68 public: 35 public:
69 KeyboardWindowDelegate() {} 36 KeyboardWindowDelegate() {}
70 virtual ~KeyboardWindowDelegate() {} 37 virtual ~KeyboardWindowDelegate() {}
71 38
72 private: 39 private:
(...skipping 30 matching lines...) Expand all
103 virtual scoped_refptr<ui::Texture> CopyTexture() OVERRIDE { return NULL; } 70 virtual scoped_refptr<ui::Texture> CopyTexture() OVERRIDE { return NULL; }
104 71
105 gfx::Rect bounds_; 72 gfx::Rect bounds_;
106 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate); 73 DISALLOW_COPY_AND_ASSIGN(KeyboardWindowDelegate);
107 }; 74 };
108 75
109 } // namespace 76 } // namespace
110 77
111 namespace keyboard { 78 namespace keyboard {
112 79
80 // LayoutManager for the virtual keyboard container. Manages a single window
81 // (the virtual keyboard) and keeps it positioned at the bottom of the
82 // owner window.
83 class KeyboardLayoutManager : public aura::LayoutManager {
84 public:
85 KeyboardLayoutManager(aura::Window* container)
86 : container_(container), keyboard_(NULL) {
87 CHECK(container_);
88 }
89
90 void AddKeyboard(aura::Window* keyboard) {
91 keyboard->Show();
92 container_->AddChild(keyboard);
93 OnWindowResized();
94 }
95
96 void ShowKeyboard() {
97 container_->parent()->StackChildAtTop(container_);
98 container_->Show();
99 }
100
101 void HideKeyboard() {
102 container_->Hide();
103 }
104
105 // Overridden from aura::LayoutManager
106 virtual void OnWindowResized() OVERRIDE {
107 if (!keyboard_)
108 return;
109 SetChildBoundsDirect(keyboard_,
110 KeyboardBoundsFromWindowBounds(container_->bounds()));
111 }
112 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
113 DCHECK(!keyboard_);
114 keyboard_ = child;
115 }
116 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
117 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
118 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
119 bool visible) OVERRIDE {}
120 virtual void SetChildBounds(aura::Window* child,
121 const gfx::Rect& requested_bounds) OVERRIDE {
122 // Drop these: the size should only be set in OnWindowResized.
123 }
124
125 private:
126 aura::Window* container_;
127 aura::Window* keyboard_;
128
129 DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
130 };
131
113 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy) 132 KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
114 : proxy_(proxy), container_(NULL) { 133 : proxy_(proxy),
134 container_(NULL),
135 layout_manager_(NULL),
136 input_method_(NULL) {
115 CHECK(proxy); 137 CHECK(proxy);
116 proxy_->GetInputMethod()->AddObserver(this); 138 input_method_ = proxy_->GetInputMethod();
139 input_method_->AddObserver(this);
117 } 140 }
118 141
119 KeyboardController::~KeyboardController() { 142 KeyboardController::~KeyboardController() {
120 if (container_) 143 input_method_->RemoveObserver(this);
121 container_->RemoveObserver(this);
sadrul 2013/04/15 16:49:37 The KC does not own the |container_|. So it is pos
bryeung 2013/04/15 17:42:36 Oops! Good catch.
122 proxy_->GetInputMethod()->RemoveObserver(this);
sadrul 2013/04/15 16:49:37 I suppose it is possible that the root-window and/
bryeung 2013/04/15 17:42:36 Done.
123 } 144 }
124 145
125 aura::Window* KeyboardController::GetContainerWindow() { 146 aura::Window* KeyboardController::GetContainerWindow() {
126 if (!container_) { 147 if (!layout_manager_) {
148 CHECK(!container_);
127 container_ = new aura::Window(new KeyboardWindowDelegate()); 149 container_ = new aura::Window(new KeyboardWindowDelegate());
128 container_->SetName("KeyboardContainer"); 150 container_->SetName("KeyboardContainer");
129 container_->Init(ui::LAYER_NOT_DRAWN); 151 container_->Init(ui::LAYER_NOT_DRAWN);
130 container_->AddObserver(this); 152 container_->AddObserver(this);
131 153
132 aura::Window* keyboard = proxy_->GetKeyboardWindow(); 154 layout_manager_ = new KeyboardLayoutManager(container_);
133 keyboard->Show(); 155 container_->SetLayoutManager(layout_manager_);
156 }
134 157
135 container_->SetLayoutManager( 158 CHECK(layout_manager_);
136 new KeyboardLayoutManager(container_, keyboard)); 159 CHECK(container_);
137 container_->AddChild(keyboard);
138 }
139 return container_; 160 return container_;
140 } 161 }
141 162
142 void KeyboardController::OnTextInputStateChanged( 163 void KeyboardController::OnTextInputStateChanged(
143 const ui::TextInputClient* client) { 164 const ui::TextInputClient* client) {
144 if (!container_) 165 if (!layout_manager_)
145 return; 166 return;
146 167
147 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) { 168 if (!client || client->GetTextInputType() == ui::TEXT_INPUT_TYPE_NONE) {
148 container_->Hide(); 169 layout_manager_->HideKeyboard();
149 } else { 170 } else {
150 container_->parent()->StackChildAtTop(container_); 171 if (container_->children().empty())
151 container_->Show(); 172 layout_manager_->AddKeyboard(proxy_->GetKeyboardWindow());
173 layout_manager_->ShowKeyboard();
sadrul 2013/04/15 16:49:37 I am not sure having |layout_manager_| is all that
bryeung 2013/04/15 17:42:36 Done.
152 } 174 }
153
154 // TODO(bryeung): whenever the TextInputClient changes we need to notify the 175 // TODO(bryeung): whenever the TextInputClient changes we need to notify the
155 // keyboard (with the TextInputType) so that it can reset it's state (e.g. 176 // keyboard (with the TextInputType) so that it can reset it's state (e.g.
156 // abandon compositions in progress) 177 // abandon compositions in progress)
157 } 178 }
158 179
159 void KeyboardController::OnWindowParentChanged(aura::Window* window, 180 void KeyboardController::OnWindowParentChanged(aura::Window* window,
160 aura::Window* parent) { 181 aura::Window* parent) {
161 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient()); 182 OnTextInputStateChanged(proxy_->GetInputMethod()->GetTextInputClient());
162 } 183 }
163 184
164 void KeyboardController::OnWindowDestroying(aura::Window* window) { 185 void KeyboardController::OnWindowDestroying(aura::Window* window) {
165 DCHECK_EQ(container_, window); 186 DCHECK_EQ(container_, window);
166 container_ = NULL; 187 container_->RemoveObserver(this);
167 } 188 }
168 189
169 } // namespace keyboard 190 } // namespace keyboard
OLDNEW
« no previous file with comments | « ui/keyboard/keyboard_controller.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698