forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathash_keyboard_ui.cc
250 lines (205 loc) · 8.8 KB
/
ash_keyboard_ui.cc
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
// Copyright 2018 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 "ash/keyboard/ash_keyboard_ui.h"
#include <set>
#include <string>
#include <utility>
#include "ash/keyboard/ash_keyboard_controller.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/ws/window_service_owner.h"
#include "base/bind.h"
#include "base/scoped_observer.h"
#include "services/ws/public/mojom/window_tree.mojom.h"
#include "services/ws/remote_view_host/server_remote_view_host.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/chromeos/input_method_manager.h"
#include "ui/base/ime/ime_bridge.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/keyboard/keyboard_controller.h"
#include "ui/views/background.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/wm/core/coordinate_conversion.h"
#include "ui/wm/core/shadow_types.h"
namespace {
const int kShadowElevationVirtualKeyboard = 2;
} // namespace
namespace ash {
////////////////////////////////////////////////////////////////////////////////
// AshKeyboardView
class AshKeyboardUI::AshKeyboardView : public views::WidgetDelegateView,
public aura::WindowObserver {
public:
explicit AshKeyboardView(aura::Window* context) : scoped_observer_(this) {
SetLayoutManager(std::make_unique<views::FillLayout>());
InitWidget(context);
// Set the background to be transparent for custom keyboard window shape.
SetBackground(views::CreateSolidBackground(SK_ColorTRANSPARENT));
window()->SetTransparent(true);
}
~AshKeyboardView() override { widget_.reset(); }
void SetShadowElevation(bool show) {
::wm::SetShadowElevation(window(),
show ? kShadowElevationVirtualKeyboard : 0);
}
void Embed(base::UnguessableToken token, const gfx::Size& size) {
VLOG(1) << "Embed contents. Size: " << size.ToString();
if (!size.IsEmpty())
window()->SetBounds(gfx::Rect(size));
if (!server_remote_view_host_) {
server_remote_view_host_ = new ws::ServerRemoteViewHost(
Shell::Get()->window_service_owner()->window_service());
AddChildView(server_remote_view_host_);
scoped_observer_.Add(server_remote_view_host_->embedding_root());
}
server_remote_view_host_->EmbedUsingToken(
token, ws::mojom::kEmbedFlagEmbedderControlsVisibility,
base::BindOnce(
[](base::UnguessableToken token, bool result) {
DVLOG(1) << "Embed: " << token << ". Result: " << result;
},
token));
Layout();
}
// views::WidgetDelegateView:
const char* GetClassName() const override { return "AshKeyboardView"; }
void DeleteDelegate() override {}
// aura::WindowObserver:
void OnWindowAdded(aura::Window* new_window) override {
if (new_window->parent() == server_remote_view_host_->embedding_root())
scoped_observer_.Add(new_window);
}
void OnWillRemoveWindow(aura::Window* window) override {
if (window->parent() == server_remote_view_host_->embedding_root())
scoped_observer_.Remove(window);
}
void OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) override {
if (window == server_remote_view_host_->embedding_root())
return;
VLOG(1) << "OnWindowBoundsChanged: " << new_bounds.ToString();
// This happens when the client requests to resize the keyboard window,
// typically through window.resizeTo in JS. Ash keyboard window bounds
// should be updated to reflect the request.
gfx::Rect new_bounds_in_screen = new_bounds;
::wm::ConvertRectToScreen(window->parent(), &new_bounds_in_screen);
GetWidget()->SetBounds(new_bounds_in_screen);
}
aura::Window* window() { return GetWidget()->GetNativeView(); }
private:
void InitWidget(aura::Window* context) {
widget_ = std::make_unique<views::Widget>();
views::Widget::InitParams params(
views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.name = "AshKeyboardUI";
params.delegate = this;
params.context = context;
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget_->Init(params);
}
std::unique_ptr<views::Widget> widget_;
ws::ServerRemoteViewHost* server_remote_view_host_ = nullptr;
ScopedObserver<aura::Window, aura::WindowObserver> scoped_observer_;
DISALLOW_COPY_AND_ASSIGN(AshKeyboardView);
};
////////////////////////////////////////////////////////////////////////////////
// AshKeyboardUI
AshKeyboardUI::AshKeyboardUI(AshKeyboardController* ash_keyboard_controller)
: ash_keyboard_controller_(ash_keyboard_controller) {}
AshKeyboardUI::~AshKeyboardUI() {
ash_keyboard_controller_->SendOnKeyboardUIDestroyed();
if (ash_keyboard_view_) {
aura::Window* keyboard_window = ash_keyboard_view_->window();
if (keyboard_window)
keyboard_window->RemoveObserver(this);
}
}
// keyboard::KeyboardUI:
aura::Window* AshKeyboardUI::LoadKeyboardWindow(LoadCallback callback) {
DVLOG(1) << "LoadKeyboardWindow";
DCHECK(!ash_keyboard_view_);
load_callback_ = std::move(callback);
ash_keyboard_view_ = std::make_unique<AshKeyboardView>(
keyboard::KeyboardController::Get()->parent_container());
if (!contents_window_token_.is_empty())
EmbedContents();
aura::Window* keyboard_window = ash_keyboard_view_->window();
keyboard_window->AddObserver(this);
ash_keyboard_controller_->SendOnLoadKeyboardContentsRequested();
return keyboard_window;
}
aura::Window* AshKeyboardUI::GetKeyboardWindow() const {
return ash_keyboard_view_ ? ash_keyboard_view_->window() : nullptr;
}
ui::InputMethod* AshKeyboardUI::GetInputMethod() {
// TODO(stevenjb/shuchen): |bridge| may be null in Multi-Process Mash.
// KeyboardController needs to use the TBD Mojo based IMF services instead.
ui::IMEBridge* bridge = ui::IMEBridge::Get();
if (!bridge || !bridge->GetInputContextHandler()) {
// Needed by a handful of browser tests that use MockInputMethod.
return Shell::GetRootWindowForNewWindows()->GetHost()->GetInputMethod();
}
return bridge->GetInputContextHandler()->GetInputMethod();
}
void AshKeyboardUI::ReloadKeyboardIfNeeded() {
DVLOG(1) << "ReloadKeyboardIfNeeded";
ash_keyboard_controller_->SendOnLoadKeyboardContentsRequested();
}
void AshKeyboardUI::KeyboardContentsLoaded(const base::UnguessableToken& token,
const gfx::Size& size) {
DVLOG(1) << "KeyboardContentsLoaded. Token: " << token
<< " Size: " << size.ToString();
if (token == contents_window_token_ && size == contents_window_size_)
return;
contents_window_token_ = token;
contents_window_size_ = size;
if (ash_keyboard_view_)
EmbedContents();
}
// aura::WindowObserver:
void AshKeyboardUI::OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) {
DVLOG(1) << "OnWindowBoundsChanged: " << window->GetName() << ": "
<< new_bounds.ToString();
// Normally OnKeyboardVisibleBoundsChanged is triggered from
// keyboard::KeyboardController::NotifyKeyboardBoundsChanging, however if the
// contents window token has not been provided, an intial bounds change
// needs to be sent so that the contents can size itself.
if (contents_window_token_.is_empty())
ash_keyboard_controller_->SendOnKeyboardVisibleBoundsChanged(new_bounds);
// The ContainerType changes after ash_keyboard_view_ is created, so update
// the shadow on bounds change. TODO(stevenjb): Find a better way to do this.
SetShadowElevation();
}
void AshKeyboardUI::OnWindowDestroyed(aura::Window* window) {
window->RemoveObserver(this);
}
void AshKeyboardUI::OnWindowParentChanged(aura::Window* window,
aura::Window* parent) {
SetShadowElevation();
}
// private methods:
void AshKeyboardUI::EmbedContents() {
ash_keyboard_view_->Embed(contents_window_token_, contents_window_size_);
if (!load_callback_.is_null())
std::move(load_callback_).Run();
}
void AshKeyboardUI::SetShadowElevation() {
ash_keyboard_view_->SetShadowElevation(
ash_keyboard_controller_->keyboard_controller()
->GetActiveContainerType() ==
keyboard::mojom::ContainerType::kFullWidth);
}
} // namespace ash