From 72720d216d0c0bb27efb4910ec85aae5e7d8eb39 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Wed, 6 Jul 2022 03:41:00 +0300 Subject: [PATCH] document layout automatic resize --- src/DocumentLayout.cpp | 8 +++++++- src/main.cpp | 16 +++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/DocumentLayout.cpp b/src/DocumentLayout.cpp index 3f3bb07..ec4cd0c 100644 --- a/src/DocumentLayout.cpp +++ b/src/DocumentLayout.cpp @@ -9,6 +9,7 @@ void DocumentLayout::run() { if (!m_target) return; + Point bound { m_margin * 2, m_margin * 2 }; Point current_position { m_margin, m_margin }; double largest_height_so_far = -1.0; @@ -22,10 +23,11 @@ void DocumentLayout::run() { } bool new_row_because_of_control_widget = (child->control_type() == ControlWidgetType::NewRow); - bool new_row_because_of_justification = (current_position.x() + child->rect().width() + m_margin) >= m_target->rect().width(); + bool new_row_because_of_justification = (current_position.x() + child->rect().width() + m_margin) >= m_target->rect().max_geometry().width(); bool should_do_new_row = new_row_because_of_control_widget || new_row_because_of_justification; if (should_do_new_row) { current_position.add(0, largest_height_so_far + m_margin); + bound.add(0, largest_height_so_far + m_margin); current_position.set_x(m_margin); } @@ -34,8 +36,12 @@ void DocumentLayout::run() { if (!new_row_because_of_control_widget) { current_position.add(child->rect().width() + m_margin, 0); + bound.add(child->rect().width() + m_margin, 0); } } + + m_target->rect().set_width(bound.x()); + m_target->rect().set_height(bound.y()); } } diff --git a/src/main.cpp b/src/main.cpp index 28d3684..7da42ce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,11 +23,17 @@ int main() { auto main_widget = window.set_main_widget(); main_widget->set_layout(6.0); - auto compile_button = main_widget->add("compile and run program"); - compile_button->set_style(&Raven::accent_button_style); - compile_button->on_click = []() { - system("./compile.sh"); - }; + auto scroll = main_widget->add(); + scroll->set_style(&Raven::accent_widget_style); + scroll->resize(200, 200); + auto target = scroll->make_target(); + target->set_layout(8.0); + target->rect().set_max_width(200); + + for (int i = 0; i < 300; i++) { + auto button = target->add(std::to_string(i)); + button->set_style(&Raven::accent_button_style); + } window.run(true); return 0;