DOMRectReadOnly.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #pragma once
  8. #include <LibGfx/Rect.h>
  9. #include <LibWeb/Bindings/PlatformObject.h>
  10. #include <LibWeb/Bindings/Serializable.h>
  11. #include <LibWeb/Forward.h>
  12. namespace Web::Geometry {
  13. // https://drafts.fxtf.org/geometry/#dictdef-domrectinit
  14. struct DOMRectInit {
  15. double x { 0.0 };
  16. double y { 0.0 };
  17. double width { 0.0 };
  18. double height { 0.0 };
  19. };
  20. // https://drafts.fxtf.org/geometry/#domrectreadonly
  21. class DOMRectReadOnly
  22. : public Bindings::PlatformObject
  23. , public Bindings::Serializable {
  24. WEB_PLATFORM_OBJECT(DOMRectReadOnly, Bindings::PlatformObject);
  25. JS_DECLARE_ALLOCATOR(DOMRectReadOnly);
  26. public:
  27. static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectReadOnly>> construct_impl(JS::Realm&, double x = 0, double y = 0, double width = 0, double height = 0);
  28. [[nodiscard]] static JS::NonnullGCPtr<DOMRectReadOnly> from_rect(JS::VM&, DOMRectInit const&);
  29. static JS::NonnullGCPtr<DOMRectReadOnly> create(JS::Realm&);
  30. virtual ~DOMRectReadOnly() override;
  31. double x() const { return m_rect.x(); }
  32. double y() const { return m_rect.y(); }
  33. double width() const { return m_rect.width(); }
  34. double height() const { return m_rect.height(); }
  35. double top() const
  36. {
  37. if (isnan(y()) || isnan(height()))
  38. return NAN;
  39. return min(y(), y() + height());
  40. }
  41. double right() const
  42. {
  43. if (isnan(x()) || isnan(width()))
  44. return NAN;
  45. return max(x(), x() + width());
  46. }
  47. double bottom() const
  48. {
  49. if (isnan(y()) || isnan(height()))
  50. return NAN;
  51. return max(y(), y() + height());
  52. }
  53. double left() const
  54. {
  55. if (isnan(x()) || isnan(width()))
  56. return NAN;
  57. return min(x(), x() + width());
  58. }
  59. virtual StringView interface_name() const override { return "DOMRectReadOnly"sv; }
  60. virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord&, bool for_storage, HTML::SerializationMemory&) override;
  61. virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const&, size_t& position, HTML::DeserializationMemory&) override;
  62. protected:
  63. DOMRectReadOnly(JS::Realm&, double x, double y, double width, double height);
  64. explicit DOMRectReadOnly(JS::Realm&);
  65. virtual void initialize(JS::Realm&) override;
  66. Gfx::DoubleRect m_rect;
  67. };
  68. }