Skip to content

Commit

Permalink
LibWeb: Let HTML::EventLoop drive the firing of resize events
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Oct 3, 2021
1 parent 81ef2b6 commit 6e341cd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
22 changes: 22 additions & 0 deletions Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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();
}

}
5 changes: 5 additions & 0 deletions Userland/Libraries/LibWeb/DOM/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ class Document
bool hidden() const;
String visibility_state() const;

void run_the_resize_steps();

private:
explicit Document(const AK::URL&);

Expand Down Expand Up @@ -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;
};

}
5 changes: 4 additions & 1 deletion Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
17 changes: 7 additions & 10 deletions Userland/Libraries/LibWeb/Page/BrowsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}

Expand All @@ -99,20 +94,22 @@ 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)
{
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)
Expand Down

0 comments on commit 6e341cd

Please sign in to comment.