From 69f3e28f2c49664f9ad6f6c581ebb9c70e3838cf Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 15 Apr 2024 19:34:09 +0200 Subject: [PATCH] editor: Use window->add_keyboard_shortcut to handle Ctrl+S --- editor/EditorWidget.cpp | 18 ------------------ editor/EditorWidget.h | 5 +++++ editor/main.cpp | 8 ++++++++ 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/editor/EditorWidget.cpp b/editor/EditorWidget.cpp index 901c3058..52d2392f 100644 --- a/editor/EditorWidget.cpp +++ b/editor/EditorWidget.cpp @@ -98,24 +98,6 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest return ui::EventResult::DidHandle; } - if (request.modifiers & ui::Mod_Ctrl) - { - switch (request.key) - { - case 's': { - auto result = save_file(); - if (result.has_error()) - { - os::eprintln("editor: failed to save file: %s", result.error_string()); - return ui::EventResult::DidNotHandle; - } - os::println("editor: buffer saved to %s successfully", m_path.name().chars()); - return ui::EventResult::DidNotHandle; - } - default: return ui::EventResult::DidNotHandle; - } - } - if (request.code == moon::K_Backspace) { if (m_cursor == 0) return ui::EventResult::DidNotHandle; diff --git a/editor/EditorWidget.h b/editor/EditorWidget.h index 3a543d92..b824fbb6 100644 --- a/editor/EditorWidget.h +++ b/editor/EditorWidget.h @@ -26,6 +26,11 @@ class EditorWidget : public ui::TextInput Result draw(ui::Canvas& canvas) override; + os::Path& path() + { + return m_path; + } + private: SharedPtr m_font; diff --git a/editor/main.cpp b/editor/main.cpp index cca1edcf..e5e0acea 100644 --- a/editor/main.cpp +++ b/editor/main.cpp @@ -9,6 +9,7 @@ #include "EditorWidget.h" #include +#include #include Result luna_main(int argc, char** argv) @@ -33,6 +34,13 @@ Result luna_main(int argc, char** argv) window->set_main_widget(*editor); if (!path.is_empty()) TRY(editor->load_file(path)); + TRY(window->add_keyboard_shortcut({ moon::K_CH26, ui::Mod_Ctrl }, true, [&](ui::Shortcut) { + auto result = editor->save_file(); + if (result.has_error()) os::eprintln("editor: failed to save file: %s", result.error_string()); + else + os::println("editor: buffer saved to %s successfully", editor->path().name().chars()); + })); + window->draw(); return app.run();