From 3e7f7ad8d77c023d5cea706d4f57db84dda75eba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 8 Dec 2022 00:01:46 +0100 Subject: [PATCH] Add CSS-style padding support to LinearLayout. Use to improve the look --- Common/UI/View.h | 7 +++++++ Common/UI/ViewGroup.cpp | 42 +++++++++++++++++++------------------- Common/UI/ViewGroup.h | 2 ++ UI/DisplayLayoutScreen.cpp | 14 +++++++------ 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/Common/UI/View.h b/Common/UI/View.h index 0d52216771..1ab1ea5f68 100644 --- a/Common/UI/View.h +++ b/Common/UI/View.h @@ -287,6 +287,13 @@ struct Margins { int vert() const { return top + bottom; } + void SetAll(float f) { + int8_t i = (int)f; + top = i; + bottom = i; + left = i; + right = i; + } int8_t top; int8_t bottom; diff --git a/Common/UI/ViewGroup.cpp b/Common/UI/ViewGroup.cpp index ce030a3249..50398dac45 100644 --- a/Common/UI/ViewGroup.cpp +++ b/Common/UI/ViewGroup.cpp @@ -539,19 +539,19 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v MeasureSpec v = vert; if (v.type == UNSPECIFIED && measuredHeight_ != 0.0f) v = MeasureSpec(AT_MOST, measuredHeight_); - view->Measure(dc, MeasureSpec(UNSPECIFIED, measuredWidth_), v - (float)margins.vert()); - if (horiz.type == AT_MOST && view->GetMeasuredWidth() + margins.horiz() > horiz.size - weightZeroSum) { + view->Measure(dc, MeasureSpec(UNSPECIFIED, measuredWidth_), v - (float)margins.vert() - (float)padding.vert()); + if (horiz.type == AT_MOST && view->GetMeasuredWidth() + margins.horiz() + padding.horiz() > horiz.size - weightZeroSum) { // Try again, this time with AT_MOST. - view->Measure(dc, horiz, v - (float)margins.vert()); + view->Measure(dc, horiz, v - (float)margins.vert() - (float)padding.vert()); } } else if (orientation_ == ORIENT_VERTICAL) { MeasureSpec h = horiz; if (h.type == UNSPECIFIED && measuredWidth_ != 0.0f) h = MeasureSpec(AT_MOST, measuredWidth_); - view->Measure(dc, h - (float)margins.horiz(), MeasureSpec(UNSPECIFIED, measuredHeight_)); - if (vert.type == AT_MOST && view->GetMeasuredHeight() + margins.vert() > vert.size - weightZeroSum) { + view->Measure(dc, h - (float)margins.horiz() - (float)padding.horiz(), MeasureSpec(UNSPECIFIED, measuredHeight_)); + if (vert.type == AT_MOST && view->GetMeasuredHeight() + margins.vert() + padding.horiz() > vert.size - weightZeroSum) { // Try again, this time with AT_MOST. - view->Measure(dc, h - (float)margins.horiz(), vert); + view->Measure(dc, h - (float)margins.horiz() - (float)padding.horiz(), vert); } } @@ -575,12 +575,12 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v } } - weightZeroSum += spacing_ * (numVisible - 1); + weightZeroSum += spacing_ * (numVisible - 1); // +(orientation_ == ORIENT_HORIZONTAL) ? padding.horiz() : padding.vert(); // Alright, got the sum. Let's take the remaining space after the fixed-size views, // and distribute among the weighted ones. if (orientation_ == ORIENT_HORIZONTAL) { - MeasureBySpec(layoutParams_->width, weightZeroSum, horiz, &measuredWidth_); + MeasureBySpec(layoutParams_->width, weightZeroSum + padding.horiz(), horiz, &measuredWidth_); // If we've got stretch, allow growing to fill the parent. float allowedWidth = measuredWidth_; @@ -588,7 +588,7 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v allowedWidth = horiz.size; } - float usedWidth = 0.0f; + float usedWidth = 0.0f + padding.horiz(); // Redistribute the stretchy ones! and remeasure the children! for (View *view : views_) { @@ -615,7 +615,7 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v if (horiz.type == EXACTLY) { h.type = EXACTLY; } - view->Measure(dc, h, v - (float)margins.vert()); + view->Measure(dc, h, v - (float)margins.vert() - (float)padding.vert()); usedWidth += view->GetMeasuredWidth(); maxOther = std::max(maxOther, view->GetMeasuredHeight() + margins.vert()); } @@ -626,9 +626,9 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v } // Measure here in case maxOther moved (can happen due to word wrap.) - MeasureBySpec(layoutParams_->height, maxOther, vert, &measuredHeight_); + MeasureBySpec(layoutParams_->height, maxOther + padding.vert(), vert, &measuredHeight_); } else { - MeasureBySpec(layoutParams_->height, weightZeroSum, vert, &measuredHeight_); + MeasureBySpec(layoutParams_->height, weightZeroSum + padding.vert(), vert, &measuredHeight_); // If we've got stretch, allow growing to fill the parent. float allowedHeight = measuredHeight_; @@ -636,7 +636,7 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v allowedHeight = vert.size; } - float usedHeight = 0.0f; + float usedHeight = 0.0f + padding.vert(); // Redistribute the stretchy ones! and remeasure the children! for (View *view : views_) { @@ -663,7 +663,7 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v if (vert.type == EXACTLY) { v.type = EXACTLY; } - view->Measure(dc, h - (float)margins.horiz(), v); + view->Measure(dc, h - (float)margins.horiz() - (float)padding.horiz(), v); usedHeight += view->GetMeasuredHeight(); maxOther = std::max(maxOther, view->GetMeasuredWidth() + margins.horiz()); } @@ -674,7 +674,7 @@ void LinearLayout::Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec v } // Measure here in case maxOther moved (can happen due to word wrap.) - MeasureBySpec(layoutParams_->width, maxOther, horiz, &measuredWidth_); + MeasureBySpec(layoutParams_->width, maxOther + padding.horiz(), horiz, &measuredWidth_); } } @@ -686,13 +686,13 @@ void LinearLayout::Layout() { float pos; if (orientation_ == ORIENT_HORIZONTAL) { - pos = bounds.x; - itemBounds.y = bounds.y; - itemBounds.h = measuredHeight_; + pos = bounds.x + padding.left; + itemBounds.y = bounds.y + padding.top; + itemBounds.h = measuredHeight_ - padding.vert(); } else { - pos = bounds.y; - itemBounds.x = bounds.x; - itemBounds.w = measuredWidth_; + pos = bounds.y + padding.top; + itemBounds.x = bounds.x + padding.left; + itemBounds.w = measuredWidth_ - padding.horiz(); } for (size_t i = 0; i < views_.size(); i++) { diff --git a/Common/UI/ViewGroup.h b/Common/UI/ViewGroup.h index 78c42e84b1..d408548b69 100644 --- a/Common/UI/ViewGroup.h +++ b/Common/UI/ViewGroup.h @@ -106,6 +106,7 @@ protected: // It simply centers the child view. class FrameLayout : public ViewGroup { public: + FrameLayout(LayoutParams *layoutParams = nullptr) : ViewGroup(layoutParams) {} void Measure(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert) override; void Layout() override; }; @@ -193,6 +194,7 @@ public: spacing_ = spacing; } std::string DescribeLog() const override { return (orientation_ == ORIENT_HORIZONTAL ? "LinearLayoutHoriz: " : "LinearLayoutVert: ") + View::DescribeLog(); } + Margins padding; protected: Orientation orientation_; diff --git a/UI/DisplayLayoutScreen.cpp b/UI/DisplayLayoutScreen.cpp index 9efb29688a..678977916d 100644 --- a/UI/DisplayLayoutScreen.cpp +++ b/UI/DisplayLayoutScreen.cpp @@ -203,14 +203,16 @@ void DisplayLayoutScreen::CreateViews() { // impossible. root_->SetExclusiveTouch(true); - ScrollView *leftScrollView = new ScrollView(ORIENT_VERTICAL, new AnchorLayoutParams(400.0f, FILL_PARENT, 10.f, 10.f, NONE, 10.f, false)); - ViewGroup *leftColumn = new LinearLayout(ORIENT_VERTICAL); + ScrollView *leftScrollView = new ScrollView(ORIENT_VERTICAL, new AnchorLayoutParams(420.0f, FILL_PARENT, 0.f, 0.f, NONE, 0.f, false)); + LinearLayout *leftColumn = new LinearLayout(ORIENT_VERTICAL); + leftColumn->padding.SetAll(8.0f); leftScrollView->Add(leftColumn); leftScrollView->SetClickableBackground(true); root_->Add(leftScrollView); - ScrollView *rightScrollView = new ScrollView(ORIENT_VERTICAL, new AnchorLayoutParams(300.0f, FILL_PARENT, NONE, 10.f, 10.f, 10.f, false)); - ViewGroup *rightColumn = new LinearLayout(ORIENT_VERTICAL); + ScrollView *rightScrollView = new ScrollView(ORIENT_VERTICAL, new AnchorLayoutParams(300.0f, FILL_PARENT, NONE, 0.f, 0.f, 0.f, false)); + LinearLayout *rightColumn = new LinearLayout(ORIENT_VERTICAL); + rightColumn->padding.SetAll(8.0f); rightScrollView->Add(rightColumn); rightScrollView->SetClickableBackground(true); root_->Add(rightScrollView); @@ -220,8 +222,8 @@ void DisplayLayoutScreen::CreateViews() { // Set backgrounds for readability Drawable backgroundWithAlpha(GetBackgroundColorWithAlpha(*screenManager()->getUIContext())); - leftScrollView->SetBG(backgroundWithAlpha); - rightScrollView->SetBG(backgroundWithAlpha); + leftColumn->SetBG(backgroundWithAlpha); + rightColumn->SetBG(backgroundWithAlpha); if (!IsVREnabled()) { auto stretch = new CheckBox(&g_Config.bDisplayStretch, gr->T("Stretch"));