Skip to content

Commit

Permalink
HorizontalLayout: weights
Browse files Browse the repository at this point in the history
Horizontal weights can be defined. This is used to align the buttons to the right.

Same code is used for vertical seems the same. However - the code breaks.
  • Loading branch information
diegoiast committed Jan 7, 2024
1 parent a7966c9 commit 94201a0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ auto HorizontalLayout::relayout(Position position, const Size size) -> void {
return;
}
auto recommended_size = Size{};
auto total_weight = 0.0;

// first pass - find all widgets with size hints, and subtract them
// from the list
Expand All @@ -28,11 +29,9 @@ auto HorizontalLayout::relayout(Position position, const Size size) -> void {
width -= hint.width;
}
width -= padding.get_horizontal();
total_weight += item->weight;
}

if (widget_count != 0) {
width = width / widget_count;
}
recommended_size.width = width;
recommended_size.height = size.height - margin.get_vertical();
position.y += margin.top;
Expand All @@ -56,7 +55,8 @@ auto HorizontalLayout::relayout(Position position, const Size size) -> void {
}
auto hint = item->size_hint();
if (hint.width <= 0) {
recommended_size.width = width;
recommended_size.width = (width * item->weight) / total_weight;

} else {
recommended_size.width = hint.width;
}
Expand Down Expand Up @@ -107,6 +107,7 @@ auto VerticalLayout::relayout(Position position, const Size size) -> void {
return;
}
auto recommended_size = Size{};
auto total_weight = 0.0;

// first pass - find all widgets with size hints, and subtract them
// from the list
Expand All @@ -123,10 +124,9 @@ auto VerticalLayout::relayout(Position position, const Size size) -> void {
height -= hint.height;
}
height -= padding.get_vertical();
total_weight += item->weight;
}
if (widget_count != 0) {
height = height / widget_count;
}

recommended_size.height = height;
recommended_size.width = size.width - margin.get_horizontal();
position.y += margin.top;
Expand All @@ -150,7 +150,8 @@ auto VerticalLayout::relayout(Position position, const Size size) -> void {
}
auto hint = item->size_hint();
if (hint.height <= 0) {
recommended_size.height = height;
recommended_size.height = height / widget_count;
// recommended_size.height = (height * item->weight) / total_weight;
} else {
recommended_size.height = hint.height;
}
Expand Down
1 change: 1 addition & 0 deletions src/layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct LayouttItem {
std::list<std::shared_ptr<LayouttItem>> sub_items;
LayoutParams padding = {};
LayoutParams margin = {};
double weight = 1;

virtual auto relayout(Position posiition, const Size size) -> void = 0;
virtual auto size_hint() const -> Size = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ int main() {
l2->margin.set_horitzonal(5);
l2->margin.set_vertical(5);
l2->padding.set_horitzonal(10);
l2->add(std::make_shared<HorizontalSpacer>());
l2->add(std::make_shared<HorizontalSpacer>())->weight = 2;

w1->add_new_to_layout<Button>(l2, Position{10, 420}, Size{200, 40}, "OK", true, [&platform]() {
spdlog::info("OK clicked!");
Expand Down

0 comments on commit 94201a0

Please sign in to comment.