forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual_keyboard_controller.cc
275 lines (236 loc) · 9.74 KB
/
virtual_keyboard_controller.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
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
// Copyright 2014 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/virtual_keyboard_controller.h"
#include <vector>
#include "ash/accessibility/accessibility_controller.h"
#include "ash/ime/ime_controller.h"
#include "ash/keyboard/ash_keyboard_controller.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller.h"
#include "ash/shell.h"
#include "ash/system/tray/system_tray_notifier.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/window_util.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "ui/base/emoji/emoji_panel_helper.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/devices/input_device.h"
#include "ui/events/devices/input_device_manager.h"
#include "ui/events/devices/touchscreen_device.h"
#include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_util.h"
#include "ui/keyboard/public/keyboard_switches.h"
namespace ash {
namespace {
// Checks if virtual keyboard is force-enabled by enable-virtual-keyboard flag.
bool IsVirtualKeyboardEnabled() {
return base::CommandLine::ForCurrentProcess()->HasSwitch(
keyboard::switches::kEnableVirtualKeyboard);
}
void ResetVirtualKeyboard() {
keyboard::SetKeyboardEnabledFromShelf(false);
if (!keyboard::IsKeyboardEnabled())
Shell::Get()->DisableKeyboard();
// Reset the keyset after disabling the virtual keyboard to prevent the IME
// extension from accidentally loading the default keyset while it's shutting
// down. See https://crbug.com/875456.
Shell::Get()->ime_controller()->OverrideKeyboardKeyset(
chromeos::input_method::mojom::ImeKeyset::kNone);
}
bool HasTouchableDisplay() {
for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
if (display.touch_support() == display::Display::TouchSupport::AVAILABLE)
return true;
}
return false;
}
} // namespace
VirtualKeyboardController::VirtualKeyboardController()
: has_external_keyboard_(false),
has_internal_keyboard_(false),
has_touchscreen_(false),
ignore_external_keyboard_(false) {
Shell::Get()->tablet_mode_controller()->AddObserver(this);
Shell::Get()->session_controller()->AddObserver(this);
ui::InputDeviceManager::GetInstance()->AddObserver(this);
UpdateDevices();
// Set callback to show the emoji panel
ui::SetShowEmojiKeyboardCallback(base::BindRepeating(
&VirtualKeyboardController::ForceShowKeyboardWithKeyset,
base::Unretained(this),
chromeos::input_method::mojom::ImeKeyset::kEmoji));
keyboard::KeyboardController::Get()->AddObserver(this);
bluetooth_devices_observer_ =
std::make_unique<BluetoothDevicesObserver>(base::BindRepeating(
&VirtualKeyboardController::OnBluetoothAdapterOrDeviceChanged,
base::Unretained(this)));
}
VirtualKeyboardController::~VirtualKeyboardController() {
keyboard::KeyboardController::Get()->RemoveObserver(this);
if (Shell::Get()->tablet_mode_controller())
Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
if (Shell::Get()->session_controller())
Shell::Get()->session_controller()->RemoveObserver(this);
ui::InputDeviceManager::GetInstance()->RemoveObserver(this);
// Reset the emoji panel callback
ui::SetShowEmojiKeyboardCallback(base::DoNothing());
}
void VirtualKeyboardController::ForceShowKeyboardWithKeyset(
chromeos::input_method::mojom::ImeKeyset keyset) {
Shell::Get()->ime_controller()->OverrideKeyboardKeyset(
keyset, base::BindOnce(&VirtualKeyboardController::ForceShowKeyboard,
base::Unretained(this)));
}
void VirtualKeyboardController::OnTabletModeEventsBlockingChanged() {
UpdateKeyboardEnabled();
}
void VirtualKeyboardController::OnInputDeviceConfigurationChanged(
uint8_t input_device_types) {
if (input_device_types & (ui::InputDeviceEventObserver::kKeyboard |
ui::InputDeviceEventObserver::kTouchscreen)) {
UpdateDevices();
}
}
void VirtualKeyboardController::ToggleIgnoreExternalKeyboard() {
ignore_external_keyboard_ = !ignore_external_keyboard_;
UpdateKeyboardEnabled();
}
aura::Window* VirtualKeyboardController::GetContainerForDisplay(
const display::Display& display) {
DCHECK(display.is_valid());
RootWindowController* controller =
Shell::Get()->GetRootWindowControllerWithDisplayId(display.id());
aura::Window* container =
controller->GetContainer(kShellWindowId_VirtualKeyboardContainer);
DCHECK(container);
return container;
}
aura::Window* VirtualKeyboardController::GetContainerForDefaultDisplay() {
const display::Screen* screen = display::Screen::GetScreen();
if (wm::GetFocusedWindow()) {
// Return the focused display if that display has touch capability or no
// other display has touch capability.
const display::Display focused_display =
screen->GetDisplayNearestWindow(wm::GetFocusedWindow());
if (focused_display.is_valid() &&
(focused_display.touch_support() ==
display::Display::TouchSupport::AVAILABLE ||
!HasTouchableDisplay())) {
return GetContainerForDisplay(focused_display);
}
}
// Otherwise, get the first touchable display.
for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
if (display.touch_support() == display::Display::TouchSupport::AVAILABLE)
return GetContainerForDisplay(display);
}
// If there are no touchable displays, then just return the primary display.
return GetContainerForDisplay(screen->GetPrimaryDisplay());
}
void VirtualKeyboardController::UpdateDevices() {
ui::InputDeviceManager* device_data_manager =
ui::InputDeviceManager::GetInstance();
// Checks for touchscreens.
has_touchscreen_ = device_data_manager->GetTouchscreenDevices().size() > 0;
// Checks for keyboards.
has_external_keyboard_ = false;
has_internal_keyboard_ = false;
for (const ui::InputDevice& device :
device_data_manager->GetKeyboardDevices()) {
if (has_internal_keyboard_ && has_external_keyboard_)
break;
ui::InputDeviceType type = device.type;
if (type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL)
has_internal_keyboard_ = true;
if (type == ui::InputDeviceType::INPUT_DEVICE_USB ||
(type == ui::InputDeviceType::INPUT_DEVICE_BLUETOOTH &&
bluetooth_devices_observer_->IsConnectedBluetoothDevice(device))) {
has_external_keyboard_ = true;
}
}
// Update keyboard state.
UpdateKeyboardEnabled();
}
void VirtualKeyboardController::UpdateKeyboardEnabled() {
if (IsVirtualKeyboardEnabled()) {
SetKeyboardEnabled(Shell::Get()
->tablet_mode_controller()
->AreInternalInputDeviceEventsBlocked());
return;
}
bool ignore_internal_keyboard = Shell::Get()
->tablet_mode_controller()
->AreInternalInputDeviceEventsBlocked();
bool is_internal_keyboard_active =
has_internal_keyboard_ && !ignore_internal_keyboard;
SetKeyboardEnabled(!is_internal_keyboard_active && has_touchscreen_ &&
(!has_external_keyboard_ || ignore_external_keyboard_));
Shell::Get()->system_tray_notifier()->NotifyVirtualKeyboardSuppressionChanged(
!is_internal_keyboard_active && has_touchscreen_ &&
has_external_keyboard_);
}
void VirtualKeyboardController::SetKeyboardEnabled(bool enabled) {
bool was_enabled = keyboard::IsKeyboardEnabled();
keyboard::SetTouchKeyboardEnabled(enabled);
bool is_enabled = keyboard::IsKeyboardEnabled();
if (is_enabled == was_enabled)
return;
if (is_enabled) {
Shell::Get()->EnableKeyboard();
} else {
Shell::Get()->DisableKeyboard();
}
}
void VirtualKeyboardController::ForceShowKeyboard() {
// If the virtual keyboard is enabled, show the keyboard directly.
auto* keyboard_controller = keyboard::KeyboardController::Get();
if (keyboard_controller->IsEnabled()) {
keyboard_controller->ShowKeyboard(false /* locked */);
return;
}
// Otherwise, temporarily enable the virtual keyboard until it is dismissed.
DCHECK(!keyboard::GetKeyboardEnabledFromShelf());
keyboard::SetKeyboardEnabledFromShelf(true);
Shell::Get()->EnableKeyboard();
keyboard_controller->ShowKeyboard(false);
}
void VirtualKeyboardController::OnKeyboardEnabledChanged(bool is_enabled) {
if (!is_enabled) {
// TODO(shend/shuchen): Consider moving this logic to ImeController.
// https://crbug.com/896284.
Shell::Get()->ime_controller()->OverrideKeyboardKeyset(
chromeos::input_method::mojom::ImeKeyset::kNone);
}
}
void VirtualKeyboardController::OnKeyboardHidden(bool is_temporary_hide) {
// The keyboard may temporarily hide (e.g. to change container behaviors).
// The keyset should not be reset in this case.
if (is_temporary_hide)
return;
// Post a task to reset the virtual keyboard to its original state.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(ResetVirtualKeyboard));
}
void VirtualKeyboardController::OnActiveUserSessionChanged(
const AccountId& account_id) {
// Force on-screen keyboard to reset.
if (keyboard::IsKeyboardEnabled())
Shell::Get()->EnableKeyboard();
}
void VirtualKeyboardController::OnBluetoothAdapterOrDeviceChanged(
device::BluetoothDevice* device) {
// We only care about keyboard type bluetooth device change.
if (!device ||
device->GetDeviceType() == device::BluetoothDeviceType::KEYBOARD ||
device->GetDeviceType() ==
device::BluetoothDeviceType::KEYBOARD_MOUSE_COMBO) {
UpdateDevices();
}
}
} // namespace ash