From 909f911260950337ef6c672675981d21fe8999cb Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Thu, 28 Jul 2022 18:54:06 +0300 Subject: [PATCH] add margin and spacing to boxlayout --- src/BoxLayout.cpp | 26 ++++++++++++++++---------- src/BoxLayout.hpp | 9 +++++++++ src/main.cpp | 19 ++++--------------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/BoxLayout.cpp b/src/BoxLayout.cpp index edca432..0d9cd20 100644 --- a/src/BoxLayout.cpp +++ b/src/BoxLayout.cpp @@ -13,9 +13,9 @@ void BoxLayout::run() { return; } - double total_space = m_target->rect().dimension_at(m_direction); + double total_space = m_target->rect().dimension_at(m_direction) - 2 * m_margin; double free_space = total_space; - Point current_position { 0.0, 0.0 }; + Point current_position { m_margin, m_margin }; int unslotted_widgets = 0; std::vector working_slots(m_slots); @@ -28,7 +28,7 @@ void BoxLayout::run() { // if the widget has a minimum or maximum size, we'll make a slot for its preferred size child->rect().update(); working_slots.push_back(Slot { 0, child->rect().dimension_at(m_direction), SlotType::Pixel }); - free_space -= child->rect().dimension_at(m_direction); + free_space -= child->rect().dimension_at(m_direction) + m_spacing; } else { // we can consider widgets with no size perference "unslotted". these widgets will be evenly spaced amongst themselves. unslotted_widgets++; @@ -40,7 +40,7 @@ void BoxLayout::run() { // if the slot is fixed, we can already clamp it and subtract from free_space if (working_slots[i].type == SlotType::Pixel) { working_slots[i].pixel = child->rect().clamp_for_dimension(m_direction, working_slots[i].pixel); - free_space -= working_slots[i].pixel; + free_space -= working_slots[i].pixel + m_spacing; } } } @@ -53,11 +53,11 @@ void BoxLayout::run() { if (slot->type == SlotType::Percent) { slot->pixel = ((free_space / 100.0) * slot->percent); slot->pixel = child->rect().clamp_for_dimension(m_direction, slot->pixel); - free_space -= slot->pixel; + free_space -= slot->pixel + m_spacing; } } - double space_per_unslotted_widget = free_space / unslotted_widgets; + double space_per_unslotted_widget = free_space / unslotted_widgets - m_spacing; for (unsigned int i = 0; i < m_target->children().size(); i++) { auto child = m_target->children()[i]; @@ -75,12 +75,12 @@ void BoxLayout::run() { child->rect().set_y(current_position.y()); if (m_direction == Direction::Horizontal) { child->rect().set_width(slot.pixel); - child->rect().set_height(m_target->rect().max_geometry().height()); - current_position.add(slot.pixel, 0); + child->rect().set_height(m_target->rect().max_geometry().height() - 2 * m_margin); + current_position.add(slot.pixel + m_spacing, 0); } else { - child->rect().set_width(m_target->rect().max_geometry().width()); + child->rect().set_width(m_target->rect().max_geometry().width() - 2 * m_margin); child->rect().set_height(slot.pixel); - current_position.add(0, slot.pixel); + current_position.add(0, slot.pixel + m_spacing); } } }; @@ -93,5 +93,11 @@ void BoxLayout::slot_pixel(double pixel) { m_slots.push_back(Slot { 0, pixel, SlotType::Pixel }); } +void BoxLayout::slot_pixel(double pixel, double times) { + for (int i = 0; i < times; i++) { + slot_pixel(pixel); + } +} + } diff --git a/src/BoxLayout.hpp b/src/BoxLayout.hpp index cd923c4..0a60146 100644 --- a/src/BoxLayout.hpp +++ b/src/BoxLayout.hpp @@ -29,7 +29,16 @@ public: void run(); void slot_percent(double percent); void slot_pixel(double pixel); + void slot_pixel(double pixel, double times); + + void set_margin(double margin) { m_margin = margin; } + double &margin() { return m_margin; } + + void set_spacing(double spacing) { m_spacing = spacing; } + double &spacing() { return m_spacing; } private: + double m_margin { 0.0 }; + double m_spacing { 0.0 }; std::vector m_slots; Direction m_direction { Direction::Horizontal }; }; diff --git a/src/main.cpp b/src/main.cpp index c0b28b9..273e3de 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,19 +16,6 @@ #include #include -class Fish : public Raven::Widget { -public: - Fish() - : Raven::Widget() {} -protected: - void on_paint() { - if (!painter()) return; - - //painter()->png("tuna.png"); - painter()->cairo()->paint(); - } -}; - int main() { Raven::Window window {}; window.spawn_window(); @@ -42,13 +29,15 @@ int main() { top_bar->set_layout(Raven::Direction::Horizontal); auto container_widget = main_widget->add(); - container_widget->set_layout(Raven::Direction::Vertical); + auto container_layout = container_widget->set_layout(Raven::Direction::Vertical); + container_layout->set_spacing(5); + container_layout->set_margin(18); container_widget->resize(400, 400); auto new_button = top_bar->add("add"); new_button->rect().set_max_width(50); new_button->on_click = [container_widget]() { - container_widget->add(); + container_widget->add("hello"); }; auto remove_button = top_bar->add("remove"); remove_button->on_click = [container_widget]() {