repeat_controller.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2011 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 UI_VIEWS_REPEAT_CONTROLLER_H_
  5. #define UI_VIEWS_REPEAT_CONTROLLER_H_
  6. #include "base/callback.h"
  7. #include "base/time/time.h"
  8. #include "base/timer/timer.h"
  9. #include "ui/views/views_export.h"
  10. namespace base {
  11. class TickClock;
  12. }
  13. namespace views {
  14. ///////////////////////////////////////////////////////////////////////////////
  15. //
  16. // RepeatController
  17. //
  18. // An object that handles auto-repeating UI actions. There is a longer initial
  19. // delay after which point repeats become constant. Users provide a callback
  20. // that is notified when each repeat occurs so that they can perform the
  21. // associated action.
  22. //
  23. ///////////////////////////////////////////////////////////////////////////////
  24. class VIEWS_EXPORT RepeatController {
  25. public:
  26. explicit RepeatController(base::RepeatingClosure callback,
  27. const base::TickClock* tick_clock = nullptr);
  28. RepeatController(const RepeatController&) = delete;
  29. RepeatController& operator=(const RepeatController&) = delete;
  30. virtual ~RepeatController();
  31. // Start repeating.
  32. void Start();
  33. // Stop repeating.
  34. void Stop();
  35. static constexpr base::TimeDelta GetInitialWaitForTesting() {
  36. return kInitialWait;
  37. }
  38. static constexpr base::TimeDelta GetRepeatingWaitForTesting() {
  39. return kRepeatingWait;
  40. }
  41. const base::OneShotTimer& timer_for_testing() const { return timer_; }
  42. private:
  43. // Initial time required before the first callback occurs.
  44. static constexpr base::TimeDelta kInitialWait = base::Milliseconds(250);
  45. // Period of callbacks after the first callback.
  46. static constexpr base::TimeDelta kRepeatingWait = base::Milliseconds(50);
  47. // Called when the timer expires.
  48. void Run();
  49. // The current timer.
  50. base::OneShotTimer timer_;
  51. base::RepeatingClosure callback_;
  52. };
  53. } // namespace views
  54. #endif // UI_VIEWS_REPEAT_CONTROLLER_H_