Truly independent web browser
/*
* Copyright (c) 2025, Jonathan Gamble <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/CSSStyleProperties.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/Layout/Box.h>
#include <LibWeb/Page/ElementResizeAction.h>
#include <LibWeb/Painting/BoxViews.h>
#include <LibWeb/Painting/ChromeMetrics.h>
// https://drafts.csswg.org/css-ui#resize
namespace Web {
static Optional<CSSPixelSize> containing_block_padding_box_size(Layout::Node const& layout_node)
{
auto parent_box = layout_node.containing_block();
if (!parent_box)
return {};
if (Painting::has_committed_box(*parent_box))
return Painting::absolute_padding_box_rect(*parent_box).size();
return {};
}
ElementResizeAction::ElementResizeAction(GC::Ref<DOM::Element> element, CSSPixelPoint pointer_down_origin)
: m_element(element)
, m_pointer_down_origin(pointer_down_origin)
{
Layout::ForcedReadScope read { element->document() };
auto const* layout_node = element->layout_node(read);
if (layout_node && Painting::has_committed_box(*layout_node))
m_initial_border_box_size = Painting::absolute_border_box_rect(*layout_node).size();
}
void ElementResizeAction::handle_pointer_move(CSSPixelPoint pointer_position)
{
auto element = m_element.ptr();
if (!element || !element->is_connected())
return;
Layout::ForcedReadScope read { element->document() };
auto const* layout_node_pointer = element->layout_node(read);
if (!layout_node_pointer || !Painting::has_committed_box(*layout_node_pointer))
return;
auto const& layout_node = *layout_node_pointer;
auto resize = layout_node.resize();
if (resize == CSS::Resize::None)
return;
bool horizontal_writing_mode = layout_node.writing_mode() == CSS::WritingMode::HorizontalTb;
bool resize_x = resize == CSS::Resize::Both
|| resize == CSS::Resize::Horizontal
|| (resize == CSS::Resize::Inline && horizontal_writing_mode)
|| (resize == CSS::Resize::Block && !horizontal_writing_mode);
bool resize_y = resize == CSS::Resize::Both
|| resize == CSS::Resize::Vertical
|| (resize == CSS::Resize::Inline && !horizontal_writing_mode)
|| (resize == CSS::Resize::Block && horizontal_writing_mode);
CSSPixels dx = resize_x ? pointer_position.x() - m_pointer_down_origin.x() : 0;
CSSPixels dy = resize_y ? pointer_position.y() - m_pointer_down_origin.y() : 0;
auto writing_mode = layout_node.writing_mode();
if ((writing_mode == CSS::WritingMode::HorizontalTb && layout_node.direction() == CSS::Direction::Rtl)
|| writing_mode == CSS::WritingMode::VerticalRl
|| writing_mode == CSS::WritingMode::SidewaysRl) {
dx = -dx;
}
CSSPixels css_width = max(ChromeMetrics::ZOOM_INVARIANT_RESIZE_GRIPPER_SIZE, m_initial_border_box_size.width() + dx);
CSSPixels css_height = max(ChromeMetrics::ZOOM_INVARIANT_RESIZE_GRIPPER_SIZE, m_initial_border_box_size.height() + dy);
auto reference_basis = containing_block_padding_box_size(layout_node);
if (reference_basis.has_value()) {
double width = css_width.to_double();
double height = css_height.to_double();
CSS::ComputedValuesFFI::rust_sizing_values_clamp_size(
&layout_node.style_group<CSS::ComputedValues::SizingValues>(),
reference_basis->width().to_double(),
reference_basis->height().to_double(),
&width,
&height);
css_width = CSSPixels::nearest_value_for(width);
css_height = CSSPixels::nearest_value_for(height);
}
if (layout_node.box_sizing() == CSS::BoxSizing::ContentBox) {
auto const metrics = Painting::box_model(layout_node);
css_width -= metrics.padding.left + metrics.padding.right + layout_node.border_left().width + layout_node.border_right().width;
css_height -= metrics.padding.top + metrics.padding.bottom + layout_node.border_top().width + layout_node.border_bottom().width;
}
auto style = element->style();
auto width_str = Utf16String::formatted("{:.2f}px", max(0.0, css_width.to_double()));
auto height_str = Utf16String::formatted("{:.2f}px", max(0.0, css_height.to_double()));
MUST(style->set_property(CSS::PropertyID::Width, width_str.utf16_view()));
MUST(style->set_property(CSS::PropertyID::Height, height_str.utf16_view()));
}
}