From 8a94ab779b359f3fbcea5bf0cbc2d3373bdba179 Mon Sep 17 00:00:00 2001 From: Michael Ragazzon Date: Wed, 11 Oct 2023 23:29:44 +0200 Subject: [PATCH] Make flex containers and tables with fixed width work in shrink-to-fit contexts, see #520 --- Source/Core/Layout/ContainerBox.cpp | 20 ++++++ Source/Core/Layout/ContainerBox.h | 4 ++ Tests/Data/VisualTests/shrink_to_fit_04.rml | 67 +++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 Tests/Data/VisualTests/shrink_to_fit_04.rml diff --git a/Source/Core/Layout/ContainerBox.cpp b/Source/Core/Layout/ContainerBox.cpp index 34c67e6fc..7db89adfb 100644 --- a/Source/Core/Layout/ContainerBox.cpp +++ b/Source/Core/Layout/ContainerBox.cpp @@ -267,6 +267,16 @@ bool FlexContainer::Close(const Vector2f content_overflow_size, const Box& box, return true; } +float FlexContainer::GetShrinkToFitWidth() const +{ + // We don't currently support shrink-to-fit layout of flex containers. However, for the trivial case of a fixed + // width, we simply return that. + if (element->GetComputedValues().width().type == Style::Width::Type::Length) + return box.GetSize().x; + + return 0.0f; +} + String FlexContainer::DebugDumpTree(int depth) const { return String(depth * 2, ' ') + "FlexContainer" + " | " + LayoutDetails::GetDebugElementName(element); @@ -291,6 +301,16 @@ void TableWrapper::Close(const Vector2f content_overflow_size, const Box& box, f SetElementBaseline(element_baseline); } +float TableWrapper::GetShrinkToFitWidth() const +{ + // We don't currently support shrink-to-fit layout of tables. However, for the trivial case of a fixed width, we + // simply return that. + if (element->GetComputedValues().width().type == Style::Width::Type::Length) + return box.GetSize().x; + + return 0.0f; +} + String TableWrapper::DebugDumpTree(int depth) const { return String(depth * 2, ' ') + "TableWrapper" + " | " + LayoutDetails::GetDebugElementName(element); diff --git a/Source/Core/Layout/ContainerBox.h b/Source/Core/Layout/ContainerBox.h index 3fe0e9fd6..3fe3efd1e 100644 --- a/Source/Core/Layout/ContainerBox.h +++ b/Source/Core/Layout/ContainerBox.h @@ -131,6 +131,8 @@ class FlexContainer final : public ContainerBox { // @returns True if it succeeds, otherwise false if it needs to be formatted again because scrollbars were enabled. bool Close(const Vector2f content_overflow_size, const Box& box, float element_baseline); + float GetShrinkToFitWidth() const override; + const Box* GetIfBox() const override { return &box; } String DebugDumpTree(int depth) const override; @@ -152,6 +154,8 @@ class TableWrapper final : public ContainerBox { // Submits the formatted box to the table element, and propagates any uncaught overflow to this box. void Close(const Vector2f content_overflow_size, const Box& box, float element_baseline); + float GetShrinkToFitWidth() const override; + const Box* GetIfBox() const override { return &box; } String DebugDumpTree(int depth) const override; diff --git a/Tests/Data/VisualTests/shrink_to_fit_04.rml b/Tests/Data/VisualTests/shrink_to_fit_04.rml new file mode 100644 index 000000000..a19c9cee8 --- /dev/null +++ b/Tests/Data/VisualTests/shrink_to_fit_04.rml @@ -0,0 +1,67 @@ + + + Shrink-to-fit 4 + + + + + + + +

The following boxes should all appear the same, with a border wrapped all the way around the background and no red visible.

+ +
+
+
Hello
+
+
+
+
+
+
Hello
+
+
+
+
+
+
Hello
+
+
+
+
+
+
Hello
+
+
+
+
+
+
Hello
+
+
+ +