2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2025-08-10 11:22:40 -03:00
|
|
|
* Copyright (c) 2018-2025, Andreas Kling <andreas@ladybird.org>
|
2022-02-26 13:09:45 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-03-30 17:40:27 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2023-08-06 13:09:39 -03:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2025-12-04 14:12:06 -03:00
|
|
|
#include <LibCore/Export.h>
|
2019-03-30 17:40:27 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
namespace Core {
|
|
|
|
|
|
2025-12-04 14:12:06 -03:00
|
|
|
class CORE_API Timer final : public EventReceiver {
|
2020-04-07 17:15:22 -03:00
|
|
|
C_OBJECT(Timer);
|
|
|
|
|
|
2019-03-30 17:40:27 -03:00
|
|
|
public:
|
2024-04-16 15:34:01 -03:00
|
|
|
static NonnullRefPtr<Timer> create();
|
2025-08-10 11:22:40 -03:00
|
|
|
static NonnullRefPtr<Timer> create_repeating(int interval_ms, Function<void()>&& timeout_handler);
|
|
|
|
|
static NonnullRefPtr<Timer> create_single_shot(int interval_ms, Function<void()>&& timeout_handler);
|
2020-04-07 17:15:22 -03:00
|
|
|
|
2024-04-16 15:34:01 -03:00
|
|
|
virtual ~Timer() override;
|
2019-03-30 17:40:27 -03:00
|
|
|
|
|
|
|
|
void start();
|
2021-05-10 06:29:41 -03:00
|
|
|
void start(int interval_ms);
|
2020-06-11 17:35:37 -03:00
|
|
|
void restart();
|
2021-05-10 06:29:41 -03:00
|
|
|
void restart(int interval_ms);
|
2019-03-30 17:40:27 -03:00
|
|
|
void stop();
|
|
|
|
|
|
2021-12-25 09:25:24 -03:00
|
|
|
void set_active(bool);
|
|
|
|
|
|
2019-03-30 17:40:27 -03:00
|
|
|
bool is_active() const { return m_active; }
|
2021-05-10 06:29:41 -03:00
|
|
|
int interval() const { return m_interval_ms; }
|
|
|
|
|
void set_interval(int interval_ms)
|
2019-04-17 23:38:04 -03:00
|
|
|
{
|
2021-05-10 06:29:41 -03:00
|
|
|
if (m_interval_ms == interval_ms)
|
2019-04-17 23:38:04 -03:00
|
|
|
return;
|
2021-05-10 06:29:41 -03:00
|
|
|
m_interval_ms = interval_ms;
|
2019-04-17 23:38:04 -03:00
|
|
|
m_interval_dirty = true;
|
|
|
|
|
}
|
2019-03-30 17:40:27 -03:00
|
|
|
|
|
|
|
|
bool is_single_shot() const { return m_single_shot; }
|
|
|
|
|
void set_single_shot(bool single_shot) { m_single_shot = single_shot; }
|
|
|
|
|
|
|
|
|
|
Function<void()> on_timeout;
|
|
|
|
|
|
|
|
|
|
private:
|
2025-08-10 11:22:40 -03:00
|
|
|
Timer();
|
|
|
|
|
Timer(int interval_ms, Function<void()>&& timeout_handler);
|
2019-09-20 10:19:46 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
virtual void timer_event(TimerEvent&) override;
|
2019-03-30 17:40:27 -03:00
|
|
|
|
|
|
|
|
bool m_active { false };
|
|
|
|
|
bool m_single_shot { false };
|
2019-04-17 23:38:04 -03:00
|
|
|
bool m_interval_dirty { false };
|
2021-05-10 06:29:41 -03:00
|
|
|
int m_interval_ms { 0 };
|
2019-03-30 17:40:27 -03:00
|
|
|
};
|
2020-02-02 08:34:39 -03:00
|
|
|
|
|
|
|
|
}
|