2019-09-29 06:32:00 -03:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-09-29 06:50:35 -03:00
|
|
|
#include <LibHTML/DOM/Document.h>
|
2019-09-28 18:02:22 -03:00
|
|
|
#include <LibHTML/DOM/Element.h>
|
2019-07-01 02:28:37 -03:00
|
|
|
#include <LibHTML/Layout/LayoutBlock.h>
|
2019-06-15 18:41:15 -03:00
|
|
|
#include <LibHTML/Layout/LayoutNode.h>
|
2019-06-15 17:49:44 -03:00
|
|
|
|
2019-09-29 06:32:00 -03:00
|
|
|
//#define DRAW_BOXES_AROUND_LAYOUT_NODES
|
2019-09-29 06:50:35 -03:00
|
|
|
//#define DRAW_BOXES_AROUND_HOVERED_NODES
|
2019-09-29 06:32:00 -03:00
|
|
|
|
2019-09-21 09:32:17 -03:00
|
|
|
LayoutNode::LayoutNode(const Node* node, StyleProperties&& style_properties)
|
2019-06-15 17:49:44 -03:00
|
|
|
: m_node(node)
|
2019-09-21 09:32:17 -03:00
|
|
|
, m_style_properties(style_properties)
|
2019-06-15 17:49:44 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LayoutNode::~LayoutNode()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-16 16:35:03 -03:00
|
|
|
void LayoutNode::layout()
|
|
|
|
|
{
|
|
|
|
|
for_each_child([](auto& child) {
|
|
|
|
|
child.layout();
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-07-01 02:28:37 -03:00
|
|
|
|
|
|
|
|
const LayoutBlock* LayoutNode::containing_block() const
|
|
|
|
|
{
|
|
|
|
|
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
|
if (ancestor->is_block())
|
|
|
|
|
return static_cast<const LayoutBlock*>(ancestor);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-09-25 06:40:37 -03:00
|
|
|
|
|
|
|
|
void LayoutNode::render(RenderingContext& context)
|
|
|
|
|
{
|
2019-09-29 06:32:00 -03:00
|
|
|
#ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
|
|
|
|
|
context.painter().draw_rect(m_rect, Color::Blue);
|
2019-09-29 06:50:35 -03:00
|
|
|
#endif
|
|
|
|
|
#ifdef DRAW_BOXES_AROUND_HOVERED_NODES
|
|
|
|
|
if (!is_anonymous() && node() == document().hovered_node())
|
|
|
|
|
context.painter().draw_rect(m_rect, Color::Red);
|
2019-09-29 06:32:00 -03:00
|
|
|
#endif
|
2019-09-29 13:05:37 -03:00
|
|
|
|
|
|
|
|
auto bgcolor = style_properties().property("background-color");
|
2019-10-01 15:25:31 -03:00
|
|
|
if (bgcolor.has_value() && bgcolor.value()->is_color()) {
|
|
|
|
|
|
|
|
|
|
Rect background_rect;
|
|
|
|
|
background_rect.set_x(rect().x() - style().padding().left.to_px());
|
|
|
|
|
background_rect.set_width(rect().width() + style().padding().left.to_px() + style().padding().right.to_px());
|
|
|
|
|
background_rect.set_y(rect().y() - style().padding().top.to_px());
|
|
|
|
|
background_rect.set_height(rect().height() + style().padding().top.to_px() + style().padding().bottom.to_px());
|
|
|
|
|
|
|
|
|
|
context.painter().fill_rect(background_rect, bgcolor.value()->to_color());
|
|
|
|
|
}
|
2019-09-29 13:05:37 -03:00
|
|
|
|
|
|
|
|
// TODO: render our border
|
2019-09-25 06:40:37 -03:00
|
|
|
for_each_child([&](auto& child) {
|
|
|
|
|
child.render(context);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-09-28 18:02:22 -03:00
|
|
|
|
|
|
|
|
HitTestResult LayoutNode::hit_test(const Point& position) const
|
|
|
|
|
{
|
2019-09-29 06:32:00 -03:00
|
|
|
// FIXME: It would be nice if we could confidently skip over hit testing
|
|
|
|
|
// parts of the layout tree, but currently we can't just check
|
|
|
|
|
// m_rect.contains() since inline text rects can't be trusted..
|
|
|
|
|
HitTestResult result { m_rect.contains(position) ? this : nullptr };
|
2019-09-28 18:02:22 -03:00
|
|
|
for_each_child([&](auto& child) {
|
|
|
|
|
auto child_result = child.hit_test(position);
|
|
|
|
|
if (child_result.layout_node)
|
|
|
|
|
result = child_result;
|
|
|
|
|
});
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-09-29 06:43:33 -03:00
|
|
|
|
|
|
|
|
const Document& LayoutNode::document() const
|
|
|
|
|
{
|
|
|
|
|
if (is_anonymous())
|
|
|
|
|
return parent()->document();
|
|
|
|
|
return node()->document();
|
|
|
|
|
}
|