IntersectionObserverEntry.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/Intrinsics.h>
  7. #include <LibWeb/DOM/Element.h>
  8. #include <LibWeb/IntersectionObserver/IntersectionObserverEntry.h>
  9. namespace Web::IntersectionObserver {
  10. WebIDL::ExceptionOr<JS::NonnullGCPtr<IntersectionObserverEntry>> IntersectionObserverEntry::construct_impl(JS::Realm& realm, Web::IntersectionObserver::IntersectionObserverEntryInit const& options)
  11. {
  12. auto& vm = realm.vm();
  13. JS::GCPtr<Geometry::DOMRectReadOnly> root_bounds;
  14. if (options.root_bounds.has_value())
  15. root_bounds = TRY(Geometry::DOMRectReadOnly::from_rect(vm, options.root_bounds.value()));
  16. auto bounding_client_rect = TRY(Geometry::DOMRectReadOnly::from_rect(vm, options.bounding_client_rect));
  17. auto intersection_rect = TRY(Geometry::DOMRectReadOnly::from_rect(vm, options.intersection_rect));
  18. return MUST_OR_THROW_OOM(realm.heap().allocate<IntersectionObserverEntry>(realm, realm, options.time, root_bounds, bounding_client_rect, intersection_rect, options.is_intersecting, options.intersection_ratio, *options.target));
  19. }
  20. IntersectionObserverEntry::IntersectionObserverEntry(JS::Realm& realm, HighResolutionTime::DOMHighResTimeStamp time, JS::GCPtr<Geometry::DOMRectReadOnly> root_bounds, JS::NonnullGCPtr<Geometry::DOMRectReadOnly> bounding_client_rect, JS::NonnullGCPtr<Geometry::DOMRectReadOnly> intersection_rect, bool is_intersecting, double intersection_ratio, JS::NonnullGCPtr<DOM::Element> target)
  21. : Bindings::PlatformObject(realm)
  22. , m_time(time)
  23. , m_root_bounds(root_bounds)
  24. , m_bounding_client_rect(bounding_client_rect)
  25. , m_intersection_rect(intersection_rect)
  26. , m_is_intersecting(is_intersecting)
  27. , m_intersection_ratio(intersection_ratio)
  28. , m_target(target)
  29. {
  30. }
  31. IntersectionObserverEntry::~IntersectionObserverEntry() = default;
  32. JS::ThrowCompletionOr<void> IntersectionObserverEntry::initialize(JS::Realm& realm)
  33. {
  34. MUST_OR_THROW_OOM(Base::initialize(realm));
  35. set_prototype(&Bindings::ensure_web_prototype<Bindings::IntersectionObserverEntryPrototype>(realm, "IntersectionObserverEntry"));
  36. return {};
  37. }
  38. void IntersectionObserverEntry::visit_edges(JS::Cell::Visitor& visitor)
  39. {
  40. Base::visit_edges(visitor);
  41. visitor.visit(m_root_bounds);
  42. visitor.visit(m_bounding_client_rect);
  43. visitor.visit(m_intersection_rect);
  44. visitor.visit(m_target);
  45. }
  46. }