57 lines
1.6 KiB
C++
57 lines
1.6 KiB
C++
/**
|
|
* @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 <os/Action.h>
|
|
#include <os/File.h>
|
|
#include <ui/Widget.h>
|
|
#include <ui/Window.h>
|
|
|
|
namespace ui
|
|
{
|
|
class Button : public Widget
|
|
{
|
|
public:
|
|
Button(ui::Window* window, ui::Widget* parent);
|
|
|
|
template <typename T> Result<T*> create_child_widget()
|
|
{
|
|
auto* widget = TRY(make<T>(window(), this));
|
|
m_child = widget;
|
|
window()->recalculate_layout();
|
|
return widget;
|
|
}
|
|
|
|
void set_action(os::Action&& action);
|
|
|
|
Result<EventResult> handle_mouse_move(Point position) override;
|
|
Result<EventResult> handle_mouse_leave() override;
|
|
Result<EventResult> handle_mouse_down(Point position, int buttons) override;
|
|
Result<EventResult> handle_mouse_up(Point position, int buttons) override;
|
|
Result<EventResult> handle_key_event(const ui::KeyEventRequest& request) override;
|
|
Result<void> draw(Canvas& canvas) override;
|
|
|
|
void recalculate_true_rects() override;
|
|
void recalculate_pseudo_rects() override;
|
|
|
|
void show_tree(int indent) override
|
|
{
|
|
os::println("%*s- ui::Button (%d,%d,%d,%d)", indent, "", m_rect.pos.x, m_rect.pos.y, m_rect.width,
|
|
m_rect.height);
|
|
if (m_child) m_child->show_tree(indent + 1);
|
|
}
|
|
|
|
private:
|
|
bool m_hovered { false };
|
|
bool m_clicked { false };
|
|
Widget* m_child { nullptr };
|
|
os::Action m_action;
|
|
};
|
|
}
|