ViewportPaintable.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Range.h>
  7. #include <LibWeb/Layout/Viewport.h>
  8. #include <LibWeb/Painting/StackingContext.h>
  9. #include <LibWeb/Painting/ViewportPaintable.h>
  10. #include <LibWeb/Selection/Selection.h>
  11. namespace Web::Painting {
  12. JS_DEFINE_ALLOCATOR(ViewportPaintable);
  13. JS::NonnullGCPtr<ViewportPaintable> ViewportPaintable::create(Layout::Viewport const& layout_viewport)
  14. {
  15. return layout_viewport.heap().allocate_without_realm<ViewportPaintable>(layout_viewport);
  16. }
  17. ViewportPaintable::ViewportPaintable(Layout::Viewport const& layout_viewport)
  18. : PaintableWithLines(layout_viewport)
  19. {
  20. }
  21. ViewportPaintable::~ViewportPaintable() = default;
  22. void ViewportPaintable::build_stacking_context_tree_if_needed()
  23. {
  24. if (stacking_context())
  25. return;
  26. build_stacking_context_tree();
  27. }
  28. void ViewportPaintable::build_stacking_context_tree()
  29. {
  30. set_stacking_context(make<StackingContext>(*this, nullptr, 0));
  31. size_t index_in_tree_order = 1;
  32. for_each_in_subtree([&](Paintable const& paintable) {
  33. const_cast<Paintable&>(paintable).invalidate_stacking_context();
  34. auto* parent_context = const_cast<Paintable&>(paintable).enclosing_stacking_context();
  35. auto establishes_stacking_context = paintable.layout_node().establishes_stacking_context();
  36. if ((paintable.is_positioned() || establishes_stacking_context) && paintable.computed_values().z_index().value_or(0) == 0)
  37. parent_context->m_positioned_descendants_with_stack_level_0_and_stacking_contexts.append(paintable);
  38. if (!paintable.is_positioned() && paintable.is_floating())
  39. parent_context->m_non_positioned_floating_descendants.append(paintable);
  40. if (!establishes_stacking_context) {
  41. VERIFY(!paintable.stacking_context());
  42. return TraversalDecision::Continue;
  43. }
  44. VERIFY(parent_context);
  45. const_cast<Paintable&>(paintable).set_stacking_context(make<Painting::StackingContext>(const_cast<Paintable&>(paintable), parent_context, index_in_tree_order++));
  46. return TraversalDecision::Continue;
  47. });
  48. stacking_context()->sort();
  49. }
  50. void ViewportPaintable::paint_all_phases(PaintContext& context)
  51. {
  52. build_stacking_context_tree_if_needed();
  53. stacking_context()->paint(context);
  54. }
  55. void ViewportPaintable::assign_scroll_frames()
  56. {
  57. int next_id = 0;
  58. for_each_in_inclusive_subtree_of_type<PaintableBox>([&](auto& paintable_box) {
  59. if (paintable_box.has_scrollable_overflow() || is<ViewportPaintable>(paintable_box)) {
  60. auto scroll_frame = adopt_ref(*new ScrollFrame());
  61. scroll_frame->id = next_id++;
  62. scroll_frame->parent = paintable_box.nearest_scroll_frame();
  63. paintable_box.set_own_scroll_frame(scroll_frame);
  64. scroll_state.set(paintable_box, move(scroll_frame));
  65. }
  66. return TraversalDecision::Continue;
  67. });
  68. for_each_in_subtree([&](auto const& paintable) {
  69. if (paintable.is_fixed_position()) {
  70. return TraversalDecision::Continue;
  71. }
  72. for (auto block = paintable.containing_block(); block; block = block->containing_block()) {
  73. if (auto scroll_frame = block->own_scroll_frame(); scroll_frame) {
  74. if (paintable.is_paintable_box()) {
  75. auto const& paintable_box = static_cast<PaintableBox const&>(paintable);
  76. const_cast<PaintableBox&>(paintable_box).set_enclosing_scroll_frame(*scroll_frame);
  77. } else if (paintable.is_inline_paintable()) {
  78. auto const& inline_paintable = static_cast<InlinePaintable const&>(paintable);
  79. const_cast<InlinePaintable&>(inline_paintable).set_enclosing_scroll_frame(*scroll_frame);
  80. }
  81. return TraversalDecision::Continue;
  82. }
  83. if (block->is_fixed_position()) {
  84. return TraversalDecision::Continue;
  85. }
  86. }
  87. VERIFY_NOT_REACHED();
  88. });
  89. }
  90. void ViewportPaintable::assign_clip_frames()
  91. {
  92. for_each_in_subtree_of_type<PaintableBox>([&](auto const& paintable_box) {
  93. auto overflow_x = paintable_box.computed_values().overflow_x();
  94. auto overflow_y = paintable_box.computed_values().overflow_y();
  95. auto has_hidden_overflow = overflow_x != CSS::Overflow::Visible && overflow_y != CSS::Overflow::Visible;
  96. if (has_hidden_overflow || paintable_box.get_clip_rect().has_value()) {
  97. auto clip_frame = adopt_ref(*new ClipFrame());
  98. clip_state.set(paintable_box, move(clip_frame));
  99. }
  100. return TraversalDecision::Continue;
  101. });
  102. for_each_in_subtree([&](auto const& paintable) {
  103. for (auto block = paintable.containing_block(); !block->is_viewport(); block = block->containing_block()) {
  104. if (auto clip_frame = clip_state.get(block); clip_frame.has_value()) {
  105. if (paintable.is_paintable_box()) {
  106. auto const& paintable_box = static_cast<PaintableBox const&>(paintable);
  107. const_cast<PaintableBox&>(paintable_box).set_enclosing_clip_frame(clip_frame.value());
  108. } else if (paintable.is_inline_paintable()) {
  109. auto const& inline_paintable = static_cast<InlinePaintable const&>(paintable);
  110. const_cast<InlinePaintable&>(inline_paintable).set_enclosing_clip_frame(clip_frame.value());
  111. }
  112. break;
  113. }
  114. if (block->has_css_transform()) {
  115. break;
  116. }
  117. }
  118. return TraversalDecision::Continue;
  119. });
  120. for (auto& it : clip_state) {
  121. auto const& paintable_box = *it.key;
  122. auto& clip_frame = *it.value;
  123. for (auto const* block = &paintable_box.layout_box(); !block->is_viewport(); block = block->containing_block()) {
  124. auto const& block_paintable_box = *block->paintable_box();
  125. auto block_overflow_x = block_paintable_box.computed_values().overflow_x();
  126. auto block_overflow_y = block_paintable_box.computed_values().overflow_y();
  127. if (block_overflow_x != CSS::Overflow::Visible && block_overflow_y != CSS::Overflow::Visible) {
  128. auto rect = block_paintable_box.absolute_padding_box_rect();
  129. clip_frame.add_clip_rect(rect, block_paintable_box.normalized_border_radii_data(ShrinkRadiiForBorders::Yes), block_paintable_box.enclosing_scroll_frame());
  130. }
  131. if (auto css_clip_property_rect = block->paintable_box()->get_clip_rect(); css_clip_property_rect.has_value()) {
  132. clip_frame.add_clip_rect(css_clip_property_rect.value(), {}, block_paintable_box.enclosing_scroll_frame());
  133. }
  134. if (block->has_css_transform()) {
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. void ViewportPaintable::refresh_scroll_state()
  141. {
  142. if (!m_needs_to_refresh_scroll_state)
  143. return;
  144. m_needs_to_refresh_scroll_state = false;
  145. for (auto& it : scroll_state) {
  146. auto const& paintable_box = *it.key;
  147. auto& scroll_frame = *it.value;
  148. scroll_frame.own_offset = -paintable_box.scroll_offset();
  149. }
  150. }
  151. void ViewportPaintable::resolve_paint_only_properties()
  152. {
  153. // Resolves layout-dependent properties not handled during layout and stores them in the paint tree.
  154. // Properties resolved include:
  155. // - Border radii
  156. // - Box shadows
  157. // - Text shadows
  158. // - Transforms
  159. // - Transform origins
  160. // - Outlines
  161. for_each_in_inclusive_subtree([&](Paintable& paintable) {
  162. paintable.resolve_paint_properties();
  163. return TraversalDecision::Continue;
  164. });
  165. }
  166. JS::GCPtr<Selection::Selection> ViewportPaintable::selection() const
  167. {
  168. return const_cast<DOM::Document&>(document()).get_selection();
  169. }
  170. void ViewportPaintable::recompute_selection_states()
  171. {
  172. // 1. Start by resetting the selection state of all layout nodes to None.
  173. for_each_in_inclusive_subtree([&](auto& layout_node) {
  174. layout_node.set_selection_state(SelectionState::None);
  175. return TraversalDecision::Continue;
  176. });
  177. // 2. If there is no active Selection or selected Range, return.
  178. auto selection = document().get_selection();
  179. if (!selection)
  180. return;
  181. auto range = selection->range();
  182. if (!range)
  183. return;
  184. auto* start_container = range->start_container();
  185. auto* end_container = range->end_container();
  186. // 3. If the selection starts and ends in the same node:
  187. if (start_container == end_container) {
  188. // 1. If the selection starts and ends at the same offset, return.
  189. if (range->start_offset() == range->end_offset()) {
  190. // NOTE: A zero-length selection should not be visible.
  191. return;
  192. }
  193. // 2. If it's a text node, mark it as StartAndEnd and return.
  194. if (is<DOM::Text>(*start_container)) {
  195. if (auto* paintable = start_container->paintable())
  196. paintable->set_selection_state(SelectionState::StartAndEnd);
  197. return;
  198. }
  199. }
  200. // 4. Mark the selection start node as Start (if text) or Full (if anything else).
  201. if (auto* paintable = start_container->paintable()) {
  202. if (is<DOM::Text>(*start_container))
  203. paintable->set_selection_state(SelectionState::Start);
  204. else
  205. paintable->set_selection_state(SelectionState::Full);
  206. }
  207. // 5. Mark the selection end node as End (if text) or Full (if anything else).
  208. if (auto* paintable = end_container->paintable()) {
  209. if (is<DOM::Text>(*end_container))
  210. paintable->set_selection_state(SelectionState::End);
  211. else
  212. paintable->set_selection_state(SelectionState::Full);
  213. }
  214. // 6. Mark the nodes between start node and end node (in tree order) as Full.
  215. for (auto* node = start_container->next_in_pre_order(); node && node != end_container; node = node->next_in_pre_order()) {
  216. if (auto* paintable = node->paintable())
  217. paintable->set_selection_state(SelectionState::Full);
  218. }
  219. }
  220. bool ViewportPaintable::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned, int, int)
  221. {
  222. return false;
  223. }
  224. void ViewportPaintable::visit_edges(Visitor& visitor)
  225. {
  226. Base::visit_edges(visitor);
  227. visitor.visit(scroll_state);
  228. visitor.visit(clip_state);
  229. }
  230. }