ResizeObservation.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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_realm);
  29. visitor.visit(m_target);
  30. for (auto& size : m_last_reported_sizes)
  31. visitor.visit(size);
  32. }
  33. // https://drafts.csswg.org/resize-observer-1/#dom-resizeobservation-isactive
  34. bool ResizeObservation::is_active()
  35. {
  36. // 1. Set currentSize by calculate box size given target and observedBox.
  37. auto current_size = ResizeObserverSize::calculate_box_size(m_realm, m_target, m_observed_box);
  38. // 2. Return true if currentSize is not equal to the first entry in this.lastReportedSizes.
  39. VERIFY(!m_last_reported_sizes.is_empty());
  40. if (!m_last_reported_sizes.first()->equals(*current_size))
  41. return true;
  42. // 3. Return false.
  43. return false;
  44. }
  45. }