From 25f53a577db5cbf56d8a6188b750f2219e034904 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 6 Mar 2023 19:51:33 +0000 Subject: [PATCH] LibWeb/HTML: Port Window.inner{Width,Height} to IDL --- Userland/Libraries/LibWeb/HTML/Window.cpp | 54 +++++++++-------------- Userland/Libraries/LibWeb/HTML/Window.h | 9 ++-- Userland/Libraries/LibWeb/HTML/Window.idl | 4 ++ 3 files changed, 27 insertions(+), 40 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index ef95665e5b2..9faa74ccc8f 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -586,26 +586,6 @@ bool Window::dispatch_event(DOM::Event& event) return DOM::EventDispatcher::dispatch(*this, event, true); } -// https://www.w3.org/TR/cssom-view-1/#dom-window-innerwidth -int Window::inner_width() const -{ - // The innerWidth attribute must return the viewport width including the size of a rendered scroll bar (if any), - // or zero if there is no viewport. - if (auto const* browsing_context = associated_document().browsing_context()) - return browsing_context->viewport_rect().width().value(); - return 0; -} - -// https://www.w3.org/TR/cssom-view-1/#dom-window-innerheight -int Window::inner_height() const -{ - // The innerHeight attribute must return the viewport height including the size of a rendered scroll bar (if any), - // or zero if there is no viewport. - if (auto const* browsing_context = associated_document().browsing_context()) - return browsing_context->viewport_rect().height().value(); - return 0; -} - Page* Window::page() { return associated_document().page(); @@ -1030,8 +1010,6 @@ WebIDL::ExceptionOr Window::initialize_web_interfaces(Badge> Window::screen() return JS::NonnullGCPtr { *m_screen }; } +// https://w3c.github.io/csswg-drafts/cssom-view/#dom-window-innerwidth +i32 Window::inner_width() const +{ + // The innerWidth attribute must return the viewport width including the size of a rendered scroll bar (if any), + // or zero if there is no viewport. + if (auto const* browsing_context = associated_document().browsing_context()) + return browsing_context->viewport_rect().width().value(); + return 0; +} + +// https://w3c.github.io/csswg-drafts/cssom-view/#dom-window-innerheight +i32 Window::inner_height() const +{ + // The innerHeight attribute must return the viewport height including the size of a rendered scroll bar (if any), + // or zero if there is no viewport. + if (auto const* browsing_context = associated_document().browsing_context()) + return browsing_context->viewport_rect().height().value(); + return 0; +} + // https://w3c.github.io/hr-time/#dom-windoworworkerglobalscope-performance WebIDL::ExceptionOr> Window::performance() { @@ -1531,18 +1529,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::location_setter) return JS::js_undefined(); } -JS_DEFINE_NATIVE_FUNCTION(Window::inner_width_getter) -{ - auto* impl = TRY(impl_from(vm)); - return JS::Value(impl->inner_width()); -} - -JS_DEFINE_NATIVE_FUNCTION(Window::inner_height_getter) -{ - auto* impl = TRY(impl_from(vm)); - return JS::Value(impl->inner_height()); -} - JS_DEFINE_NATIVE_FUNCTION(Window::device_pixel_ratio_getter) { auto* impl = TRY(impl_from(vm)); diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index ab0dacff048..0137a0b7877 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -87,9 +87,6 @@ public: void queue_microtask_impl(WebIDL::CallbackType& callback); - int inner_width() const; - int inner_height() const; - void did_set_location_href(Badge, AK::URL const& new_href); void did_call_location_reload(Badge); void did_call_location_replace(Badge, DeprecatedString url); @@ -165,6 +162,9 @@ public: WebIDL::ExceptionOr> match_media(String const& query); WebIDL::ExceptionOr> screen(); + i32 inner_width() const; + i32 inner_height() const; + WebIDL::ExceptionOr> performance(); WebIDL::ExceptionOr> crypto(); @@ -231,9 +231,6 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(location_setter); - JS_DECLARE_NATIVE_FUNCTION(inner_width_getter); - JS_DECLARE_NATIVE_FUNCTION(inner_height_getter); - JS_DECLARE_NATIVE_FUNCTION(device_pixel_ratio_getter); JS_DECLARE_NATIVE_FUNCTION(scroll_x_getter); diff --git a/Userland/Libraries/LibWeb/HTML/Window.idl b/Userland/Libraries/LibWeb/HTML/Window.idl index ef02814c526..7534866308f 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.idl +++ b/Userland/Libraries/LibWeb/HTML/Window.idl @@ -48,6 +48,10 @@ interface Window : EventTarget { [NewObject] MediaQueryList matchMedia(CSSOMString query); [SameObject, Replaceable] readonly attribute Screen screen; + // viewport + [Replaceable] readonly attribute long innerWidth; + [Replaceable] readonly attribute long innerHeight; + // FIXME: Everything from here on should be shared through WindowOrWorkerGlobalScope // https://w3c.github.io/hr-time/#the-performance-attribute [Replaceable] readonly attribute Performance performance;