Luna/gui/libui/include/ui/Window.h

116 lines
2.6 KiB
C
Raw Normal View History

/**
* @file Window.h
* @author apio (cloudapio.eu)
* @brief UI windows.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#pragma once
#include <luna/OwnedPtr.h>
#include <luna/String.h>
#include <luna/StringView.h>
#include <ui/Canvas.h>
2023-08-15 13:10:13 +00:00
#include <ui/Mouse.h>
#include <ui/Rect.h>
#include <ui/Widget.h>
#include <ui/ipc/Server.h>
namespace ui
{
enum class WindowType : u8
{
Normal,
NotDecorated,
System,
};
struct [[gnu::packed]] Shortcut
{
moon::KeyCode key;
int modifiers;
bool operator==(const Shortcut& other) const
{
return key == other.key && modifiers == other.modifiers;
}
};
class Window
{
public:
static Result<Window*> create(Rect rect, WindowType type = WindowType::Normal);
void set_title(StringView title);
void set_background(Color color)
{
m_background = color;
}
void set_main_widget(Widget& widget)
{
check(!m_main_widget);
widget.set_window(this, m_window_canvas.rect(), {});
m_main_widget = &widget;
}
Canvas& canvas()
{
return m_window_canvas;
}
void update();
void close();
void set_special_attributes(WindowAttributes attributes);
Result<void> draw();
Result<ui::EventResult> handle_mouse_leave();
Result<ui::EventResult> handle_mouse_move(ui::Point position);
Result<ui::EventResult> handle_mouse_buttons(ui::Point position, int buttons);
Result<ui::EventResult> handle_key_event(const ui::KeyEventRequest& request);
Result<void> add_keyboard_shortcut(ui::Shortcut shortcut, bool intercept, Function<ui::Shortcut>&& action);
int id() const
{
return m_id;
}
void on_close(Action&& action)
{
m_on_close_action = move(action);
m_has_on_close_action = true;
}
~Window();
private:
int m_id;
Canvas m_canvas;
Canvas m_titlebar_canvas;
Canvas m_window_canvas;
String m_name;
Widget* m_main_widget { nullptr };
Option<Color> m_background {};
2023-08-15 13:10:13 +00:00
Option<int> m_old_mouse_buttons;
bool m_decorated { false };
Action m_on_close_action;
bool m_has_on_close_action { false };
struct ShortcutAction
{
bool intercept;
Function<Shortcut> action;
};
HashMap<Shortcut, ShortcutAction> m_shortcuts;
Result<void> draw_titlebar();
};
}