1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 13:57:36 +00:00

LibWeb+WebContent+headless-browser: Make Page aware of the display scale

For now, we just report it as "1" everywhere.

Replaced `screen_rect()` with `web_exposed_screen_area()` from the spec.
This commit is contained in:
Sam Atkins 2022-11-25 17:07:19 +00:00 committed by Linus Groh
parent 6361584d4a
commit 8dfeb67f8c
9 changed files with 118 additions and 34 deletions

View file

@ -149,7 +149,7 @@ void ConnectionFromClient::paint(Gfx::IntRect const& content_rect, i32 backing_s
void ConnectionFromClient::flush_pending_paint_requests()
{
for (auto& pending_paint : m_pending_paint_requests) {
m_page_host->paint(pending_paint.content_rect, *pending_paint.bitmap);
m_page_host->paint(pending_paint.content_rect.to_type<Web::DevicePixels>(), *pending_paint.bitmap);
async_did_paint(pending_paint.content_rect, pending_paint.bitmap_id);
}
m_pending_paint_requests.clear();
@ -461,9 +461,9 @@ Messages::WebContentServer::TakeDocumentScreenshotResponse ConnectionFromClient:
return { {} };
auto const& content_size = m_page_host->content_size();
Gfx::IntRect rect { { 0, 0 }, content_size };
Web::DevicePixelRect rect { { 0, 0 }, content_size };
auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, rect.size()).release_value_but_fixme_should_propagate_errors();
auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
m_page_host->paint(rect, *bitmap);
return { bitmap->to_shareable_bitmap() };

View file

@ -104,10 +104,10 @@ Web::Layout::InitialContainingBlock* PageHost::layout_root()
return document->layout_node();
}
void PageHost::paint(Gfx::IntRect const& content_rect, Gfx::Bitmap& target)
void PageHost::paint(Web::DevicePixelRect const& content_rect, Gfx::Bitmap& target)
{
Gfx::Painter painter(target);
Gfx::IntRect bitmap_rect { {}, content_rect.size() };
Gfx::IntRect bitmap_rect { {}, content_rect.size().to_type<int>() };
if (auto* document = page().top_level_browsing_context().active_document())
document->update_layout();
@ -118,9 +118,9 @@ void PageHost::paint(Gfx::IntRect const& content_rect, Gfx::Bitmap& target)
return;
}
Web::PaintContext context(painter, palette(), content_rect.top_left());
Web::PaintContext context(painter, palette(), content_rect.top_left().to_type<int>());
context.set_should_show_line_box_borders(m_should_show_line_box_borders);
context.set_viewport_rect(content_rect);
context.set_viewport_rect(content_rect.to_type<int>());
context.set_has_focus(m_has_focus);
layout_root->paint_all_phases(context);
}
@ -152,10 +152,10 @@ void PageHost::page_did_layout()
auto* layout_root = this->layout_root();
VERIFY(layout_root);
if (layout_root->paint_box()->has_overflow())
m_content_size = enclosing_int_rect(layout_root->paint_box()->scrollable_overflow_rect().value()).size();
m_content_size = page().enclosing_device_rect(layout_root->paint_box()->scrollable_overflow_rect().value().to_type<Web::CSSPixels>()).size();
else
m_content_size = enclosing_int_rect(layout_root->paint_box()->absolute_rect()).size();
m_client.async_did_layout(m_content_size);
m_content_size = page().enclosing_device_rect(layout_root->paint_box()->absolute_rect().to_type<Web::CSSPixels>()).size();
m_client.async_did_layout(m_content_size.to_type<int>());
}
void PageHost::page_did_change_title(DeprecatedString const& title)

View file

@ -9,6 +9,7 @@
#include <LibGfx/Rect.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/PixelUnits.h>
#include <WebContent/Forward.h>
namespace WebContent {
@ -26,11 +27,12 @@ public:
virtual Web::Page& page() override { return *m_page; }
virtual Web::Page const& page() const override { return *m_page; }
virtual void paint(Gfx::IntRect const& content_rect, Gfx::Bitmap&) override;
virtual void paint(Web::DevicePixelRect const& content_rect, Gfx::Bitmap&) override;
void set_palette_impl(Gfx::PaletteImpl const&);
void set_viewport_rect(Gfx::IntRect const&);
void set_screen_rects(Vector<Gfx::IntRect, 4> const& rects, size_t main_screen_index) { m_screen_rect = rects[main_screen_index]; };
void set_screen_rects(Vector<Gfx::IntRect, 4> const& rects, size_t main_screen_index) { m_screen_rect = rects[main_screen_index].to_type<Web::DevicePixels>(); }
void set_screen_display_scale(float device_pixels_per_css_pixel) { m_screen_display_scale = device_pixels_per_css_pixel; }
void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);
void set_should_show_line_box_borders(bool b) { m_should_show_line_box_borders = b; }
void set_has_focus(bool);
@ -38,7 +40,7 @@ public:
void set_window_position(Gfx::IntPoint);
void set_window_size(Gfx::IntSize);
Gfx::IntSize content_size() const { return m_content_size; }
Web::DevicePixelSize content_size() const { return m_content_size; }
ErrorOr<void> connect_to_webdriver(DeprecatedString const& webdriver_ipc_path);
@ -50,7 +52,8 @@ private:
// ^PageClient
virtual bool is_connection_open() const override;
virtual Gfx::Palette palette() const override;
virtual Gfx::IntRect screen_rect() const override { return m_screen_rect; }
virtual Web::DevicePixelRect screen_rect() const override { return m_screen_rect; }
virtual float device_pixels_per_css_pixel() const override { return m_screen_display_scale; }
virtual Web::CSS::PreferredColorScheme preferred_color_scheme() const override { return m_preferred_color_scheme; }
virtual void page_did_invalidate(Gfx::IntRect const&) override;
virtual void page_did_change_selection() override;
@ -104,8 +107,10 @@ private:
ConnectionFromClient& m_client;
NonnullOwnPtr<Web::Page> m_page;
RefPtr<Gfx::PaletteImpl> m_palette_impl;
Gfx::IntRect m_screen_rect;
Gfx::IntSize m_content_size;
Web::DevicePixelRect m_screen_rect;
Web::DevicePixelSize m_content_size;
// FIXME: Actually set this based on the device's pixel ratio.
float m_screen_display_scale { 1.0f };
bool m_should_show_line_box_borders { false };
bool m_has_focus { false };

View file

@ -1521,7 +1521,7 @@ Messages::WebDriverClient::TakeScreenshotResponse WebDriverConnection::take_scre
auto root_rect = calculate_absolute_rect_of_element(m_page_client.page(), *document->document_element());
auto encoded_string = TRY(Web::WebDriver::capture_element_screenshot(
[&](auto const& rect, auto& bitmap) { m_page_client.paint(rect, bitmap); },
[&](auto const& rect, auto& bitmap) { m_page_client.paint(rect.template to_type<Web::DevicePixels>(), bitmap); },
m_page_client.page(),
*document->document_element(),
root_rect));
@ -1554,7 +1554,7 @@ Messages::WebDriverClient::TakeElementScreenshotResponse WebDriverConnection::ta
auto element_rect = calculate_absolute_rect_of_element(m_page_client.page(), *element);
auto encoded_string = TRY(Web::WebDriver::capture_element_screenshot(
[&](auto const& rect, auto& bitmap) { m_page_client.paint(rect, bitmap); },
[&](auto const& rect, auto& bitmap) { m_page_client.paint(rect.template to_type<Web::DevicePixels>(), bitmap); },
m_page_client.page(),
*element,
element_rect));