1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:37:35 +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

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -54,9 +55,17 @@ Gfx::Palette Page::palette() const
return m_client.palette();
}
Gfx::IntRect Page::screen_rect() const
// https://w3c.github.io/csswg-drafts/cssom-view-1/#web-exposed-screen-area
CSSPixelRect Page::web_exposed_screen_area() const
{
return m_client.screen_rect();
auto device_pixel_rect = m_client.screen_rect();
auto scale = client().device_pixels_per_css_pixel();
return {
device_pixel_rect.x().value() / scale,
device_pixel_rect.y().value() / scale,
device_pixel_rect.width().value() / scale,
device_pixel_rect.height().value() / scale
};
}
CSS::PreferredColorScheme Page::preferred_color_scheme() const
@ -64,6 +73,55 @@ CSS::PreferredColorScheme Page::preferred_color_scheme() const
return m_client.preferred_color_scheme();
}
CSSPixelPoint Page::device_to_css_point(DevicePixelPoint point) const
{
return {
point.x().value() / client().device_pixels_per_css_pixel(),
point.y().value() / client().device_pixels_per_css_pixel(),
};
}
DevicePixelPoint Page::css_to_device_point(CSSPixelPoint point) const
{
return {
point.x().value() * client().device_pixels_per_css_pixel(),
point.y().value() * client().device_pixels_per_css_pixel(),
};
}
CSSPixelRect Page::device_to_css_rect(DevicePixelRect rect) const
{
auto scale = client().device_pixels_per_css_pixel();
return {
rect.x().value() / scale,
rect.y().value() / scale,
rect.width().value() / scale,
rect.height().value() / scale
};
}
DevicePixelRect Page::enclosing_device_rect(CSSPixelRect rect) const
{
auto scale = client().device_pixels_per_css_pixel();
return {
floorf(rect.x().value() * scale),
floorf(rect.y().value() * scale),
ceilf(rect.width().value() * scale),
ceilf(rect.height().value() * scale)
};
}
DevicePixelRect Page::rounded_device_rect(CSSPixelRect rect) const
{
auto scale = client().device_pixels_per_css_pixel();
return {
roundf(rect.x().value() * scale),
roundf(rect.y().value() * scale),
roundf(rect.width().value() * scale),
roundf(rect.height().value() * scale)
};
}
bool Page::handle_mousewheel(Gfx::IntPoint position, unsigned button, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y)
{
return top_level_browsing_context().event_handler().handle_mousewheel(position, button, buttons, modifiers, wheel_delta_x, wheel_delta_y);

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -25,6 +26,7 @@
#include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Loader/FileRequest.h>
#include <LibWeb/PixelUnits.h>
namespace Web {
@ -54,6 +56,12 @@ public:
void load_html(StringView, const AK::URL&);
CSSPixelPoint device_to_css_point(DevicePixelPoint) const;
DevicePixelPoint css_to_device_point(CSSPixelPoint) const;
CSSPixelRect device_to_css_rect(DevicePixelRect) const;
DevicePixelRect enclosing_device_rect(CSSPixelRect) const;
DevicePixelRect rounded_device_rect(CSSPixelRect) const;
bool handle_mouseup(Gfx::IntPoint, unsigned button, unsigned buttons, unsigned modifiers);
bool handle_mousedown(Gfx::IntPoint, unsigned button, unsigned buttons, unsigned modifiers);
bool handle_mousemove(Gfx::IntPoint, unsigned buttons, unsigned modifiers);
@ -64,7 +72,7 @@ public:
bool handle_keyup(KeyCode, unsigned modifiers, u32 code_point);
Gfx::Palette palette() const;
Gfx::IntRect screen_rect() const;
CSSPixelRect web_exposed_screen_area() const;
CSS::PreferredColorScheme preferred_color_scheme() const;
bool is_same_origin_policy_enabled() const { return m_same_origin_policy_enabled; }
@ -139,9 +147,10 @@ public:
virtual Page const& page() const = 0;
virtual bool is_connection_open() const = 0;
virtual Gfx::Palette palette() const = 0;
virtual Gfx::IntRect screen_rect() const = 0;
virtual DevicePixelRect screen_rect() const = 0;
virtual float device_pixels_per_css_pixel() const = 0;
virtual CSS::PreferredColorScheme preferred_color_scheme() const = 0;
virtual void paint(Gfx::IntRect const&, Gfx::Bitmap&) = 0;
virtual void paint(DevicePixelRect const&, Gfx::Bitmap&) = 0;
virtual void page_did_change_title(DeprecatedString const&) { }
virtual void page_did_request_navigate_back() { }
virtual void page_did_request_navigate_forward() { }