/** * @file Window.h * @author apio (cloudapio.eu) * @brief UI windows. * * @copyright Copyright (c) 2023, the Luna authors. * */ #pragma once #include #include #include #include #include #include #include #include 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 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 draw(); Result handle_mouse_leave(); Result handle_mouse_move(ui::Point position); Result handle_mouse_buttons(ui::Point position, int buttons); Result handle_key_event(const ui::KeyEventRequest& request); Result add_keyboard_shortcut(ui::Shortcut shortcut, bool intercept, os::Function&& action); int id() const { return m_id; } void on_close(os::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 m_background {}; Option m_old_mouse_buttons; bool m_decorated { false }; os::Action m_on_close_action; bool m_has_on_close_action { false }; struct ShortcutAction { bool intercept; os::Function action; }; HashMap m_shortcuts; Result draw_titlebar(); }; }