ResizeObservation.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Heap/Heap.h>
  7. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  8. #include <LibWeb/DOM/Element.h>
  9. #include <LibWeb/Painting/PaintableBox.h>
  10. #include <LibWeb/ResizeObserver/ResizeObservation.h>
  11. namespace Web::ResizeObserver {
  12. JS_DEFINE_ALLOCATOR(ResizeObservation);
  13. WebIDL::ExceptionOr<JS::NonnullGCPtr<ResizeObservation>> ResizeObservation::create(JS::Realm& realm, DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
  14. {
  15. return realm.heap().allocate<ResizeObservation>(realm, realm, target, observed_box);
  16. }
  17. ResizeObservation::ResizeObservation(JS::Realm& realm, DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
  18. : m_realm(realm)
  19. , m_target(target)
  20. , m_observed_box(observed_box)
  21. {
  22. auto computed_size = realm.heap().allocate<ResizeObserverSize>(realm, realm);
  23. m_last_reported_sizes.append(computed_size);
  24. }
  25. void ResizeObservation::visit_edges(JS::Cell::Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. visitor.visit(m_target);
  29. for (auto& size : m_last_reported_sizes)
  30. visitor.visit(size);
  31. }
  32. // https://drafts.csswg.org/resize-observer-1/#dom-resizeobservation-isactive
  33. bool ResizeObservation::is_active()
  34. {
  35. // 1. Set currentSize by calculate box size given target and observedBox.
  36. auto current_size = ResizeObserverSize::calculate_box_size(m_realm, m_target, m_observed_box);
  37. // 2. Return true if currentSize is not equal to the first entry in this.lastReportedSizes.
  38. VERIFY(!m_last_reported_sizes.is_empty());
  39. if (!m_last_reported_sizes.first()->equals(*current_size))
  40. return true;
  41. // 3. Return false.
  42. return false;
  43. }
  44. }