summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2023-03-07 18:12:58 +0000
committerLinus Groh <mail@linusgroh.de>2023-03-07 23:33:34 +0000
commit211e6c1fbc79d4b414e2e30d8a42283d69da1620 (patch)
tree37ef87cb990a5ef2393984e2e7afc5d34ebea8e5 /Userland/Libraries
parent86589f09dcdd6e546a9f5d8de56eee6f4d791591 (diff)
downloadserenity-211e6c1fbc79d4b414e2e30d8a42283d69da1620.zip
LibWeb/HTML: Port Window.requestAnimationFrame() to IDL
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibWeb/HTML/AnimationFrameProvider.idl8
-rw-r--r--Userland/Libraries/LibWeb/HTML/Window.cpp41
-rw-r--r--Userland/Libraries/LibWeb/HTML/Window.h4
-rw-r--r--Userland/Libraries/LibWeb/HTML/Window.idl2
4 files changed, 25 insertions, 30 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/AnimationFrameProvider.idl b/Userland/Libraries/LibWeb/HTML/AnimationFrameProvider.idl
new file mode 100644
index 0000000000..91db5edabb
--- /dev/null
+++ b/Userland/Libraries/LibWeb/HTML/AnimationFrameProvider.idl
@@ -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);
+};
diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp
index bf83d56ab1..22d077b44f 100644
--- a/Userland/Libraries/LibWeb/HTML/Window.cpp
+++ b/Userland/Libraries/LibWeb/HTML/Window.cpp
@@ -536,21 +536,6 @@ i32 Window::run_timer_initialization_steps(TimerHandler handler, i32 timeout, JS
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)
{
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, "clearInterval", clear_interval, 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, "queueMicrotask", queue_microtask, 1, attr);
@@ -1434,6 +1418,19 @@ double Window::device_pixel_ratio() const
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
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();
}
-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)
{
auto* impl = TRY(impl_from(vm));
diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h
index 1935e63018..ef8cd46d08 100644
--- a/Userland/Libraries/LibWeb/HTML/Window.h
+++ b/Userland/Libraries/LibWeb/HTML/Window.h
@@ -88,7 +88,6 @@ public:
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);
- i32 request_animation_frame_impl(WebIDL::CallbackType& js_callback);
void cancel_animation_frame_impl(i32);
bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
@@ -178,6 +177,8 @@ public:
i32 outer_height() const;
double device_pixel_ratio() const;
+ i32 request_animation_frame(WebIDL::CallbackType&);
+
u32 request_idle_callback(WebIDL::CallbackType&, RequestIdleCallback::IdleRequestOptions const&);
void cancel_idle_callback(u32 handle);
@@ -258,7 +259,6 @@ private:
JS_DECLARE_NATIVE_FUNCTION(set_timeout);
JS_DECLARE_NATIVE_FUNCTION(clear_interval);
JS_DECLARE_NATIVE_FUNCTION(clear_timeout);
- JS_DECLARE_NATIVE_FUNCTION(request_animation_frame);
JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
JS_DECLARE_NATIVE_FUNCTION(queue_microtask);
diff --git a/Userland/Libraries/LibWeb/HTML/Window.idl b/Userland/Libraries/LibWeb/HTML/Window.idl
index 08b093efd6..ed2d7997da 100644
--- a/Userland/Libraries/LibWeb/HTML/Window.idl
+++ b/Userland/Libraries/LibWeb/HTML/Window.idl
@@ -5,6 +5,7 @@
#import <DOM/EventHandler.idl>
#import <DOM/EventTarget.idl>
#import <HighResolutionTime/Performance.idl>
+#import <HTML/AnimationFrameProvider.idl>
#import <HTML/Navigator.idl>
#import <HTML/WindowOrWorkerGlobalScope.idl>
#import <RequestIdleCallback/IdleRequest.idl>
@@ -92,6 +93,7 @@ interface Window : EventTarget {
// https://w3c.github.io/webcrypto/#crypto-interface
[SameObject] readonly attribute Crypto crypto;
};
+Window includes AnimationFrameProvider;
Window includes GlobalEventHandlers;
Window includes WindowEventHandlers;
Window includes WindowOrWorkerGlobalScope;