From 8b1ffb9b39a259e957727de558fde8758bb61198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 24 Oct 2026 12:49:45 +0200 Subject: [PATCH] UI: Apply GridLayout children's margins, drop the unused grid orientation Grid children are given LinearLayoutParams almost everywhere, so their margins are taken from those as well as from GridLayoutParams, which gains margins. Co-Authored-By: Claude Opus 5.5 (1M context) --- Common/UI/ViewGroup.cpp | 21 ++++++++++++++++----- Common/UI/ViewGroup.h | 12 +++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Common/UI/ViewGroup.cpp b/Common/UI/ViewGroup.cpp index 111a7f54fc..5ac6eee1d3 100644 --- a/Common/UI/ViewGroup.cpp +++ b/Common/UI/ViewGroup.cpp @@ -1016,9 +1016,19 @@ void AnchorLayout::Layout() { } GridLayout::GridLayout(GridLayoutSettings settings, LayoutParams *layoutParams) - : ViewGroup(layoutParams), settings_(settings) { - if (settings.orientation != ORIENT_HORIZONTAL) - ERROR_LOG(Log::UI, "GridLayout: Vertical layouts not yet supported"); + : ViewGroup(layoutParams), settings_(settings) {} + +// Grid children are mostly given LinearLayoutParams, so take the margins from those too. +static Margins GetGridChildMargins(const View *view) { + if (const GridLayoutParams *lp = view->GetLayoutParams()->As()) { + return lp->margins; + } + if (const LinearLayoutParams *lp = view->GetLayoutParams()->As()) { + if (lp->HasMargins()) { + return lp->margins; + } + } + return Margins(0); } void GridLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) { @@ -1027,7 +1037,8 @@ void GridLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec ver int numItems = 0; for (size_t i = 0; i < views_.size(); i++) { if (views_[i]->GetVisibility() != V_GONE) { - views_[i]->Measure(dc, MeasureSpec(measureType, settings_.columnWidth), MeasureSpec(measureType, settings_.rowHeight)); + const Margins margins = GetGridChildMargins(views_[i]); + views_[i]->Measure(dc, MeasureSpec(measureType, settings_.columnWidth - margins.horiz()), MeasureSpec(measureType, settings_.rowHeight - margins.vert())); numItems++; } } @@ -1063,7 +1074,7 @@ void GridLayout::Layout() { itemBounds.w = settings_.columnWidth; itemBounds.h = settings_.rowHeight; - ApplyGravity(itemBounds, Margins(0.0f), + ApplyGravity(itemBounds, GetGridChildMargins(views_[i]), views_[i]->GetMeasuredWidth(), views_[i]->GetMeasuredHeight(), grav, innerBounds); diff --git a/Common/UI/ViewGroup.h b/Common/UI/ViewGroup.h index dda760b2cb..90dce181c3 100644 --- a/Common/UI/ViewGroup.h +++ b/Common/UI/ViewGroup.h @@ -267,14 +267,12 @@ private: }; // GridLayout is a little different from the Android layout. This one has fixed size -// rows and columns. Items are not allowed to deviate from the set sizes. -// Initially, only horizontal layout is supported. +// rows and columns, filled row by row. Items are not allowed to deviate from the set sizes. struct GridLayoutSettings { - GridLayoutSettings() : orientation(ORIENT_HORIZONTAL), columnWidth(100), rowHeight(50), spacing(5), fillCells(false) {} + GridLayoutSettings() : columnWidth(100), rowHeight(50), spacing(5), fillCells(false) {} GridLayoutSettings(int colW, int colH, int spac = 5) - : orientation(ORIENT_HORIZONTAL), columnWidth(colW), rowHeight(colH), spacing(spac), fillCells(false) {} + : columnWidth(colW), rowHeight(colH), spacing(spac), fillCells(false) {} - Orientation orientation; int columnWidth; int rowHeight; int spacing; @@ -288,8 +286,12 @@ public: explicit GridLayoutParams(Gravity grav) : LayoutParams(LP_GRID), gravity(grav) { } + GridLayoutParams(Gravity grav, const Margins &mgn) + : LayoutParams(LP_GRID), gravity(grav), margins(mgn) { + } Gravity gravity; + Margins margins; static LayoutParamsType StaticType() { return LP_GRID;