StackingContext.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibWeb/DOM/Node.h>
  9. #include <LibWeb/Layout/Box.h>
  10. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  11. #include <LibWeb/Painting/StackingContext.h>
  12. namespace Web::Layout {
  13. StackingContext::StackingContext(Box& box, StackingContext* parent)
  14. : m_box(box)
  15. , m_parent(parent)
  16. {
  17. VERIFY(m_parent != this);
  18. if (m_parent) {
  19. m_parent->m_children.append(this);
  20. // FIXME: Don't sort on every append..
  21. // FIXME: Apparently this also breaks tree order inside layers
  22. quick_sort(m_parent->m_children, [](auto& a, auto& b) {
  23. return a->m_box.computed_values().z_index().value_or(0) < b->m_box.computed_values().z_index().value_or(0);
  24. });
  25. }
  26. }
  27. void StackingContext::paint_descendants(PaintContext& context, Node& box, StackingContextPaintPhase phase)
  28. {
  29. box.for_each_child([&](auto& child) {
  30. switch (phase) {
  31. case StackingContextPaintPhase::BackgroundAndBorders:
  32. if (!child.is_floating() && !child.is_positioned()) {
  33. child.paint(context, PaintPhase::Background);
  34. child.paint(context, PaintPhase::Border);
  35. paint_descendants(context, child, phase);
  36. }
  37. break;
  38. case StackingContextPaintPhase::Floats:
  39. if (!child.is_positioned()) {
  40. if (child.is_floating()) {
  41. child.paint(context, PaintPhase::Background);
  42. child.paint(context, PaintPhase::Border);
  43. paint_descendants(context, child, StackingContextPaintPhase::BackgroundAndBorders);
  44. }
  45. paint_descendants(context, child, phase);
  46. }
  47. break;
  48. case StackingContextPaintPhase::Foreground:
  49. if (!child.is_positioned()) {
  50. child.paint(context, PaintPhase::Foreground);
  51. child.before_children_paint(context, PaintPhase::Foreground);
  52. paint_descendants(context, child, phase);
  53. child.after_children_paint(context, PaintPhase::Foreground);
  54. }
  55. break;
  56. case StackingContextPaintPhase::FocusAndOverlay:
  57. if (context.has_focus()) {
  58. child.paint(context, PaintPhase::FocusOutline);
  59. }
  60. child.paint(context, PaintPhase::Overlay);
  61. paint_descendants(context, child, phase);
  62. break;
  63. }
  64. });
  65. }
  66. void StackingContext::paint(PaintContext& context)
  67. {
  68. // For a more elaborate description of the algorithm, see CSS 2.1 Appendix E
  69. // Draw the background and borders for the context root (steps 1, 2)
  70. m_box.paint(context, PaintPhase::Background);
  71. m_box.paint(context, PaintPhase::Border);
  72. // Draw positioned descendants with negative z-indices (step 3)
  73. for (auto* child : m_children) {
  74. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() < 0)
  75. child->paint(context);
  76. }
  77. // Draw the background and borders for block-level children (step 4)
  78. paint_descendants(context, m_box, StackingContextPaintPhase::BackgroundAndBorders);
  79. // Draw the non-positioned floats (step 5)
  80. paint_descendants(context, m_box, StackingContextPaintPhase::Floats);
  81. // Draw inline content, replaced content, etc. (steps 6, 7)
  82. m_box.paint(context, PaintPhase::Foreground);
  83. paint_descendants(context, m_box, StackingContextPaintPhase::Foreground);
  84. // Draw other positioned descendants (steps 8, 9)
  85. for (auto* child : m_children) {
  86. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() < 0)
  87. continue;
  88. child->paint(context);
  89. }
  90. m_box.paint(context, PaintPhase::FocusOutline);
  91. m_box.paint(context, PaintPhase::Overlay);
  92. paint_descendants(context, m_box, StackingContextPaintPhase::FocusAndOverlay);
  93. }
  94. HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  95. {
  96. HitTestResult result;
  97. if (!is<InitialContainingBlockBox>(m_box)) {
  98. result = m_box.hit_test(position, type);
  99. } else {
  100. // NOTE: InitialContainingBlockBox::hit_test() merely calls StackingContext::hit_test()
  101. // so we call its base class instead.
  102. result = downcast<InitialContainingBlockBox>(m_box).BlockBox::hit_test(position, type);
  103. }
  104. int z_index = m_box.computed_values().z_index().value_or(0);
  105. for (auto* child : m_children) {
  106. int child_z_index = child->m_box.computed_values().z_index().value_or(0);
  107. if (result.layout_node && (child_z_index < z_index))
  108. continue;
  109. auto result_here = child->hit_test(position, type);
  110. if (result_here.layout_node)
  111. result = result_here;
  112. }
  113. return result;
  114. }
  115. void StackingContext::dump(int indent) const
  116. {
  117. StringBuilder builder;
  118. for (int i = 0; i < indent; ++i)
  119. builder.append(' ');
  120. builder.appendff("SC for {}({}) {} [children: {}]", m_box.class_name(), m_box.dom_node() ? m_box.dom_node()->node_name().characters() : "(anonymous)", m_box.absolute_rect().to_string().characters(), m_children.size());
  121. dbgln("{}", builder.string_view());
  122. for (auto& child : m_children)
  123. child->dump(indent + 1);
  124. }
  125. }