diff --git a/gui/apps/editor/EditorWidget.cpp b/gui/apps/editor/EditorWidget.cpp index 52d2392f..0276861c 100644 --- a/gui/apps/editor/EditorWidget.cpp +++ b/gui/apps/editor/EditorWidget.cpp @@ -10,10 +10,12 @@ #include "EditorWidget.h" #include #include +#include #include #include #include #include +#include EditorWidget::EditorWidget(SharedPtr font) : ui::TextInput(), m_font(font) { @@ -27,7 +29,8 @@ Result EditorWidget::load_file(const os::Path& path) if (!rc.has_error() && !S_ISREG(st.st_mode)) { - os::eprintln("editor: not loading %s as it is not a regular file", path.name().chars()); + auto message = TRY(RefString::format("%s is not a regular file", path.name().chars())); + ui::Dialog::show_message("Error", message.view()); return {}; } @@ -41,9 +44,9 @@ Result EditorWidget::load_file(const os::Path& path) m_cursor = m_data.size(); - m_path = path; + m_path = TRY(String::from_string_view(path.name())); - auto basename = TRY(PathParser::basename(m_path.name())); + auto basename = TRY(PathParser::basename(m_path.view())); String title = TRY(String::format("Text Editor - %s"_sv, basename.chars())); window()->set_title(title.view()); @@ -126,15 +129,38 @@ Result EditorWidget::handle_key_event(const ui::KeyEventRequest return ui::EventResult::DidHandle; } +Result EditorWidget::save_file_as() +{ + ui::Dialog::show_input_dialog( + "Save file as...", "Please enter the path to save this file to:", [this](StringView path) { + m_path = String::from_string_view(path).release_value(); + auto rc = save_file(); + if (rc.has_error()) + { + os::eprintln("Failed to save file %s: %s", m_path.chars(), rc.error_string()); + ui::Dialog::show_message("Error", "Failed to save file"); + } + else + { + auto basename = PathParser::basename(m_path.view()).release_value(); + + String title = String::format("Text Editor - %s"_sv, basename.chars()).release_value(); + window()->set_title(title.view()); + } + }); + + return {}; +} + Result EditorWidget::save_file() { - if (m_path.is_empty_path()) + if (m_path.is_empty()) { - os::eprintln("editor: no file to save buffer to!"); + TRY(save_file_as()); return err(ENOENT); } - auto file = TRY(os::File::open(m_path, os::File::WriteOnly)); + auto file = TRY(os::File::open_or_create(m_path.view(), os::File::WriteOnly)); return file->write(m_data); } diff --git a/gui/apps/editor/EditorWidget.h b/gui/apps/editor/EditorWidget.h index b824fbb6..d73bfee8 100644 --- a/gui/apps/editor/EditorWidget.h +++ b/gui/apps/editor/EditorWidget.h @@ -21,14 +21,15 @@ class EditorWidget : public ui::TextInput Result load_file(const os::Path& path); Result save_file(); + Result save_file_as(); Result handle_key_event(const ui::KeyEventRequest& request) override; Result draw(ui::Canvas& canvas) override; - os::Path& path() + os::Path path() { - return m_path; + return m_path.view(); } private: @@ -41,7 +42,7 @@ class EditorWidget : public ui::TextInput }; Vector m_lines; - os::Path m_path { AT_FDCWD }; + String m_path; Result recalculate_lines(); void recalculate_cursor_position(); diff --git a/gui/apps/editor/main.cpp b/gui/apps/editor/main.cpp index e5e0acea..9e17a816 100644 --- a/gui/apps/editor/main.cpp +++ b/gui/apps/editor/main.cpp @@ -11,6 +11,7 @@ #include #include #include +#include Result luna_main(int argc, char** argv) { @@ -32,15 +33,20 @@ Result luna_main(int argc, char** argv) auto* editor = TRY(make(ui::Font::default_font())); window->set_main_widget(*editor); - if (!path.is_empty()) TRY(editor->load_file(path)); + if (!path.is_empty()) editor->load_file(path); TRY(window->add_keyboard_shortcut({ moon::K_CH26, ui::Mod_Ctrl }, true, [&](ui::Shortcut) { auto result = editor->save_file(); - if (result.has_error()) os::eprintln("editor: failed to save file: %s", result.error_string()); - else - os::println("editor: buffer saved to %s successfully", editor->path().name().chars()); + if (result.has_error()) + { + os::eprintln("Failed to save file %s: %s", editor->path().name().chars(), result.error_string()); + ui::Dialog::show_message("Error", "Failed to save file"); + } })); + TRY(window->add_keyboard_shortcut({ moon::K_CH26, ui::Mod_Ctrl | ui::Mod_Shift }, true, + [&](ui::Shortcut) { editor->save_file_as(); })); + window->draw(); return app.run();