2023-08-15 08:23:37 +00:00
|
|
|
/**
|
|
|
|
* @file Window.h
|
|
|
|
* @author apio (cloudapio.eu)
|
|
|
|
* @brief UI windows.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <luna/OwnedPtr.h>
|
2023-12-27 11:56:40 +00:00
|
|
|
#include <luna/String.h>
|
2023-08-15 08:23:37 +00:00
|
|
|
#include <luna/StringView.h>
|
|
|
|
#include <ui/Canvas.h>
|
2023-08-15 13:10:13 +00:00
|
|
|
#include <ui/Mouse.h>
|
2023-08-15 08:23:37 +00:00
|
|
|
#include <ui/Rect.h>
|
2023-08-15 10:28:47 +00:00
|
|
|
#include <ui/Widget.h>
|
2023-09-27 16:14:32 +00:00
|
|
|
#include <ui/ipc/Server.h>
|
2023-08-15 08:23:37 +00:00
|
|
|
|
|
|
|
namespace ui
|
|
|
|
{
|
2024-02-04 12:16:50 +00:00
|
|
|
enum class WindowType : u8
|
|
|
|
{
|
|
|
|
Normal,
|
|
|
|
NotDecorated,
|
|
|
|
System,
|
|
|
|
};
|
|
|
|
|
2024-04-15 17:33:32 +00:00
|
|
|
struct [[gnu::packed]] Shortcut
|
|
|
|
{
|
|
|
|
moon::KeyCode key;
|
|
|
|
int modifiers;
|
|
|
|
|
|
|
|
bool operator==(const Shortcut& other) const
|
|
|
|
{
|
|
|
|
return key == other.key && modifiers == other.modifiers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-15 08:23:37 +00:00
|
|
|
class Window
|
|
|
|
{
|
|
|
|
public:
|
2023-09-27 16:14:32 +00:00
|
|
|
static Result<Window*> create(Rect rect, WindowType type = WindowType::Normal);
|
2023-08-15 08:23:37 +00:00
|
|
|
|
|
|
|
void set_title(StringView title);
|
|
|
|
|
2023-08-15 10:28:47 +00:00
|
|
|
void set_background(Color color)
|
|
|
|
{
|
|
|
|
m_background = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_main_widget(Widget& widget)
|
|
|
|
{
|
|
|
|
check(!m_main_widget);
|
2023-12-27 11:56:40 +00:00
|
|
|
widget.set_window(this, m_window_canvas.rect(), {});
|
2023-08-15 10:28:47 +00:00
|
|
|
m_main_widget = &widget;
|
|
|
|
}
|
|
|
|
|
2023-08-15 08:23:37 +00:00
|
|
|
Canvas& canvas()
|
|
|
|
{
|
2023-12-27 11:56:40 +00:00
|
|
|
return m_window_canvas;
|
2023-08-15 08:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void update();
|
|
|
|
|
2023-08-15 09:20:17 +00:00
|
|
|
void close();
|
|
|
|
|
2024-12-13 20:53:12 +00:00
|
|
|
void set_layer(Layer layer);
|
2024-02-04 12:35:50 +00:00
|
|
|
|
2023-08-15 10:28:47 +00:00
|
|
|
Result<void> draw();
|
2023-09-11 17:38:29 +00:00
|
|
|
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);
|
2023-09-16 09:45:19 +00:00
|
|
|
Result<ui::EventResult> handle_key_event(const ui::KeyEventRequest& request);
|
2023-08-15 10:28:47 +00:00
|
|
|
|
2024-10-19 19:25:17 +00:00
|
|
|
Result<void> add_keyboard_shortcut(ui::Shortcut shortcut, bool intercept, Function<ui::Shortcut>&& action);
|
2024-04-15 17:33:32 +00:00
|
|
|
|
2023-08-15 09:20:17 +00:00
|
|
|
int id() const
|
|
|
|
{
|
|
|
|
return m_id;
|
|
|
|
}
|
|
|
|
|
2024-10-19 19:25:17 +00:00
|
|
|
void on_close(Action&& action)
|
2024-09-18 19:47:31 +00:00
|
|
|
{
|
|
|
|
m_on_close_action = move(action);
|
|
|
|
m_has_on_close_action = true;
|
|
|
|
}
|
|
|
|
|
2023-08-15 08:23:37 +00:00
|
|
|
~Window();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_id;
|
|
|
|
Canvas m_canvas;
|
2023-12-27 11:56:40 +00:00
|
|
|
Canvas m_titlebar_canvas;
|
|
|
|
Canvas m_window_canvas;
|
|
|
|
String m_name;
|
2023-08-15 10:28:47 +00:00
|
|
|
Widget* m_main_widget { nullptr };
|
2023-09-16 11:29:42 +00:00
|
|
|
Option<Color> m_background {};
|
2023-08-15 13:10:13 +00:00
|
|
|
Option<int> m_old_mouse_buttons;
|
2023-12-27 11:56:40 +00:00
|
|
|
bool m_decorated { false };
|
|
|
|
|
2024-10-19 19:25:17 +00:00
|
|
|
Action m_on_close_action;
|
2024-09-18 19:47:31 +00:00
|
|
|
bool m_has_on_close_action { false };
|
|
|
|
|
2024-04-15 17:33:32 +00:00
|
|
|
struct ShortcutAction
|
|
|
|
{
|
|
|
|
bool intercept;
|
2024-10-19 19:25:17 +00:00
|
|
|
Function<Shortcut> action;
|
2024-04-15 17:33:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
HashMap<Shortcut, ShortcutAction> m_shortcuts;
|
|
|
|
|
2023-12-27 11:56:40 +00:00
|
|
|
Result<void> draw_titlebar();
|
2023-08-15 08:23:37 +00:00
|
|
|
};
|
|
|
|
}
|