From fc17399d824243f29dab0d27b8908405fb2e163a Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 6 Mar 2024 22:41:21 +0100 Subject: [PATCH] editor: Remove insert mode and use the arrow keys to navigate, plus Ctrl+S to save --- editor/EditorWidget.cpp | 127 +++++++++++++++++++--------------------- editor/EditorWidget.h | 2 - 2 files changed, 60 insertions(+), 69 deletions(-) diff --git a/editor/EditorWidget.cpp b/editor/EditorWidget.cpp index 094053db..7b5e619e 100644 --- a/editor/EditorWidget.cpp +++ b/editor/EditorWidget.cpp @@ -46,57 +46,8 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest // Avoid handling "key released" events if (!request.pressed) return ui::EventResult::DidNotHandle; - if (m_insert) + if (request.code == moon::K_UpArrow) { - if (request.code == moon::K_Esc) - { - m_insert = false; - String title = TRY(String::from_cstring("Text Editor")); - if (!m_path.is_empty_path()) title = TRY(String::format("Text Editor - %s"_sv, m_path.name().chars())); - ui::App::the().main_window()->set_title(title.view()); - } - - if (request.code == moon::K_Backspace) - { - if (m_cursor == 0) return ui::EventResult::DidNotHandle; - m_cursor--; - - usize size = m_data.size() - m_cursor; - u8* slice = TRY(m_data.slice(m_cursor, size)); - memmove(slice, slice + 1, size - 1); - TRY(m_data.try_resize(m_data.size() - 1)); - - TRY(recalculate_lines()); - - m_cursor_timer->restart(); - m_cursor_activated = true; - - return ui::EventResult::DidHandle; - } - - if (request.letter != '\n' && iscntrl(request.letter)) return ui::EventResult::DidNotHandle; - - if (m_cursor == m_data.size()) TRY(m_data.append_data((const u8*)&request.letter, 1)); - else - { - usize size = m_data.size() - m_cursor; - u8* slice = TRY(m_data.slice(m_cursor, size + 1)); - memmove(slice + 1, slice, size); - *slice = request.letter; - } - - m_cursor++; - TRY(recalculate_lines()); - - m_cursor_timer->restart(); - m_cursor_activated = true; - - return ui::EventResult::DidHandle; - } - - switch (request.letter) - { - case 'w': { if (m_cursor_position.y > 0) m_cursor_position.y--; else return ui::EventResult::DidNotHandle; @@ -105,7 +56,9 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest m_cursor_activated = true; return ui::EventResult::DidHandle; } - case 's': { + + if (request.code == moon::K_DownArrow) + { if (m_cursor_position.y + 1 < (int)m_lines.size()) m_cursor_position.y++; else return ui::EventResult::DidNotHandle; @@ -114,7 +67,9 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest m_cursor_activated = true; return ui::EventResult::DidHandle; } - case 'a': { + + if (request.code == moon::K_LeftArrow) + { if (m_cursor > 0) m_cursor--; else return ui::EventResult::DidNotHandle; @@ -123,7 +78,9 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest m_cursor_activated = true; return ui::EventResult::DidHandle; } - case 'd': { + + if (request.code == moon::K_RightArrow) + { if (m_cursor < m_data.size()) m_cursor++; else return ui::EventResult::DidNotHandle; @@ -132,25 +89,61 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest m_cursor_activated = true; return ui::EventResult::DidHandle; } - case 'i': { - m_insert = true; - String title = TRY(String::from_cstring("Text Editor (insert)")); - if (!m_path.is_empty_path()) title = TRY(String::format("Text Editor - %s (insert)"_sv, m_path.name().chars())); - ui::App::the().main_window()->set_title(title.view()); - return ui::EventResult::DidHandle; - } - case 'f': { - auto result = save_file(); - if (result.has_error()) + + if (request.modifiers & ui::Mod_Ctrl) + { + switch (request.key) { - os::eprintln("TextEditor: failed to save file: %s", result.error_string()); + case 's': { + auto result = save_file(); + if (result.has_error()) + { + os::eprintln("TextEditor: failed to save file: %s", result.error_string()); + return ui::EventResult::DidNotHandle; + } + os::println("TextEditor: buffer saved to %s successfully", m_path.name().chars()); return ui::EventResult::DidNotHandle; } - os::println("TextEditor: buffer saved to %s successfully", m_path.name().chars()); - return ui::EventResult::DidNotHandle; + default: return ui::EventResult::DidNotHandle; + } } - default: return ui::EventResult::DidNotHandle; + + if (request.code == moon::K_Backspace) + { + if (m_cursor == 0) return ui::EventResult::DidNotHandle; + m_cursor--; + + usize size = m_data.size() - m_cursor; + u8* slice = TRY(m_data.slice(m_cursor, size)); + memmove(slice, slice + 1, size - 1); + TRY(m_data.try_resize(m_data.size() - 1)); + + TRY(recalculate_lines()); + + m_cursor_timer->restart(); + m_cursor_activated = true; + + return ui::EventResult::DidHandle; } + + if (request.letter != '\n' && iscntrl(request.letter)) return ui::EventResult::DidNotHandle; + + if (m_cursor == m_data.size()) TRY(m_data.append_data((const u8*)&request.letter, 1)); + else + { + usize size = m_data.size() - m_cursor; + u8* slice = TRY(m_data.slice(m_cursor, size + 1)); + memmove(slice + 1, slice, size); + *slice = request.letter; + } + + m_cursor++; + TRY(recalculate_lines()); + + m_cursor_timer->restart(); + m_cursor_activated = true; + + return ui::EventResult::DidHandle; } Result EditorWidget::save_file() diff --git a/editor/EditorWidget.h b/editor/EditorWidget.h index 418c269e..024b24b6 100644 --- a/editor/EditorWidget.h +++ b/editor/EditorWidget.h @@ -43,8 +43,6 @@ class EditorWidget : public ui::Widget OwnedPtr m_cursor_timer; bool m_cursor_activated = true; - bool m_insert = false; - os::Path m_path { AT_FDCWD }; void tick_cursor();