CTimer.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include <AK/Function.h>
  3. #include <LibCore/CObject.h>
  4. #include <LibCore/ObjectPtr.h>
  5. class CTimer final : public CObject {
  6. C_OBJECT(CTimer)
  7. public:
  8. virtual ~CTimer() override;
  9. void start();
  10. void start(int interval);
  11. void restart(int interval);
  12. void stop();
  13. bool is_active() const { return m_active; }
  14. int interval() const { return m_interval; }
  15. void set_interval(int interval)
  16. {
  17. if (m_interval == interval)
  18. return;
  19. m_interval = interval;
  20. m_interval_dirty = true;
  21. }
  22. bool is_single_shot() const { return m_single_shot; }
  23. void set_single_shot(bool single_shot) { m_single_shot = single_shot; }
  24. Function<void()> on_timeout;
  25. private:
  26. explicit CTimer(CObject* parent = nullptr);
  27. CTimer(int interval, Function<void()>&& timeout_handler, CObject* parent = nullptr);
  28. virtual void timer_event(CTimerEvent&) override;
  29. bool m_active { false };
  30. bool m_single_shot { false };
  31. bool m_interval_dirty { false };
  32. int m_interval { 0 };
  33. };