diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 57a6f25e247172..cdff35cc2db95e 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include @@ -1083,4 +1084,25 @@ String Document::visibility_state() const return hidden() ? "hidden" : "visible"; } +// https://drafts.csswg.org/cssom-view/#run-the-resize-steps +void Document::run_the_resize_steps() +{ + // 1. If doc’s viewport has had its width or height changed + // (e.g. as a result of the user resizing the browser window, or changing the page zoom scale factor, + // or an iframe element’s dimensions are changed) since the last time these steps were run, + // fire an event named resize at the Window object associated with doc. + + if (!browsing_context()) + return; + + auto viewport_size = browsing_context()->viewport_rect().size(); + if (m_last_viewport_size == viewport_size) + return; + m_last_viewport_size = viewport_size; + + dispatch_event(DOM::Event::create(UIEvents::EventNames::resize)); + + update_layout(); +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index dfa07c1b052a22..b871c0e8132bb7 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -301,6 +301,8 @@ class Document bool hidden() const; String visibility_state() const; + void run_the_resize_steps(); + private: explicit Document(const AK::URL&); @@ -388,6 +390,9 @@ class Document // https://html.spec.whatwg.org/#page-showing bool m_page_showing { false }; + + // Used by run_the_resize_steps(). + Gfx::IntSize m_last_viewport_size; }; } diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp index 0edfaa22074bc7..7d5110fd98f71e 100644 --- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp +++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp @@ -153,7 +153,10 @@ void EventLoop::process() // FIXME: 6. For each fully active Document in docs, flush autofocus candidates for that Document if its browsing context is a top-level browsing context. - // FIXME: 7. For each fully active Document in docs, run the resize steps for that Document, passing in now as the timestamp. [CSSOMVIEW] + // 7. For each fully active Document in docs, run the resize steps for that Document, passing in now as the timestamp. [CSSOMVIEW] + for_each_fully_active_document_in_docs([&](DOM::Document& document) { + document.run_the_resize_steps(); + }); // FIXME: 8. For each fully active Document in docs, run the scroll steps for that Document, passing in now as the timestamp. [CSSOMVIEW] diff --git a/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp b/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp index ea637cacd43df2..219d9c04ed183a 100644 --- a/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp @@ -5,16 +5,15 @@ */ #include -#include #include #include +#include #include #include #include #include #include #include -#include namespace Web { @@ -83,10 +82,6 @@ void BrowsingContext::set_viewport_rect(Gfx::IntRect const& rect) if (m_size != rect.size()) { m_size = rect.size(); - if (active_document()) { - active_document()->window().dispatch_event(DOM::Event::create(UIEvents::EventNames::resize)); - active_document()->update_layout(); - } did_change = true; } @@ -99,6 +94,9 @@ void BrowsingContext::set_viewport_rect(Gfx::IntRect const& rect) for (auto* client : m_viewport_clients) client->browsing_context_did_set_viewport_rect(rect); } + + // Schedule the HTML event loop to ensure that a `resize` event gets fired. + HTML::main_thread_event_loop().schedule(); } void BrowsingContext::set_size(Gfx::IntSize const& size) @@ -106,13 +104,12 @@ void BrowsingContext::set_size(Gfx::IntSize const& size) if (m_size == size) return; m_size = size; - if (active_document()) { - active_document()->window().dispatch_event(DOM::Event::create(UIEvents::EventNames::resize)); - active_document()->update_layout(); - } for (auto* client : m_viewport_clients) client->browsing_context_did_set_viewport_rect(viewport_rect()); + + // Schedule the HTML event loop to ensure that a `resize` event gets fired. + HTML::main_thread_event_loop().schedule(); } void BrowsingContext::set_viewport_scroll_offset(Gfx::IntPoint const& offset)