InitialContainingBlock.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Dump.h>
  7. #include <LibWeb/Layout/InitialContainingBlock.h>
  8. #include <LibWeb/Painting/PaintableBox.h>
  9. #include <LibWeb/Painting/StackingContext.h>
  10. namespace Web::Layout {
  11. InitialContainingBlock::InitialContainingBlock(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
  12. : BlockContainer(document, &document, move(style))
  13. {
  14. }
  15. InitialContainingBlock::~InitialContainingBlock() = default;
  16. void InitialContainingBlock::build_stacking_context_tree_if_needed()
  17. {
  18. if (paint_box()->stacking_context())
  19. return;
  20. build_stacking_context_tree();
  21. }
  22. void InitialContainingBlock::build_stacking_context_tree()
  23. {
  24. const_cast<Painting::PaintableWithLines*>(paint_box())->set_stacking_context(make<Painting::StackingContext>(*this, nullptr));
  25. for_each_in_subtree_of_type<Box>([&](Box& box) {
  26. const_cast<Painting::PaintableBox*>(box.paint_box())->invalidate_stacking_context();
  27. if (!box.establishes_stacking_context()) {
  28. VERIFY(!box.paint_box()->stacking_context());
  29. return IterationDecision::Continue;
  30. }
  31. auto* parent_context = const_cast<Painting::PaintableBox*>(box.paint_box())->enclosing_stacking_context();
  32. VERIFY(parent_context);
  33. const_cast<Painting::PaintableBox*>(box.paint_box())->set_stacking_context(make<Painting::StackingContext>(box, parent_context));
  34. return IterationDecision::Continue;
  35. });
  36. const_cast<Painting::PaintableWithLines*>(paint_box())->stacking_context()->sort();
  37. }
  38. void InitialContainingBlock::paint_all_phases(PaintContext& context)
  39. {
  40. build_stacking_context_tree_if_needed();
  41. context.painter().fill_rect(enclosing_int_rect(paint_box()->absolute_rect()), context.palette().base());
  42. context.painter().translate(-context.viewport_rect().location());
  43. paint_box()->stacking_context()->paint(context);
  44. }
  45. void InitialContainingBlock::recompute_selection_states()
  46. {
  47. SelectionState state = SelectionState::None;
  48. auto selection = this->selection().normalized();
  49. for_each_in_inclusive_subtree([&](auto& layout_node) {
  50. if (!selection.is_valid()) {
  51. // Everything gets SelectionState::None.
  52. } else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
  53. state = SelectionState::StartAndEnd;
  54. } else if (&layout_node == selection.start().layout_node) {
  55. state = SelectionState::Start;
  56. } else if (&layout_node == selection.end().layout_node) {
  57. state = SelectionState::End;
  58. } else {
  59. if (state == SelectionState::Start)
  60. state = SelectionState::Full;
  61. else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
  62. state = SelectionState::None;
  63. }
  64. layout_node.set_selection_state(state);
  65. return IterationDecision::Continue;
  66. });
  67. }
  68. void InitialContainingBlock::set_selection(const LayoutRange& selection)
  69. {
  70. m_selection = selection;
  71. recompute_selection_states();
  72. }
  73. void InitialContainingBlock::set_selection_end(const LayoutPosition& position)
  74. {
  75. m_selection.set_end(position);
  76. recompute_selection_states();
  77. }
  78. }