Commit a704d434 authored by Xiaoqian Dai's avatar Xiaoqian Dai Committed by Commit Bot

Only update internal event blocker when needed.

A small refactoring. After the cl, the touchpad/keyboard is only
enabled/disabled when needed.

Bug: None
Change-Id: I1d247f48f8e65cb5345153cfca78efd321ea0280
Reviewed-on: https://chromium-review.googlesource.com/c/1344146
Commit-Queue: Xiaoqian Dai <xdai@chromium.org>
Reviewed-by: default avatarMitsuru Oshima <oshima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610948}
parent cecb281b
...@@ -28,42 +28,24 @@ InternalInputDevicesEventBlocker::InternalInputDevicesEventBlocker() { ...@@ -28,42 +28,24 @@ InternalInputDevicesEventBlocker::InternalInputDevicesEventBlocker() {
InternalInputDevicesEventBlocker::~InternalInputDevicesEventBlocker() { InternalInputDevicesEventBlocker::~InternalInputDevicesEventBlocker() {
ui::InputDeviceManager::GetInstance()->RemoveObserver(this); ui::InputDeviceManager::GetInstance()->RemoveObserver(this);
if (is_blocked_) if (should_be_blocked_)
UpdateInternalInputDevices(/*should_block=*/false); UpdateInternalInputDevices(/*should_block=*/false);
} }
void InternalInputDevicesEventBlocker::OnKeyboardDeviceConfigurationChanged() { void InternalInputDevicesEventBlocker::OnKeyboardDeviceConfigurationChanged() {
UpdateInternalInputDevices(is_blocked_); UpdateInternalKeyboard(should_be_blocked_);
} }
void InternalInputDevicesEventBlocker::OnTouchpadDeviceConfigurationChanged() { void InternalInputDevicesEventBlocker::OnTouchpadDeviceConfigurationChanged() {
UpdateInternalInputDevices(is_blocked_); UpdateInternalTouchpad(should_be_blocked_);
} }
void InternalInputDevicesEventBlocker::UpdateInternalInputDevices( void InternalInputDevicesEventBlocker::UpdateInternalInputDevices(
bool should_block) { bool should_be_blocked) {
is_blocked_ = should_block; should_be_blocked_ = should_be_blocked;
// Block or unblock the internal touchpad. UpdateInternalTouchpad(should_be_blocked);
if (HasInternalTouchpad()) { UpdateInternalKeyboard(should_be_blocked);
Shell::Get()->touch_devices_controller()->SetTouchpadEnabled(
!should_block, TouchDeviceEnabledSource::GLOBAL);
}
// Block or unblock the internal keyboard. Note InputDeviceControllerClient
// may be null in tests.
if (HasInternalKeyboard() && GetInputDeviceControllerClient()) {
std::vector<ui::DomCode> allowed_keys;
if (should_block) {
// Only allow the acccessible keys present on the side of some devices to
// continue working if the internal keyboard events should be blocked.
allowed_keys.push_back(ui::DomCode::VOLUME_DOWN);
allowed_keys.push_back(ui::DomCode::VOLUME_UP);
allowed_keys.push_back(ui::DomCode::POWER);
}
GetInputDeviceControllerClient()->SetInternalKeyboardFilter(should_block,
allowed_keys);
}
} }
bool InternalInputDevicesEventBlocker::HasInternalTouchpad() { bool InternalInputDevicesEventBlocker::HasInternalTouchpad() {
...@@ -84,4 +66,38 @@ bool InternalInputDevicesEventBlocker::HasInternalKeyboard() { ...@@ -84,4 +66,38 @@ bool InternalInputDevicesEventBlocker::HasInternalKeyboard() {
return false; return false;
} }
void InternalInputDevicesEventBlocker::UpdateInternalTouchpad(
bool should_be_blocked) {
if (should_be_blocked == is_touchpad_blocked_)
return;
if (HasInternalTouchpad()) {
Shell::Get()->touch_devices_controller()->SetTouchpadEnabled(
!should_be_blocked, TouchDeviceEnabledSource::GLOBAL);
is_touchpad_blocked_ = should_be_blocked;
}
}
void InternalInputDevicesEventBlocker::UpdateInternalKeyboard(
bool should_be_blocked) {
if (should_be_blocked == is_keyboard_blocked_)
return;
// Block or unblock the internal keyboard. Note InputDeviceControllerClient
// may be null in tests.
if (HasInternalKeyboard() && GetInputDeviceControllerClient()) {
std::vector<ui::DomCode> allowed_keys;
if (should_be_blocked) {
// Only allow the acccessible keys present on the side of some devices to
// continue working if the internal keyboard events should be blocked.
allowed_keys.push_back(ui::DomCode::VOLUME_DOWN);
allowed_keys.push_back(ui::DomCode::VOLUME_UP);
allowed_keys.push_back(ui::DomCode::POWER);
}
GetInputDeviceControllerClient()->SetInternalKeyboardFilter(
should_be_blocked, allowed_keys);
is_keyboard_blocked_ = should_be_blocked;
}
}
} // namespace ash } // namespace ash
...@@ -24,13 +24,26 @@ class ASH_EXPORT InternalInputDevicesEventBlocker ...@@ -24,13 +24,26 @@ class ASH_EXPORT InternalInputDevicesEventBlocker
void UpdateInternalInputDevices(bool should_block); void UpdateInternalInputDevices(bool should_block);
bool is_blocked() const { return is_blocked_; } bool should_be_blocked() const { return should_be_blocked_; }
private: private:
bool HasInternalTouchpad(); bool HasInternalTouchpad();
bool HasInternalKeyboard(); bool HasInternalKeyboard();
bool is_blocked_ = false; void UpdateInternalTouchpad(bool should_block);
void UpdateInternalKeyboard(bool should_block);
// |should_be_blocked_| might not be equal to (|is_touchpad_blocked_| &&
// |is_keyboard_blocked_|) as when UpdateInternalInputDevices() is called,
// |should_be_blocked_| is guranteed to be updated, but |is_touchpad_blocked_|
// or |is_keyboard_blocked_| might not be updated because there might not be
// an internal touchpad or keyboard at that moment (currently it can only be
// the case for Whiskers keyboard, which is a detachable keyboard but is
// regarded as the internal keyboard for Moewth).
bool should_be_blocked_ = false;
bool is_touchpad_blocked_ = false;
bool is_keyboard_blocked_ = false;
DISALLOW_COPY_AND_ASSIGN(InternalInputDevicesEventBlocker); DISALLOW_COPY_AND_ASSIGN(InternalInputDevicesEventBlocker);
}; };
......
...@@ -241,7 +241,7 @@ bool TabletModeController::ShouldAutoHideTitlebars(views::Widget* widget) { ...@@ -241,7 +241,7 @@ bool TabletModeController::ShouldAutoHideTitlebars(views::Widget* widget) {
} }
bool TabletModeController::AreInternalInputDeviceEventsBlocked() const { bool TabletModeController::AreInternalInputDeviceEventsBlocked() const {
return event_blocker_->is_blocked(); return event_blocker_->should_be_blocked();
} }
void TabletModeController::FlushForTesting() { void TabletModeController::FlushForTesting() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment