ladybird/Libraries/LibWeb/ResizeObserver/ResizeObserverSize.cpp
Shannon Booth f87041bf3a LibGC+Everywhere: Factor out a LibGC from LibJS
Resulting in a massive rename across almost everywhere! Alongside the
namespace change, we now have the following names:

 * JS::NonnullGCPtr -> GC::Ref
 * JS::GCPtr -> GC::Ptr
 * JS::HeapFunction -> GC::Function
 * JS::CellImpl -> GC::Cell
 * JS::Handle -> GC::Root
2024-11-15 14:49:20 +01:00

68 lines
3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGC/Heap.h>
#include <LibWeb/Bindings/ResizeObserverSizePrototype.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Painting/PaintableBox.h>
#include <LibWeb/ResizeObserver/ResizeObserverSize.h>
namespace Web::ResizeObserver {
GC_DEFINE_ALLOCATOR(ResizeObserverSize);
void ResizeObserverSize::initialize(JS::Realm& realm)
{
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE(ResizeObserverSize);
}
// https://drafts.csswg.org/resize-observer-1/#calculate-box-size
GC::Ref<ResizeObserverSize> ResizeObserverSize::calculate_box_size(JS::Realm& realm, DOM::Element& target, Bindings::ResizeObserverBoxOptions observed_box)
{
// 1. Let computedSize be a new ResizeObserverSize object.
auto computed_size = realm.create<ResizeObserverSize>(realm);
// FIXME: 2. If target is an SVGGraphicsElement that does not have an associated CSS layout box:
// Otherwise:
if (target.paintable_box()) {
auto const& paintable_box = *target.paintable_box();
switch (observed_box) {
case Bindings::ResizeObserverBoxOptions::BorderBox:
// 1. Set computedSizes inlineSize attribute to targets border area inline length.
computed_size->set_inline_size(paintable_box.border_box_width().to_double());
// 2. Set computedSizes blockSize attribute to targets border area block length.
computed_size->set_block_size(paintable_box.border_box_height().to_double());
break;
case Bindings::ResizeObserverBoxOptions::ContentBox:
// 1. Set computedSizes inlineSize attribute to targets content area inline length.
computed_size->set_inline_size(paintable_box.content_width().to_double());
// 2. Set computedSizes blockSize attribute to targets content area block length.
computed_size->set_block_size(paintable_box.content_height().to_double());
break;
case Bindings::ResizeObserverBoxOptions::DevicePixelContentBox: {
auto device_pixel_ratio = target.document().window()->device_pixel_ratio();
// 1. Set computedSizes inlineSize attribute to targets content area inline length, in integral device pixels.
computed_size->set_inline_size(paintable_box.border_box_width().to_double() * device_pixel_ratio);
// 2. Set computedSizes blockSize attribute to targets content area block length, in integral device pixels.
computed_size->set_block_size(paintable_box.border_box_height().to_double() * device_pixel_ratio);
break;
}
default:
VERIFY_NOT_REACHED();
}
}
// 3. Return computedSize.s
return computed_size;
}
bool ResizeObserverSize::equals(ResizeObserverSize const& other) const
{
return m_inline_size == other.m_inline_size && m_block_size == other.m_block_size;
}
}