1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 20:07:34 +00:00

LibWeb/HTML: Port Window.requestAnimationFrame() to IDL

This commit is contained in:
Linus Groh 2023-03-07 18:12:58 +00:00
parent 86589f09dc
commit 211e6c1fbc
4 changed files with 25 additions and 30 deletions

View file

@ -0,0 +1,8 @@
#import <HighResolutionTime/DOMHighResTimeStamp.idl>
callback FrameRequestCallback = undefined (DOMHighResTimeStamp time);
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#animationframeprovider
interface mixin AnimationFrameProvider {
unsigned long requestAnimationFrame(FrameRequestCallback callback);
};

View file

@ -536,21 +536,6 @@ i32 Window::run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS
return id; return id;
} }
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#run-the-animation-frame-callbacks
i32 Window::request_animation_frame_impl(WebIDL::CallbackType& js_callback)
{
// FIXME: `now` is supposed to be passed in
auto now = HighResolutionTime::unsafe_shared_current_time();
return m_animation_frame_callback_driver.add([this, now, js_callback = JS::make_handle(js_callback)](auto) {
// 3. Invoke callback, passing now as the only argument,
auto result = WebIDL::invoke_callback(*js_callback, {}, JS::Value(now));
// and if an exception is thrown, report the exception.
if (result.is_error())
HTML::report_exception(result, realm());
});
}
void Window::cancel_animation_frame_impl(i32 id) void Window::cancel_animation_frame_impl(i32 id)
{ {
m_animation_frame_callback_driver.remove(id); m_animation_frame_callback_driver.remove(id);
@ -911,7 +896,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
define_native_function(realm, "setTimeout", set_timeout, 1, attr); define_native_function(realm, "setTimeout", set_timeout, 1, attr);
define_native_function(realm, "clearInterval", clear_interval, 1, attr); define_native_function(realm, "clearInterval", clear_interval, 1, attr);
define_native_function(realm, "clearTimeout", clear_timeout, 1, attr); define_native_function(realm, "clearTimeout", clear_timeout, 1, attr);
define_native_function(realm, "requestAnimationFrame", request_animation_frame, 1, attr);
define_native_function(realm, "cancelAnimationFrame", cancel_animation_frame, 1, attr); define_native_function(realm, "cancelAnimationFrame", cancel_animation_frame, 1, attr);
define_native_function(realm, "queueMicrotask", queue_microtask, 1, attr); define_native_function(realm, "queueMicrotask", queue_microtask, 1, attr);
@ -1434,6 +1418,19 @@ double Window::device_pixel_ratio() const
return 1; return 1;
} }
// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#dom-animationframeprovider-requestanimationframe
i32 Window::request_animation_frame(WebIDL::CallbackType& callback)
{
// FIXME: Make this fully spec compliant. Currently implements a mix of 'requestAnimationFrame()' and 'run the animation frame callbacks'.
auto now = HighResolutionTime::unsafe_shared_current_time();
return m_animation_frame_callback_driver.add([this, now, callback = JS::make_handle(callback)](auto) {
// 3. Invoke callback, passing now as the only argument, and if an exception is thrown, report the exception.
auto result = WebIDL::invoke_callback(*callback, {}, JS::Value(now));
if (result.is_error())
HTML::report_exception(result, realm());
});
}
// https://w3c.github.io/requestidlecallback/#dom-window-requestidlecallback // https://w3c.github.io/requestidlecallback/#dom-window-requestidlecallback
u32 Window::request_idle_callback(WebIDL::CallbackType& callback, RequestIdleCallback::IdleRequestOptions const& options) u32 Window::request_idle_callback(WebIDL::CallbackType& callback, RequestIdleCallback::IdleRequestOptions const& options)
{ {
@ -1578,18 +1575,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::clear_interval)
return JS::js_undefined(); return JS::js_undefined();
} }
JS_DEFINE_NATIVE_FUNCTION(Window::request_animation_frame)
{
auto* impl = TRY(impl_from(vm));
if (!vm.argument_count())
return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
auto* callback_object = TRY(vm.argument(0).to_object(vm));
if (!callback_object->is_function())
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunctionNoParam);
auto callback = vm.heap().allocate_without_realm<WebIDL::CallbackType>(*callback_object, HTML::incumbent_settings_object());
return JS::Value(impl->request_animation_frame_impl(*callback));
}
JS_DEFINE_NATIVE_FUNCTION(Window::cancel_animation_frame) JS_DEFINE_NATIVE_FUNCTION(Window::cancel_animation_frame)
{ {
auto* impl = TRY(impl_from(vm)); auto* impl = TRY(impl_from(vm));

View file

@ -88,7 +88,6 @@ public:
void set_import_maps_allowed(bool import_maps_allowed) { m_import_maps_allowed = import_maps_allowed; } void set_import_maps_allowed(bool import_maps_allowed) { m_import_maps_allowed = import_maps_allowed; }
WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open_impl(StringView url, StringView target, StringView features); WebIDL::ExceptionOr<JS::GCPtr<HTML::WindowProxy>> open_impl(StringView url, StringView target, StringView features);
i32 request_animation_frame_impl(WebIDL::CallbackType& js_callback);
void cancel_animation_frame_impl(i32); void cancel_animation_frame_impl(i32);
bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); } bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
@ -178,6 +177,8 @@ public:
i32 outer_height() const; i32 outer_height() const;
double device_pixel_ratio() const; double device_pixel_ratio() const;
i32 request_animation_frame(WebIDL::CallbackType&);
u32 request_idle_callback(WebIDL::CallbackType&, RequestIdleCallback::IdleRequestOptions const&); u32 request_idle_callback(WebIDL::CallbackType&, RequestIdleCallback::IdleRequestOptions const&);
void cancel_idle_callback(u32 handle); void cancel_idle_callback(u32 handle);
@ -258,7 +259,6 @@ private:
JS_DECLARE_NATIVE_FUNCTION(set_timeout); JS_DECLARE_NATIVE_FUNCTION(set_timeout);
JS_DECLARE_NATIVE_FUNCTION(clear_interval); JS_DECLARE_NATIVE_FUNCTION(clear_interval);
JS_DECLARE_NATIVE_FUNCTION(clear_timeout); JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
JS_DECLARE_NATIVE_FUNCTION(request_animation_frame);
JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame); JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
JS_DECLARE_NATIVE_FUNCTION(queue_microtask); JS_DECLARE_NATIVE_FUNCTION(queue_microtask);

View file

@ -5,6 +5,7 @@
#import <DOM/EventHandler.idl> #import <DOM/EventHandler.idl>
#import <DOM/EventTarget.idl> #import <DOM/EventTarget.idl>
#import <HighResolutionTime/Performance.idl> #import <HighResolutionTime/Performance.idl>
#import <HTML/AnimationFrameProvider.idl>
#import <HTML/Navigator.idl> #import <HTML/Navigator.idl>
#import <HTML/WindowOrWorkerGlobalScope.idl> #import <HTML/WindowOrWorkerGlobalScope.idl>
#import <RequestIdleCallback/IdleRequest.idl> #import <RequestIdleCallback/IdleRequest.idl>
@ -92,6 +93,7 @@ interface Window : EventTarget {
// https://w3c.github.io/webcrypto/#crypto-interface // https://w3c.github.io/webcrypto/#crypto-interface
[SameObject] readonly attribute Crypto crypto; [SameObject] readonly attribute Crypto crypto;
}; };
Window includes AnimationFrameProvider;
Window includes GlobalEventHandlers; Window includes GlobalEventHandlers;
Window includes WindowEventHandlers; Window includes WindowEventHandlers;
Window includes WindowOrWorkerGlobalScope; Window includes WindowOrWorkerGlobalScope;