1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-25 22:15:06 +00:00

LibGUI: Start adding an automatic widget layout system.

My needs are really quite simple, so I'm just going to add what I need
as I go along. The first thing I needed was a simple box layout with
widgets being able to say whether they prefer fixed or fill for both
their vertical and horizontal sizes.

I also made a simple GStatusBar so FileManager can show how many bytes
worth of files are in the current directory.
This commit is contained in:
Andreas Kling 2019-02-10 11:07:13 +01:00
parent 2cf1dd5b6f
commit 2def3d8d3f
22 changed files with 411 additions and 29 deletions

View file

@ -2,6 +2,7 @@
#include "GEvent.h"
#include "GEventLoop.h"
#include "GWindow.h"
#include <LibGUI/GLayout.h>
#include <AK/Assertions.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <SharedGraphics/Painter.h>
@ -12,6 +13,9 @@ GWidget::GWidget(GWidget* parent)
set_font(nullptr);
m_background_color = Color::LightGray;
m_foreground_color = Color::Black;
if (parent && parent->layout())
parent->layout()->add_widget(*this);
}
GWidget::~GWidget()
@ -42,7 +46,7 @@ void GWidget::event(GEvent& event)
m_has_pending_paint_event = false;
return handle_paint_event(static_cast<GPaintEvent&>(event));
case GEvent::Resize:
return resize_event(static_cast<GResizeEvent&>(event));
return handle_resize_event(static_cast<GResizeEvent&>(event));
case GEvent::FocusIn:
return focusin_event(event);
case GEvent::FocusOut:
@ -87,6 +91,41 @@ void GWidget::handle_paint_event(GPaintEvent& event)
}
}
void GWidget::set_layout(OwnPtr<GLayout>&& layout)
{
if (m_layout.ptr() == layout.ptr())
return;
if (m_layout)
m_layout->notify_disowned(Badge<GWidget>(), *this);
m_layout = move(layout);
if (m_layout) {
m_layout->notify_adopted(Badge<GWidget>(), *this);
do_layout();
} else {
update();
}
}
void GWidget::do_layout()
{
if (!m_layout)
return;
m_layout->run(*this);
update();
}
void GWidget::notify_layout_changed(Badge<GLayout>)
{
do_layout();
}
void GWidget::handle_resize_event(GResizeEvent& event)
{
if (layout())
do_layout();
return resize_event(event);
}
void GWidget::resize_event(GResizeEvent&)
{
}
@ -216,3 +255,30 @@ bool GWidget::global_cursor_tracking() const
return false;
return win->global_cursor_tracking_widget() == this;
}
void GWidget::set_preferred_size(const Size& size)
{
if (m_preferred_size == size)
return;
m_preferred_size = size;
invalidate_layout();
}
void GWidget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
{
if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
return;
m_horizontal_size_policy = horizontal_policy;
m_vertical_size_policy = vertical_policy;
invalidate_layout();
}
void GWidget::invalidate_layout()
{
auto* w = window();
if (!w)
return;
if (!w->main_widget())
return;
w->main_widget()->do_layout();
}