From dfa13dc46d4125dbf4e0b31f34b3025b2b41ee20 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Fri, 28 Oct 2022 02:26:31 +0300 Subject: [PATCH] add "ListLayout" and remove columnlayout and rowlayout --- meson.build | 6 ++--- src/ColumnLayout.cpp | 41 ------------------------------- src/ColumnLayout.hpp | 27 --------------------- src/ListLayout.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/ListLayout.hpp | 28 ++++++++++++++++++++++ src/RowLayout.cpp | 41 ------------------------------- src/RowLayout.hpp | 27 --------------------- src/main.cpp | 14 ++++++++++- 8 files changed, 100 insertions(+), 141 deletions(-) delete mode 100644 src/ColumnLayout.cpp delete mode 100644 src/ColumnLayout.hpp create mode 100644 src/ListLayout.cpp create mode 100644 src/ListLayout.hpp delete mode 100644 src/RowLayout.cpp delete mode 100644 src/RowLayout.hpp diff --git a/meson.build b/meson.build index 5c5673a..073ba8d 100644 --- a/meson.build +++ b/meson.build @@ -33,9 +33,8 @@ raven_source_files = [ './src/ScrollContainer.cpp', './src/Button.cpp', './src/DocumentLayout.cpp', - './src/RowLayout.cpp', - './src/ColumnLayout.cpp', './src/BoxLayout.cpp', + './src/ListLayout.cpp', './src/Label.cpp', './src/ListView.cpp', './src/TextInput.cpp', @@ -47,7 +46,6 @@ raven_header_files = [ './src/Box.hpp', './src/BoxLayout.hpp', './src/Button.hpp', - './src/ColumnLayout.hpp', './src/DocumentLayout.hpp', './src/Events.hpp', './src/Forward.hpp', @@ -57,8 +55,8 @@ raven_header_files = [ './src/Painter.hpp', './src/Point.hpp', './src/RGB.hpp', - './src/RowLayout.hpp', './src/ScrollContainer.hpp', + './src/ListLayout.hpp', './src/Styles.hpp', './src/Widget.hpp', './src/SvgWidget.hpp', diff --git a/src/ColumnLayout.cpp b/src/ColumnLayout.cpp deleted file mode 100644 index ede1397..0000000 --- a/src/ColumnLayout.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "ColumnLayout.hpp" -#include "Point.hpp" -#include "Widget.hpp" - -namespace Raven { - -bool ColumnLayout::run() { - if (!m_target) { - return false; - } - - Point current_point { m_margin, m_margin }; - double max_width_so_far = 0; - double requested_height = 0; - - auto& children = m_target->children(); - for (auto child : children) { - if (child->absolute()) { - continue; - } - - if (child->rect().width() > max_width_so_far) { - max_width_so_far = child->rect().width(); - } - - requested_height += child->rect().height() + m_margin; - - child->rect().set_x(current_point.x()); - child->rect().set_y(current_point.y()); - - current_point.add(0, child->rect().height() + m_margin); - } - - m_target->rect().set_height(requested_height + m_margin); - m_target->rect().set_width(max_width_so_far + m_margin * 2); - - return false; -} - -} - diff --git a/src/ColumnLayout.hpp b/src/ColumnLayout.hpp deleted file mode 100644 index b78bc09..0000000 --- a/src/ColumnLayout.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "Layout.hpp" - -namespace Raven { - -class ColumnLayout : public Layout { -private: - double m_margin { 0.0 }; -public: - ColumnLayout() - : Layout() {} - - ColumnLayout(double margin) - : Layout() - , m_margin(margin) {} - - bool run() override; - - double margin() { return m_margin; } - void set_margin(double margin) { m_margin = margin; run(); } - - virtual ~ColumnLayout() {} -}; - -} - diff --git a/src/ListLayout.cpp b/src/ListLayout.cpp new file mode 100644 index 0000000..e0b474c --- /dev/null +++ b/src/ListLayout.cpp @@ -0,0 +1,57 @@ +#include "ListLayout.hpp" +#include "Widget.hpp" +#include "Box.hpp" +#include "src/Events.hpp" + +namespace Raven { + +bool ListLayout::run() { + if (!m_target) { + return false; + } + + double current_position = m_margin; + double maximum_secondary_dimension = m_margin; + + if (m_direction == Direction::Horizontal) { + for (auto &child : m_target->children()) { + if (child->layout() && child->layout()->dynamically_sizes_target()) { + auto event = RelayoutSubtreeEvent(); + child->dispatch_event(event); + } + 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) { + maximum_secondary_dimension = child->rect().height(); + } + } + + m_target->rect().set_width(current_position + m_margin); + m_target->rect().set_height(maximum_secondary_dimension + m_margin * 2); + } else { + for (auto &child : m_target->children()) { + if (child->layout() && child->layout()->dynamically_sizes_target()) { + auto event = RelayoutSubtreeEvent(); + child->dispatch_event(event); + } + 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) { + maximum_secondary_dimension = child->rect().width(); + } + } + m_target->rect().set_width(maximum_secondary_dimension + m_margin * 2); + m_target->rect().set_height(current_position + m_margin); + } + + for (auto &child : m_target->children()) { + auto event = RelayoutSubtreeEvent(); + child->dispatch_event(event); + } + + return true; +} + +} diff --git a/src/ListLayout.hpp b/src/ListLayout.hpp new file mode 100644 index 0000000..adda29e --- /dev/null +++ b/src/ListLayout.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "Layout.hpp" +#include "Box.hpp" + + +namespace Raven { + +class ListLayout : public Layout { +public: + ListLayout(Direction direction) + : Layout() + , m_direction(direction) {} + + bool run(); + + double margin() { return m_margin; } + void set_margin(double margin) { m_margin = margin; run(); } + + double spacing() { return m_spacing; } + void set_spacing(double spacing) { m_spacing = spacing; run(); } +private: + double m_margin { 0.0 }; + double m_spacing { 0.0 }; + Direction m_direction; +}; + +} diff --git a/src/RowLayout.cpp b/src/RowLayout.cpp deleted file mode 100644 index f6343a0..0000000 --- a/src/RowLayout.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "RowLayout.hpp" -#include "Point.hpp" -#include "Widget.hpp" - -namespace Raven { - -bool RowLayout::run() { - if (!m_target) { - return false; - } - - Point current_point { m_margin, m_margin }; - double max_height_so_far = 0; - double requested_width = 0; - - auto& children = m_target->children(); - for (auto child : children) { - if (child->absolute()) { - continue; - } - - if (child->rect().height() > max_height_so_far) { - max_height_so_far = child->rect().height(); - } - - requested_width += child->rect().width() + m_margin; - - child->rect().set_x(current_point.x()); - child->rect().set_y(current_point.y()); - - current_point.add(child->rect().width() + m_margin, 0); - } - - m_target->rect().set_width(requested_width + m_margin); - m_target->rect().set_height(max_height_so_far + m_margin * 2); - - return false; -} - -} - diff --git a/src/RowLayout.hpp b/src/RowLayout.hpp deleted file mode 100644 index e0df646..0000000 --- a/src/RowLayout.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "Layout.hpp" - -namespace Raven { - -class RowLayout : public Layout { -private: - double m_margin { 0.0 }; -public: - RowLayout() - : Layout() {} - - RowLayout(double margin) - : Layout() - , m_margin(margin) {} - - bool run() override; - - double margin() { return m_margin; } - void set_margin(double margin) { m_margin = margin; run(); } - - virtual ~RowLayout() {} -}; - -} - diff --git a/src/main.cpp b/src/main.cpp index 0a4c03f..753fbd7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ #include "Label.hpp" #include "Application.hpp" -#include "ColumnLayout.hpp" +#include "ListLayout.hpp" #include "ListView.hpp" #include "TextInput.hpp" #include "Window.hpp" @@ -24,8 +24,20 @@ int main() { Raven::Application application {}; auto window = application.add_window(); + auto test_window = application.add_window(); window->spawn_window(); + test_window->spawn_window(); + + auto test_container = test_window->set_main_widget(); + auto test_widget = test_container->make_target(); + auto test_layout = test_widget->set_layout(Raven::Direction::Vertical); + test_layout->set_margin(69); + test_layout->set_spacing(10); + + for (int i = 0; i < 1000; i++) { + test_widget->add("Hello: " + std::to_string(i)); + } auto main_widget = window->set_main_widget(); auto main_widget_layout = main_widget->set_layout(Raven::Direction::Horizontal);