StackingContext.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/QuickSort.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibWeb/DOM/Node.h>
  29. #include <LibWeb/Layout/Box.h>
  30. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  31. #include <LibWeb/Painting/StackingContext.h>
  32. namespace Web::Layout {
  33. StackingContext::StackingContext(Box& box, StackingContext* parent)
  34. : m_box(box)
  35. , m_parent(parent)
  36. {
  37. ASSERT(m_parent != this);
  38. if (m_parent) {
  39. m_parent->m_children.append(this);
  40. // FIXME: Don't sort on every append..
  41. quick_sort(m_parent->m_children, [](auto& a, auto& b) {
  42. return a->m_box.computed_values().z_index().value_or(0) < b->m_box.computed_values().z_index().value_or(0);
  43. });
  44. }
  45. }
  46. void StackingContext::paint(PaintContext& context, PaintPhase phase)
  47. {
  48. if (!is<InitialContainingBlockBox>(m_box)) {
  49. m_box.paint(context, phase);
  50. } else {
  51. // NOTE: InitialContainingBlockBox::paint() merely calls StackingContext::paint()
  52. // so we call its base class instead.
  53. downcast<InitialContainingBlockBox>(m_box).BlockBox::paint(context, phase);
  54. }
  55. for (auto* child : m_children) {
  56. child->paint(context, phase);
  57. }
  58. }
  59. HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  60. {
  61. HitTestResult result;
  62. if (!is<InitialContainingBlockBox>(m_box)) {
  63. result = m_box.hit_test(position, type);
  64. } else {
  65. // NOTE: InitialContainingBlockBox::hit_test() merely calls StackingContext::hit_test()
  66. // so we call its base class instead.
  67. result = downcast<InitialContainingBlockBox>(m_box).BlockBox::hit_test(position, type);
  68. }
  69. for (auto* child : m_children) {
  70. auto result_here = child->hit_test(position, type);
  71. if (result_here.layout_node)
  72. result = result_here;
  73. }
  74. return result;
  75. }
  76. void StackingContext::dump(int indent) const
  77. {
  78. StringBuilder builder;
  79. for (int i = 0; i < indent; ++i)
  80. builder.append(' ');
  81. 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());
  82. dbgln("{}", builder.string_view());
  83. for (auto& child : m_children)
  84. child->dump(indent + 1);
  85. }
  86. }