ladybird/Userland/Libraries/LibWeb/Platform/Timer.cpp
Andreas Kling 9567e211e7 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.
2022-09-07 20:30:31 +02:00

38 lines
967 B
C++

/*
* 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;
}
}