timer.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include <QtCore/QObject>
  9. #include <QtCore/QThread>
  10. #include "base/flat_map.h"
  11. #include <crl/crl_time.h>
  12. namespace base {
  13. void CheckLocalTime();
  14. class Timer final : private QObject {
  15. public:
  16. explicit Timer(
  17. not_null<QThread*> thread,
  18. Fn<void()> callback = nullptr);
  19. explicit Timer(Fn<void()> callback = nullptr);
  20. static Qt::TimerType DefaultType(crl::time timeout) {
  21. constexpr auto kThreshold = crl::time(240);
  22. return (timeout > kThreshold) ? Qt::CoarseTimer : Qt::PreciseTimer;
  23. }
  24. void setCallback(Fn<void()> callback) {
  25. _callback = std::move(callback);
  26. }
  27. void callOnce(crl::time timeout) {
  28. callOnce(timeout, DefaultType(timeout));
  29. }
  30. void callEach(crl::time timeout) {
  31. callEach(timeout, DefaultType(timeout));
  32. }
  33. void callOnce(crl::time timeout, Qt::TimerType type) {
  34. start(timeout, type, Repeat::SingleShot);
  35. }
  36. void callEach(crl::time timeout, Qt::TimerType type) {
  37. start(timeout, type, Repeat::Interval);
  38. }
  39. bool isActive() const {
  40. return (_timerId != 0);
  41. }
  42. void cancel();
  43. crl::time remainingTime() const;
  44. static void Adjust();
  45. protected:
  46. void timerEvent(QTimerEvent *e) override;
  47. private:
  48. enum class Repeat : unsigned {
  49. Interval = 0,
  50. SingleShot = 1,
  51. };
  52. void start(crl::time timeout, Qt::TimerType type, Repeat repeat);
  53. void adjust();
  54. void setTimeout(crl::time timeout);
  55. int timeout() const;
  56. void setRepeat(Repeat repeat) {
  57. _repeat = static_cast<unsigned>(repeat);
  58. }
  59. Repeat repeat() const {
  60. return static_cast<Repeat>(_repeat);
  61. }
  62. Fn<void()> _callback;
  63. crl::time _next = 0;
  64. int _timeout = 0;
  65. int _timerId = 0;
  66. Qt::TimerType _type : 2;
  67. bool _adjusted : 1 = false;
  68. unsigned _repeat : 1 = 0;
  69. };
  70. class DelayedCallTimer final : private QObject {
  71. public:
  72. int call(crl::time timeout, FnMut<void()> callback) {
  73. return call(
  74. timeout,
  75. std::move(callback),
  76. Timer::DefaultType(timeout));
  77. }
  78. int call(
  79. crl::time timeout,
  80. FnMut<void()> callback,
  81. Qt::TimerType type);
  82. void cancel(int callId);
  83. protected:
  84. void timerEvent(QTimerEvent *e) override;
  85. private:
  86. base::flat_map<int, FnMut<void()>> _callbacks;
  87. };
  88. } // namespace base