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 <AK/RefCounted.h>
  8. #include <LibGfx/Rect.h>
  9. #include <LibWeb/Bindings/Wrappable.h>
  10. #include <LibWeb/Forward.h>
  11. namespace Web::Geometry {
  12. // https://drafts.fxtf.org/geometry/#domrectreadonly
  13. class DOMRectReadOnly
  14. : public RefCounted<DOMRectReadOnly>
  15. , public Bindings::Wrappable {
  16. public:
  17. using WrapperType = Bindings::DOMRectReadOnlyWrapper;
  18. static NonnullRefPtr<DOMRectReadOnly> create_with_global_object(Bindings::WindowObject&, double x = 0, double y = 0, double width = 0, double height = 0)
  19. {
  20. return DOMRectReadOnly::create(x, y, width, height);
  21. }
  22. static NonnullRefPtr<DOMRectReadOnly> create(double x = 0, double y = 0, double width = 0, double height = 0)
  23. {
  24. return adopt_ref(*new DOMRectReadOnly(x, y, width, height));
  25. }
  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(float x, float y, float width, float height)
  36. : m_rect(x, y, width, height)
  37. {
  38. }
  39. Gfx::FloatRect m_rect;
  40. };
  41. }