LayoutNode.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/DOM/Element.h>
  3. #include <LibHTML/Layout/LayoutBlock.h>
  4. #include <LibHTML/Layout/LayoutNode.h>
  5. //#define DRAW_BOXES_AROUND_LAYOUT_NODES
  6. LayoutNode::LayoutNode(const Node* node, StyleProperties&& style_properties)
  7. : m_node(node)
  8. , m_style_properties(style_properties)
  9. {
  10. }
  11. LayoutNode::~LayoutNode()
  12. {
  13. }
  14. void LayoutNode::layout()
  15. {
  16. for_each_child([](auto& child) {
  17. child.layout();
  18. });
  19. }
  20. const LayoutBlock* LayoutNode::containing_block() const
  21. {
  22. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  23. if (ancestor->is_block())
  24. return static_cast<const LayoutBlock*>(ancestor);
  25. }
  26. return nullptr;
  27. }
  28. void LayoutNode::render(RenderingContext& context)
  29. {
  30. #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
  31. context.painter().draw_rect(m_rect, Color::Blue);
  32. #endif
  33. // TODO: render our background and border
  34. for_each_child([&](auto& child) {
  35. child.render(context);
  36. });
  37. }
  38. HitTestResult LayoutNode::hit_test(const Point& position) const
  39. {
  40. // FIXME: It would be nice if we could confidently skip over hit testing
  41. // parts of the layout tree, but currently we can't just check
  42. // m_rect.contains() since inline text rects can't be trusted..
  43. HitTestResult result { m_rect.contains(position) ? this : nullptr };
  44. for_each_child([&](auto& child) {
  45. auto child_result = child.hit_test(position);
  46. if (child_result.layout_node)
  47. result = child_result;
  48. });
  49. return result;
  50. }