diff --git a/libui/include/ui/Window.h b/libui/include/ui/Window.h index b2fea178..83d4f00f 100644 --- a/libui/include/ui/Window.h +++ b/libui/include/ui/Window.h @@ -26,6 +26,17 @@ namespace ui 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: @@ -62,6 +73,8 @@ namespace ui 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; @@ -80,6 +93,14 @@ namespace ui Option m_old_mouse_buttons; bool m_decorated { false }; + struct ShortcutAction + { + bool intercept; + os::Function action; + }; + + HashMap m_shortcuts; + Result draw_titlebar(); }; } diff --git a/libui/src/Window.cpp b/libui/src/Window.cpp index ce99d21b..44f240dd 100644 --- a/libui/src/Window.cpp +++ b/libui/src/Window.cpp @@ -212,7 +212,25 @@ namespace ui Result Window::handle_key_event(const ui::KeyEventRequest& request) { + if (request.pressed) + { + auto* shortcut = m_shortcuts.try_get_ref({ request.code, request.modifiers }); + if (shortcut) + { + shortcut->action({ request.code, request.modifiers }); + if (shortcut->intercept) return ui::EventResult::DidHandle; + } + } + if (!m_main_widget) return ui::EventResult::DidNotHandle; return m_main_widget->handle_key_event(request); } + + Result Window::add_keyboard_shortcut(ui::Shortcut shortcut, bool intercept, + os::Function&& action) + { + TRY(m_shortcuts.try_set(shortcut, { intercept, move(action) })); + + return {}; + } }