1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:57:35 +00:00

WebContent: Basic scrolling support! :^)

The WebContentView widget now inherits from GUI::ScrollableWidget and
will pass its scroll offset over to the WebContent process as it's
changing. This is not super efficient and can get a bit laggy, but it
will do fine as an initial scrolling implementation.

Just like the single-process Web::PageView widget, WebContentView also
paints scrolled content by translating the Gfx::Painter.
This commit is contained in:
Andreas Kling 2020-07-04 23:19:32 +02:00
parent ada3c9ca46
commit e91871aed7
7 changed files with 58 additions and 13 deletions

View file

@ -74,3 +74,11 @@ void WebContentClient::handle(const Messages::WebContentClient::DidChangeSelecti
#endif
m_view.notify_server_did_change_selection({});
}
void WebContentClient::handle(const Messages::WebContentClient::DidLayout& message)
{
#ifdef DEBUG_SPAM
dbg() << "handle: WebContentClient::DidLayout! content_size=" << message.content_size();
#endif
m_view.notify_server_did_layout({}, message.content_size());
}

View file

@ -48,6 +48,7 @@ private:
virtual void handle(const Messages::WebContentClient::DidFinishLoad&) override;
virtual void handle(const Messages::WebContentClient::DidInvalidateContentRect&) override;
virtual void handle(const Messages::WebContentClient::DidChangeSelection&) override;
virtual void handle(const Messages::WebContentClient::DidLayout&) override;
WebContentView& m_view;
};

View file

@ -28,6 +28,7 @@
#include "WebContentClient.h"
#include <AK/SharedBuffer.h>
#include <LibGUI/Painter.h>
#include <LibGUI/ScrollBar.h>
#include <LibGfx/SystemTheme.h>
WebContentView::WebContentView()
@ -59,8 +60,8 @@ void WebContentView::resize_event(GUI::ResizeEvent& event)
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, event.size());
m_bitmap = bitmap->to_bitmap_backed_by_shared_buffer();
m_bitmap->shared_buffer()->share_with(client().server_pid());
client().post_message(Messages::WebContentServer::SetViewportRect(Gfx::IntRect({ 0, 0 }, event.size())));
client().post_message(Messages::WebContentServer::Paint(m_bitmap->rect(), m_bitmap->shbuf_id()));
client().post_message(Messages::WebContentServer::SetViewportRect(Gfx::IntRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, event.size())));
request_repaint();
}
void WebContentView::mousedown_event(GUI::MouseEvent& event)
@ -97,9 +98,20 @@ void WebContentView::notify_server_did_change_selection(Badge<WebContentClient>)
request_repaint();
}
void WebContentView::notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size)
{
set_content_size(content_size);
}
void WebContentView::did_scroll()
{
client().post_message(Messages::WebContentServer::SetViewportRect(Gfx::IntRect({ horizontal_scrollbar().value(), vertical_scrollbar().value() }, size())));
request_repaint();
}
void WebContentView::request_repaint()
{
client().post_message(Messages::WebContentServer::Paint(m_bitmap->rect(), m_bitmap->shbuf_id()));
client().post_message(Messages::WebContentServer::Paint(m_bitmap->rect().translated(horizontal_scrollbar().value(), vertical_scrollbar().value()), m_bitmap->shbuf_id()));
}
WebContentClient& WebContentView::client()

View file

@ -26,11 +26,12 @@
#pragma once
#include <LibGUI/ScrollableWidget.h>
#include <LibGUI/Widget.h>
class WebContentClient;
class WebContentView final : public GUI::Widget {
class WebContentView final : public GUI::ScrollableWidget {
C_OBJECT(WebContentView);
public:
@ -38,6 +39,7 @@ public:
void load(const URL&);
void notify_server_did_layout(Badge<WebContentClient>, const Gfx::IntSize& content_size);
void notify_server_did_paint(Badge<WebContentClient>, i32 shbuf_id);
void notify_server_did_invalidate_content_rect(Badge<WebContentClient>, const Gfx::IntRect&);
void notify_server_did_change_selection(Badge<WebContentClient>);
@ -45,12 +47,16 @@ public:
private:
WebContentView();
// ^Widget
virtual void paint_event(GUI::PaintEvent&) override;
virtual void resize_event(GUI::ResizeEvent&) override;
virtual void mousedown_event(GUI::MouseEvent&) override;
virtual void mouseup_event(GUI::MouseEvent&) override;
virtual void mousemove_event(GUI::MouseEvent&) override;
// ^ScrollableWidget
virtual void did_scroll() override;
void request_repaint();
WebContentClient& client();

View file

@ -66,26 +66,33 @@ void PageHost::set_palette_impl(const Gfx::PaletteImpl& impl)
m_palette_impl = impl;
}
Web::LayoutDocument* PageHost::layout_root()
{
auto* document = page().main_frame().document();
if (!document)
return nullptr;
return document->layout_node();
}
void PageHost::paint(const Gfx::IntRect& content_rect, Gfx::Bitmap& target)
{
Gfx::Painter painter(target);
Gfx::IntRect bitmap_rect { {}, content_rect.size() };
auto* document = page().main_frame().document();
if (!document)
return;
auto* layout_root = document->layout_node();
auto* layout_root = this->layout_root();
if (!layout_root) {
painter.fill_rect(content_rect, Color::White);
painter.fill_rect(bitmap_rect, Color::White);
return;
}
painter.fill_rect(content_rect, document->background_color(palette()));
painter.fill_rect(bitmap_rect, layout_root->document().background_color(palette()));
if (auto background_bitmap = document->background_image()) {
painter.draw_tiled_bitmap(content_rect, *background_bitmap);
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(), Gfx::IntPoint());
context.set_viewport_rect(content_rect);
layout_root->paint_all_phases(context);
@ -109,4 +116,12 @@ void PageHost::page_did_change_selection()
m_client.post_message(Messages::WebContentClient::DidChangeSelection());
}
void PageHost::page_did_layout()
{
auto* layout_root = this->layout_root();
ASSERT(layout_root);
auto content_size = enclosing_int_rect(layout_root->absolute_rect()).size();
m_client.post_message(Messages::WebContentClient::DidLayout(content_size));
}
}

View file

@ -53,9 +53,11 @@ private:
virtual Gfx::Palette palette() const override;
virtual void page_did_invalidate(const Gfx::IntRect&) override;
virtual void page_did_change_selection() override;
virtual void page_did_layout() override;
explicit PageHost(ClientConnection&);
Web::LayoutDocument* layout_root();
void setup_palette();
ClientConnection& m_client;

View file

@ -4,4 +4,5 @@ endpoint WebContentClient = 90
DidPaint(Gfx::IntRect content_rect, i32 shbuf_id) =|
DidInvalidateContentRect(Gfx::IntRect content_rect) =|
DidChangeSelection() =|
DidLayout(Gfx::IntSize content_size) =|
}