Forráskód Böngészése

LibWeb: Add Document::viewport_rect()

This is a convenience helper that returns an empty rect if there is no
browsing context (to get the actual rect from).
Andreas Kling 1 éve
szülő
commit
ae5313d33c

+ 9 - 5
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -968,7 +968,7 @@ void Document::update_layout()
     if (!browsing_context())
         return;
 
-    auto viewport_rect = browsing_context()->viewport_rect();
+    auto viewport_rect = this->viewport_rect();
 
     if (!m_layout_root) {
         Layout::TreeBuilder tree_builder;
@@ -2101,10 +2101,7 @@ void Document::run_the_resize_steps()
     //    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().to_type<int>();
+    auto viewport_size = viewport_rect().size().to_type<int>();
     if (m_last_viewport_size == viewport_size)
         return;
     m_last_viewport_size = viewport_size;
@@ -2987,6 +2984,13 @@ HTML::ListOfAvailableImages const& Document::list_of_available_images() const
     return *m_list_of_available_images;
 }
 
+CSSPixelRect Document::viewport_rect() const
+{
+    if (auto* browsing_context = this->browsing_context())
+        return browsing_context->viewport_rect();
+    return CSSPixelRect {};
+}
+
 JS::NonnullGCPtr<CSS::VisualViewport> Document::visual_viewport()
 {
     if (!m_visual_viewport)

+ 1 - 0
Userland/Libraries/LibWeb/DOM/Document.h

@@ -390,6 +390,7 @@ public:
     void add_media_query_list(JS::NonnullGCPtr<CSS::MediaQueryList>);
 
     JS::NonnullGCPtr<CSS::VisualViewport> visual_viewport();
+    [[nodiscard]] CSSPixelRect viewport_rect() const;
 
     bool has_focus() const;