InitialContainingBlock.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (!box.paint_box())
  27. return IterationDecision::Continue;
  28. const_cast<Painting::PaintableBox*>(box.paint_box())->invalidate_stacking_context();
  29. if (!box.establishes_stacking_context()) {
  30. VERIFY(!box.paint_box()->stacking_context());
  31. return IterationDecision::Continue;
  32. }
  33. auto* parent_context = const_cast<Painting::PaintableBox*>(box.paint_box())->enclosing_stacking_context();
  34. VERIFY(parent_context);
  35. const_cast<Painting::PaintableBox*>(box.paint_box())->set_stacking_context(make<Painting::StackingContext>(box, parent_context));
  36. return IterationDecision::Continue;
  37. });
  38. const_cast<Painting::PaintableWithLines*>(paint_box())->stacking_context()->sort();
  39. }
  40. void InitialContainingBlock::paint_all_phases(PaintContext& context)
  41. {
  42. build_stacking_context_tree_if_needed();
  43. context.painter().fill_rect(enclosing_int_rect(paint_box()->absolute_rect()), document().background_color(context.palette()));
  44. context.painter().translate(-context.viewport_rect().location());
  45. paint_box()->stacking_context()->paint(context);
  46. }
  47. void InitialContainingBlock::recompute_selection_states()
  48. {
  49. SelectionState state = SelectionState::None;
  50. auto selection = this->selection().normalized();
  51. for_each_in_inclusive_subtree([&](auto& layout_node) {
  52. if (!selection.is_valid()) {
  53. // Everything gets SelectionState::None.
  54. } else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) {
  55. state = SelectionState::StartAndEnd;
  56. } else if (&layout_node == selection.start().layout_node) {
  57. state = SelectionState::Start;
  58. } else if (&layout_node == selection.end().layout_node) {
  59. state = SelectionState::End;
  60. } else {
  61. if (state == SelectionState::Start)
  62. state = SelectionState::Full;
  63. else if (state == SelectionState::End || state == SelectionState::StartAndEnd)
  64. state = SelectionState::None;
  65. }
  66. layout_node.set_selection_state(state);
  67. return IterationDecision::Continue;
  68. });
  69. }
  70. void InitialContainingBlock::set_selection(LayoutRange const& selection)
  71. {
  72. m_selection = selection;
  73. recompute_selection_states();
  74. }
  75. void InitialContainingBlock::set_selection_end(LayoutPosition const& position)
  76. {
  77. m_selection.set_end(position);
  78. recompute_selection_states();
  79. }
  80. }