editor: Use TextInput as a base class

This commit is contained in:
apio 2024-03-29 20:17:12 +01:00
parent 677bcb861e
commit cc3be43633
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 13 additions and 44 deletions

View File

@ -14,9 +14,8 @@
#include <os/FileSystem.h>
#include <ui/App.h>
EditorWidget::EditorWidget(SharedPtr<ui::Font> font) : ui::Widget(), m_font(font)
EditorWidget::EditorWidget(SharedPtr<ui::Font> 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<void> 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<ui::EventResult> 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<ui::EventResult> 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<ui::EventResult> 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<ui::EventResult> 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<ui::EventResult> 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<ui::EventResult> 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<void> 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 };

View File

@ -10,9 +10,10 @@
#include <luna/String.h>
#include <os/Timer.h>
#include <ui/Font.h>
#include <ui/TextInput.h>
#include <ui/Widget.h>
class EditorWidget : public ui::Widget
class EditorWidget : public ui::TextInput
{
public:
EditorWidget(SharedPtr<ui::Font> font);
@ -28,8 +29,6 @@ class EditorWidget : public ui::Widget
private:
SharedPtr<ui::Font> m_font;
Buffer m_data;
struct Line
{
usize begin;
@ -37,16 +36,8 @@ class EditorWidget : public ui::Widget
};
Vector<Line> m_lines;
usize m_cursor { 0 };
ui::Point m_cursor_position { 0, 0 };
OwnedPtr<os::Timer> m_cursor_timer;
bool m_cursor_activated = true;
os::Path m_path { AT_FDCWD };
void tick_cursor();
Result<void> recalculate_lines();
void recalculate_cursor_position();
void recalculate_cursor_index();

View File

@ -30,8 +30,8 @@ Result<int> luna_main(int argc, char** argv)
app.set_main_window(window);
auto* editor = TRY(make<EditorWidget>(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();