LayoutNode.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // TODO: render our background and border
  40. for_each_child([&](auto& child) {
  41. child.render(context);
  42. });
  43. }
  44. HitTestResult LayoutNode::hit_test(const Point& position) const
  45. {
  46. // FIXME: It would be nice if we could confidently skip over hit testing
  47. // parts of the layout tree, but currently we can't just check
  48. // m_rect.contains() since inline text rects can't be trusted..
  49. HitTestResult result { m_rect.contains(position) ? this : nullptr };
  50. for_each_child([&](auto& child) {
  51. auto child_result = child.hit_test(position);
  52. if (child_result.layout_node)
  53. result = child_result;
  54. });
  55. return result;
  56. }
  57. const Document& LayoutNode::document() const
  58. {
  59. if (is_anonymous())
  60. return parent()->document();
  61. return node()->document();
  62. }