diff --git a/editor/EditorWidget.cpp b/editor/EditorWidget.cpp index 3bba0713..ad1b8ebc 100644 --- a/editor/EditorWidget.cpp +++ b/editor/EditorWidget.cpp @@ -14,9 +14,8 @@ #include #include -EditorWidget::EditorWidget(SharedPtr font) : ui::Widget(), m_font(font) +EditorWidget::EditorWidget(SharedPtr font) : ui::TextInput(), m_font(font) { - m_cursor_timer = os::Timer::create_repeating(500, [this]() { this->tick_cursor(); }).release_value(); recalculate_lines(); } @@ -44,7 +43,7 @@ Result EditorWidget::load_file(const os::Path& path) m_path = path; String title = TRY(String::format("Text Editor - %s"_sv, m_path.name().chars())); - ui::App::the().main_window()->set_title(title.view()); + window()->set_title(title.view()); TRY(recalculate_lines()); @@ -62,8 +61,7 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest else return ui::EventResult::DidNotHandle; recalculate_cursor_index(); - m_cursor_timer->restart(); - m_cursor_activated = true; + update_cursor(); return ui::EventResult::DidHandle; } @@ -73,8 +71,7 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest else return ui::EventResult::DidNotHandle; recalculate_cursor_index(); - m_cursor_timer->restart(); - m_cursor_activated = true; + update_cursor(); return ui::EventResult::DidHandle; } @@ -84,8 +81,7 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest else return ui::EventResult::DidNotHandle; recalculate_cursor_position(); - m_cursor_timer->restart(); - m_cursor_activated = true; + update_cursor(); return ui::EventResult::DidHandle; } @@ -95,8 +91,7 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest else return ui::EventResult::DidNotHandle; recalculate_cursor_position(); - m_cursor_timer->restart(); - m_cursor_activated = true; + update_cursor(); return ui::EventResult::DidHandle; } @@ -123,15 +118,11 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest 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)); + delete_current_character(); TRY(recalculate_lines()); - m_cursor_timer->restart(); - m_cursor_activated = true; + update_cursor(); return ui::EventResult::DidHandle; } @@ -140,18 +131,12 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest 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; - } + TRY(insert_character(request.letter)); m_cursor++; TRY(recalculate_lines()); - m_cursor_timer->restart(); - m_cursor_activated = true; + update_cursor(); return ui::EventResult::DidHandle; } @@ -234,13 +219,6 @@ Result EditorWidget::recalculate_lines() return {}; } -void EditorWidget::tick_cursor() -{ - m_cursor_activated = !m_cursor_activated; - - ui::App::the().main_window()->draw(); -} - void EditorWidget::recalculate_cursor_position() { if (m_cursor == 0) m_cursor_position = { 0, 0 }; diff --git a/editor/EditorWidget.h b/editor/EditorWidget.h index 024b24b6..3a543d92 100644 --- a/editor/EditorWidget.h +++ b/editor/EditorWidget.h @@ -10,9 +10,10 @@ #include #include #include +#include #include -class EditorWidget : public ui::Widget +class EditorWidget : public ui::TextInput { public: EditorWidget(SharedPtr font); @@ -28,8 +29,6 @@ class EditorWidget : public ui::Widget private: SharedPtr m_font; - Buffer m_data; - struct Line { usize begin; @@ -37,16 +36,8 @@ class EditorWidget : public ui::Widget }; Vector m_lines; - usize m_cursor { 0 }; - ui::Point m_cursor_position { 0, 0 }; - - OwnedPtr m_cursor_timer; - bool m_cursor_activated = true; - os::Path m_path { AT_FDCWD }; - void tick_cursor(); - Result recalculate_lines(); void recalculate_cursor_position(); void recalculate_cursor_index(); diff --git a/editor/main.cpp b/editor/main.cpp index 57d0012e..cca1edcf 100644 --- a/editor/main.cpp +++ b/editor/main.cpp @@ -30,8 +30,8 @@ Result luna_main(int argc, char** argv) app.set_main_window(window); auto* editor = TRY(make(ui::Font::default_font())); - if (!path.is_empty()) TRY(editor->load_file(path)); window->set_main_widget(*editor); + if (!path.is_empty()) TRY(editor->load_file(path)); window->draw();