DOMRectReadOnly.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 { return min(y(), y() + height()); }
  31. double right() const { return max(x(), x() + width()); }
  32. double bottom() const { return max(y(), y() + height()); }
  33. double left() const { return min(x(), x() + width()); }
  34. protected:
  35. DOMRectReadOnly(JS::Realm&, double x, double y, double width, double height);
  36. virtual void initialize(JS::Realm&) override;
  37. Gfx::DoubleRect m_rect;
  38. };
  39. }