further modify box layout to better support minimum and maximum sizes

This commit is contained in:
hippoz 2022-07-10 03:40:05 +03:00
parent a869385d09
commit 45e322f947
No known key found for this signature in database
GPG key ID: 7C52899193467641
3 changed files with 36 additions and 36 deletions

View file

@ -13,13 +13,14 @@ void BoxLayout::run() {
return;
}
Box free_space = m_target->rect().max_geometry();
double total_space = m_direction == Direction::Horizontal ? m_target->rect().width() : m_target->rect().height();
double free_space = total_space;
Point current_position { 0.0, 0.0 };
int unslotted_widgets = 0;
std::vector<Slot> working_slots(m_slots);
for (unsigned int i = 0; i < m_target->children().size(); i++) {
if (i >= m_slots.size()) {
if (i >= working_slots.size()) {
auto child = m_target->children()[i];
if (m_direction == Direction::Horizontal) {
if (child->rect().min_width() != -1 || child->rect().max_width() != -1) {
@ -58,41 +59,37 @@ void BoxLayout::run() {
}
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 (slot->type == SlotType::Auto) {
continue;
}
if (slot->type == SlotType::Percent) {
slot->pixel = ((free_space / 100.0) * slot->percent);
}
if (m_direction == Direction::Horizontal) {
if (slot->type == SlotType::Percent) {
slot->pixel = ((free_space.width() / 100.0) * slot->percent);
}
if (child->rect().max_width() != -1 && slot->pixel > child->rect().max_width()) {
slot->pixel = child->rect().max_width();
}
if (child->rect().min_width() != -1 && slot->pixel < child->rect().min_width()) {
slot->pixel = child->rect().min_width();
}
free_space.set_width(free_space.width() - slot->pixel);
} else {
if (slot->type == SlotType::Percent) {
slot->pixel = ((free_space.height() / 100.0) * slot->percent);
}
if (child->rect().max_height() != -1 && slot->pixel > child->rect().max_height()) {
slot->pixel = child->rect().max_height();
}
if (child->rect().min_height() != -1 && slot->pixel < child->rect().min_height()) {
slot->pixel = child->rect().min_height();
}
free_space.set_height(free_space.height() - slot->pixel);
}
}
}
double space_per_unslotted_widget = m_direction == Direction::Horizontal ? free_space.width() / unslotted_widgets : free_space.height() / unslotted_widgets;
free_space -= slot->pixel;
}
double space_per_unslotted_widget = free_space / unslotted_widgets;
for (unsigned int i = 0; i < m_target->children().size(); i++) {
auto child = m_target->children()[i];
@ -109,10 +106,10 @@ 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(free_space.height());
child->rect().set_height(m_target->rect().max_geometry().height());
current_position.add(slot.pixel, 0);
} else {
child->rect().set_width(free_space.width());
child->rect().set_width(m_target->rect().max_geometry().width());
child->rect().set_height(slot.pixel);
current_position.add(0, slot.pixel);
}

View file

@ -175,15 +175,16 @@ void Widget::handle_repaint_rect(RepaintRectEvent &event) {
void Widget::handle_relayout_subtree(RelayoutSubtreeEvent &event) {
on_layout(); // hack
for (auto child : m_children) {
child->dispatch_event(event);
}
if (m_layout) {
m_layout->run();
}
m_window_relative = compute_window_relative();
for (auto child : m_children) {
child->dispatch_event(event);
}
}
void Widget::handle_mouse_move_event(MouseMoveEvent &event) {

View file

@ -45,6 +45,8 @@ int main() {
remove_button->on_click = [container_widget]() {
container_widget->remove_child(container_widget->children()[0]);
};
auto dummy_button = top_bar->add<Raven::Button>("dummy");
dummy_button->rect().set_max_width(60);
window.run(true);
return 0;