LibGUI: Implement very bad widget system :D

This commit is contained in:
2025-06-27 13:49:15 +03:00
parent 4695fa061d
commit d73a667437
15 changed files with 1076 additions and 2 deletions

View File

@@ -0,0 +1,54 @@
#pragma once
#include <BAN/Function.h>
#include <BAN/StringView.h>
#include <LibGUI/Widget/RoundedWidget.h>
namespace LibGUI::Widget
{
class Button : public RoundedWidget
{
public:
struct Style : RoundedWidget::Style
{
Style()
: RoundedWidget::Style()
, color_hovered(0x808080)
, color_text(0x000000)
{}
uint32_t color_hovered;
uint32_t color_text;
};
public:
static BAN::ErrorOr<BAN::RefPtr<Button>> create(BAN::RefPtr<Widget> parent, BAN::StringView text, Rectangle geometry = {});
BAN::ErrorOr<void> set_text(BAN::StringView);
Style& style() { return m_style; }
const Style& style() const { return m_style; }
void set_click_callback(BAN::Function<void()> callback) { m_click_callback = callback; }
protected:
Button(BAN::RefPtr<Widget> parent, Rectangle area)
: RoundedWidget(parent, area)
{ }
void update_impl() override;
void show_impl() override;
bool on_mouse_button_impl(LibGUI::EventPacket::MouseButtonEvent::event_t) override;
private:
Style m_style;
bool m_hover_state { false };
BAN::String m_text;
BAN::Function<void()> m_click_callback;
};
}

View File

@@ -0,0 +1,42 @@
#pragma once
#include <LibGUI/Widget/Widget.h>
namespace LibGUI::Widget
{
class Grid : public Widget
{
public:
static BAN::ErrorOr<BAN::RefPtr<Grid>> create(BAN::RefPtr<Widget> parent, uint32_t cols, uint32_t rows, uint32_t color = color_invisible, Rectangle geometry = {});
BAN::ErrorOr<void> set_widget_position(BAN::RefPtr<Widget> widget, uint32_t col, uint32_t col_span, uint32_t row, uint32_t row_span);
protected:
Grid(BAN::RefPtr<Widget> parent, Rectangle geometry, uint32_t cols, uint32_t rows)
: Widget(parent, geometry)
, m_cols(cols)
, m_rows(rows)
{ }
BAN::ErrorOr<void> update_geometry_impl() override;
private:
struct GridElement
{
BAN::RefPtr<Widget> widget;
uint32_t col;
uint32_t col_span;
uint32_t row;
uint32_t row_span;
};
Rectangle grid_element_area(const GridElement& element) const;
private:
const uint32_t m_cols;
const uint32_t m_rows;
BAN::Vector<GridElement> m_grid_elements;
};
}

View File

@@ -0,0 +1,44 @@
#pragma once
#include <BAN/StringView.h>
#include <LibGUI/Widget/RoundedWidget.h>
namespace LibGUI::Widget
{
class Label : public RoundedWidget
{
public:
struct Style : RoundedWidget::Style
{
Style()
: RoundedWidget::Style()
, color_text(0x000000)
{}
uint32_t color_text;
};
public:
static BAN::ErrorOr<BAN::RefPtr<Label>> create(BAN::RefPtr<Widget> parent, BAN::StringView text, Rectangle geometry = {});
BAN::StringView text() const { return m_text; }
BAN::ErrorOr<void> set_text(BAN::StringView);
Style& style() { return m_style; }
const Style& style() const { return m_style; }
protected:
Label(BAN::RefPtr<Widget> parent, Rectangle area)
: RoundedWidget(parent, area)
{ }
void show_impl() override;
private:
Style m_style;
BAN::String m_text;
};
}

View File

@@ -0,0 +1,42 @@
#pragma once
#include <LibGUI/Widget/Widget.h>
namespace LibGUI::Widget
{
class RoundedWidget : public Widget
{
public:
struct Style
{
Style(uint32_t color_normal = 0xA0A0A0, uint32_t border_width = 1, uint32_t color_border = 0x000000, uint32_t corner_radius = 5)
: color_normal(color_normal)
, border_width(border_width)
, color_border(color_border)
, corner_radius(corner_radius)
{}
uint32_t color_normal;
uint32_t border_width;
uint32_t color_border;
uint32_t corner_radius;
};
Style& style() { return m_style; }
const Style& style() const { return m_style; }
protected:
RoundedWidget(BAN::RefPtr<Widget> parent, Rectangle area)
: Widget(parent, area)
{ }
bool contains(Point point) const override;
void show_impl() override;
private:
Style m_style;
};
}

View File

@@ -0,0 +1,50 @@
#pragma once
#include <BAN/StringView.h>
#include <LibGUI/Widget/RoundedWidget.h>
namespace LibGUI::Widget
{
class TextArea : public RoundedWidget
{
public:
struct Style : RoundedWidget::Style
{
Style()
: RoundedWidget::Style()
, color_text(0x000000)
{}
uint32_t color_text;
};
public:
static BAN::ErrorOr<BAN::RefPtr<TextArea>> create(BAN::RefPtr<Widget> parent, BAN::StringView text, Rectangle geometry = {});
BAN::StringView text() const { return m_text; }
BAN::ErrorOr<void> set_text(BAN::StringView);
uint32_t get_required_height() const;
Style& style() { return m_style; }
const Style& style() const { return m_style; }
protected:
TextArea(BAN::RefPtr<Widget> parent, Rectangle area)
: RoundedWidget(parent, area)
{ }
BAN::ErrorOr<void> wrap_text();
BAN::ErrorOr<void> update_geometry_impl() override;
void show_impl() override;
private:
Style m_style;
BAN::String m_text;
BAN::Vector<BAN::String> m_wrapped_text;
};
}

View File

@@ -0,0 +1,163 @@
#pragma once
#include <BAN/RefPtr.h>
#include <LibGUI/Texture.h>
#include <LibGUI/Packet.h>
namespace LibGUI { class Window; }
namespace LibGUI::Widget
{
class Widget : public BAN::RefCounted<Widget>
{
public:
static constexpr uint32_t color_invisible = Texture::color_invisible;
struct Point
{
int32_t x, y;
};
struct FloatRectangle
{
float x, y;
float w, h;
};
struct Rectangle
{
int32_t x, y;
uint32_t w, h;
struct Bounds
{
int32_t min_x, min_y;
int32_t max_x, max_y;
};
bool contains(Point point) const
{
if (point.x < x || point.x >= x + static_cast<int32_t>(w))
return false;
if (point.y < y || point.y >= y + static_cast<int32_t>(h))
return false;
return true;
}
Bounds bounds(Rectangle other) const
{
return Bounds {
.min_x = BAN::Math::max(x, other.x),
.min_y = BAN::Math::max(y, other.y),
.max_x = BAN::Math::min(x + static_cast<int32_t>(w), other.x + static_cast<int32_t>(other.w)),
.max_y = BAN::Math::min(y + static_cast<int32_t>(h), other.y + static_cast<int32_t>(other.h)),
};
};
Rectangle overlap(Rectangle other) const
{
const auto min_x = BAN::Math::max(x, other.x);
const auto min_y = BAN::Math::max(y, other.y);
const auto max_x = BAN::Math::min(x + static_cast<int32_t>(w), other.x + static_cast<int32_t>(other.w));
const auto max_y = BAN::Math::min(y + static_cast<int32_t>(h), other.y + static_cast<int32_t>(other.h));
if (min_x >= max_x || min_y >= max_y)
return {};
return Rectangle {
.x = min_x,
.y = min_y,
.w = static_cast<uint32_t>(max_x - min_x),
.h = static_cast<uint32_t>(max_y - min_y),
};
}
Rectangle bounding_box(Rectangle other) const
{
if (w == 0 || h == 0)
return other;
if (other.w == 0 || other.h == 0)
return *this;
const auto min_x = BAN::Math::min(x, other.x);
const auto min_y = BAN::Math::min(y, other.y);
const auto max_x = BAN::Math::max(x + static_cast<int32_t>(w), other.x + static_cast<int32_t>(other.w));
const auto max_y = BAN::Math::max(y + static_cast<int32_t>(h), other.y + static_cast<int32_t>(other.h));
return Rectangle {
.x = min_x,
.y = min_y,
.w = static_cast<uint32_t>(max_x - min_x),
.h = static_cast<uint32_t>(max_y - min_y),
};
}
};
public:
static BAN::ErrorOr<BAN::RefPtr<Widget>> create(BAN::RefPtr<Widget> parent, uint32_t color = color_invisible, Rectangle geometry = {});
static BAN::ErrorOr<void> set_default_font(BAN::StringView path);
static const LibFont::Font& default_font();
void show();
void hide();
BAN::ErrorOr<void> set_fixed_geometry(Rectangle);
BAN::ErrorOr<void> set_relative_geometry(FloatRectangle);
BAN::RefPtr<Widget> parent() { return m_parent; }
uint32_t width() const { return m_fixed_area.w; }
uint32_t height() const { return m_fixed_area.h; }
private:
void before_mouse_move();
void after_mouse_move();
bool on_mouse_move(LibGUI::EventPacket::MouseMoveEvent::event_t);
bool on_mouse_button(LibGUI::EventPacket::MouseButtonEvent::event_t);
protected:
Widget(BAN::RefPtr<Widget> parent, Rectangle area)
: m_parent(parent)
, m_fixed_area(area)
{ }
BAN::ErrorOr<void> initialize(uint32_t color);
virtual bool contains(Point point) const { return Rectangle { 0, 0, width(), height() }.contains(point); }
bool is_hovered() const { return m_hovered; }
bool is_child_hovered() const;
bool is_shown() const { return m_shown; }
Rectangle render(Texture& output, Point parent_position, Rectangle out_area);
virtual void update_impl() {}
virtual void show_impl() {}
virtual BAN::ErrorOr<void> update_geometry_impl();
virtual void on_hover_change_impl(bool hovered) { (void)hovered; }
virtual bool on_mouse_move_impl(LibGUI::EventPacket::MouseMoveEvent::event_t) { return true; }
virtual bool on_mouse_button_impl(LibGUI::EventPacket::MouseButtonEvent::event_t) { return true; }
protected:
Texture m_texture;
private:
BAN::RefPtr<Widget> m_parent;
BAN::Vector<BAN::RefPtr<Widget>> m_children;
bool m_shown { false };
Rectangle m_fixed_area;
BAN::Optional<FloatRectangle> m_relative_area;
bool m_changed { false };
bool m_hovered { false };
bool m_old_hovered { false };
friend class LibGUI::Window;
};
}

View File

@@ -6,6 +6,7 @@
#include <LibGUI/Packet.h>
#include <LibGUI/Texture.h>
#include <LibGUI/Widget/Widget.h>
namespace LibFont { class Font; }
@@ -32,6 +33,9 @@ namespace LibGUI
static BAN::ErrorOr<BAN::UniqPtr<Window>> create(uint32_t width, uint32_t height, BAN::StringView title, Attributes attributes = default_attributes);
BAN::ErrorOr<void> set_root_widget(BAN::RefPtr<Widget::Widget> widget);
BAN::RefPtr<Widget::Widget> root_widget() { return m_root_widget; }
Texture& texture() { return m_texture; }
const Texture& texture() const { return m_texture; }
@@ -94,6 +98,7 @@ namespace LibGUI
uint32_t m_height { 0 };
Texture m_texture;
BAN::RefPtr<Widget::Widget> m_root_widget;
BAN::Function<void()> m_socket_error_callback;
BAN::Function<void()> m_close_window_event_callback;