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
All checks were successful
Build and test / build (push) Successful in 1m47s
This commit is contained in:
parent
f0ce6c36de
commit
42e1700e7a
@ -46,57 +46,8 @@ 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)
|
|
||||||
{
|
|
||||||
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--;
|
if (m_cursor_position.y > 0) m_cursor_position.y--;
|
||||||
else
|
else
|
||||||
return ui::EventResult::DidNotHandle;
|
return ui::EventResult::DidNotHandle;
|
||||||
@ -105,7 +56,9 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
|
|||||||
m_cursor_activated = true;
|
m_cursor_activated = true;
|
||||||
return ui::EventResult::DidHandle;
|
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++;
|
if (m_cursor_position.y + 1 < (int)m_lines.size()) m_cursor_position.y++;
|
||||||
else
|
else
|
||||||
return ui::EventResult::DidNotHandle;
|
return ui::EventResult::DidNotHandle;
|
||||||
@ -114,7 +67,9 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
|
|||||||
m_cursor_activated = true;
|
m_cursor_activated = true;
|
||||||
return ui::EventResult::DidHandle;
|
return ui::EventResult::DidHandle;
|
||||||
}
|
}
|
||||||
case 'a': {
|
|
||||||
|
if (request.code == moon::K_LeftArrow)
|
||||||
|
{
|
||||||
if (m_cursor > 0) m_cursor--;
|
if (m_cursor > 0) m_cursor--;
|
||||||
else
|
else
|
||||||
return ui::EventResult::DidNotHandle;
|
return ui::EventResult::DidNotHandle;
|
||||||
@ -123,7 +78,9 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
|
|||||||
m_cursor_activated = true;
|
m_cursor_activated = true;
|
||||||
return ui::EventResult::DidHandle;
|
return ui::EventResult::DidHandle;
|
||||||
}
|
}
|
||||||
case 'd': {
|
|
||||||
|
if (request.code == moon::K_RightArrow)
|
||||||
|
{
|
||||||
if (m_cursor < m_data.size()) m_cursor++;
|
if (m_cursor < m_data.size()) m_cursor++;
|
||||||
else
|
else
|
||||||
return ui::EventResult::DidNotHandle;
|
return ui::EventResult::DidNotHandle;
|
||||||
@ -132,25 +89,61 @@ Result<ui::EventResult> EditorWidget::handle_key_event(const ui::KeyEventRequest
|
|||||||
m_cursor_activated = true;
|
m_cursor_activated = true;
|
||||||
return ui::EventResult::DidHandle;
|
return ui::EventResult::DidHandle;
|
||||||
}
|
}
|
||||||
case 'i': {
|
|
||||||
m_insert = true;
|
if (request.modifiers & ui::Mod_Ctrl)
|
||||||
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()));
|
switch (request.key)
|
||||||
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());
|
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;
|
return ui::EventResult::DidNotHandle;
|
||||||
}
|
}
|
||||||
os::println("TextEditor: buffer saved to %s successfully", m_path.name().chars());
|
default: return ui::EventResult::DidNotHandle;
|
||||||
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<void> EditorWidget::save_file()
|
Result<void> EditorWidget::save_file()
|
||||||
|
@ -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