2022-05-05 20:08:29 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2024-08-04 16:36:50 +02:00
|
|
|
* Copyright (c) 2024, Andreas Kling <andreas@ladybird.org>
|
2022-05-05 20:08:29 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2023-02-25 10:42:45 -07:00
|
|
|
#include <AK/HashMap.h>
|
2024-04-03 12:48:45 +02:00
|
|
|
#include <LibCore/Timer.h>
|
2022-05-05 20:08:29 +01:00
|
|
|
#include <LibWeb/HTML/EventLoop/EventLoop.h>
|
2024-08-04 16:36:50 +02:00
|
|
|
#include <LibWeb/WebIDL/Types.h>
|
2022-05-05 20:08:29 +01:00
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
|
|
|
struct AnimationFrameCallbackDriver {
|
2024-07-01 12:21:16 -06:00
|
|
|
using Callback = Function<void(double)>;
|
2022-05-05 20:08:29 +01:00
|
|
|
|
2024-08-04 16:36:50 +02:00
|
|
|
[[nodiscard]] WebIDL::UnsignedLong add(Callback handler)
|
2022-05-05 20:08:29 +01:00
|
|
|
{
|
2024-08-04 16:36:50 +02:00
|
|
|
auto id = ++m_animation_frame_callback_identifier;
|
2022-05-05 20:08:29 +01:00
|
|
|
m_callbacks.set(id, move(handler));
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2024-08-04 16:36:50 +02:00
|
|
|
bool remove(WebIDL::UnsignedLong id)
|
2022-05-05 20:08:29 +01:00
|
|
|
{
|
|
|
|
auto it = m_callbacks.find(id);
|
|
|
|
if (it == m_callbacks.end())
|
|
|
|
return false;
|
|
|
|
m_callbacks.remove(it);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-04 21:08:10 +00:00
|
|
|
void run(double now)
|
2022-05-05 20:08:29 +01:00
|
|
|
{
|
|
|
|
auto taken_callbacks = move(m_callbacks);
|
|
|
|
for (auto& [id, callback] : taken_callbacks)
|
2024-02-04 21:08:10 +00:00
|
|
|
callback(now);
|
2022-05-05 20:08:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool has_callbacks() const
|
|
|
|
{
|
|
|
|
return !m_callbacks.is_empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2024-08-04 16:36:50 +02:00
|
|
|
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#animation-frame-callback-identifier
|
|
|
|
WebIDL::UnsignedLong m_animation_frame_callback_identifier { 0 };
|
|
|
|
|
|
|
|
OrderedHashMap<WebIDL::UnsignedLong, Callback> m_callbacks;
|
2022-05-05 20:08:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|