LayoutDocument.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2018-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 <LibWeb/Dump.h>
  27. #include <LibWeb/Frame/Frame.h>
  28. #include <LibWeb/Layout/LayoutDocument.h>
  29. #include <LibWeb/Layout/LayoutImage.h>
  30. #include <LibWeb/Layout/LayoutWidget.h>
  31. #include <LibWeb/Painting/StackingContext.h>
  32. namespace Web {
  33. LayoutDocument::LayoutDocument(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
  34. : LayoutBlock(document, &document, move(style))
  35. {
  36. }
  37. LayoutDocument::~LayoutDocument()
  38. {
  39. }
  40. void LayoutDocument::build_stacking_context_tree()
  41. {
  42. if (stacking_context())
  43. return;
  44. set_stacking_context(make<StackingContext>(*this, nullptr));
  45. for_each_in_subtree_of_type<LayoutBox>([&](LayoutBox& box) {
  46. if (&box == this)
  47. return IterationDecision::Continue;
  48. if (!box.establishes_stacking_context()) {
  49. ASSERT(!box.stacking_context());
  50. return IterationDecision::Continue;
  51. }
  52. auto* parent_context = box.enclosing_stacking_context();
  53. ASSERT(parent_context);
  54. box.set_stacking_context(make<StackingContext>(box, parent_context));
  55. return IterationDecision::Continue;
  56. });
  57. }
  58. void LayoutDocument::layout(LayoutMode layout_mode)
  59. {
  60. build_stacking_context_tree();
  61. set_width(frame().size().width());
  62. LayoutNode::layout(layout_mode);
  63. ASSERT(!children_are_inline());
  64. float lowest_bottom = 0;
  65. for_each_child([&](auto& child) {
  66. ASSERT(is<LayoutBlock>(child));
  67. auto& child_block = downcast<LayoutBlock>(child);
  68. lowest_bottom = max(lowest_bottom, child_block.absolute_rect().bottom());
  69. });
  70. set_height(lowest_bottom);
  71. layout_absolutely_positioned_descendants();
  72. // FIXME: This is a total hack. Make sure any GUI::Widgets are moved into place after layout.
  73. // We should stop embedding GUI::Widgets entirely, since that won't work out-of-process.
  74. for_each_in_subtree_of_type<LayoutWidget>([&](auto& widget) {
  75. widget.update_widget();
  76. return IterationDecision::Continue;
  77. });
  78. }
  79. void LayoutDocument::did_set_viewport_rect(Badge<Frame>, const Gfx::IntRect& a_viewport_rect)
  80. {
  81. Gfx::FloatRect viewport_rect(a_viewport_rect.x(), a_viewport_rect.y(), a_viewport_rect.width(), a_viewport_rect.height());
  82. for_each_in_subtree_of_type<LayoutImage>([&](auto& layout_image) {
  83. const_cast<LayoutImage&>(layout_image).set_visible_in_viewport({}, viewport_rect.intersects(layout_image.absolute_rect()));
  84. return IterationDecision::Continue;
  85. });
  86. }
  87. void LayoutDocument::paint_all_phases(PaintContext& context)
  88. {
  89. paint(context, PaintPhase::Background);
  90. paint(context, PaintPhase::Border);
  91. paint(context, PaintPhase::Foreground);
  92. paint(context, PaintPhase::Overlay);
  93. }
  94. void LayoutDocument::paint(PaintContext& context, PaintPhase phase)
  95. {
  96. stacking_context()->paint(context, phase);
  97. }
  98. HitTestResult LayoutDocument::hit_test(const Gfx::IntPoint& position) const
  99. {
  100. return stacking_context()->hit_test(position);
  101. }
  102. }