From 315dc8d81b9f9839a222d7802561169def1f861a Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 10 Jul 2020 21:20:39 -0400 Subject: [PATCH] FontEditor: Add a "Save as..." menu item --- Applications/FontEditor/FontEditor.cpp | 18 ++++++++++++------ Applications/FontEditor/FontEditor.h | 4 ++++ Applications/FontEditor/main.cpp | 9 +++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Applications/FontEditor/FontEditor.cpp b/Applications/FontEditor/FontEditor.cpp index b5531ceeeb..a3bbc78286 100644 --- a/Applications/FontEditor/FontEditor.cpp +++ b/Applications/FontEditor/FontEditor.cpp @@ -219,12 +219,7 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr&& edite save_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill); save_button.set_preferred_size(80, 0); save_button.set_text("Save"); - save_button.on_click = [this](auto) { - auto ret_val = m_edited_font->write_to_file(m_path); - if (!ret_val) { - GUI::MessageBox::show("The font file could not be saved.", "Save failed", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window()); - } - }; + save_button.on_click = [this](auto) { save_as(m_path); }; auto& quit_button = bottom_container.add(); quit_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill); @@ -296,3 +291,14 @@ FontEditorWidget::FontEditorWidget(const String& path, RefPtr&& edite FontEditorWidget::~FontEditorWidget() { } + +bool FontEditorWidget::save_as(const String& path) +{ + auto ret_val = m_edited_font->write_to_file(path); + if (!ret_val) { + GUI::MessageBox::show("The font file could not be saved.", "Save failed", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window()); + return false; + } + m_path = path; + return true; +} diff --git a/Applications/FontEditor/FontEditor.h b/Applications/FontEditor/FontEditor.h index f620976e0e..785af863e3 100644 --- a/Applications/FontEditor/FontEditor.h +++ b/Applications/FontEditor/FontEditor.h @@ -40,6 +40,10 @@ public: int preferred_width() { return m_preferred_width; } int preferred_height() { return m_preferred_height; } + bool save_as(const String&); + + const String& path() { return m_path; } + private: FontEditorWidget(const String& path, RefPtr&&); RefPtr m_edited_font; diff --git a/Applications/FontEditor/main.cpp b/Applications/FontEditor/main.cpp index 206baa9409..0f0d2afbc6 100644 --- a/Applications/FontEditor/main.cpp +++ b/Applications/FontEditor/main.cpp @@ -105,6 +105,15 @@ int main(int argc, char** argv) set_edited_font(open_path.value(), move(new_font), window->position()); })); + app_menu.add_action(GUI::Action::create("Save as...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), [&](auto&) { + FontEditorWidget* editor = static_cast(window->main_widget()); + Optional save_path = GUI::FilePicker::get_save_filepath(editor->path(), ".font"); + if (!save_path.has_value()) + return; + + if (editor->save_as(save_path.value())) + window->set_title(String::format("%s - Font Editor", save_path.value().characters())); + })); app_menu.add_separator(); app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit();