From fc59ad6e105f2a6b7cdbd670baf305f128f0ea76 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Fri, 28 Oct 2022 02:45:39 +0300 Subject: [PATCH] add "inherit_secondary_dimension" to listlayout --- src/ListLayout.cpp | 17 +++++++++++++++-- src/ListLayout.hpp | 4 ++++ src/main.cpp | 13 +++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/ListLayout.cpp b/src/ListLayout.cpp index e0b474c..4d4ec45 100644 --- a/src/ListLayout.cpp +++ b/src/ListLayout.cpp @@ -22,11 +22,17 @@ bool ListLayout::run() { child->rect().set_x(current_position); child->rect().set_y(m_margin); current_position += child->rect().width() + m_spacing; - if (child->rect().height() > maximum_secondary_dimension) { + if (m_inherit_secondary_dimension) { + child->rect().set_height(m_target->rect().height() - m_margin * 2); + } else if (child->rect().height() > maximum_secondary_dimension) { maximum_secondary_dimension = child->rect().height(); } } + if (m_inherit_secondary_dimension) { + maximum_secondary_dimension = m_target->rect().height(); + } + m_target->rect().set_width(current_position + m_margin); m_target->rect().set_height(maximum_secondary_dimension + m_margin * 2); } else { @@ -38,10 +44,17 @@ bool ListLayout::run() { child->rect().set_y(current_position); child->rect().set_x(m_margin); current_position += child->rect().height() + m_spacing; - if (child->rect().width() > maximum_secondary_dimension) { + if (m_inherit_secondary_dimension) { + child->rect().set_width(m_target->rect().width() - m_margin * 2); + } else if (child->rect().width() > maximum_secondary_dimension) { maximum_secondary_dimension = child->rect().width(); } } + + if (m_inherit_secondary_dimension) { + maximum_secondary_dimension = m_target->rect().width(); + } + m_target->rect().set_width(maximum_secondary_dimension + m_margin * 2); m_target->rect().set_height(current_position + m_margin); } diff --git a/src/ListLayout.hpp b/src/ListLayout.hpp index adda29e..e7f0988 100644 --- a/src/ListLayout.hpp +++ b/src/ListLayout.hpp @@ -19,9 +19,13 @@ public: double spacing() { return m_spacing; } void set_spacing(double spacing) { m_spacing = spacing; run(); } + + bool inherit_secondary_dimension() { return m_inherit_secondary_dimension; } + void set_inherit_secondary_dimension(bool inherit_secondary_dimension) { m_inherit_secondary_dimension = inherit_secondary_dimension; } private: double m_margin { 0.0 }; double m_spacing { 0.0 }; + bool m_inherit_secondary_dimension { false }; Direction m_direction; }; diff --git a/src/main.cpp b/src/main.cpp index 58da265..c07508a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,9 +34,18 @@ int main() { auto test_layout = test_widget->set_layout(Raven::Direction::Vertical); test_layout->set_margin(69); test_layout->set_spacing(10); + test_layout->set_inherit_secondary_dimension(true); - for (int i = 0; i < 50; i++) { - test_widget->add("Hello: " + std::to_string(i)); + for (int i = 0; i < 150; i++) { + auto button = test_widget->add("Hello: " + std::to_string(i)); + button->set_style(&Raven::raised_button_style); + button->on_click = [button]() { + if (button->style() == &Raven::accent_button_style) { + button->set_style(&Raven::raised_button_style); + } else { + button->set_style(&Raven::accent_button_style); + } + }; } auto main_widget = window->set_main_widget();