Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / ash / virtual_keyboard_controller.cc
blob2f46e5cdac97fe733c388e375ff4b2b7c09cadec
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 !base::CommandLine::ForCurrentProcess()->HasSwitch(
32 keyboard::switches::kDisableSmartVirtualKeyboard);
35 } // namespace
37 VirtualKeyboardController::VirtualKeyboardController()
38 : has_external_keyboard_(false),
39 has_internal_keyboard_(false),
40 has_touchscreen_(false),
41 ignore_external_keyboard_(false) {
42 Shell::GetInstance()->AddShellObserver(this);
43 ui::DeviceDataManager::GetInstance()->AddObserver(this);
44 UpdateDevices();
47 VirtualKeyboardController::~VirtualKeyboardController() {
48 Shell::GetInstance()->RemoveShellObserver(this);
49 ui::DeviceDataManager::GetInstance()->RemoveObserver(this);
52 void VirtualKeyboardController::OnMaximizeModeStarted() {
53 if (!IsSmartVirtualKeyboardEnabled()) {
54 SetKeyboardEnabled(true);
55 } else {
56 UpdateKeyboardEnabled();
60 void VirtualKeyboardController::OnMaximizeModeEnded() {
61 if (!IsSmartVirtualKeyboardEnabled()) {
62 SetKeyboardEnabled(false);
63 } else {
64 UpdateKeyboardEnabled();
68 void VirtualKeyboardController::OnTouchscreenDeviceConfigurationChanged() {
69 UpdateDevices();
72 void VirtualKeyboardController::OnKeyboardDeviceConfigurationChanged() {
73 UpdateDevices();
76 void VirtualKeyboardController::ToggleIgnoreExternalKeyboard() {
77 ignore_external_keyboard_ = !ignore_external_keyboard_;
78 UpdateKeyboardEnabled();
81 void VirtualKeyboardController::UpdateDevices() {
82 ui::DeviceDataManager* device_data_manager =
83 ui::DeviceDataManager::GetInstance();
85 // Checks for touchscreens.
86 has_touchscreen_ = device_data_manager->touchscreen_devices().size() > 0;
88 // Checks for keyboards.
89 has_external_keyboard_ = false;
90 has_internal_keyboard_ = false;
91 for (const ui::KeyboardDevice& device :
92 device_data_manager->keyboard_devices()) {
93 if (has_internal_keyboard_ && has_external_keyboard_)
94 break;
95 ui::InputDeviceType type = device.type;
96 if (type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL)
97 has_internal_keyboard_ = true;
98 if (type == ui::InputDeviceType::INPUT_DEVICE_EXTERNAL)
99 has_external_keyboard_ = true;
101 // Update keyboard state.
102 UpdateKeyboardEnabled();
105 void VirtualKeyboardController::UpdateKeyboardEnabled() {
106 if (!IsSmartVirtualKeyboardEnabled()) {
107 SetKeyboardEnabled(Shell::GetInstance()
108 ->maximize_mode_controller()
109 ->IsMaximizeModeWindowManagerEnabled());
110 return;
112 bool ignore_internal_keyboard = Shell::GetInstance()
113 ->maximize_mode_controller()
114 ->IsMaximizeModeWindowManagerEnabled();
115 bool is_internal_keyboard_active =
116 has_internal_keyboard_ && !ignore_internal_keyboard;
117 SetKeyboardEnabled(!is_internal_keyboard_active && has_touchscreen_ &&
118 (!has_external_keyboard_ || ignore_external_keyboard_));
119 ash::Shell::GetInstance()
120 ->system_tray_notifier()
121 ->NotifyVirtualKeyboardSuppressionChanged(!is_internal_keyboard_active &&
122 has_touchscreen_ &&
123 has_external_keyboard_);
126 void VirtualKeyboardController::SetKeyboardEnabled(bool enabled) {
127 keyboard::SetTouchKeyboardEnabled(enabled);
128 if (enabled) {
129 Shell::GetInstance()->CreateKeyboard();
130 } else {
131 if (!keyboard::IsKeyboardEnabled())
132 Shell::GetInstance()->DeactivateKeyboard();
136 } // namespace ash