libui: Add Margins to layouts

This commit is contained in:
apio 2023-09-27 18:51:54 +02:00
parent d4e834f734
commit 3d46e56386
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 36 additions and 18 deletions

View File

@ -25,10 +25,19 @@ namespace ui
Yes
};
struct Margins
{
int left;
int right;
int top;
int bottom;
};
class HorizontalLayout final : public Widget
{
public:
HorizontalLayout(AdjustHeight adjust_height = AdjustHeight::Yes, AdjustWidth adjust_width = AdjustWidth::Yes);
HorizontalLayout(Margins margins = Margins { 0, 0, 0, 0 }, AdjustHeight adjust_height = AdjustHeight::Yes,
AdjustWidth adjust_width = AdjustWidth::Yes);
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave() override;
@ -42,6 +51,7 @@ namespace ui
private:
Vector<Widget*> m_widgets;
Margins m_margins;
AdjustHeight m_adjust_height;
AdjustWidth m_adjust_width;
int m_used_width { 0 };
@ -50,7 +60,8 @@ namespace ui
class VerticalLayout final : public Widget
{
public:
VerticalLayout(AdjustHeight adjust_height = AdjustHeight::Yes, AdjustWidth adjust_width = AdjustWidth::Yes);
VerticalLayout(Margins margins = Margins { 0, 0, 0, 0 }, AdjustHeight adjust_height = AdjustHeight::Yes,
AdjustWidth adjust_width = AdjustWidth::Yes);
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave() override;
@ -64,6 +75,7 @@ namespace ui
private:
Vector<Widget*> m_widgets;
Margins m_margins;
AdjustHeight m_adjust_height;
AdjustWidth m_adjust_width;
int m_used_height { 0 };

View File

@ -12,8 +12,8 @@
namespace ui
{
HorizontalLayout::HorizontalLayout(AdjustHeight adjust_height, AdjustWidth adjust_width)
: m_adjust_height(adjust_height), m_adjust_width(adjust_width)
HorizontalLayout::HorizontalLayout(Margins margins, AdjustHeight adjust_height, AdjustWidth adjust_width)
: m_margins(margins), m_adjust_height(adjust_height), m_adjust_width(adjust_width)
{
}
@ -95,8 +95,8 @@ namespace ui
if (m_adjust_width == AdjustWidth::No)
{
widget.rect().pos.x = m_rect.pos.x + m_used_width;
m_used_width += widget.rect().width;
widget.rect().pos.x = m_rect.pos.x + m_used_width + m_margins.left;
m_used_width += m_margins.left + widget.rect().width + m_margins.right;
}
else
{
@ -104,24 +104,27 @@ namespace ui
div_t result = div(m_rect.width, (int)m_widgets.size());
for (auto w : m_widgets)
{
w->rect().pos.x = m_rect.pos.x + used_width;
w->rect().width = result.quot;
w->rect().pos.x = m_rect.pos.x + used_width + m_margins.left;
w->rect().width = result.quot - (m_margins.left + m_margins.right);
used_width += result.quot;
}
m_widgets[m_widgets.size() - 1]->rect().width += result.rem;
}
widget.rect().pos.y = m_rect.pos.y;
widget.rect().pos.y = m_rect.pos.y + m_margins.top;
if (m_adjust_height == AdjustHeight::Yes) { widget.rect().height = m_rect.height; }
if (m_adjust_height == AdjustHeight::Yes)
{
widget.rect().height = m_rect.height - (m_margins.top + m_margins.bottom);
}
widget.set_parent(this);
return {};
}
VerticalLayout::VerticalLayout(AdjustHeight adjust_height, AdjustWidth adjust_width)
: m_adjust_height(adjust_height), m_adjust_width(adjust_width)
VerticalLayout::VerticalLayout(Margins margins, AdjustHeight adjust_height, AdjustWidth adjust_width)
: m_margins(margins), m_adjust_height(adjust_height), m_adjust_width(adjust_width)
{
}
@ -203,8 +206,8 @@ namespace ui
if (m_adjust_height == AdjustHeight::No)
{
widget.rect().pos.y = m_rect.pos.y + m_used_height;
m_used_height += widget.rect().height;
widget.rect().pos.y = m_rect.pos.y + m_used_height + m_margins.top;
m_used_height += m_margins.top + widget.rect().height + m_margins.bottom;
}
else
{
@ -212,16 +215,19 @@ namespace ui
div_t result = div(m_rect.height, (int)m_widgets.size());
for (auto w : m_widgets)
{
w->rect().pos.y = m_rect.pos.y + used_height;
w->rect().height = result.quot;
w->rect().pos.y = m_rect.pos.y + used_height + m_margins.top;
w->rect().height = result.quot - (m_margins.top + m_margins.bottom);
used_height += result.quot;
}
m_widgets[m_widgets.size() - 1]->rect().height += result.rem;
}
widget.rect().pos.x = m_rect.pos.x;
widget.rect().pos.x = m_rect.pos.x + m_margins.left;
if (m_adjust_width == AdjustWidth::Yes) { widget.rect().width = m_rect.width; }
if (m_adjust_width == AdjustWidth::Yes)
{
widget.rect().width = m_rect.width - (m_margins.left + m_margins.right);
}
widget.set_parent(this);