Skip to content

Commit

Permalink
UI: rework container relayout
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Nov 2, 2024
1 parent c14673e commit 5cd4b87
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/uilib/hbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,30 @@ class HBox : public Container {
size.width = _minSize.width;
}
Container::setSize(size);
// TODO: move this to relayout and run relayout instead
for (auto child : _children) {
child->setTop(_padding + child->getMargin().top);
child->setHeight(size.height - child->getTop() - child->getMargin().bottom - _padding);
}
// FIXME: make this depend on vgrow of *all* chrildren
if (!_children.empty() && _children.back()->getHGrow()) {
// FIXME: this code actually gets run for every addChild()
_children.back()->setWidth(size.width - _children.back()->getLeft() - _padding);
}
relayout();
}

void relayout() { // TODO: virtual?
if (!_children.empty()) {
// sum up all hgrow and resize children based on their hgrow
int totalHGrow = 0;
int extraWidth = _size.width - _minSize.width;
for (auto child: _children) totalHGrow += child->getHGrow();
// if only the last child grows, we can skip resizing all children and we could skip the relayout
if (extraWidth != 0 && totalHGrow == _children.back()->getHGrow() && _children.back()->getMaxWidth() != _children.back()->getWidth()) {
auto child = _children.back();
child->setWidth(_size.width - _padding - child->getLeft());
}
else if (extraWidth != 0 && totalHGrow > 0) {
for (auto child: _children)
child->setWidth(child->getMinWidth() + extraWidth * child->getHGrow() / totalHGrow);
}
}
int x=_padding;
for (auto& child : _children) {
x += child->getMargin().left;
child->setTop(child->getMargin().top + _padding);
child->setHeight(_size.height - child->getTop() - child->getMargin().bottom - _padding);
child->setLeft(x);
x += child->getWidth() + child->getMargin().right + _spacing;
}
Expand Down
20 changes: 19 additions & 1 deletion src/uilib/simplecontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,28 @@ class SimpleContainer : public Container {
if (fireMin) onMinSizeChanged.emit(this);
if (fireMax) onMaxSizeChanged.emit(this);
}

bool calculateMinSize()
{
bool res = false;
for (auto child: _children) {
res |= calculateMinSize(child);
}
return res;
}

bool calculateMaxSize()
{
bool res = false;
for (auto child: _children) {
res |= calculateMaxSize(child);
}
return res;
}

Signal<> onMinSizeChanged; // TODO: make split between simplecontainer and container more logical
Signal<> onMaxSizeChanged;

private:
bool calculateMinSize(Widget* child)
{
Expand Down

0 comments on commit 5cd4b87

Please sign in to comment.