From a869385d09264b7cac11ee96f349fff8fb1ae4a4 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 10 Jul 2022 00:18:12 +0300 Subject: [PATCH] modify box layout to better support of minimum and maximum sizes of widgets, still super janky --- src/BoxLayout.cpp | 64 +++++++++++++++++++++++++++++++------------- src/BoxLayout.hpp | 3 ++- src/ColumnLayout.cpp | 39 +++++++++++++++++++++++++++ src/ColumnLayout.hpp | 27 +++++++++++++++++++ src/RowLayout.cpp | 39 +++++++++++++++++++++++++++ src/RowLayout.hpp | 27 +++++++++++++++++++ src/main.cpp | 4 +-- 7 files changed, 181 insertions(+), 22 deletions(-) create mode 100644 src/ColumnLayout.cpp create mode 100644 src/ColumnLayout.hpp create mode 100644 src/RowLayout.cpp create mode 100644 src/RowLayout.hpp diff --git a/src/BoxLayout.cpp b/src/BoxLayout.cpp index 27a3a1b..b9c0ac9 100644 --- a/src/BoxLayout.cpp +++ b/src/BoxLayout.cpp @@ -16,16 +16,53 @@ void BoxLayout::run() { Box free_space = m_target->rect().max_geometry(); Point current_position { 0.0, 0.0 }; int unslotted_widgets = 0; + std::vector working_slots(m_slots); - // Loop through all children: - // - Children which do not have a corresponding slot are counted into the unslotted_widgets variable, which will be used to designate the amount of space per unslotted widget. - // - Slots with a matching child will have their percentages computed. The amount of remaining free space after slotted widgets are added is also calculated. for (unsigned int i = 0; i < m_target->children().size(); i++) { if (i >= m_slots.size()) { - unslotted_widgets++; - } else { - Slot *slot = &m_slots[i]; auto child = m_target->children()[i]; + if (m_direction == Direction::Horizontal) { + if (child->rect().min_width() != -1 || child->rect().max_width() != -1) { + child->rect().update(); + working_slots.push_back(Slot { + 0, + child->rect().width(), + SlotType::Pixel + }); + } else { + unslotted_widgets++; + working_slots.push_back(Slot { + 0, + 0, + SlotType::Auto + }); + } + } else { + if (child->rect().min_height() != -1 || child->rect().max_height() != -1) { + child->rect().update(); + working_slots.push_back(Slot { + 0, + child->rect().height(), + SlotType::Pixel + }); + } else { + unslotted_widgets++; + working_slots.push_back(Slot { + 0, + 0, + SlotType::Auto + }); + } + } + } + } + + for (unsigned int i = 0; i < m_target->children().size(); i++) { + if (i < m_slots.size()) { + Slot *slot = &working_slots[i]; + auto child = m_target->children()[i]; + + if (slot->type == SlotType::Auto) continue; if (m_direction == Direction::Horizontal) { if (slot->type == SlotType::Percent) { @@ -59,18 +96,9 @@ void BoxLayout::run() { for (unsigned int i = 0; i < m_target->children().size(); i++) { auto child = m_target->children()[i]; - - // Determine the slot to use for this widget. - // Widgets which don't have a slot will get a default one based on space_per_unslotted_widget. - Slot slot; - if (i >= m_slots.size()) { - slot = Slot { - 0.0, - space_per_unslotted_widget, - SlotType::Pixel - }; - } else { - slot = m_slots[i]; + Slot slot = working_slots[i]; + if (slot.type == SlotType::Auto) { + slot.pixel = space_per_unslotted_widget; } // make sure pixel values are aligned to the pixel grid diff --git a/src/BoxLayout.hpp b/src/BoxLayout.hpp index 296d6d9..37f3abe 100644 --- a/src/BoxLayout.hpp +++ b/src/BoxLayout.hpp @@ -13,7 +13,8 @@ public: enum class SlotType { No, Percent, - Pixel + Pixel, + Auto }; struct Slot { double percent, pixel; diff --git a/src/ColumnLayout.cpp b/src/ColumnLayout.cpp new file mode 100644 index 0000000..f547fed --- /dev/null +++ b/src/ColumnLayout.cpp @@ -0,0 +1,39 @@ +#include "ColumnLayout.hpp" +#include "Point.hpp" +#include "Widget.hpp" + +namespace Raven { + +void VerticalBoxLayout::run() { + if (!m_target) { + return; + } + + 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().height(); + } + + 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); +} + +} + diff --git a/src/ColumnLayout.hpp b/src/ColumnLayout.hpp new file mode 100644 index 0000000..19f87ff --- /dev/null +++ b/src/ColumnLayout.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "Layout.hpp" + +namespace Raven { + +class VerticalBoxLayout : public Layout { +private: + double m_margin { 0.0 }; +public: + VerticalBoxLayout() + : Layout() {} + + VerticalBoxLayout(double margin) + : Layout() + , m_margin(margin) {} + + void run(); + + double margin() { return m_margin; } + void set_margin(double margin) { m_margin = margin; run(); } + + virtual ~VerticalBoxLayout() {} +}; + +} + diff --git a/src/RowLayout.cpp b/src/RowLayout.cpp new file mode 100644 index 0000000..46802c8 --- /dev/null +++ b/src/RowLayout.cpp @@ -0,0 +1,39 @@ +#include "RowLayout.hpp" +#include "Point.hpp" +#include "Widget.hpp" + +namespace Raven { + +void HorizontalBoxLayout::run() { + if (!m_target) { + return; + } + + 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); +} + +} + diff --git a/src/RowLayout.hpp b/src/RowLayout.hpp new file mode 100644 index 0000000..e0685af --- /dev/null +++ b/src/RowLayout.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "Layout.hpp" + +namespace Raven { + +class HorizontalBoxLayout : public Layout { +private: + double m_margin { 0.0 }; +public: + HorizontalBoxLayout() + : Layout() {} + + HorizontalBoxLayout(double margin) + : Layout() + , m_margin(margin) {} + + void run(); + + double margin() { return m_margin; } + void set_margin(double margin) { m_margin = margin; run(); } + + virtual ~HorizontalBoxLayout() {} +}; + +} + diff --git a/src/main.cpp b/src/main.cpp index 17956dc..12749f2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,9 +26,7 @@ int main() { auto top_bar = main_widget->add(); top_bar->set_style(&Raven::accent_widget_style); - auto top_layout = top_bar->set_layout(Raven::Direction::Horizontal); - top_layout->slot_percent(50); - top_layout->slot_percent(100); + top_bar->set_layout(Raven::Direction::Horizontal); auto scroll_container = main_widget->add(); auto container_widget = scroll_container->make_target();