1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:57:44 +00:00

Terminal+LibGUI: Make the terminal cursor blink.

Added a GTimer class to help with this. It's just a simple GObject subclass
that sets up an event loop timer and invokes a callback on timeout.
This commit is contained in:
Andreas Kling 2019-03-30 21:40:27 +01:00
parent 245c4bd7c8
commit 25f28a54a1
5 changed files with 96 additions and 1 deletions

39
LibGUI/GTimer.cpp Normal file
View file

@ -0,0 +1,39 @@
#include <LibGUI/GTimer.h>
GTimer::GTimer(GObject* parent)
: GObject(parent)
{
}
GTimer::~GTimer()
{
}
void GTimer::start()
{
start(m_interval);
}
void GTimer::start(int interval)
{
if (m_active)
return;
start_timer(interval);
m_active = true;
}
void GTimer::stop()
{
if (!m_active)
return;
stop_timer();
m_active = false;
}
void GTimer::timer_event(GTimerEvent&)
{
if (m_single_shot)
stop();
if (on_timeout)
on_timeout();
}

32
LibGUI/GTimer.h Normal file
View file

@ -0,0 +1,32 @@
#pragma once
#include <LibGUI/GObject.h>
#include <AK/Function.h>
class GTimer final : public GObject {
public:
explicit GTimer(GObject* parent = nullptr);
virtual ~GTimer() override;
void start();
void start(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; }
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;
virtual const char* class_name() const override { return "GTimer"; }
private:
virtual void timer_event(GTimerEvent&) override;
bool m_active { false };
bool m_single_shot { false };
int m_interval { 0 };
};

View file

@ -57,6 +57,7 @@ LIBGUI_OBJS = \
GTreeView.o \
GFileSystemModel.o \
GSplitter.o \
GTimer.o \
GWindow.o
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)