editor: Remove insert mode and use the arrow keys to navigate, plus Ctrl+S to save
All checks were successful
Build and test / build (push) Successful in 1m47s

This commit is contained in:
apio 2024-03-06 22:41:21 +01:00
parent f0ce6c36de
commit 42e1700e7a
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 60 additions and 69 deletions

View File

@ -46,14 +46,66 @@ Result<ui::EventResult> 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)
if (m_cursor_position.y > 0) m_cursor_position.y--;
else
return ui::EventResult::DidNotHandle;
recalculate_cursor_index();
m_cursor_timer->restart();
m_cursor_activated = true;
return ui::EventResult::DidHandle;
}
if (request.code == moon::K_DownArrow)
{
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 (m_cursor_position.y + 1 < (int)m_lines.size()) m_cursor_position.y++;
else
return ui::EventResult::DidNotHandle;
recalculate_cursor_index();
m_cursor_timer->restart();
m_cursor_activated = true;
return ui::EventResult::DidHandle;
}
if (request.code == moon::K_LeftArrow)
{
if (m_cursor > 0) m_cursor--;
else
return ui::EventResult::DidNotHandle;
recalculate_cursor_position();
m_cursor_timer->restart();
m_cursor_activated = true;
return ui::EventResult::DidHandle;
}
if (request.code == moon::K_RightArrow)
{
if (m_cursor < m_data.size()) m_cursor++;
else
return ui::EventResult::DidNotHandle;
recalculate_cursor_position();
m_cursor_timer->restart();
m_cursor_activated = true;
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("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;
}
default: return ui::EventResult::DidNotHandle;
}
}
if (request.code == moon::K_Backspace)
@ -92,65 +144,6 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
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;
recalculate_cursor_index();
m_cursor_timer->restart();
m_cursor_activated = true;
return ui::EventResult::DidHandle;
}
case 's': {
if (m_cursor_position.y + 1 < (int)m_lines.size()) m_cursor_position.y++;
else
return ui::EventResult::DidNotHandle;
recalculate_cursor_index();
m_cursor_timer->restart();
m_cursor_activated = true;
return ui::EventResult::DidHandle;
}
case 'a': {
if (m_cursor > 0) m_cursor--;
else
return ui::EventResult::DidNotHandle;
recalculate_cursor_position();
m_cursor_timer->restart();
m_cursor_activated = true;
return ui::EventResult::DidHandle;
}
case 'd': {
if (m_cursor < m_data.size()) m_cursor++;
else
return ui::EventResult::DidNotHandle;
recalculate_cursor_position();
m_cursor_timer->restart();
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())
{
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;
}
default: return ui::EventResult::DidNotHandle;
}
}
Result<void> EditorWidget::save_file()

View File

@ -43,8 +43,6 @@ class EditorWidget : public ui::Widget
OwnedPtr<os::Timer> m_cursor_timer;
bool m_cursor_activated = true;
bool m_insert = false;
os::Path m_path { AT_FDCWD };
void tick_cursor();