add "inherit_secondary_dimension" to listlayout

This commit is contained in:
hippoz 2022-10-28 02:45:39 +03:00
parent a2120328f1
commit fc59ad6e10
Signed by: hippoz
GPG key ID: 7C52899193467641
3 changed files with 30 additions and 4 deletions

View file

@ -22,11 +22,17 @@ bool ListLayout::run() {
child->rect().set_x(current_position);
child->rect().set_y(m_margin);
current_position += child->rect().width() + m_spacing;
if (child->rect().height() > maximum_secondary_dimension) {
if (m_inherit_secondary_dimension) {
child->rect().set_height(m_target->rect().height() - m_margin * 2);
} else if (child->rect().height() > maximum_secondary_dimension) {
maximum_secondary_dimension = child->rect().height();
}
}
if (m_inherit_secondary_dimension) {
maximum_secondary_dimension = m_target->rect().height();
}
m_target->rect().set_width(current_position + m_margin);
m_target->rect().set_height(maximum_secondary_dimension + m_margin * 2);
} else {
@ -38,10 +44,17 @@ bool ListLayout::run() {
child->rect().set_y(current_position);
child->rect().set_x(m_margin);
current_position += child->rect().height() + m_spacing;
if (child->rect().width() > maximum_secondary_dimension) {
if (m_inherit_secondary_dimension) {
child->rect().set_width(m_target->rect().width() - m_margin * 2);
} else if (child->rect().width() > maximum_secondary_dimension) {
maximum_secondary_dimension = child->rect().width();
}
}
if (m_inherit_secondary_dimension) {
maximum_secondary_dimension = m_target->rect().width();
}
m_target->rect().set_width(maximum_secondary_dimension + m_margin * 2);
m_target->rect().set_height(current_position + m_margin);
}

View file

@ -19,9 +19,13 @@ public:
double spacing() { return m_spacing; }
void set_spacing(double spacing) { m_spacing = spacing; run(); }
bool inherit_secondary_dimension() { return m_inherit_secondary_dimension; }
void set_inherit_secondary_dimension(bool inherit_secondary_dimension) { m_inherit_secondary_dimension = inherit_secondary_dimension; }
private:
double m_margin { 0.0 };
double m_spacing { 0.0 };
bool m_inherit_secondary_dimension { false };
Direction m_direction;
};

View file

@ -34,9 +34,18 @@ int main() {
auto test_layout = test_widget->set_layout<Raven::ListLayout>(Raven::Direction::Vertical);
test_layout->set_margin(69);
test_layout->set_spacing(10);
test_layout->set_inherit_secondary_dimension(true);
for (int i = 0; i < 50; i++) {
test_widget->add<Raven::Button>("Hello: " + std::to_string(i));
for (int i = 0; i < 150; i++) {
auto button = test_widget->add<Raven::Button>("Hello: " + std::to_string(i));
button->set_style(&Raven::raised_button_style);
button->on_click = [button]() {
if (button->style() == &Raven::accent_button_style) {
button->set_style(&Raven::raised_button_style);
} else {
button->set_style(&Raven::accent_button_style);
}
};
}
auto main_widget = window->set_main_widget<Raven::Widget>();