add horizontal box layout

This commit is contained in:
hippoz 2022-06-15 04:44:37 +03:00
parent 93b33c433b
commit 96a7739341
Signed by: hippoz
GPG key ID: 7C52899193467641
2 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,41 @@
#include "HorizontalBoxLayout.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());
//std::cout << child->rect().x() << ", " << child->rect().y() << ", " << child->rect().width() << ", " << child->rect().height() << std::endl;
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);
}
}

View 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() {}
};
}