DOMRect.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <LibGfx/Rect.h>
  9. #include <LibWeb/Bindings/Wrappable.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web::Geometry {
  12. // FIXME: Split this into DOMRectReadOnly and DOMRect
  13. // https://drafts.fxtf.org/geometry/#DOMRect
  14. class DOMRect final
  15. : public RefCounted<DOMRect>
  16. , public Bindings::Wrappable {
  17. public:
  18. using WrapperType = Bindings::DOMRectWrapper;
  19. static NonnullRefPtr<DOMRect> create_with_global_object(Bindings::WindowObject&, double x = 0, double y = 0, double width = 0, double height = 0)
  20. {
  21. return DOMRect::create(x, y, width, height);
  22. }
  23. static NonnullRefPtr<DOMRect> create(double x = 0, double y = 0, double width = 0, double height = 0)
  24. {
  25. return adopt_ref(*new DOMRect(x, y, width, height));
  26. }
  27. static NonnullRefPtr<DOMRect> create(Gfx::FloatRect const& rect)
  28. {
  29. return adopt_ref(*new DOMRect(rect.x(), rect.y(), rect.width(), rect.height()));
  30. }
  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 { return min(y(), y() + height()); }
  36. double right() const { return max(x(), x() + width()); }
  37. double bottom() const { return max(y(), y() + height()); }
  38. double left() const { return min(x(), x() + width()); }
  39. private:
  40. DOMRect(float x, float y, float width, float height)
  41. : m_rect(x, y, width, height)
  42. {
  43. }
  44. Gfx::FloatRect m_rect;
  45. };
  46. }