power_button_screenshot_controller.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2017 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.
  4. #ifndef ASH_SYSTEM_POWER_POWER_BUTTON_SCREENSHOT_CONTROLLER_H_
  5. #define ASH_SYSTEM_POWER_POWER_BUTTON_SCREENSHOT_CONTROLLER_H_
  6. #include "ash/ash_export.h"
  7. #include "base/time/time.h"
  8. #include "base/timer/timer.h"
  9. #include "ui/base/accelerators/accelerator.h"
  10. #include "ui/events/event_handler.h"
  11. namespace base {
  12. class TickClock;
  13. } // namespace base
  14. namespace ash {
  15. // Handles power button screenshot accelerator. The screenshot condition is
  16. // pressing power button and volume down key simultaneously, similar to Android
  17. // phones.
  18. class ASH_EXPORT PowerButtonScreenshotController : public ui::EventHandler {
  19. public:
  20. // Time that volume down key and power button must be pressed within this
  21. // interval of each other to make a screenshot.
  22. static constexpr base::TimeDelta kScreenshotChordDelay =
  23. base::Milliseconds(150);
  24. explicit PowerButtonScreenshotController(const base::TickClock* tick_clock);
  25. PowerButtonScreenshotController(const PowerButtonScreenshotController&) =
  26. delete;
  27. PowerButtonScreenshotController& operator=(
  28. const PowerButtonScreenshotController&) = delete;
  29. ~PowerButtonScreenshotController() override;
  30. // Returns true if power button event is consumed by |this|, otherwise false.
  31. bool OnPowerButtonEvent(bool down, const base::TimeTicks& timestamp);
  32. // Overridden from ui::EventHandler:
  33. void OnKeyEvent(ui::KeyEvent* event) override;
  34. private:
  35. friend class PowerButtonScreenshotControllerTestApi;
  36. // Helper method used to intercept power button event or volume down key event
  37. // to check screenshot chord condition. Return true if screenshot is taken to
  38. // indicate that power button and volume down key is consumed by screenshot.
  39. bool InterceptScreenshotChord();
  40. // Called by |volume_down_timer_| or |volume_up_timer_| to perform volume down
  41. // or up accelerator.
  42. void OnVolumeControlTimeout(const ui::Accelerator& accelerator, bool down);
  43. // True if volume down/up key is pressed.
  44. bool volume_down_key_pressed_ = false;
  45. bool volume_up_key_pressed_ = false;
  46. // True if volume down/up key is consumed by screenshot accelerator.
  47. bool consume_volume_down_ = false;
  48. bool consume_volume_up_ = false;
  49. // True if power button is pressed.
  50. bool power_button_pressed_ = false;
  51. // Saves the most recent volume down/up key pressed time.
  52. base::TimeTicks volume_down_key_pressed_time_;
  53. base::TimeTicks volume_up_key_pressed_time_;
  54. // Saves the most recent power button pressed time.
  55. base::TimeTicks power_button_pressed_time_;
  56. // Started when volume down/up key is pressed and power button is not pressed.
  57. // Stopped when power button is pressed. Runs OnVolumeControlTimeout to
  58. // perform a volume down/up accelerator.
  59. base::OneShotTimer volume_down_timer_;
  60. base::OneShotTimer volume_up_timer_;
  61. // Time source for performed action times.
  62. const base::TickClock* tick_clock_; // Not owned.
  63. };
  64. } // namespace ash
  65. #endif // ASH_SYSTEM_POWER_POWER_BUTTON_SCREENSHOT_CONTROLLER_H_