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
|
// 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/virtual_keyboard_controller.h"
#include <vector>
#include "ash/common/keyboard/keyboard_ui.h"
#include "ash/common/system/tray/system_tray_notifier.h"
#include "ash/common/wm/maximize_mode/maximize_mode_controller.h"
#include "ash/common/wm_shell.h"
#include "ash/common/wm_window.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "base/command_line.h"
#include "base/strings/string_util.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_switches.h"
#include "ui/keyboard/keyboard_util.h"
namespace ash {
namespace {
// Checks whether smart deployment is enabled.
bool IsSmartVirtualKeyboardEnabled() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
keyboard::switches::kEnableVirtualKeyboard)) {
return false;
}
return keyboard::IsSmartDeployEnabled();
}
void MoveKeyboardToDisplayInternal(const int64_t display_id) {
// Remove the keyboard from curent root window controller
WmShell::Get()->keyboard_ui()->Hide();
RootWindowController::ForWindow(
keyboard::KeyboardController::GetInstance()->GetContainerWindow())
->DeactivateKeyboard(keyboard::KeyboardController::GetInstance());
for (RootWindowController* controller :
Shell::GetInstance()->GetAllRootWindowControllers()) {
if (display::Screen::GetScreen()
->GetDisplayNearestWindow(controller->GetRootWindow())
.id() == display_id) {
controller->ActivateKeyboard(keyboard::KeyboardController::GetInstance());
break;
}
}
}
void MoveKeyboardToFirstTouchableDisplay() {
// Move the keyboard to the first display with touch capability.
for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
if (display.touch_support() ==
display::Display::TouchSupport::TOUCH_SUPPORT_AVAILABLE) {
MoveKeyboardToDisplayInternal(display.id());
return;
}
}
}
} // namespace
VirtualKeyboardController::VirtualKeyboardController()
: has_external_keyboard_(false),
has_internal_keyboard_(false),
has_touchscreen_(false),
ignore_external_keyboard_(false) {
WmShell::Get()->AddShellObserver(this);
ui::InputDeviceManager::GetInstance()->AddObserver(this);
UpdateDevices();
}
VirtualKeyboardController::~VirtualKeyboardController() {
WmShell::Get()->RemoveShellObserver(this);
ui::InputDeviceManager::GetInstance()->RemoveObserver(this);
}
void VirtualKeyboardController::OnMaximizeModeStarted() {
if (!IsSmartVirtualKeyboardEnabled()) {
SetKeyboardEnabled(true);
} else {
UpdateKeyboardEnabled();
}
keyboard::SetOverscrollEnabledWithAccessibilityKeyboard(true);
}
void VirtualKeyboardController::OnMaximizeModeEnded() {
if (!IsSmartVirtualKeyboardEnabled()) {
SetKeyboardEnabled(false);
} else {
UpdateKeyboardEnabled();
}
keyboard::SetOverscrollEnabledWithAccessibilityKeyboard(false);
}
void VirtualKeyboardController::OnTouchscreenDeviceConfigurationChanged() {
UpdateDevices();
}
void VirtualKeyboardController::OnKeyboardDeviceConfigurationChanged() {
UpdateDevices();
}
void VirtualKeyboardController::ToggleIgnoreExternalKeyboard() {
ignore_external_keyboard_ = !ignore_external_keyboard_;
UpdateKeyboardEnabled();
}
void VirtualKeyboardController::MoveKeyboardToDisplay(int64_t display_id) {
DCHECK(keyboard::KeyboardController::GetInstance() != nullptr);
DCHECK(display_id != display::kInvalidDisplayId);
aura::Window* container =
keyboard::KeyboardController::GetInstance()->GetContainerWindow();
DCHECK(container != nullptr);
const display::Screen* screen = display::Screen::GetScreen();
const display::Display current_display =
screen->GetDisplayNearestWindow(container);
if (display_id != current_display.id())
MoveKeyboardToDisplayInternal(display_id);
}
void VirtualKeyboardController::MoveKeyboardToTouchableDisplay() {
DCHECK(keyboard::KeyboardController::GetInstance() != nullptr);
aura::Window* container =
keyboard::KeyboardController::GetInstance()->GetContainerWindow();
DCHECK(container != nullptr);
const display::Screen* screen = display::Screen::GetScreen();
const display::Display current_display =
screen->GetDisplayNearestWindow(container);
if (WmShell::Get()->GetFocusedWindow() != nullptr) {
// Move the virtual keyboard to the focused display if that display has
// touch capability or keyboard is locked
const display::Display focused_display =
WmShell::Get()->GetFocusedWindow()->GetDisplayNearestWindow();
if (current_display.id() != focused_display.id() &&
focused_display.id() != display::kInvalidDisplayId &&
focused_display.touch_support() ==
display::Display::TouchSupport::TOUCH_SUPPORT_AVAILABLE) {
MoveKeyboardToDisplayInternal(focused_display.id());
return;
}
}
if (current_display.touch_support() !=
display::Display::TouchSupport::TOUCH_SUPPORT_AVAILABLE) {
// The keyboard is currently on the display without touch capability.
MoveKeyboardToFirstTouchableDisplay();
}
}
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_EXTERNAL)
has_external_keyboard_ = true;
}
// Update keyboard state.
UpdateKeyboardEnabled();
}
void VirtualKeyboardController::UpdateKeyboardEnabled() {
if (!IsSmartVirtualKeyboardEnabled()) {
SetKeyboardEnabled(WmShell::Get()
->maximize_mode_controller()
->IsMaximizeModeWindowManagerEnabled());
return;
}
bool ignore_internal_keyboard = WmShell::Get()
->maximize_mode_controller()
->IsMaximizeModeWindowManagerEnabled();
bool is_internal_keyboard_active =
has_internal_keyboard_ && !ignore_internal_keyboard;
SetKeyboardEnabled(!is_internal_keyboard_active && has_touchscreen_ &&
(!has_external_keyboard_ || ignore_external_keyboard_));
WmShell::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::GetInstance()->CreateKeyboard();
} else {
Shell::GetInstance()->DeactivateKeyboard();
}
}
} // namespace ash
|