ContainerBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #include "ContainerBox.h"
  2. #include "../../../Include/RmlUi/Core/ComputedValues.h"
  3. #include "../../../Include/RmlUi/Core/Element.h"
  4. #include "../../../Include/RmlUi/Core/ElementScroll.h"
  5. #include "../../../Include/RmlUi/Core/Profiling.h"
  6. #include "FlexFormattingContext.h"
  7. #include "FormattingContext.h"
  8. #include "LayoutDetails.h"
  9. #include <algorithm>
  10. #include <cmath>
  11. namespace Rml {
  12. void ContainerBox::ResetScrollbars(const Box& box)
  13. {
  14. RMLUI_ASSERT(element);
  15. if (overflow_x == Style::Overflow::Scroll)
  16. element->GetElementScroll()->EnableScrollbar(ElementScroll::HORIZONTAL, box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Padding));
  17. else
  18. element->GetElementScroll()->DisableScrollbar(ElementScroll::HORIZONTAL);
  19. if (overflow_y == Style::Overflow::Scroll)
  20. element->GetElementScroll()->EnableScrollbar(ElementScroll::VERTICAL, box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Padding));
  21. else
  22. element->GetElementScroll()->DisableScrollbar(ElementScroll::VERTICAL);
  23. }
  24. void ContainerBox::AddAbsoluteElement(Element* element, Vector2f static_position, Element* static_relative_offset_parent)
  25. {
  26. // We may possibly be adding the same element from a previous layout iteration. If so, this ensures it is updated with the latest static position.
  27. absolute_elements[element] = AbsoluteElement{static_position, static_relative_offset_parent};
  28. }
  29. void ContainerBox::AddRelativeElement(Element* element)
  30. {
  31. // The same relative element may be added multiple times during repeated layout iterations, avoid any duplicates.
  32. if (std::find(relative_elements.begin(), relative_elements.end(), element) == relative_elements.end())
  33. relative_elements.push_back(element);
  34. }
  35. bool ContainerBox::IsScrollContainer() const
  36. {
  37. return LayoutDetails::IsScrollContainer(overflow_x, overflow_y);
  38. }
  39. void ContainerBox::ClosePositionedElements()
  40. {
  41. // Any relatively positioned elements that we act as containing block for may need to be have their positions
  42. // updated to reflect changes to the size of this block box. Update relative offsets before handling absolute
  43. // elements, as this may affect the resolved static position of the absolute elements.
  44. for (Element* child : relative_elements)
  45. child->UpdateOffset();
  46. relative_elements.clear();
  47. while (!absolute_elements.empty())
  48. {
  49. // New absolute elements may be added to this box during formatting below. To avoid invalidated iterators and
  50. // references, move the list to a local copy to iterate over, and repeat if new elements are added.
  51. AbsoluteElementMap absolute_elements_iterate = std::move(absolute_elements);
  52. absolute_elements.clear();
  53. for (const auto& absolute_element_pair : absolute_elements_iterate)
  54. {
  55. Element* absolute_element = absolute_element_pair.first;
  56. const Vector2f static_position = absolute_element_pair.second.static_position;
  57. Element* static_position_offset_parent = absolute_element_pair.second.static_position_offset_parent;
  58. // Find the static position relative to this containing block. First, calculate the offset from ourself to
  59. // the static position's offset parent. Assumes (1) that this container box is part of the containing block
  60. // chain of the static position offset parent, and (2) that all offsets in this chain has been set already.
  61. Vector2f relative_position;
  62. for (Element* ancestor = static_position_offset_parent; ancestor && ancestor != element; ancestor = ancestor->GetOffsetParent())
  63. relative_position += ancestor->GetRelativeOffset(BoxArea::Border);
  64. // Now simply add the result to the stored static position to get the static position in our local space.
  65. Vector2f offset = relative_position + static_position;
  66. // Lay out the element.
  67. FormattingContext::FormatIndependent(this, absolute_element, nullptr, FormattingContextType::Block);
  68. // Now that the element's box has been built, we can offset the position we determined was appropriate for
  69. // it by the element's margin. This is necessary because the coordinate system for the box begins at the
  70. // border, not the margin.
  71. offset.x += absolute_element->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Left);
  72. offset.y += absolute_element->GetBox().GetEdge(BoxArea::Margin, BoxEdge::Top);
  73. // Set the offset of the element; the element itself will take care of any RCSS-defined positional offsets.
  74. absolute_element->SetOffset(offset, element);
  75. }
  76. }
  77. }
  78. void ContainerBox::SetElementBaseline(float element_baseline)
  79. {
  80. element->SetBaseline(element_baseline);
  81. }
  82. void ContainerBox::SubmitElementLayout()
  83. {
  84. element->OnLayout();
  85. }
  86. ContainerBox::ContainerBox(Type type, Element* element, ContainerBox* parent_container) :
  87. LayoutBox(type), element(element), parent_container(parent_container)
  88. {
  89. if (element)
  90. {
  91. const auto& computed = element->GetComputedValues();
  92. overflow_x = computed.overflow_x();
  93. overflow_y = computed.overflow_y();
  94. is_absolute_positioning_containing_block = (computed.position() != Style::Position::Static || computed.has_local_transform() ||
  95. computed.has_local_perspective() || computed.has_filter() || computed.has_backdrop_filter() || computed.has_mask_image());
  96. }
  97. }
  98. bool ContainerBox::CatchOverflow(const Vector2f content_overflow_size, const Box& box, const float max_height) const
  99. {
  100. if (!IsScrollContainer())
  101. return true;
  102. const Vector2f padding_bottom_right = {box.GetEdge(BoxArea::Padding, BoxEdge::Right), box.GetEdge(BoxArea::Padding, BoxEdge::Bottom)};
  103. const float padding_width = box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Padding);
  104. Vector2f available_space = box.GetSize();
  105. if (available_space.y < 0.f)
  106. available_space.y = max_height;
  107. if (available_space.y < 0.f)
  108. available_space.y = HUGE_VALF;
  109. RMLUI_ASSERT(available_space.x >= 0.f && available_space.y >= 0.f);
  110. // Allow overflow onto the padding area.
  111. available_space += padding_bottom_right;
  112. ElementScroll* element_scroll = element->GetElementScroll();
  113. bool scrollbar_size_changed = false;
  114. // @performance If we have auto-height sizing and the horizontal scrollbar is enabled, then we can in principle
  115. // simply add the scrollbar size to the height instead of formatting the element all over again.
  116. if (overflow_x == Style::Overflow::Auto && content_overflow_size.x > available_space.x + 0.5f)
  117. {
  118. if (element_scroll->GetScrollbarSize(ElementScroll::HORIZONTAL) == 0.f)
  119. {
  120. element_scroll->EnableScrollbar(ElementScroll::HORIZONTAL, padding_width);
  121. const float new_size = element_scroll->GetScrollbarSize(ElementScroll::HORIZONTAL);
  122. scrollbar_size_changed = (new_size != 0.f);
  123. available_space.y -= new_size;
  124. }
  125. }
  126. // If we're auto-scrolling and our height is fixed, we have to check if this box has exceeded our client height.
  127. if (overflow_y == Style::Overflow::Auto && content_overflow_size.y > available_space.y + 0.5f)
  128. {
  129. if (element_scroll->GetScrollbarSize(ElementScroll::VERTICAL) == 0.f)
  130. {
  131. element_scroll->EnableScrollbar(ElementScroll::VERTICAL, padding_width);
  132. const float new_size = element_scroll->GetScrollbarSize(ElementScroll::VERTICAL);
  133. scrollbar_size_changed |= (new_size != 0.f);
  134. }
  135. }
  136. return !scrollbar_size_changed;
  137. }
  138. bool ContainerBox::SubmitBox(const Vector2f content_overflow_size, const Box& box, const float max_height)
  139. {
  140. Vector2f visible_overflow_size;
  141. // Set the computed box on the element.
  142. if (element)
  143. {
  144. // Calculate the dimensions of the box's scrollable overflow rectangle. This is the union of the tightest-
  145. // fitting box around all of the internal elements, and this element's padding box. We really only care about
  146. // overflow on the bottom-right sides, as these are the only ones allowed to be scrolled to in CSS.
  147. //
  148. // If we are a scroll container (use any other value than 'overflow: visible'), then any overflow outside our
  149. // padding box should be caught here. Otherwise, our overflow should be included in the overflow calculations of
  150. // our nearest scroll container ancestor.
  151. // If our content is larger than our padding box, we can add scrollbars if we're set to auto-scrollbars. If
  152. // we're set to always use scrollbars, then the scrollbars have already been enabled.
  153. if (!CatchOverflow(content_overflow_size, box, max_height))
  154. return false;
  155. const Vector2f padding_top_left = {box.GetEdge(BoxArea::Padding, BoxEdge::Left), box.GetEdge(BoxArea::Padding, BoxEdge::Top)};
  156. const Vector2f padding_bottom_right = {box.GetEdge(BoxArea::Padding, BoxEdge::Right), box.GetEdge(BoxArea::Padding, BoxEdge::Bottom)};
  157. const Vector2f padding_size = box.GetSize() + padding_top_left + padding_bottom_right;
  158. const bool is_scroll_container = IsScrollContainer();
  159. const Vector2f scrollbar_size = {
  160. is_scroll_container ? element->GetElementScroll()->GetScrollbarSize(ElementScroll::VERTICAL) : 0.f,
  161. is_scroll_container ? element->GetElementScroll()->GetScrollbarSize(ElementScroll::HORIZONTAL) : 0.f,
  162. };
  163. element->SetBox(box);
  164. // Scrollable overflow is the set of things extending our padding area, for which scrolling could be provided.
  165. const Vector2f scrollable_overflow_size = Math::Max(padding_size - scrollbar_size, padding_top_left + content_overflow_size);
  166. // Set the overflow size but defer clamping of the scroll offset, see `LayoutEngine::FormatElement`.
  167. element->SetScrollableOverflowRectangle(scrollable_overflow_size, false);
  168. const Vector2f border_size = padding_size + box.GetFrameSize(BoxArea::Border);
  169. // Set the visible overflow size so that ancestors can catch any overflow produced by us. That is, hiding it or
  170. // providing a scrolling mechanism. If this box is a scroll container we catch our own overflow here. Thus, in
  171. // this case, only our border box is visible from our ancestor's perpective.
  172. if (is_scroll_container)
  173. {
  174. visible_overflow_size = border_size;
  175. // Format any scrollbars in case they were enabled on this element.
  176. element->GetElementScroll()->FormatScrollbars();
  177. }
  178. else
  179. {
  180. const Vector2f border_top_left = {box.GetEdge(BoxArea::Border, BoxEdge::Left), box.GetEdge(BoxArea::Border, BoxEdge::Top)};
  181. visible_overflow_size = Math::Max(border_size, content_overflow_size + border_top_left + padding_top_left);
  182. }
  183. }
  184. SetVisibleOverflowSize(visible_overflow_size);
  185. return true;
  186. }
  187. String RootBox::DebugDumpTree(int depth) const
  188. {
  189. return String(depth * 2, ' ') + "RootBox";
  190. }
  191. FlexContainer::FlexContainer(Element* element, ContainerBox* parent_container) : ContainerBox(Type::FlexContainer, element, parent_container)
  192. {
  193. RMLUI_ASSERT(element);
  194. }
  195. bool FlexContainer::Close(const Vector2f content_overflow_size, const Box& box, float element_baseline)
  196. {
  197. if (!SubmitBox(content_overflow_size, box, -1.f))
  198. return false;
  199. ClosePositionedElements();
  200. SubmitElementLayout();
  201. SetElementBaseline(element_baseline);
  202. return true;
  203. }
  204. float FlexContainer::GetShrinkToFitWidth() const
  205. {
  206. // For the trivial case of a fixed width, we simply return that.
  207. if (element->GetComputedValues().width().type == Style::Width::Type::Length)
  208. return box.GetSize().x;
  209. // Infer shrink-to-fit width from the intrinsic width of the element.
  210. return FlexFormattingContext::GetMaxContentSize(element).x;
  211. }
  212. String FlexContainer::DebugDumpTree(int depth) const
  213. {
  214. return String(depth * 2, ' ') + "FlexContainer" + " | " + LayoutDetails::GetDebugElementName(element);
  215. }
  216. TableWrapper::TableWrapper(Element* element, ContainerBox* parent_container) : ContainerBox(Type::TableWrapper, element, parent_container)
  217. {
  218. RMLUI_ASSERT(element);
  219. }
  220. void TableWrapper::Close(const Vector2f content_overflow_size, const Box& box, float element_baseline)
  221. {
  222. bool result = SubmitBox(content_overflow_size, box, -1.f);
  223. // Since the table wrapper cannot generate scrollbars, this should always pass.
  224. RMLUI_ASSERT(result);
  225. (void)result;
  226. ClosePositionedElements();
  227. SubmitElementLayout();
  228. SetElementBaseline(element_baseline);
  229. }
  230. float TableWrapper::GetShrinkToFitWidth() const
  231. {
  232. // We don't currently support shrink-to-fit layout of tables. However, for the trivial case of a fixed width, we
  233. // simply return that.
  234. if (element->GetComputedValues().width().type == Style::Width::Type::Length)
  235. return box.GetSize().x;
  236. return 0.0f;
  237. }
  238. String TableWrapper::DebugDumpTree(int depth) const
  239. {
  240. return String(depth * 2, ' ') + "TableWrapper" + " | " + LayoutDetails::GetDebugElementName(element);
  241. }
  242. } // namespace Rml