Viewport.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Range.h>
  7. #include <LibWeb/Dump.h>
  8. #include <LibWeb/Layout/Viewport.h>
  9. #include <LibWeb/Painting/PaintableBox.h>
  10. #include <LibWeb/Painting/StackingContext.h>
  11. namespace Web::Layout {
  12. Viewport::Viewport(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
  13. : BlockContainer(document, &document, move(style))
  14. {
  15. }
  16. Viewport::~Viewport() = default;
  17. JS::GCPtr<Selection::Selection> Viewport::selection() const
  18. {
  19. return const_cast<DOM::Document&>(document()).get_selection();
  20. }
  21. void Viewport::build_stacking_context_tree_if_needed()
  22. {
  23. if (paint_box()->stacking_context())
  24. return;
  25. build_stacking_context_tree();
  26. }
  27. void Viewport::build_stacking_context_tree()
  28. {
  29. const_cast<Painting::PaintableWithLines*>(paint_box())->set_stacking_context(make<Painting::StackingContext>(*this, nullptr));
  30. for_each_in_subtree_of_type<Box>([&](Box& box) {
  31. if (!box.paint_box())
  32. return IterationDecision::Continue;
  33. const_cast<Painting::PaintableBox*>(box.paint_box())->invalidate_stacking_context();
  34. if (!box.establishes_stacking_context()) {
  35. VERIFY(!box.paint_box()->stacking_context());
  36. return IterationDecision::Continue;
  37. }
  38. auto* parent_context = const_cast<Painting::PaintableBox*>(box.paint_box())->enclosing_stacking_context();
  39. VERIFY(parent_context);
  40. const_cast<Painting::PaintableBox*>(box.paint_box())->set_stacking_context(make<Painting::StackingContext>(box, parent_context));
  41. return IterationDecision::Continue;
  42. });
  43. const_cast<Painting::PaintableWithLines*>(paint_box())->stacking_context()->sort();
  44. }
  45. void Viewport::paint_all_phases(PaintContext& context)
  46. {
  47. build_stacking_context_tree_if_needed();
  48. context.painter().fill_rect(context.enclosing_device_rect(paint_box()->absolute_rect()).to_type<int>(), document().background_color(context.palette()));
  49. context.painter().translate(-context.device_viewport_rect().location().to_type<int>());
  50. paint_box()->stacking_context()->paint(context);
  51. }
  52. void Viewport::recompute_selection_states()
  53. {
  54. // 1. Start by resetting the selection state of all layout nodes to None.
  55. for_each_in_inclusive_subtree([&](auto& layout_node) {
  56. layout_node.set_selection_state(SelectionState::None);
  57. return IterationDecision::Continue;
  58. });
  59. // 2. If there is no active Selection or selected Range, return.
  60. auto selection = document().get_selection();
  61. if (!selection)
  62. return;
  63. auto range = selection->range();
  64. if (!range)
  65. return;
  66. auto* start_container = range->start_container();
  67. auto* end_container = range->end_container();
  68. // 3. If the selection starts and ends in the same node:
  69. if (start_container == end_container) {
  70. // 1. If the selection starts and ends at the same offset, return.
  71. if (range->start_offset() == range->end_offset()) {
  72. // NOTE: A zero-length selection should not be visible.
  73. return;
  74. }
  75. // 2. If it's a text node, mark it as StartAndEnd and return.
  76. if (is<DOM::Text>(*start_container)) {
  77. if (auto* layout_node = start_container->layout_node()) {
  78. layout_node->set_selection_state(SelectionState::StartAndEnd);
  79. }
  80. return;
  81. }
  82. }
  83. if (start_container == end_container && is<DOM::Text>(*start_container)) {
  84. if (auto* layout_node = start_container->layout_node()) {
  85. layout_node->set_selection_state(SelectionState::StartAndEnd);
  86. }
  87. return;
  88. }
  89. // 4. Mark the selection start node as Start (if text) or Full (if anything else).
  90. if (auto* layout_node = start_container->layout_node()) {
  91. if (is<DOM::Text>(*start_container))
  92. layout_node->set_selection_state(SelectionState::Start);
  93. else
  94. layout_node->set_selection_state(SelectionState::Full);
  95. }
  96. // 5. Mark the selection end node as End (if text) or Full (if anything else).
  97. if (auto* layout_node = end_container->layout_node()) {
  98. if (is<DOM::Text>(*end_container))
  99. layout_node->set_selection_state(SelectionState::End);
  100. else
  101. layout_node->set_selection_state(SelectionState::Full);
  102. }
  103. // 6. Mark the nodes between start node and end node (in tree order) as Full.
  104. for (auto* node = start_container->next_in_pre_order(); node && node != end_container; node = node->next_in_pre_order()) {
  105. if (auto* layout_node = node->layout_node())
  106. layout_node->set_selection_state(SelectionState::Full);
  107. }
  108. }
  109. }