From c2b3bab984865796dee3c385b50cb601f84f7363 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Fri, 7 Jan 2022 17:35:31 -0500 Subject: [PATCH] PixelPaint: Move `save` and `save_as` logic inside `ImageEditor` Previously the save logic was hardcoded to only work for the active editor, so closing editors in the background would not properly handle the unsaved changes. This patch brings us closer to be able to fix that problem. --- .../Applications/PixelPaint/ImageEditor.cpp | 33 +++++++++++++++++++ .../Applications/PixelPaint/ImageEditor.h | 5 ++- .../Applications/PixelPaint/MainWidget.cpp | 33 +++---------------- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp index 0468ef861a..8c560b1fab 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.cpp +++ b/Userland/Applications/PixelPaint/ImageEditor.cpp @@ -14,7 +14,9 @@ #include "Tools/Tool.h" #include #include +#include #include +#include #include #include #include @@ -698,6 +700,37 @@ void ImageEditor::image_select_layer(Layer* layer) set_active_layer(layer); } +void ImageEditor::save_project() +{ + if (path().is_empty()) { + save_project_as(); + return; + } + auto response = FileSystemAccessClient::Client::the().request_file(window()->window_id(), path(), Core::OpenMode::Truncate | Core::OpenMode::WriteOnly); + if (response.error != 0) + return; + auto result = save_project_to_fd_and_close(*response.fd); + if (result.is_error()) { + GUI::MessageBox::show_error(window(), String::formatted("Could not save {}: {}", *response.chosen_file, result.error())); + return; + } + undo_stack().set_current_unmodified(); +} + +void ImageEditor::save_project_as() +{ + auto save_result = FileSystemAccessClient::Client::the().save_file(window()->window_id(), "untitled", "pp"); + if (save_result.error != 0) + return; + auto result = save_project_to_fd_and_close(*save_result.fd); + if (result.is_error()) { + GUI::MessageBox::show_error(window(), String::formatted("Could not save {}: {}", *save_result.chosen_file, result.error())); + return; + } + set_path(*save_result.chosen_file); + undo_stack().set_current_unmodified(); +} + Result ImageEditor::save_project_to_fd_and_close(int fd) const { StringBuilder builder; diff --git a/Userland/Applications/PixelPaint/ImageEditor.h b/Userland/Applications/PixelPaint/ImageEditor.h index a660ac1202..fd8cf7ca47 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.h +++ b/Userland/Applications/PixelPaint/ImageEditor.h @@ -109,7 +109,8 @@ public: Gfx::FloatPoint image_position_to_editor_position(Gfx::IntPoint const&) const; Gfx::FloatPoint editor_position_to_image_position(Gfx::IntPoint const&) const; - Result save_project_to_fd_and_close(int fd) const; + void save_project_as(); + void save_project(); NonnullRefPtrVector const& guides() const { return m_guides; } bool guide_visibility() { return m_show_guides; } @@ -149,6 +150,8 @@ private: GUI::MouseEvent event_adjusted_for_layer(GUI::MouseEvent const&, Layer const&) const; GUI::MouseEvent event_with_pan_and_scale_applied(GUI::MouseEvent const&) const; + Result save_project_to_fd_and_close(int fd) const; + void clamped_scale_by(float, bool do_relayout); void relayout(); diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp index 144d0deb20..bd4dfc1f73 100644 --- a/Userland/Applications/PixelPaint/MainWidget.cpp +++ b/Userland/Applications/PixelPaint/MainWidget.cpp @@ -133,38 +133,13 @@ void MainWidget::initialize_menubar(GUI::Window& window) }); m_save_image_as_action = GUI::CommonActions::make_save_as_action([&](auto&) { - auto* editor = current_image_editor(); - if (!editor) - return; - auto save_result = FileSystemAccessClient::Client::the().save_file(window.window_id(), "untitled", "pp"); - if (save_result.error != 0) - return; - auto result = editor->save_project_to_fd_and_close(*save_result.fd); - if (result.is_error()) { - GUI::MessageBox::show_error(&window, String::formatted("Could not save {}: {}", *save_result.chosen_file, result.error())); - return; - } - editor->set_path(*save_result.chosen_file); - editor->undo_stack().set_current_unmodified(); + if (auto* editor = current_image_editor()) + editor->save_project_as(); }); m_save_image_action = GUI::CommonActions::make_save_action([&](auto&) { - auto* editor = current_image_editor(); - if (!editor) - return; - if (editor->path().is_empty()) { - m_save_image_as_action->activate(); - return; - } - auto response = FileSystemAccessClient::Client::the().request_file(window.window_id(), editor->path(), Core::OpenMode::Truncate | Core::OpenMode::WriteOnly); - if (response.error != 0) - return; - auto result = editor->save_project_to_fd_and_close(*response.fd); - if (result.is_error()) { - GUI::MessageBox::show_error(&window, String::formatted("Could not save {}: {}", *response.chosen_file, result.error())); - return; - } - editor->undo_stack().set_current_unmodified(); + if (auto* editor = current_image_editor()) + editor->save_project(); }); file_menu.add_action(*m_new_image_action);