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.
This commit is contained in:
Mustafa Quraish 2022-01-07 17:35:31 -05:00 committed by Andreas Kling
parent b3e47f0bd5
commit c2b3bab984
3 changed files with 41 additions and 30 deletions

View file

@ -14,7 +14,9 @@
#include "Tools/Tool.h"
#include <AK/LexicalPath.h>
#include <LibConfig/Client.h>
#include <LibFileSystemAccessClient/Client.h>
#include <LibGUI/Command.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGfx/DisjointRectSet.h>
#include <LibGfx/Palette.h>
@ -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<void, String> ImageEditor::save_project_to_fd_and_close(int fd) const
{
StringBuilder builder;

View file

@ -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<void, String> save_project_to_fd_and_close(int fd) const;
void save_project_as();
void save_project();
NonnullRefPtrVector<Guide> 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<void, String> save_project_to_fd_and_close(int fd) const;
void clamped_scale_by(float, bool do_relayout);
void relayout();

View file

@ -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);