libui: Add Buttons
Some checks failed
continuous-integration/drone/pr Build is failing

This commit is contained in:
apio 2023-08-15 15:33:44 +02:00
parent fda3954964
commit 462c48001f
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 116 additions and 1 deletions

View File

@ -1,4 +1,6 @@
#include <os/Process.h>
#include <ui/App.h>
#include <ui/Button.h>
#include <ui/Container.h>
#include <ui/Image.h>
#include <ui/Layout.h>
@ -19,8 +21,15 @@ Result<int> luna_main(int argc, char** argv)
ui::HorizontalLayout layout(ui::AdjustHeight::Yes, ui::AdjustWidth::No);
window->set_main_widget(layout);
ui::Button button({ 0, 0, 50, 50 });
layout.add_widget(button);
ui::Container container({ 0, 0, 50, 50 }, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center);
layout.add_widget(container);
button.set_widget(container);
button.set_action([] {
StringView args[] = { "/usr/bin/gclient" };
os::Process::spawn("/usr/bin/gclient", Slice<StringView> { args, 1 }, false);
});
auto image = TRY(ui::ImageWidget::load("/usr/share/icons/32x32/start-icon.tga"));
container.set_widget(*image);

View File

@ -15,6 +15,7 @@ set(SOURCES
src/Layout.cpp
src/Alignment.cpp
src/Container.cpp
src/Button.cpp
)
add_library(ui ${SOURCES})

35
libui/include/ui/Button.h Normal file
View File

@ -0,0 +1,35 @@
/**
* @file Button.h
* @author apio (cloudapio.eu)
* @brief A clickable component that triggers an action when pressed.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#pragma once
#include <ui/Widget.h>
namespace ui
{
class Button : public Widget
{
public:
Button(Rect rect);
void set_widget(Widget& widget);
void set_action(void (*action)(void));
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave(Point position) override;
Result<EventResult> handle_mouse_down(Point position, int buttons) override;
Result<EventResult> handle_mouse_up(Point position, int buttons) override;
Result<void> draw(Canvas& canvas) override;
private:
bool m_hovered { false };
bool m_clicked { false };
Widget* m_child;
void (*m_action)(void);
};
}

View File

@ -105,6 +105,7 @@ namespace ui
TRY(m_client->recv_typed(request));
auto* window = find_window(request.window);
window->handle_mouse_move(request.position);
window->handle_mouse_buttons(request.position, request.buttons);
window->draw();
return {};
}

68
libui/src/Button.cpp Normal file
View File

@ -0,0 +1,68 @@
/**
* @file Button.cpp
* @author apio (cloudapio.eu)
* @brief A clickable component that triggers an action when pressed.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#include <ui/Button.h>
#include <ui/Mouse.h>
namespace ui
{
Button::Button(Rect rect)
{
m_rect = rect;
}
void Button::set_widget(Widget& widget)
{
widget.rect() = m_rect;
m_child = &widget;
widget.set_parent(this);
}
void Button::set_action(void (*action)(void))
{
m_action = action;
}
Result<EventResult> Button::handle_mouse_move(Point position)
{
m_hovered = true;
return m_child->handle_mouse_move(position);
}
Result<EventResult> Button::handle_mouse_leave(Point position)
{
m_hovered = m_clicked = false;
return m_child->handle_mouse_leave(position);
}
Result<EventResult> Button::handle_mouse_down(Point position, int buttons)
{
auto result = TRY(m_child->handle_mouse_down(position, buttons));
if (result == EventResult::DidNotHandle)
{
if (!m_clicked && (buttons == ui::MouseButtons::LEFT))
{
m_clicked = true;
m_action();
}
}
return EventResult::DidHandle;
}
Result<EventResult> Button::handle_mouse_up(Point position, int buttons)
{
if (buttons & ui::MouseButtons::LEFT) m_clicked = false;
return m_child->handle_mouse_up(position, buttons);
}
Result<void> Button::draw(Canvas& canvas)
{
return m_child->draw(canvas);
}
}

View File

@ -21,6 +21,7 @@ namespace ui
{
m_widget = &widget;
widget.rect() = ui::align(m_rect, widget.rect(), m_valign, m_halign);
widget.set_parent(this);
}
Result<EventResult> Container::handle_mouse_move(Point position)