Luna/editor/EditorWidget.h

56 lines
1.1 KiB
C++

/**
* @file EditorWidget.h
* @author apio (cloudapio.eu)
* @brief Multiline text editing widget.
*
* @copyright Copyright (c) 2024, the Luna authors.
*
*/
#include <luna/String.h>
#include <os/Timer.h>
#include <ui/Font.h>
#include <ui/Widget.h>
class EditorWidget : public ui::Widget
{
public:
EditorWidget(SharedPtr<ui::Font> font);
Result<void> load_file(const os::Path& path);
Result<void> save_file();
Result<ui::EventResult> handle_key_event(const ui::KeyEventRequest& request) override;
Result<void> draw(ui::Canvas& canvas) override;
private:
SharedPtr<ui::Font> m_font;
Buffer m_data;
struct Line
{
usize begin;
usize end;
};
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;
bool m_insert = false;
os::Path m_path { AT_FDCWD };
void tick_cursor();
Result<void> recalculate_lines();
void recalculate_cursor_position();
void recalculate_cursor_index();
};