LayoutNode.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/Layout/LayoutBlock.h>
  5. #include <LibHTML/Layout/LayoutNode.h>
  6. //#define DRAW_BOXES_AROUND_LAYOUT_NODES
  7. //#define DRAW_BOXES_AROUND_HOVERED_NODES
  8. LayoutNode::LayoutNode(const Node* node, StyleProperties&& style_properties)
  9. : m_node(node)
  10. , m_style_properties(style_properties)
  11. {
  12. }
  13. LayoutNode::~LayoutNode()
  14. {
  15. }
  16. void LayoutNode::layout()
  17. {
  18. for_each_child([](auto& child) {
  19. child.layout();
  20. });
  21. }
  22. const LayoutBlock* LayoutNode::containing_block() const
  23. {
  24. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  25. if (ancestor->is_block())
  26. return static_cast<const LayoutBlock*>(ancestor);
  27. }
  28. return nullptr;
  29. }
  30. void LayoutNode::render(RenderingContext& context)
  31. {
  32. #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
  33. context.painter().draw_rect(m_rect, Color::Blue);
  34. #endif
  35. #ifdef DRAW_BOXES_AROUND_HOVERED_NODES
  36. if (!is_anonymous() && node() == document().hovered_node())
  37. context.painter().draw_rect(m_rect, Color::Red);
  38. #endif
  39. Rect padded_rect;
  40. padded_rect.set_x(rect().x() - style().padding().left.to_px());
  41. padded_rect.set_width(rect().width() + style().padding().left.to_px() + style().padding().right.to_px());
  42. padded_rect.set_y(rect().y() - style().padding().top.to_px());
  43. padded_rect.set_height(rect().height() + style().padding().top.to_px() + style().padding().bottom.to_px());
  44. auto bgcolor = style_properties().property("background-color");
  45. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  46. context.painter().fill_rect(padded_rect, bgcolor.value()->to_color());
  47. }
  48. auto border_width_value = style_properties().property("border-width");
  49. auto border_color_value = style_properties().property("border-color");
  50. if (border_width_value.has_value() && border_color_value.has_value()) {
  51. int border_width = border_width_value.value()->to_length().to_px();
  52. Color border_color = border_color_value.value()->to_color();
  53. context.painter().draw_line(padded_rect.top_left(), padded_rect.top_right(), border_color, border_width);
  54. context.painter().draw_line(padded_rect.top_right(), padded_rect.bottom_right(), border_color, border_width);
  55. context.painter().draw_line(padded_rect.bottom_right(), padded_rect.bottom_left(), border_color, border_width);
  56. context.painter().draw_line(padded_rect.bottom_left(), padded_rect.top_left(), border_color, border_width);
  57. }
  58. // TODO: render our border
  59. for_each_child([&](auto& child) {
  60. child.render(context);
  61. });
  62. }
  63. HitTestResult LayoutNode::hit_test(const Point& position) const
  64. {
  65. // FIXME: It would be nice if we could confidently skip over hit testing
  66. // parts of the layout tree, but currently we can't just check
  67. // m_rect.contains() since inline text rects can't be trusted..
  68. HitTestResult result { m_rect.contains(position) ? this : nullptr };
  69. for_each_child([&](auto& child) {
  70. auto child_result = child.hit_test(position);
  71. if (child_result.layout_node)
  72. result = child_result;
  73. });
  74. return result;
  75. }
  76. const Document& LayoutNode::document() const
  77. {
  78. if (is_anonymous())
  79. return parent()->document();
  80. return node()->document();
  81. }