modify box layout to better support of minimum and maximum sizes of widgets, still super janky
This commit is contained in:
parent
1ceb227165
commit
a869385d09
7 changed files with 181 additions and 22 deletions
|
@ -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<Slot> 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
|
||||
|
|
|
@ -13,7 +13,8 @@ public:
|
|||
enum class SlotType {
|
||||
No,
|
||||
Percent,
|
||||
Pixel
|
||||
Pixel,
|
||||
Auto
|
||||
};
|
||||
struct Slot {
|
||||
double percent, pixel;
|
||||
|
|
39
src/ColumnLayout.cpp
Normal file
39
src/ColumnLayout.cpp
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
27
src/ColumnLayout.hpp
Normal file
27
src/ColumnLayout.hpp
Normal file
|
@ -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() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
39
src/RowLayout.cpp
Normal file
39
src/RowLayout.cpp
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
27
src/RowLayout.hpp
Normal file
27
src/RowLayout.hpp
Normal file
|
@ -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() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -26,9 +26,7 @@ int main() {
|
|||
|
||||
auto top_bar = main_widget->add<Raven::Widget>();
|
||||
top_bar->set_style(&Raven::accent_widget_style);
|
||||
auto top_layout = top_bar->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal);
|
||||
top_layout->slot_percent(50);
|
||||
top_layout->slot_percent(100);
|
||||
top_bar->set_layout<Raven::BoxLayout>(Raven::Direction::Horizontal);
|
||||
|
||||
auto scroll_container = main_widget->add<Raven::ScrollContainer>();
|
||||
auto container_widget = scroll_container->make_target();
|
||||
|
|
Loading…
Reference in a new issue