summaryrefslogtreecommitdiff
path: root/LibCore
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-04-18 04:38:04 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-04-18 04:38:04 +0200
commitd73ed74d1cf4537cbca78a7de0326f722323f94c (patch)
tree5fa68da0ed0b65748f9d25ce5c5375269bf450a5 /LibCore
parenta747a10eabd12427bf3c4626e6929041aa81fa7c (diff)
downloadserenity-d73ed74d1cf4537cbca78a7de0326f722323f94c.zip
LibCore: Add CTimer::restart() and make set_interval() take effect soon.
Diffstat (limited to 'LibCore')
-rw-r--r--LibCore/CTimer.cpp14
-rw-r--r--LibCore/CTimer.h10
2 files changed, 23 insertions, 1 deletions
diff --git a/LibCore/CTimer.cpp b/LibCore/CTimer.cpp
index cb479a1d18..fb55ad7e63 100644
--- a/LibCore/CTimer.cpp
+++ b/LibCore/CTimer.cpp
@@ -30,6 +30,13 @@ void CTimer::start(int interval)
m_active = true;
}
+void CTimer::restart(int interval)
+{
+ if (m_active)
+ stop();
+ start(interval);
+}
+
void CTimer::stop()
{
if (!m_active)
@@ -42,6 +49,13 @@ void CTimer::timer_event(CTimerEvent&)
{
if (m_single_shot)
stop();
+ else {
+ if (m_interval_dirty) {
+ stop();
+ start(m_interval);
+ }
+ }
+
if (on_timeout)
on_timeout();
}
diff --git a/LibCore/CTimer.h b/LibCore/CTimer.h
index 736b8c17e5..4d0507512f 100644
--- a/LibCore/CTimer.h
+++ b/LibCore/CTimer.h
@@ -11,11 +11,18 @@ public:
void start();
void start(int interval);
+ void restart(int interval);
void stop();
bool is_active() const { return m_active; }
int interval() const { return m_interval; }
- void set_interval(int interval) { m_interval = interval; }
+ void set_interval(int interval)
+ {
+ if (m_interval == interval)
+ return;
+ m_interval = interval;
+ m_interval_dirty = true;
+ }
bool is_single_shot() const { return m_single_shot; }
void set_single_shot(bool single_shot) { m_single_shot = single_shot; }
@@ -29,5 +36,6 @@ private:
bool m_active { false };
bool m_single_shot { false };
+ bool m_interval_dirty { false };
int m_interval { 0 };
};