Browse Source

Allow layout caching, even under max-content constraint

This introduces a new rule that max-content constraint should also format things the same way, as long as the parameters passed to the cache is the same too.

If we want to stray from this rule, we could later add separate caches for max-content and the final content. Or alternatively, say that we can use the final cache for max-content, but never the max-content cache for final layout.

This change further improves nested flexbox layout (from 18 to 15).
Michael Ragazzon 5 months ago
parent
commit
ddac6fcd2f
2 changed files with 17 additions and 19 deletions
  1. 16 18
      Source/Core/Layout/FormattingContext.cpp
  2. 1 1
      Tests/Source/UnitTests/FlexFormatting.cpp

+ 16 - 18
Source/Core/Layout/FormattingContext.cpp

@@ -92,7 +92,7 @@ UniquePtr<LayoutBox> FormattingContext::FormatIndependent(ContainerBox* parent_c
 	if (type == FormattingContextType::None)
 	if (type == FormattingContextType::None)
 		return nullptr;
 		return nullptr;
 
 
-	if (element->GetId() == "outer")
+	if (element->GetId() == "special")
 		int x = 0;
 		int x = 0;
 
 
 #ifdef RMLUI_DEBUG
 #ifdef RMLUI_DEBUG
@@ -119,18 +119,16 @@ UniquePtr<LayoutBox> FormattingContext::FormatIndependent(ContainerBox* parent_c
 	UniquePtr<LayoutBox> layout_box;
 	UniquePtr<LayoutBox> layout_box;
 
 
 	const FormattingMode& formatting_mode = parent_container->GetFormattingMode();
 	const FormattingMode& formatting_mode = parent_container->GetFormattingMode();
-	if (formatting_mode.constraint == FormattingMode::Constraint::None)
+
 	{
 	{
 		LayoutNode* layout_node = element->GetLayoutNode();
 		LayoutNode* layout_node = element->GetLayoutNode();
 		if (layout_node->CommittedLayoutMatches(parent_container->GetContainingBlockSize(Style::Position::Static),
 		if (layout_node->CommittedLayoutMatches(parent_container->GetContainingBlockSize(Style::Position::Static),
 				parent_container->GetContainingBlockSize(Style::Position::Static), override_initial_box))
 				parent_container->GetContainingBlockSize(Style::Position::Static), override_initial_box))
 		{
 		{
+#ifdef RMLUI_DEBUG
 			Log::Message(Log::LT_INFO, "Layout cache match on element%s: %s",
 			Log::Message(Log::LT_INFO, "Layout cache match on element%s: %s",
 				(formatting_mode.allow_format_independent_cache ? "" : " (skipping cache due to formatting mode)"), element->GetAddress().c_str());
 				(formatting_mode.allow_format_independent_cache ? "" : " (skipping cache due to formatting mode)"), element->GetAddress().c_str());
-			// TODO: How to deal with ShrinkToFitWidth, in particular for the returned box? Store it in the LayoutNode?
-			//   Maybe best not to use this committed layout at all during max-content layouting. Instead, skip this here,
-			//   return zero in the CacheContainerBox, and make a separate LayoutNode cache for shrink-to-fit width that
-			//   is fetched in LayoutDetails::ShrinkToFitWidth().
+#endif
 			if (formatting_mode.allow_format_independent_cache)
 			if (formatting_mode.allow_format_independent_cache)
 			{
 			{
 				layout_box = MakeUnique<CachedContainer>(element, parent_container, element->GetBox(),
 				layout_box = MakeUnique<CachedContainer>(element, parent_container, element->GetBox(),
@@ -169,19 +167,19 @@ UniquePtr<LayoutBox> FormattingContext::FormatIndependent(ContainerBox* parent_c
 		}
 		}
 
 
 		// TODO: If MaxContent constraint, and containing block = -1, -1, store resulting size as max-content size.
 		// TODO: If MaxContent constraint, and containing block = -1, -1, store resulting size as max-content size.
-	}
-
-	if (layout_box && formatting_mode.constraint == FormattingMode::Constraint::None)
-	{
-		Optional<float> baseline_of_last_line;
-		float baseline_of_last_line_value = 0.f;
-		if (layout_box->GetBaselineOfLastLine(baseline_of_last_line_value))
-			baseline_of_last_line = baseline_of_last_line_value;
 
 
-		LayoutNode* layout_node = element->GetLayoutNode();
-		layout_node->CommitLayout(parent_container->GetContainingBlockSize(Style::Position::Static),
-			parent_container->GetContainingBlockSize(Style::Position::Absolute), override_initial_box, layout_box->GetVisibleOverflowSize(),
-			baseline_of_last_line);
+		if (layout_box)
+		{
+			Optional<float> baseline_of_last_line;
+			float baseline_of_last_line_value = 0.f;
+			if (layout_box->GetBaselineOfLastLine(baseline_of_last_line_value))
+				baseline_of_last_line = baseline_of_last_line_value;
+
+			LayoutNode* layout_node = element->GetLayoutNode();
+			layout_node->CommitLayout(parent_container->GetContainingBlockSize(Style::Position::Static),
+				parent_container->GetContainingBlockSize(Style::Position::Absolute), override_initial_box, layout_box->GetVisibleOverflowSize(),
+				baseline_of_last_line);
+		}
 	}
 	}
 
 
 #ifdef RMLUI_DEBUG
 #ifdef RMLUI_DEBUG

+ 1 - 1
Tests/Source/UnitTests/FlexFormatting.cpp

@@ -258,7 +258,7 @@ static const String rml_flexbox_shrink_to_fit = R"(
 	<div class="outer" id="main">
 	<div class="outer" id="main">
 		<div class="inner">
 		<div class="inner">
 			<div class="outer">
 			<div class="outer">
-				<div class="inner">
+				<div class="inner" id="special">
 					<div class="outer">
 					<div class="outer">
 						<div class="inner">Flex</div>
 						<div class="inner">Flex</div>
 					</div>
 					</div>