Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1543)

Unified Diff: ash/wm/power_button_controller.cc

Issue 2474913004: Tablet-like power button behavior on Convertible/Tablet ChromeOS devices (Closed)
Patch Set: cr based on comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ash/wm/power_button_controller.cc
diff --git a/ash/wm/power_button_controller.cc b/ash/wm/power_button_controller.cc
index 92ca9c12aabcd64eeb6996e10f6ab273b57e803c..e53965aa4f073bb217d875db826010bf46a890c7 100644
--- a/ash/wm/power_button_controller.cc
+++ b/ash/wm/power_button_controller.cc
@@ -28,6 +28,28 @@
namespace ash {
+namespace {
+
+// Amount of time the power button must be held to start the pre-shutdown
+// animation when in tablet mode.
+constexpr int kTabletShutdownTimeoutMs = 1000;
+
+} // namespace
+
+PowerButtonController::TestApi::TestApi(PowerButtonController* controller)
+ : controller_(controller) {}
+
+PowerButtonController::TestApi::~TestApi() {}
+
+bool PowerButtonController::TestApi::TabletPowerButtonTimerIsRunning() const {
+ return controller_->tablet_power_button_timer_.IsRunning();
+}
+
+void PowerButtonController::TestApi::TriggerTabletPowerButtonTimeout() {
+ controller_->OnTabletPowerButtonTimeout();
+ controller_->tablet_power_button_timer_.Stop();
+}
+
PowerButtonController::PowerButtonController(LockStateController* controller)
: power_button_down_(false),
lock_button_down_(false),
@@ -46,13 +68,17 @@ PowerButtonController::PowerButtonController(LockStateController* controller)
#else
enable_quick_lock_(false),
#endif
- controller_(controller) {
+ controller_(controller),
+ power_button_down_while_screen_off_(false),
+ backlights_forced_off_(false) {
#if defined(OS_CHROMEOS)
chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
this);
Shell::GetInstance()->display_configurator()->AddObserver(this);
#endif
Shell::GetInstance()->PrependPreTargetHandler(this);
+
+ GetInitialBacklightsForcedOff();
}
PowerButtonController::~PowerButtonController() {
@@ -76,16 +102,25 @@ void PowerButtonController::OnPowerButtonEvent(
if (controller_->ShutdownRequested())
return;
+ bool should_take_screenshot =
+ down && volume_down_pressed_ && IsTabletModeActive();
+
+ if (down)
+ power_button_down_while_screen_off_ = brightness_is_zero_;
+
+ if (!has_legacy_power_button_ && !should_take_screenshot &&
+ IsTabletModeSupported()) {
+ OnTabletPowerButtonEvent(down, timestamp);
+ return;
+ }
+
// Avoid starting the lock/shutdown sequence if the power button is pressed
// while the screen is off (http://crbug.com/128451), unless an external
// display is still on (http://crosbug.com/p/24912).
if (brightness_is_zero_ && !internal_display_off_and_external_display_on_)
return;
- if (volume_down_pressed_ && down &&
- WmShell::Get()
- ->maximize_mode_controller()
- ->IsMaximizeModeWindowManagerEnabled()) {
+ if (should_take_screenshot) {
SystemTray* system_tray = Shell::GetInstance()->GetPrimarySystemTray();
if (system_tray && system_tray->GetTrayAudio())
system_tray->GetTrayAudio()->HideDetailedView(false);
@@ -127,10 +162,7 @@ void PowerButtonController::OnPowerButtonEvent(
if (session_state_delegate->CanLockScreen() &&
!session_state_delegate->IsUserSessionBlocked()) {
- if (WmShell::Get()
- ->maximize_mode_controller()
- ->IsMaximizeModeWindowManagerEnabled() &&
- enable_quick_lock_)
+ if (IsTabletModeActive() && enable_quick_lock_)
controller_->StartLockAnimationAndLockImmediately(true);
else
controller_->StartLockAnimation(true);
@@ -170,6 +202,11 @@ void PowerButtonController::OnLockButtonEvent(
}
void PowerButtonController::OnKeyEvent(ui::KeyEvent* event) {
+ if (!IsTabletModeActive() && backlights_forced_off_) {
+ SetBacklightsForcedOff(false);
+ return;
+ }
+
if (event->key_code() == ui::VKEY_VOLUME_DOWN) {
volume_down_pressed_ = event->type() == ui::ET_KEY_PRESSED;
#if defined(OS_CHROMEOS)
@@ -183,6 +220,12 @@ void PowerButtonController::OnKeyEvent(ui::KeyEvent* event) {
}
}
+void PowerButtonController::OnMouseEvent(ui::MouseEvent* event) {
+ if (!IsTabletModeActive() && backlights_forced_off_) {
+ SetBacklightsForcedOff(false);
+ }
+}
+
#if defined(OS_CHROMEOS)
void PowerButtonController::OnDisplayModeChanged(
const ui::DisplayConfigurator::DisplayStateList& display_states) {
@@ -207,4 +250,89 @@ void PowerButtonController::PowerButtonEventReceived(
}
#endif // defined(OS_CHROMEOS)
+void PowerButtonController::OnTabletPowerButtonEvent(
Daniel Erat 2016/11/04 20:18:34 hmm... so i've noticed that the tablet power butto
Qiang(Joe) Xu 2016/11/04 23:18:21 Actually there are some overlaps. Like TabletPower
+ bool down,
+ const base::TimeTicks& timestamp) {
+ if (down) {
+ SetBacklightsForcedOff(false);
+ StartTabletPowerButtonTimer();
+ } else {
+ if (tablet_power_button_timer_.IsRunning()) {
+ tablet_power_button_timer_.Stop();
+ if (!power_button_down_while_screen_off_) {
+ SetBacklightsForcedOff(true);
+ LockScreenIfRequired();
+ }
+ }
+ power_button_down_while_screen_off_ = false;
+
+ // When power button is released, Cancel Shutdown animation whenever it is
+ // still cancellable.
+ if (controller_->CanCancelShutdownAnimation())
+ controller_->CancelShutdownAnimation();
+ }
+}
+
+void PowerButtonController::SetBacklightsForcedOff(bool forced_off) {
+ if (backlights_forced_off_ == forced_off)
+ return;
+
+#if defined(OS_CHROMEOS)
+ chromeos::DBusThreadManager::Get()
+ ->GetPowerManagerClient()
+ ->SetBacklightsForcedOff(forced_off);
+ backlights_forced_off_ = forced_off;
+#endif
+}
+
+void PowerButtonController::GetInitialBacklightsForcedOff() {
+#if defined(OS_CHROMEOS)
+ chromeos::DBusThreadManager::Get()
+ ->GetPowerManagerClient()
+ ->GetBacklightsForcedOff(
+ base::Bind(&PowerButtonController::OnGotInitialBacklightsForcedOff,
+ base::Unretained(this)));
+#endif
+}
+
+void PowerButtonController::OnGotInitialBacklightsForcedOff(
+ bool is_forced_off) {
+ backlights_forced_off_ = is_forced_off;
+}
+
+void PowerButtonController::StartTabletPowerButtonTimer() {
+ tablet_power_button_timer_.Start(
+ FROM_HERE, base::TimeDelta::FromMilliseconds(kTabletShutdownTimeoutMs),
+ this, &PowerButtonController::OnTabletPowerButtonTimeout);
+}
+
+void PowerButtonController::OnTabletPowerButtonTimeout() {
+ controller_->StartShutdownAnimation();
+}
+
+void PowerButtonController::LockScreenIfRequired() {
+ SessionStateDelegate* session_state_delegate =
+ WmShell::Get()->GetSessionStateDelegate();
+ if (session_state_delegate->ShouldLockScreenAutomatically() &&
+ session_state_delegate->CanLockScreen() &&
+ !session_state_delegate->IsUserSessionBlocked() &&
+ !controller_->LockRequested()) {
+ session_state_delegate->LockScreen();
+ }
+}
+
+bool PowerButtonController::IsTabletModeSupported() const {
+ MaximizeModeController* maximize_mode_controller =
+ WmShell::Get()->maximize_mode_controller();
+ return maximize_mode_controller &&
+ maximize_mode_controller->CanEnterMaximizeMode();
+}
+
+bool PowerButtonController::IsTabletModeActive() const {
+ MaximizeModeController* maximize_mode_controller =
+ WmShell::Get()->maximize_mode_controller();
+ return maximize_mode_controller &&
+ maximize_mode_controller->IsMaximizeModeWindowManagerEnabled();
+}
+
} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698