Roll src/third_party/WebKit f9e4789:c8f1b7f (svn 202029:202030)
[chromium-blink-merge.git] / ash / virtual_keyboard_controller.cc
blob60254092b170b84e08a41aab3293084306ec1108
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/virtual_keyboard_controller.h"
7 #include <vector>
9 #include "ash/shell.h"
10 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
11 #include "ash_switches.h"
12 #include "base/command_line.h"
13 #include "base/strings/string_util.h"
14 #include "ui/events/devices/device_data_manager.h"
15 #include "ui/events/devices/input_device.h"
16 #include "ui/events/devices/keyboard_device.h"
17 #include "ui/events/devices/touchscreen_device.h"
18 #include "ui/gfx/x/x11_types.h"
19 #include "ui/keyboard/keyboard_switches.h"
20 #include "ui/keyboard/keyboard_util.h"
22 namespace ash {
23 namespace {
25 // Checks whether smart deployment is enabled.
26 bool IsSmartVirtualKeyboardEnabled() {
27 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
28 keyboard::switches::kEnableVirtualKeyboard)) {
29 return false;
31 return keyboard::IsSmartDeployEnabled();
34 } // namespace
36 VirtualKeyboardController::VirtualKeyboardController()
37 : has_external_keyboard_(false),
38 has_internal_keyboard_(false),
39 has_touchscreen_(false),
40 ignore_external_keyboard_(false) {
41 Shell::GetInstance()->AddShellObserver(this);
42 ui::DeviceDataManager::GetInstance()->AddObserver(this);
43 UpdateDevices();
46 VirtualKeyboardController::~VirtualKeyboardController() {
47 Shell::GetInstance()->RemoveShellObserver(this);
48 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
51 void VirtualKeyboardController::OnMaximizeModeStarted() {
52 if (!IsSmartVirtualKeyboardEnabled()) {
53 SetKeyboardEnabled(true);
54 } else {
55 UpdateKeyboardEnabled();
59 void VirtualKeyboardController::OnMaximizeModeEnded() {
60 if (!IsSmartVirtualKeyboardEnabled()) {
61 SetKeyboardEnabled(false);
62 } else {
63 UpdateKeyboardEnabled();
67 void VirtualKeyboardController::OnTouchscreenDeviceConfigurationChanged() {
68 UpdateDevices();
71 void VirtualKeyboardController::OnKeyboardDeviceConfigurationChanged() {
72 UpdateDevices();
75 void VirtualKeyboardController::ToggleIgnoreExternalKeyboard() {
76 ignore_external_keyboard_ = !ignore_external_keyboard_;
77 UpdateKeyboardEnabled();
80 void VirtualKeyboardController::UpdateDevices() {
81 ui::DeviceDataManager* device_data_manager =
82 ui::DeviceDataManager::GetInstance();
84 // Checks for touchscreens.
85 has_touchscreen_ = device_data_manager->touchscreen_devices().size() > 0;
87 // Checks for keyboards.
88 has_external_keyboard_ = false;
89 has_internal_keyboard_ = false;
90 for (const ui::KeyboardDevice& device :
91 device_data_manager->keyboard_devices()) {
92 if (has_internal_keyboard_ && has_external_keyboard_)
93 break;
94 ui::InputDeviceType type = device.type;
95 if (type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL)
96 has_internal_keyboard_ = true;
97 if (type == ui::InputDeviceType::INPUT_DEVICE_EXTERNAL)
98 has_external_keyboard_ = true;
100 // Update keyboard state.
101 UpdateKeyboardEnabled();
104 void VirtualKeyboardController::UpdateKeyboardEnabled() {
105 if (!IsSmartVirtualKeyboardEnabled()) {
106 SetKeyboardEnabled(Shell::GetInstance()
107 ->maximize_mode_controller()
108 ->IsMaximizeModeWindowManagerEnabled());
109 return;
111 bool ignore_internal_keyboard = Shell::GetInstance()
112 ->maximize_mode_controller()
113 ->IsMaximizeModeWindowManagerEnabled();
114 bool is_internal_keyboard_active =
115 has_internal_keyboard_ && !ignore_internal_keyboard;
116 SetKeyboardEnabled(!is_internal_keyboard_active && has_touchscreen_ &&
117 (!has_external_keyboard_ || ignore_external_keyboard_));
118 ash::Shell::GetInstance()
119 ->system_tray_notifier()
120 ->NotifyVirtualKeyboardSuppressionChanged(!is_internal_keyboard_active &&
121 has_touchscreen_ &&
122 has_external_keyboard_);
125 void VirtualKeyboardController::SetKeyboardEnabled(bool enabled) {
126 bool was_enabled = keyboard::IsKeyboardEnabled();
127 keyboard::SetTouchKeyboardEnabled(enabled);
128 bool is_enabled = keyboard::IsKeyboardEnabled();
129 if (is_enabled == was_enabled)
130 return;
131 if (is_enabled) {
132 Shell::GetInstance()->CreateKeyboard();
133 } else {
134 Shell::GetInstance()->DeactivateKeyboard();
138 } // namespace ash