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 2m4s
All checks were successful
Build and test / build (push) Successful in 2m4s
This commit is contained in:
parent
aae5f21f7f
commit
fc17399d82
@ -46,14 +46,66 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
|
|||||||
// Avoid handling "key released" events
|
// Avoid handling "key released" events
|
||||||
if (!request.pressed) return ui::EventResult::DidNotHandle;
|
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;
|
if (m_cursor_position.y + 1 < (int)m_lines.size()) m_cursor_position.y++;
|
||||||
String title = TRY(String::from_cstring("Text Editor"));
|
else
|
||||||
if (!m_path.is_empty_path()) title = TRY(String::format("Text Editor - %s"_sv, m_path.name().chars()));
|
return ui::EventResult::DidNotHandle;
|
||||||
ui::App::the().main_window()->set_title(title.view());
|
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)
|
if (request.code == moon::K_Backspace)
|
||||||
@ -94,65 +146,6 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
|
|||||||
return ui::EventResult::DidHandle;
|
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()
|
Result<void> EditorWidget::save_file()
|
||||||
{
|
{
|
||||||
if (m_path.is_empty_path())
|
if (m_path.is_empty_path())
|
||||||
|
@ -43,8 +43,6 @@ class EditorWidget : public ui::Widget
|
|||||||
OwnedPtr<os::Timer> m_cursor_timer;
|
OwnedPtr<os::Timer> m_cursor_timer;
|
||||||
bool m_cursor_activated = true;
|
bool m_cursor_activated = true;
|
||||||
|
|
||||||
bool m_insert = false;
|
|
||||||
|
|
||||||
os::Path m_path { AT_FDCWD };
|
os::Path m_path { AT_FDCWD };
|
||||||
|
|
||||||
void tick_cursor();
|
void tick_cursor();
|
||||||
|
Loading…
Reference in New Issue
Block a user