add margin and spacing to boxlayout

This commit is contained in:
hippoz 2022-07-28 18:54:06 +03:00
parent a7051174a9
commit 909f911260
No known key found for this signature in database
GPG key ID: 7C52899193467641
3 changed files with 29 additions and 25 deletions

View file

@ -13,9 +13,9 @@ void BoxLayout::run() {
return; 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; double free_space = total_space;
Point current_position { 0.0, 0.0 }; Point current_position { m_margin, m_margin };
int unslotted_widgets = 0; int unslotted_widgets = 0;
std::vector<Slot> working_slots(m_slots); std::vector<Slot> 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 // if the widget has a minimum or maximum size, we'll make a slot for its preferred size
child->rect().update(); child->rect().update();
working_slots.push_back(Slot { 0, child->rect().dimension_at(m_direction), SlotType::Pixel }); 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 { } else {
// we can consider widgets with no size perference "unslotted". these widgets will be evenly spaced amongst themselves. // we can consider widgets with no size perference "unslotted". these widgets will be evenly spaced amongst themselves.
unslotted_widgets++; 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 the slot is fixed, we can already clamp it and subtract from free_space
if (working_slots[i].type == SlotType::Pixel) { if (working_slots[i].type == SlotType::Pixel) {
working_slots[i].pixel = child->rect().clamp_for_dimension(m_direction, working_slots[i].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) { if (slot->type == SlotType::Percent) {
slot->pixel = ((free_space / 100.0) * slot->percent); slot->pixel = ((free_space / 100.0) * slot->percent);
slot->pixel = child->rect().clamp_for_dimension(m_direction, slot->pixel); 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++) { for (unsigned int i = 0; i < m_target->children().size(); i++) {
auto child = m_target->children()[i]; auto child = m_target->children()[i];
@ -75,12 +75,12 @@ void BoxLayout::run() {
child->rect().set_y(current_position.y()); child->rect().set_y(current_position.y());
if (m_direction == Direction::Horizontal) { if (m_direction == Direction::Horizontal) {
child->rect().set_width(slot.pixel); child->rect().set_width(slot.pixel);
child->rect().set_height(m_target->rect().max_geometry().height()); child->rect().set_height(m_target->rect().max_geometry().height() - 2 * m_margin);
current_position.add(slot.pixel, 0); current_position.add(slot.pixel + m_spacing, 0);
} else { } 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); 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 }); 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);
}
}
} }

View file

@ -29,7 +29,16 @@ public:
void run(); void run();
void slot_percent(double percent); void slot_percent(double percent);
void slot_pixel(double pixel); 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: private:
double m_margin { 0.0 };
double m_spacing { 0.0 };
std::vector<Slot> m_slots; std::vector<Slot> m_slots;
Direction m_direction { Direction::Horizontal }; Direction m_direction { Direction::Horizontal };
}; };

View file

@ -16,19 +16,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
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() { int main() {
Raven::Window window {}; Raven::Window window {};
window.spawn_window(); window.spawn_window();
@ -42,13 +29,15 @@ int main() {
top_bar->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal); top_bar->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal);
auto container_widget = main_widget->add<Raven::Widget>(); auto container_widget = main_widget->add<Raven::Widget>();
container_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Vertical); auto container_layout = container_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Vertical);
container_layout->set_spacing(5);
container_layout->set_margin(18);
container_widget->resize(400, 400); container_widget->resize(400, 400);
auto new_button = top_bar->add<Raven::Button>("add"); auto new_button = top_bar->add<Raven::Button>("add");
new_button->rect().set_max_width(50); new_button->rect().set_max_width(50);
new_button->on_click = [container_widget]() { new_button->on_click = [container_widget]() {
container_widget->add<Fish>(); container_widget->add<Raven::Button>("hello");
}; };
auto remove_button = top_bar->add<Raven::Button>("remove"); auto remove_button = top_bar->add<Raven::Button>("remove");
remove_button->on_click = [container_widget]() { remove_button->on_click = [container_widget]() {