2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-10-15 16:48:38 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-06-15 17:29:35 +02:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-07-25 21:31:47 -07:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-11-22 15:53:01 +01:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2021-09-19 17:46:28 +01:00
|
|
|
#include <LibWeb/Painting/BorderPainting.h>
|
2020-06-18 21:39:27 +02:00
|
|
|
#include <LibWeb/Painting/StackingContext.h>
|
2019-10-15 16:48:38 +02:00
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
namespace Web::Layout {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
class Box : public NodeWithStyleAndBoxModelMetrics {
|
2019-10-15 16:48:38 +02:00
|
|
|
public:
|
2021-10-14 22:23:20 +02:00
|
|
|
struct OverflowData {
|
|
|
|
Gfx::FloatRect scrollable_overflow_rect;
|
|
|
|
Gfx::FloatPoint scroll_offset;
|
|
|
|
};
|
|
|
|
|
2020-06-10 10:42:29 +02:00
|
|
|
const Gfx::FloatRect absolute_rect() const;
|
|
|
|
|
|
|
|
Gfx::FloatPoint effective_offset() const;
|
|
|
|
|
|
|
|
void set_offset(const Gfx::FloatPoint& offset);
|
|
|
|
void set_offset(float x, float y) { set_offset({ x, y }); }
|
|
|
|
|
2022-02-06 00:49:09 +01:00
|
|
|
Gfx::FloatSize const& content_size() const { return m_content_size; }
|
|
|
|
void set_content_size(Gfx::FloatSize const&);
|
|
|
|
void set_content_size(float width, float height) { set_content_size({ width, height }); }
|
2020-06-10 10:42:29 +02:00
|
|
|
|
2022-02-06 00:49:09 +01:00
|
|
|
void set_content_width(float width) { set_content_size(width, content_height()); }
|
|
|
|
void set_content_height(float height) { set_content_size(content_width(), height); }
|
|
|
|
float content_width() const { return m_content_size.width(); }
|
|
|
|
float content_height() const { return m_content_size.height(); }
|
2020-06-10 10:42:29 +02:00
|
|
|
|
2022-02-11 21:57:56 +01:00
|
|
|
Gfx::FloatRect absolute_padding_box_rect() const
|
2021-02-22 18:52:58 +01:00
|
|
|
{
|
2021-09-15 14:18:17 +02:00
|
|
|
auto absolute_rect = this->absolute_rect();
|
2021-02-22 18:52:58 +01:00
|
|
|
Gfx::FloatRect rect;
|
2021-09-15 14:18:17 +02:00
|
|
|
rect.set_x(absolute_rect.x() - box_model().padding.left);
|
2022-02-06 00:49:09 +01:00
|
|
|
rect.set_width(content_width() + box_model().padding.left + box_model().padding.right);
|
2021-09-15 14:18:17 +02:00
|
|
|
rect.set_y(absolute_rect.y() - box_model().padding.top);
|
2022-02-06 00:49:09 +01:00
|
|
|
rect.set_height(content_height() + box_model().padding.top + box_model().padding.bottom);
|
2021-02-22 18:52:58 +01:00
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2022-02-11 21:57:56 +01:00
|
|
|
Gfx::FloatRect absolute_border_box_rect() const
|
2021-02-22 18:52:58 +01:00
|
|
|
{
|
2022-02-11 21:57:56 +01:00
|
|
|
auto padded_rect = this->absolute_padding_box_rect();
|
2021-02-22 18:52:58 +01:00
|
|
|
Gfx::FloatRect rect;
|
|
|
|
rect.set_x(padded_rect.x() - box_model().border.left);
|
|
|
|
rect.set_width(padded_rect.width() + box_model().border.left + box_model().border.right);
|
|
|
|
rect.set_y(padded_rect.y() - box_model().border.top);
|
|
|
|
rect.set_height(padded_rect.height() + box_model().border.top + box_model().border.bottom);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:13:41 +01:00
|
|
|
float border_box_width() const
|
|
|
|
{
|
2020-12-12 21:02:06 +01:00
|
|
|
auto border_box = box_model().border_box();
|
2022-02-06 00:49:09 +01:00
|
|
|
return content_width() + border_box.left + border_box.right;
|
2020-12-07 21:13:41 +01:00
|
|
|
}
|
|
|
|
|
2021-01-02 03:46:26 +01:00
|
|
|
float border_box_height() const
|
|
|
|
{
|
|
|
|
auto border_box = box_model().border_box();
|
2022-02-06 00:49:09 +01:00
|
|
|
return content_height() + border_box.top + border_box.bottom;
|
2021-01-02 03:46:26 +01:00
|
|
|
}
|
|
|
|
|
2020-06-10 10:42:29 +02:00
|
|
|
float absolute_x() const { return absolute_rect().x(); }
|
|
|
|
float absolute_y() const { return absolute_rect().y(); }
|
|
|
|
Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }
|
|
|
|
|
2021-09-15 12:19:42 +01:00
|
|
|
bool is_out_of_flow(FormattingContext const&) const;
|
|
|
|
|
2020-08-05 16:55:56 +02:00
|
|
|
virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
|
2019-10-15 16:48:38 +02:00
|
|
|
virtual void set_needs_display() override;
|
|
|
|
|
2019-10-19 11:49:46 +02:00
|
|
|
bool is_body() const;
|
|
|
|
|
2022-02-27 10:26:38 +01:00
|
|
|
struct LineBoxFragmentCoordinate {
|
|
|
|
size_t line_box_index { 0 };
|
|
|
|
size_t fragment_index { 0 };
|
|
|
|
};
|
|
|
|
void set_containing_line_box_fragment(LineBoxFragmentCoordinate);
|
2020-06-10 10:42:29 +02:00
|
|
|
|
2020-06-15 17:29:35 +02:00
|
|
|
StackingContext* stacking_context() { return m_stacking_context; }
|
2020-07-01 19:02:28 +02:00
|
|
|
const StackingContext* stacking_context() const { return m_stacking_context; }
|
2020-06-15 17:29:35 +02:00
|
|
|
void set_stacking_context(NonnullOwnPtr<StackingContext> context) { m_stacking_context = move(context); }
|
|
|
|
StackingContext* enclosing_stacking_context();
|
|
|
|
|
2020-06-18 21:35:44 +02:00
|
|
|
virtual void paint(PaintContext&, PaintPhase) override;
|
2021-04-20 13:39:36 +02:00
|
|
|
virtual void paint_border(PaintContext& context);
|
2021-07-23 21:31:31 +02:00
|
|
|
virtual void paint_box_shadow(PaintContext& context);
|
2021-05-15 20:54:01 +02:00
|
|
|
virtual void paint_background(PaintContext& context);
|
2020-06-15 17:29:35 +02:00
|
|
|
|
2021-09-19 17:46:28 +01:00
|
|
|
Painting::BorderRadiusData normalized_border_radius_data();
|
2021-05-16 00:15:37 +02:00
|
|
|
|
2021-10-14 18:37:24 +02:00
|
|
|
virtual Optional<float> intrinsic_width() const { return {}; }
|
|
|
|
virtual Optional<float> intrinsic_height() const { return {}; }
|
2021-10-14 18:48:49 +02:00
|
|
|
virtual Optional<float> intrinsic_aspect_ratio() const { return {}; }
|
2021-10-14 18:37:24 +02:00
|
|
|
|
|
|
|
bool has_intrinsic_width() const { return intrinsic_width().has_value(); }
|
|
|
|
bool has_intrinsic_height() const { return intrinsic_height().has_value(); }
|
2021-10-14 18:48:49 +02:00
|
|
|
bool has_intrinsic_aspect_ratio() const { return intrinsic_aspect_ratio().has_value(); }
|
2021-10-14 18:37:24 +02:00
|
|
|
|
2022-02-27 10:55:39 +01:00
|
|
|
bool has_overflow() const { return m_overflow_data.has_value(); }
|
2021-10-14 22:23:20 +02:00
|
|
|
|
|
|
|
Optional<Gfx::FloatRect> scrollable_overflow_rect() const
|
|
|
|
{
|
2022-02-27 10:55:39 +01:00
|
|
|
if (!m_overflow_data.has_value())
|
2021-10-14 22:23:20 +02:00
|
|
|
return {};
|
|
|
|
return m_overflow_data->scrollable_overflow_rect;
|
|
|
|
}
|
|
|
|
|
2022-02-27 10:55:39 +01:00
|
|
|
void set_overflow_data(Optional<OverflowData> data) { m_overflow_data = move(data); }
|
2021-10-14 22:23:20 +02:00
|
|
|
|
2021-12-04 10:09:09 +01:00
|
|
|
virtual void before_children_paint(PaintContext&, PaintPhase) override;
|
|
|
|
virtual void after_children_paint(PaintContext&, PaintPhase) override;
|
2021-10-28 17:08:42 +02:00
|
|
|
|
2019-10-15 16:48:38 +02:00
|
|
|
protected:
|
2020-11-22 15:53:01 +01:00
|
|
|
Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
|
|
|
|
: NodeWithStyleAndBoxModelMetrics(document, node, move(style))
|
2019-10-15 16:48:38 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:10:53 +01:00
|
|
|
Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
|
|
|
|
: NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-10 10:42:29 +02:00
|
|
|
virtual void did_set_rect() { }
|
2020-06-05 19:13:27 +02:00
|
|
|
|
2019-10-15 16:48:38 +02:00
|
|
|
private:
|
2021-01-07 17:31:26 +01:00
|
|
|
virtual bool is_box() const final { return true; }
|
|
|
|
|
2020-06-10 10:42:29 +02:00
|
|
|
Gfx::FloatPoint m_offset;
|
2022-02-06 00:49:09 +01:00
|
|
|
Gfx::FloatSize m_content_size;
|
2020-06-10 10:42:29 +02:00
|
|
|
|
|
|
|
// Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
|
2022-02-27 10:26:38 +01:00
|
|
|
Optional<LineBoxFragmentCoordinate> m_containing_line_box_fragment;
|
2020-06-15 17:29:35 +02:00
|
|
|
|
|
|
|
OwnPtr<StackingContext> m_stacking_context;
|
2021-10-14 22:23:20 +02:00
|
|
|
|
2022-02-27 10:55:39 +01:00
|
|
|
Optional<OverflowData> m_overflow_data;
|
2019-10-15 16:48:38 +02:00
|
|
|
};
|
|
|
|
|
2021-01-07 17:31:26 +01:00
|
|
|
template<>
|
2021-01-17 09:34:01 +01:00
|
|
|
inline bool Node::fast_is<Box>() const { return is_box(); }
|
2021-01-07 17:31:26 +01:00
|
|
|
|
|
|
|
}
|