forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch_devices_controller.cc
203 lines (170 loc) · 6.56 KB
/
touch_devices_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
// Copyright 2017 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/touch/touch_devices_controller.h"
#include <utility>
#include "ash/public/cpp/ash_pref_names.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/metrics/histogram_macros.h"
#include "chromeos/constants/chromeos_switches.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/ozone/public/input_controller.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/wm/core/cursor_manager.h"
namespace ash {
namespace {
PrefService* GetActivePrefService() {
return Shell::Get()->session_controller()->GetActivePrefService();
}
} // namespace
// static
void TouchDevicesController::RegisterProfilePrefs(PrefRegistrySimple* registry,
bool for_test) {
registry->RegisterBooleanPref(
prefs::kTapDraggingEnabled, false,
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF);
registry->RegisterBooleanPref(prefs::kTouchpadEnabled, true);
registry->RegisterBooleanPref(prefs::kTouchscreenEnabled, true);
if (for_test) {
registry->RegisterBooleanPref(
prefs::kNaturalScroll,
base::CommandLine::ForCurrentProcess()->HasSwitch(
chromeos::switches::kNaturalScrollDefault),
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF);
}
}
TouchDevicesController::TouchDevicesController() {
Shell::Get()->session_controller()->AddObserver(this);
}
TouchDevicesController::~TouchDevicesController() {
Shell::Get()->session_controller()->RemoveObserver(this);
}
void TouchDevicesController::ToggleTouchpad() {
PrefService* prefs = GetActivePrefService();
if (!prefs)
return;
const bool touchpad_enabled = prefs->GetBoolean(prefs::kTouchpadEnabled);
prefs->SetBoolean(prefs::kTouchpadEnabled, !touchpad_enabled);
}
bool TouchDevicesController::GetTouchpadEnabled(
TouchDeviceEnabledSource source) const {
if (source == TouchDeviceEnabledSource::GLOBAL)
return global_touchpad_enabled_;
PrefService* prefs = GetActivePrefService();
return prefs && prefs->GetBoolean(prefs::kTouchpadEnabled);
}
void TouchDevicesController::SetTouchpadEnabled(
bool enabled,
TouchDeviceEnabledSource source) {
if (source == TouchDeviceEnabledSource::GLOBAL) {
global_touchpad_enabled_ = enabled;
UpdateTouchpadEnabled();
return;
}
PrefService* prefs = GetActivePrefService();
if (!prefs)
return;
prefs->SetBoolean(prefs::kTouchpadEnabled, enabled);
}
bool TouchDevicesController::GetTouchscreenEnabled(
TouchDeviceEnabledSource source) const {
if (source == TouchDeviceEnabledSource::GLOBAL)
return global_touchscreen_enabled_;
PrefService* prefs = GetActivePrefService();
return prefs && prefs->GetBoolean(prefs::kTouchscreenEnabled);
}
void TouchDevicesController::SetTouchscreenEnabled(
bool enabled,
TouchDeviceEnabledSource source) {
if (source == TouchDeviceEnabledSource::GLOBAL) {
global_touchscreen_enabled_ = enabled;
// Explicitly call |UpdateTouchscreenEnabled()| to update the actual
// touchscreen state from multiple sources.
UpdateTouchscreenEnabled();
return;
}
PrefService* prefs = GetActivePrefService();
if (!prefs)
return;
prefs->SetBoolean(prefs::kTouchscreenEnabled, enabled);
}
void TouchDevicesController::OnUserSessionAdded(const AccountId& account_id) {
uma_record_callback_ = base::BindOnce([](PrefService* prefs) {
UMA_HISTOGRAM_BOOLEAN("Touchpad.TapDragging.Started",
prefs->GetBoolean(prefs::kTapDraggingEnabled));
});
}
void TouchDevicesController::OnSigninScreenPrefServiceInitialized(
PrefService* prefs) {
ObservePrefs(prefs);
}
void TouchDevicesController::OnActiveUserPrefServiceChanged(
PrefService* prefs) {
if (uma_record_callback_)
std::move(uma_record_callback_).Run(prefs);
ObservePrefs(prefs);
}
void TouchDevicesController::ObservePrefs(PrefService* prefs) {
// Watch for pref updates.
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
pref_change_registrar_->Init(prefs);
pref_change_registrar_->Add(
prefs::kTapDraggingEnabled,
base::BindRepeating(&TouchDevicesController::UpdateTapDraggingEnabled,
base::Unretained(this)));
pref_change_registrar_->Add(
prefs::kTouchpadEnabled,
base::BindRepeating(&TouchDevicesController::UpdateTouchpadEnabled,
base::Unretained(this)));
pref_change_registrar_->Add(
prefs::kTouchscreenEnabled,
base::BindRepeating(&TouchDevicesController::UpdateTouchscreenEnabled,
base::Unretained(this)));
// Load current state.
UpdateTapDraggingEnabled();
UpdateTouchpadEnabled();
UpdateTouchscreenEnabled();
}
void TouchDevicesController::UpdateTapDraggingEnabled() {
PrefService* prefs = GetActivePrefService();
const bool enabled = prefs->GetBoolean(prefs::kTapDraggingEnabled);
if (tap_dragging_enabled_ == enabled)
return;
tap_dragging_enabled_ = enabled;
UMA_HISTOGRAM_BOOLEAN("Touchpad.TapDragging.Changed", enabled);
ui::OzonePlatform::GetInstance()->GetInputController()->SetTapDragging(
enabled);
}
void TouchDevicesController::UpdateTouchpadEnabled() {
bool enabled = GetTouchpadEnabled(TouchDeviceEnabledSource::GLOBAL) &&
GetTouchpadEnabled(TouchDeviceEnabledSource::USER_PREF);
ui::InputController* input_controller =
ui::OzonePlatform::GetInstance()->GetInputController();
const bool old_value = input_controller->IsInternalTouchpadEnabled();
input_controller->SetInternalTouchpadEnabled(enabled);
if (old_value == input_controller->IsInternalTouchpadEnabled())
return; // Value didn't actually change.
::wm::CursorManager* cursor_manager = Shell::Get()->cursor_manager();
if (!cursor_manager)
return;
if (enabled)
cursor_manager->ShowCursor();
else
cursor_manager->HideCursor();
}
void TouchDevicesController::UpdateTouchscreenEnabled() {
ui::OzonePlatform::GetInstance()
->GetInputController()
->SetTouchscreensEnabled(
GetTouchscreenEnabled(TouchDeviceEnabledSource::GLOBAL) &&
GetTouchscreenEnabled(TouchDeviceEnabledSource::USER_PREF));
}
} // namespace ash