libui: Add VerticalLayout
This commit is contained in:
parent
4c7828f0eb
commit
f71cb4ee09
@ -42,7 +42,7 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
ColorWidget blue(ui::BLUE);
|
ColorWidget blue(ui::BLUE);
|
||||||
layout.add_widget(blue);
|
layout.add_widget(blue);
|
||||||
|
|
||||||
ui::HorizontalLayout sublayout(ui::AdjustHeight::No, ui::AdjustWidth::Yes);
|
ui::VerticalLayout sublayout;
|
||||||
layout.add_widget(sublayout);
|
layout.add_widget(sublayout);
|
||||||
|
|
||||||
ColorWidget red(ui::RED);
|
ColorWidget red(ui::RED);
|
||||||
|
@ -42,4 +42,22 @@ namespace ui
|
|||||||
AdjustWidth m_adjust_width;
|
AdjustWidth m_adjust_width;
|
||||||
int m_used_width;
|
int m_used_width;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class VerticalLayout final : public Widget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VerticalLayout(AdjustHeight adjust_height = AdjustHeight::Yes, AdjustWidth adjust_width = AdjustWidth::Yes);
|
||||||
|
|
||||||
|
Result<EventResult> handle_mousemove(Point position) override;
|
||||||
|
|
||||||
|
Result<void> draw(Canvas& canvas) override;
|
||||||
|
|
||||||
|
Result<void> add_widget(Widget& widget);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Vector<Widget*> m_widgets;
|
||||||
|
AdjustHeight m_adjust_height;
|
||||||
|
AdjustWidth m_adjust_width;
|
||||||
|
int m_used_height;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -69,4 +69,62 @@ namespace ui
|
|||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VerticalLayout::VerticalLayout(AdjustHeight adjust_height, AdjustWidth adjust_width)
|
||||||
|
: m_adjust_height(adjust_height), m_adjust_width(adjust_width)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<EventResult> VerticalLayout::handle_mousemove(Point position)
|
||||||
|
{
|
||||||
|
for (auto widget : m_widgets)
|
||||||
|
{
|
||||||
|
if (widget->rect().contains(position)) return widget->handle_mousemove(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ui::EventResult::DidNotHandle;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> VerticalLayout::draw(Canvas& canvas)
|
||||||
|
{
|
||||||
|
for (auto widget : m_widgets)
|
||||||
|
{
|
||||||
|
ui::Rect rect = { m_rect.relative(widget->rect().pos), widget->rect().width, widget->rect().height };
|
||||||
|
auto subcanvas = canvas.subcanvas(rect);
|
||||||
|
TRY(widget->draw(subcanvas));
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> VerticalLayout::add_widget(Widget& widget)
|
||||||
|
{
|
||||||
|
TRY(m_widgets.try_append(&widget));
|
||||||
|
|
||||||
|
if (m_adjust_height == AdjustHeight::No)
|
||||||
|
{
|
||||||
|
widget.rect().pos.y = m_rect.pos.y + m_used_height;
|
||||||
|
m_used_height += widget.rect().height;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int used_height = 0;
|
||||||
|
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;
|
||||||
|
used_height += result.quot;
|
||||||
|
}
|
||||||
|
m_widgets[m_widgets.size() - 1]->rect().height += result.rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
widget.rect().pos.x = m_rect.pos.x;
|
||||||
|
|
||||||
|
if (m_adjust_width == AdjustWidth::Yes) { widget.rect().width = m_rect.width; }
|
||||||
|
|
||||||
|
widget.set_parent(this);
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user