ladybird/Userland/Libraries/LibWeb/ResizeObserver/ResizeObservation.cpp
Aliaksandr Kalenik fb8edcea00 LibWeb: Flesh out implementation of ResizeObserver interfaces
Adds the initial implementation for interfaces defined in the
ResizeObserver specification. These interfaces will be used to
construct and send observation events in the upcoming changes.
2024-02-20 10:55:10 +01:00

54 lines
1.7 KiB
C++

/*
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Heap/Heap.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/Painting/PaintableBox.h>
#include <LibWeb/ResizeObserver/ResizeObservation.h>
namespace Web::ResizeObserver {
JS_DEFINE_ALLOCATOR(ResizeObservation);
WebIDL::ExceptionOr<JS::NonnullGCPtr<ResizeObservation>> ResizeObservation::create(JS::Realm& realm, DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
{
return realm.heap().allocate<ResizeObservation>(realm, realm, target, observed_box);
}
ResizeObservation::ResizeObservation(JS::Realm& realm, DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
: m_realm(realm)
, m_target(target)
, m_observed_box(observed_box)
{
auto computed_size = realm.heap().allocate<ResizeObserverSize>(realm, realm);
m_last_reported_sizes.append(computed_size);
}
void ResizeObservation::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_target);
for (auto& size : m_last_reported_sizes)
visitor.visit(size);
}
// https://drafts.csswg.org/resize-observer-1/#dom-resizeobservation-isactive
bool ResizeObservation::is_active()
{
// 1. Set currentSize by calculate box size given target and observedBox.
auto current_size = ResizeObserverSize::calculate_box_size(m_realm, m_target, m_observed_box);
// 2. Return true if currentSize is not equal to the first entry in this.lastReportedSizes.
VERIFY(!m_last_reported_sizes.is_empty());
if (!m_last_reported_sizes.first()->equals(*current_size))
return true;
// 3. Return false.
return false;
}
}