From 94201a0ba187dfc2ca2fed38ba354e83c4241933 Mon Sep 17 00:00:00 2001 From: Diego Iastrubni Date: Sun, 7 Jan 2024 15:42:15 +0200 Subject: [PATCH] HorizontalLayout: weights Horizontal weights can be defined. This is used to align the buttons to the right. Same code is used for vertical seems the same. However - the code breaks. --- src/layout.cpp | 17 +++++++++-------- src/layout.h | 1 + src/main.cxx | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/layout.cpp b/src/layout.cpp index 0551290..9dd53f6 100644 --- a/src/layout.cpp +++ b/src/layout.cpp @@ -12,6 +12,7 @@ auto HorizontalLayout::relayout(Position position, const Size size) -> void { return; } auto recommended_size = Size{}; + auto total_weight = 0.0; // first pass - find all widgets with size hints, and subtract them // from the list @@ -28,11 +29,9 @@ auto HorizontalLayout::relayout(Position position, const Size size) -> void { width -= hint.width; } width -= padding.get_horizontal(); + total_weight += item->weight; } - if (widget_count != 0) { - width = width / widget_count; - } recommended_size.width = width; recommended_size.height = size.height - margin.get_vertical(); position.y += margin.top; @@ -56,7 +55,8 @@ auto HorizontalLayout::relayout(Position position, const Size size) -> void { } auto hint = item->size_hint(); if (hint.width <= 0) { - recommended_size.width = width; + recommended_size.width = (width * item->weight) / total_weight; + } else { recommended_size.width = hint.width; } @@ -107,6 +107,7 @@ auto VerticalLayout::relayout(Position position, const Size size) -> void { return; } auto recommended_size = Size{}; + auto total_weight = 0.0; // first pass - find all widgets with size hints, and subtract them // from the list @@ -123,10 +124,9 @@ auto VerticalLayout::relayout(Position position, const Size size) -> void { height -= hint.height; } height -= padding.get_vertical(); + total_weight += item->weight; } - if (widget_count != 0) { - height = height / widget_count; - } + recommended_size.height = height; recommended_size.width = size.width - margin.get_horizontal(); position.y += margin.top; @@ -150,7 +150,8 @@ auto VerticalLayout::relayout(Position position, const Size size) -> void { } auto hint = item->size_hint(); if (hint.height <= 0) { - recommended_size.height = height; + recommended_size.height = height / widget_count; + // recommended_size.height = (height * item->weight) / total_weight; } else { recommended_size.height = hint.height; } diff --git a/src/layout.h b/src/layout.h index 92aec70..d9c36a6 100644 --- a/src/layout.h +++ b/src/layout.h @@ -46,6 +46,7 @@ struct LayouttItem { std::list> sub_items; LayoutParams padding = {}; LayoutParams margin = {}; + double weight = 1; virtual auto relayout(Position posiition, const Size size) -> void = 0; virtual auto size_hint() const -> Size = 0; diff --git a/src/main.cxx b/src/main.cxx index 42c49e5..996dba9 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -195,7 +195,7 @@ int main() { l2->margin.set_horitzonal(5); l2->margin.set_vertical(5); l2->padding.set_horitzonal(10); - l2->add(std::make_shared()); + l2->add(std::make_shared())->weight = 2; w1->add_new_to_layout