DOMRectReadOnly.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGfx/Rect.h>
  8. #include <LibWeb/Bindings/PlatformObject.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::Geometry {
  11. // https://drafts.fxtf.org/geometry/#dictdef-domrectinit
  12. struct DOMRectInit {
  13. double x { 0.0 };
  14. double y { 0.0 };
  15. double width { 0.0 };
  16. double height { 0.0 };
  17. };
  18. // https://drafts.fxtf.org/geometry/#domrectreadonly
  19. class DOMRectReadOnly : public Bindings::PlatformObject {
  20. WEB_PLATFORM_OBJECT(DOMRectReadOnly, Bindings::PlatformObject);
  21. JS_DECLARE_ALLOCATOR(DOMRectReadOnly);
  22. public:
  23. static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectReadOnly>> construct_impl(JS::Realm&, double x = 0, double y = 0, double width = 0, double height = 0);
  24. [[nodiscard]] static JS::NonnullGCPtr<DOMRectReadOnly> from_rect(JS::VM&, DOMRectInit const&);
  25. virtual ~DOMRectReadOnly() override;
  26. double x() const { return m_rect.x(); }
  27. double y() const { return m_rect.y(); }
  28. double width() const { return m_rect.width(); }
  29. double height() const { return m_rect.height(); }
  30. double top() const
  31. {
  32. if (isnan(y()) || isnan(height()))
  33. return NAN;
  34. return min(y(), y() + height());
  35. }
  36. double right() const
  37. {
  38. if (isnan(x()) || isnan(width()))
  39. return NAN;
  40. return max(x(), x() + width());
  41. }
  42. double bottom() const
  43. {
  44. if (isnan(y()) || isnan(height()))
  45. return NAN;
  46. return max(y(), y() + height());
  47. }
  48. double left() const
  49. {
  50. if (isnan(x()) || isnan(width()))
  51. return NAN;
  52. return min(x(), x() + width());
  53. }
  54. protected:
  55. DOMRectReadOnly(JS::Realm&, double x, double y, double width, double height);
  56. virtual void initialize(JS::Realm&) override;
  57. Gfx::DoubleRect m_rect;
  58. };
  59. }