From 442256b5f858740fc935988d502a25385e9e2875 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 24 Jul 2019 06:32:30 +0200 Subject: [PATCH] TextEditor: Add "Save as..." action. Add a basic "save as" action to the TextEditor app, and make "save" fall back to using "save as" if there's no name already set. Fixes #282. --- Applications/TextEditor/TextEditorWidget.cpp | 42 ++++++++++++-------- Applications/TextEditor/TextEditorWidget.h | 4 +- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/Applications/TextEditor/TextEditorWidget.cpp b/Applications/TextEditor/TextEditorWidget.cpp index 43feb8a110..a399a80d29 100644 --- a/Applications/TextEditor/TextEditorWidget.cpp +++ b/Applications/TextEditor/TextEditorWidget.cpp @@ -29,28 +29,20 @@ TextEditorWidget::TextEditorWidget() statusbar->set_text(builder.to_string()); }; - auto new_action = GAction::create("New document", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [](const GAction&) { + auto new_action = GAction::create("New", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [](const GAction&) { dbgprintf("FIXME: Implement File/New\n"); }); - auto open_action = GAction::create("Open document", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [this](const GAction&) { + auto open_action = GAction::create("Open...", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), [this](const GAction&) { Optional open_name = GFilePicker::get_open_filepath(); if (!open_name.has_value()) return; - m_path = open_name.value(); open_sesame(m_path); }); - auto save_action = GAction::create("Save document", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) { - if (!m_path.is_empty()) { - if (!m_editor->write_to_file(m_path)) - GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window()); - - return; - } - + auto save_as_action = GAction::create("Save as...", { Mod_None, Key_F12 }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) { Optional save_name = GFilePicker::get_save_filepath(); if (!save_name.has_value()) return; @@ -60,9 +52,18 @@ TextEditorWidget::TextEditorWidget() return; } - m_path = save_name.value(); - window()->set_title(String::format("Text Editor: %s", m_path.characters())); - dbgprintf("Wrote document to '%s'\n", m_path.characters()); + set_path(save_name.value()); + dbg() << "Wrote document to " << save_name.value(); + }); + + auto save_action = GAction::create("Save", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [&](const GAction&) { + if (!m_path.is_empty()) { + if (!m_editor->write_to_file(m_path)) + GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window()); + return; + } + + save_as_action->activate(); }); auto menubar = make(); @@ -77,6 +78,7 @@ TextEditorWidget::TextEditorWidget() file_menu->add_action(new_action); file_menu->add_action(open_action); file_menu->add_action(save_action); + file_menu->add_action(save_as_action); menubar->add_menu(move(file_menu)); auto edit_menu = make("Edit"); @@ -129,6 +131,15 @@ TextEditorWidget::~TextEditorWidget() { } +void TextEditorWidget::set_path(const StringView& path) +{ + m_path = path; + StringBuilder builder; + builder.append("Text Editor: "); + builder.append(path); + window()->set_title(builder.to_string()); +} + void TextEditorWidget::open_sesame(const String& path) { dbgprintf("Our path to file in open_sesame: %s\n", path.characters()); @@ -138,7 +149,6 @@ void TextEditorWidget::open_sesame(const String& path) GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window()); } - window()->set_title(String::format("Text Editor: %s", path.characters())); m_editor->set_text(String::copy(file.read_all())); - m_path = path; + set_path(path); } diff --git a/Applications/TextEditor/TextEditorWidget.h b/Applications/TextEditor/TextEditorWidget.h index 08be955f28..cfeb335ee2 100644 --- a/Applications/TextEditor/TextEditorWidget.h +++ b/Applications/TextEditor/TextEditorWidget.h @@ -15,6 +15,8 @@ public: void open_sesame(const String& path); private: + void set_path(const StringView&); + GTextEditor* m_editor { nullptr }; String m_path; -}; \ No newline at end of file +};