improve box layout unslotted widget justification

This commit is contained in:
hippoz 2022-07-08 15:32:07 +03:00
parent 43bbc4ddff
commit 3ab3e74cd1
Signed by: hippoz
GPG key ID: 7C52899193467641
4 changed files with 50 additions and 34 deletions

View file

@ -1,6 +1,7 @@
#include "BoxLayout.hpp" #include "BoxLayout.hpp"
#include "Box.hpp" #include "Box.hpp"
#include "Widget.hpp" #include "Widget.hpp"
#include <cmath>
#include <vector> #include <vector>
@ -12,19 +13,39 @@ void BoxLayout::run() {
} }
Box free_space = m_target->rect().max_geometry(); Box free_space = m_target->rect().max_geometry();
Box total_space = m_target->rect().max_geometry();
Point current_position { 0.0, 0.0 }; Point current_position { 0.0, 0.0 };
int unslotted_widgets = 0; int unslotted_widgets = 0;
// 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++) { for (unsigned int i = 0; i < m_target->children().size(); i++) {
if (i >= m_slots.size()) { if (i >= m_slots.size()) {
unslotted_widgets++; unslotted_widgets++;
} else {
Slot *slot = &m_slots[i];
if (m_direction == Direction::Horizontal) {
if (slot->type == SlotType::Percent) {
slot->pixel = (free_space.width() / 100.0) * slot->percent;
}
free_space.set_width(free_space.width() - slot->pixel);
} else {
if (slot->type == SlotType::Percent) {
slot->pixel = (free_space.height() / 100.0) * slot->percent;
}
free_space.set_height(free_space.height() - slot->pixel);
}
} }
} }
double space_per_unslotted_widget = m_direction == Direction::Horizontal ? total_space.width() / unslotted_widgets : total_space.height() / unslotted_widgets;
double space_per_unslotted_widget = m_direction == Direction::Horizontal ? free_space.width() / unslotted_widgets : free_space.height() / unslotted_widgets;
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];
// 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; Slot slot;
if (i >= m_slots.size()) { if (i >= m_slots.size()) {
slot = Slot { slot = Slot {
@ -36,36 +57,20 @@ void BoxLayout::run() {
slot = m_slots[i]; slot = m_slots[i];
} }
// resolve slot size // make sure pixel values are aligned to the pixel grid
if (slot.type == SlotType::Percent) { slot.pixel = std::floor(slot.pixel);
if (m_direction == Direction::Horizontal) {
slot.pixel = (free_space.width() / 100.0) * slot.percent;
} else {
slot.pixel = (free_space.height() / 100.0) * slot.percent;
}
}
// justify child geometry, stop looping if free space has been exhausted // position all children according to the slots
child->rect().set_x(current_position.x()); child->rect().set_x(current_position.x());
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(free_space.height()); child->rect().set_height(free_space.height());
current_position.add(slot.pixel, 0); current_position.add(slot.pixel, 0);
free_space.set_width(free_space.width() - slot.pixel);
if (free_space.width() <= 0) {
break;
}
} else { } else {
child->rect().set_width(free_space.width()); child->rect().set_width(free_space.width());
child->rect().set_height(slot.pixel); child->rect().set_height(slot.pixel);
current_position.add(0, slot.pixel); current_position.add(0, slot.pixel);
free_space.set_height(free_space.height() - slot.pixel);
if (free_space.height() <= 0) {
break;
}
} }
} }
}; };

View file

@ -9,7 +9,7 @@ void DocumentLayout::run() {
if (!m_target) if (!m_target)
return; return;
Point bound { m_margin * 2, m_margin * 2 }; Point bound { m_margin, m_margin };
Point current_position { m_margin, m_margin }; Point current_position { m_margin, m_margin };
double largest_height_so_far = -1.0; double largest_height_so_far = -1.0;

View file

@ -224,7 +224,7 @@ void Widget::handle_mouse_button_event(MouseButtonEvent &event) {
bool update_activation_to = event.was_left_button_pressed(); bool update_activation_to = event.was_left_button_pressed();
if (!m_rect.contains_point(event.point())) { if (!m_rect.contains_point(event.point())) {
return; update_activation_to = false;
} }
on_mouse_button(event); on_mouse_button(event);

View file

@ -23,18 +23,29 @@ int main() {
window.spawn_window(); window.spawn_window();
auto main_widget = window.set_main_widget<Raven::Widget>(); auto main_widget = window.set_main_widget<Raven::Widget>();
main_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal); auto main_layout = main_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Vertical);
main_layout->slot_pixel(32); // top bar
auto inner = main_widget->add<Raven::Widget>(); auto top_bar = main_widget->add<Raven::Widget>();
inner->resize(400, 200); top_bar->set_style(&Raven::accent_widget_style);
inner->set_style(&Raven::accent_widget_style); top_bar->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal);
auto inner_layout = inner->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal);
inner_layout->slot_pixel(20);
inner_layout->slot_percent(50);
for (int i = 0; i < 4; i++) { auto scroll_container = main_widget->add<Raven::ScrollContainer>();
auto button = inner->add<Raven::Button>("h"); auto container_widget = scroll_container->make_target();
} container_widget->set_layout<Raven::BoxLayout>(Raven::Direction::Vertical);
container_widget->resize(400, 400);
auto new_button = top_bar->add<Raven::Button>("add");
new_button->on_click = [container_widget]() {
auto button = container_widget->add<Raven::Button>("button");
button->on_click = [button]() {
button->set_style(&Raven::accent_button_style);
};
};
auto remove_button = top_bar->add<Raven::Button>("remove");
remove_button->on_click = [container_widget]() {
container_widget->remove_child(container_widget->children()[0]);
};
window.run(true); window.run(true);
return 0; return 0;