DOMRectReadOnly.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. public:
  22. static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMRectReadOnly>> construct_impl(JS::Realm&, double x = 0, double y = 0, double width = 0, double height = 0);
  23. [[nodiscard]] static JS::NonnullGCPtr<DOMRectReadOnly> from_rect(JS::VM&, DOMRectInit const&);
  24. virtual ~DOMRectReadOnly() override;
  25. double x() const { return m_rect.x(); }
  26. double y() const { return m_rect.y(); }
  27. double width() const { return m_rect.width(); }
  28. double height() const { return m_rect.height(); }
  29. double top() const { return min(y(), y() + height()); }
  30. double right() const { return max(x(), x() + width()); }
  31. double bottom() const { return max(y(), y() + height()); }
  32. double left() const { return min(x(), x() + width()); }
  33. protected:
  34. DOMRectReadOnly(JS::Realm&, double x, double y, double width, double height);
  35. virtual void initialize(JS::Realm&) override;
  36. Gfx::DoubleRect m_rect;
  37. };
  38. }