/src/serenity/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2024, Kenneth Myhra <kennethmyhra@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <LibGfx/Rect.h> |
11 | | #include <LibWeb/Bindings/PlatformObject.h> |
12 | | #include <LibWeb/Bindings/Serializable.h> |
13 | | #include <LibWeb/Forward.h> |
14 | | |
15 | | namespace Web::Geometry { |
16 | | |
17 | | // https://drafts.fxtf.org/geometry/#dictdef-domrectinit |
18 | | struct DOMRectInit { |
19 | | double x { 0.0 }; |
20 | | double y { 0.0 }; |
21 | | double width { 0.0 }; |
22 | | double height { 0.0 }; |
23 | | }; |
24 | | |
25 | | // https://drafts.fxtf.org/geometry/#domrectreadonly |
26 | | class DOMRectReadOnly |
27 | | : public Bindings::PlatformObject |
28 | | , public Bindings::Serializable { |
29 | | WEB_PLATFORM_OBJECT(DOMRectReadOnly, Bindings::PlatformObject); |
30 | | JS_DECLARE_ALLOCATOR(DOMRectReadOnly); |
31 | | |
32 | | public: |
33 | | static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectReadOnly>> construct_impl(JS::Realm&, double x = 0, double y = 0, double width = 0, double height = 0); |
34 | | [[nodiscard]] static JS::NonnullGCPtr<DOMRectReadOnly> from_rect(JS::VM&, DOMRectInit const&); |
35 | | static JS::NonnullGCPtr<DOMRectReadOnly> create(JS::Realm&); |
36 | | |
37 | | virtual ~DOMRectReadOnly() override; |
38 | | |
39 | 0 | double x() const { return m_rect.x(); } |
40 | 0 | double y() const { return m_rect.y(); } |
41 | 0 | double width() const { return m_rect.width(); } |
42 | 0 | double height() const { return m_rect.height(); } |
43 | | |
44 | | double top() const |
45 | 0 | { |
46 | 0 | if (isnan(y()) || isnan(height())) |
47 | 0 | return NAN; |
48 | 0 | return min(y(), y() + height()); |
49 | 0 | } |
50 | | |
51 | | double right() const |
52 | 0 | { |
53 | 0 | if (isnan(x()) || isnan(width())) |
54 | 0 | return NAN; |
55 | 0 | return max(x(), x() + width()); |
56 | 0 | } |
57 | | |
58 | | double bottom() const |
59 | 0 | { |
60 | 0 | if (isnan(y()) || isnan(height())) |
61 | 0 | return NAN; |
62 | 0 | return max(y(), y() + height()); |
63 | 0 | } |
64 | | |
65 | | double left() const |
66 | 0 | { |
67 | 0 | if (isnan(x()) || isnan(width())) |
68 | 0 | return NAN; |
69 | 0 | return min(x(), x() + width()); |
70 | 0 | } |
71 | | |
72 | 0 | virtual StringView interface_name() const override { return "DOMRectReadOnly"sv; } |
73 | | virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord&, bool for_storage, HTML::SerializationMemory&) override; |
74 | | virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const&, size_t& position, HTML::DeserializationMemory&) override; |
75 | | |
76 | | protected: |
77 | | DOMRectReadOnly(JS::Realm&, double x, double y, double width, double height); |
78 | | explicit DOMRectReadOnly(JS::Realm&); |
79 | | |
80 | | virtual void initialize(JS::Realm&) override; |
81 | | |
82 | | Gfx::DoubleRect m_rect; |
83 | | }; |
84 | | } |