summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-10-03 16:42:03 +0200
committerAndreas Kling <kling@serenityos.org>2021-10-03 16:42:34 +0200
commit6e341cd6962fa9cd2411fcc970ed70da3f26231d (patch)
treea71e8d59c942a105bf4f68b428b150b01e23967d /Userland
parent81ef2b646e9ffcf809110a5f8a4fd08986eb797b (diff)
downloadserenity-6e341cd6962fa9cd2411fcc970ed70da3f26231d.zip
LibWeb: Let HTML::EventLoop drive the firing of `resize` events
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.cpp22
-rw-r--r--Userland/Libraries/LibWeb/DOM/Document.h5
-rw-r--r--Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp5
-rw-r--r--Userland/Libraries/LibWeb/Page/BrowsingContext.cpp17
4 files changed, 38 insertions, 11 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp
index 57a6f25e24..cdff35cc2d 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.cpp
+++ b/Userland/Libraries/LibWeb/DOM/Document.cpp
@@ -57,6 +57,7 @@
#include <LibWeb/Page/BrowsingContext.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/SVG/TagNames.h>
+#include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/KeyboardEvent.h>
#include <LibWeb/UIEvents/MouseEvent.h>
@@ -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 dfa07c1b05..b871c0e813 100644
--- a/Userland/Libraries/LibWeb/DOM/Document.h
+++ b/Userland/Libraries/LibWeb/DOM/Document.h
@@ -301,6 +301,8 @@ public:
bool hidden() const;
String visibility_state() const;
+ void run_the_resize_steps();
+
private:
explicit Document(const AK::URL&);
@@ -388,6 +390,9 @@ private:
// 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 0edfaa2207..7d5110fd98 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 ea637cacd4..219d9c04ed 100644
--- a/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp
+++ b/Userland/Libraries/LibWeb/Page/BrowsingContext.cpp
@@ -5,16 +5,15 @@
*/
#include <LibWeb/DOM/Document.h>
-#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/HTMLCollection.h>
#include <LibWeb/DOM/Window.h>
+#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/Layout/BreakNode.h>
#include <LibWeb/Layout/InitialContainingBlock.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Page/BrowsingContext.h>
#include <LibWeb/Page/Page.h>
-#include <LibWeb/UIEvents/EventNames.h>
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)