ladybird/Userland/Libraries/LibWeb/CSS/Screen.cpp
Aliaksandr Kalenik 147c3b3d97 LibWeb+WebContent: Forbid access to underlying type of CSSPixels
Although DistinctNumeric, which is supposed to abstract the underlying
type, was used to represent CSSPixels, we have a whole bunch of places
in the layout code that assume CSSPixels::value() returns a
floating-point type. This assumption makes it difficult to replace the
underlying type in CSSPixels with a non-floating type.

To make it easier to transition CSSPixels to fixed-point math, one step
we can take is to prevent access to the underlying type using value()
and instead use explicit conversions with the to_float(), to_double(),
and to_int() methods.
2023-06-13 06:08:27 +02:00

52 lines
1.3 KiB
C++

/*
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/Rect.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/ScreenPrototype.h>
#include <LibWeb/CSS/Screen.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Page/Page.h>
namespace Web::CSS {
WebIDL::ExceptionOr<JS::NonnullGCPtr<Screen>> Screen::create(HTML::Window& window)
{
return MUST_OR_THROW_OOM(window.heap().allocate<Screen>(window.realm(), window));
}
Screen::Screen(HTML::Window& window)
: PlatformObject(window.realm())
, m_window(window)
{
}
JS::ThrowCompletionOr<void> Screen::initialize(JS::Realm& realm)
{
MUST_OR_THROW_OOM(Base::initialize(realm));
set_prototype(&Bindings::ensure_web_prototype<Bindings::ScreenPrototype>(realm, "Screen"));
return {};
}
void Screen::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_window.ptr());
}
Gfx::IntRect Screen::screen_rect() const
{
auto screen_rect_in_css_pixels = window().page()->web_exposed_screen_area();
return {
screen_rect_in_css_pixels.x().to_int(),
screen_rect_in_css_pixels.y().to_int(),
screen_rect_in_css_pixels.width().to_int(),
screen_rect_in_css_pixels.height().to_int()
};
}
}