From 7f9f9164709870739540673f5bb21ac174ea0023 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 7 Mar 2021 13:46:20 +0100 Subject: [PATCH] LibWeb: Make tiled backgrounds scroll with content Previously the page background was always draw relative to the viewport instead of following with the content. This should eventually become an opt-in mode (via CSS "background-attachment") but for now let's have the default behavior be that backgrounds scroll with content. Also take this opportunity to move the background painting code from the two web views to a shared location in InitialContainingBlockBox. --- .../Libraries/LibWeb/InProcessWebView.cpp | 7 ------- .../Layout/InitialContainingBlockBox.cpp | 19 +++++++++++++++++++ .../LibWeb/Layout/InitialContainingBlockBox.h | 2 ++ Userland/Services/WebContent/PageHost.cpp | 8 -------- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibWeb/InProcessWebView.cpp b/Userland/Libraries/LibWeb/InProcessWebView.cpp index 7f664b3920..758f9a47a4 100644 --- a/Userland/Libraries/LibWeb/InProcessWebView.cpp +++ b/Userland/Libraries/LibWeb/InProcessWebView.cpp @@ -262,14 +262,7 @@ void InProcessWebView::paint_event(GUI::PaintEvent& event) return; } - painter.fill_rect(event.rect(), document()->background_color(palette())); - - if (auto background_bitmap = document()->background_image()) { - painter.draw_tiled_bitmap(event.rect(), *background_bitmap); - } - painter.translate(frame_thickness(), frame_thickness()); - painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value()); PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() }); context.set_should_show_line_box_borders(m_should_show_line_box_borders); diff --git a/Userland/Libraries/LibWeb/Layout/InitialContainingBlockBox.cpp b/Userland/Libraries/LibWeb/Layout/InitialContainingBlockBox.cpp index fa74a76dda..b545670f97 100644 --- a/Userland/Libraries/LibWeb/Layout/InitialContainingBlockBox.cpp +++ b/Userland/Libraries/LibWeb/Layout/InitialContainingBlockBox.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include #include @@ -61,8 +62,26 @@ void InitialContainingBlockBox::build_stacking_context_tree() }); } +void InitialContainingBlockBox::paint_document_background(PaintContext& context) +{ + context.painter().fill_rect(Gfx::IntRect { {}, context.viewport_rect().size() }, document().background_color(context.palette())); + context.painter().translate(-context.viewport_rect().location()); + + if (auto background_bitmap = document().background_image()) { + Gfx::IntRect background_rect { + 0, + 0, + context.viewport_rect().x() + context.viewport_rect().width(), + context.viewport_rect().y() + context.viewport_rect().height() + }; + context.painter().blit_tiled(background_rect, *background_bitmap, background_bitmap->rect()); + } +} + void InitialContainingBlockBox::paint_all_phases(PaintContext& context) { + paint_document_background(context); + paint(context, PaintPhase::Background); paint(context, PaintPhase::Border); paint(context, PaintPhase::Foreground); diff --git a/Userland/Libraries/LibWeb/Layout/InitialContainingBlockBox.h b/Userland/Libraries/LibWeb/Layout/InitialContainingBlockBox.h index 92d2746ef4..edbf94b48b 100644 --- a/Userland/Libraries/LibWeb/Layout/InitialContainingBlockBox.h +++ b/Userland/Libraries/LibWeb/Layout/InitialContainingBlockBox.h @@ -41,6 +41,8 @@ public: void paint_all_phases(PaintContext&); virtual void paint(PaintContext&, PaintPhase) override; + void paint_document_background(PaintContext&); + virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override; const LayoutRange& selection() const { return m_selection; } diff --git a/Userland/Services/WebContent/PageHost.cpp b/Userland/Services/WebContent/PageHost.cpp index 6afa672646..46a9c0c09e 100644 --- a/Userland/Services/WebContent/PageHost.cpp +++ b/Userland/Services/WebContent/PageHost.cpp @@ -84,14 +84,6 @@ void PageHost::paint(const Gfx::IntRect& content_rect, Gfx::Bitmap& target) return; } - painter.fill_rect(bitmap_rect, layout_root->document().background_color(palette())); - - if (auto background_bitmap = layout_root->document().background_image()) { - painter.draw_tiled_bitmap(bitmap_rect, *background_bitmap); - } - - painter.translate(-content_rect.x(), -content_rect.y()); - Web::PaintContext context(painter, palette(), content_rect.top_left()); context.set_should_show_line_box_borders(m_should_show_line_box_borders); context.set_viewport_rect(content_rect);