mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 08:07:34 +00:00
LibWeb+WebContent: Add abstraction layer for event loop and timers
Instead of using Core::EventLoop and Core::Timer directly, LibWeb now goes through a Web::Platform abstraction layer instead. This will allow us to plug in Qt's event loop (and QTimer) over in Ladybird, to avoid having to deal with multiple event loops.
This commit is contained in:
parent
7e5a8bd4b0
commit
9567e211e7
28 changed files with 365 additions and 42 deletions
28
Userland/Libraries/LibWeb/Platform/EventLoopPlugin.cpp
Normal file
28
Userland/Libraries/LibWeb/Platform/EventLoopPlugin.cpp
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <LibWeb/Platform/EventLoopPlugin.h>
|
||||
|
||||
namespace Web::Platform {
|
||||
|
||||
EventLoopPlugin* s_the;
|
||||
|
||||
EventLoopPlugin& EventLoopPlugin::the()
|
||||
{
|
||||
VERIFY(s_the);
|
||||
return *s_the;
|
||||
}
|
||||
|
||||
void EventLoopPlugin::install(EventLoopPlugin& plugin)
|
||||
{
|
||||
VERIFY(!s_the);
|
||||
s_the = &plugin;
|
||||
}
|
||||
|
||||
EventLoopPlugin::~EventLoopPlugin() = default;
|
||||
|
||||
}
|
26
Userland/Libraries/LibWeb/Platform/EventLoopPlugin.h
Normal file
26
Userland/Libraries/LibWeb/Platform/EventLoopPlugin.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web::Platform {
|
||||
|
||||
class EventLoopPlugin {
|
||||
public:
|
||||
static EventLoopPlugin& the();
|
||||
static void install(EventLoopPlugin&);
|
||||
|
||||
virtual ~EventLoopPlugin();
|
||||
|
||||
virtual void spin_until(Function<bool()> goal_condition) = 0;
|
||||
virtual void deferred_invoke(Function<void()>) = 0;
|
||||
virtual NonnullRefPtr<Timer> create_timer() = 0;
|
||||
};
|
||||
|
||||
}
|
38
Userland/Libraries/LibWeb/Platform/Timer.cpp
Normal file
38
Userland/Libraries/LibWeb/Platform/Timer.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <LibWeb/Platform/EventLoopPlugin.h>
|
||||
#include <LibWeb/Platform/Timer.h>
|
||||
|
||||
namespace Web::Platform {
|
||||
|
||||
Timer::~Timer() = default;
|
||||
|
||||
NonnullRefPtr<Timer> Timer::create()
|
||||
{
|
||||
return EventLoopPlugin::the().create_timer();
|
||||
}
|
||||
|
||||
NonnullRefPtr<Timer> Timer::create_repeating(int interval_ms, Function<void()>&& timeout_handler)
|
||||
{
|
||||
auto timer = EventLoopPlugin::the().create_timer();
|
||||
timer->set_single_shot(false);
|
||||
timer->set_interval(interval_ms);
|
||||
timer->on_timeout = move(timeout_handler);
|
||||
return timer;
|
||||
}
|
||||
|
||||
NonnullRefPtr<Timer> Timer::create_single_shot(int interval_ms, Function<void()>&& timeout_handler)
|
||||
{
|
||||
auto timer = EventLoopPlugin::the().create_timer();
|
||||
timer->set_single_shot(true);
|
||||
timer->set_interval(interval_ms);
|
||||
timer->on_timeout = move(timeout_handler);
|
||||
return timer;
|
||||
}
|
||||
|
||||
}
|
40
Userland/Libraries/LibWeb/Platform/Timer.h
Normal file
40
Userland/Libraries/LibWeb/Platform/Timer.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/RefCounted.h>
|
||||
|
||||
namespace Web::Platform {
|
||||
|
||||
class Timer : public RefCounted<Timer> {
|
||||
public:
|
||||
static NonnullRefPtr<Timer> create();
|
||||
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);
|
||||
|
||||
virtual ~Timer();
|
||||
|
||||
virtual void start() = 0;
|
||||
virtual void start(int interval_ms) = 0;
|
||||
virtual void restart() = 0;
|
||||
virtual void restart(int interval_ms) = 0;
|
||||
virtual void stop() = 0;
|
||||
|
||||
virtual void set_active(bool) = 0;
|
||||
|
||||
virtual bool is_active() const = 0;
|
||||
virtual int interval() const = 0;
|
||||
virtual void set_interval(int interval_ms) = 0;
|
||||
|
||||
virtual bool is_single_shot() const = 0;
|
||||
virtual void set_single_shot(bool) = 0;
|
||||
|
||||
Function<void()> on_timeout;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue