summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorMustafa Quraish <mustafaq9@gmail.com>2022-01-07 17:35:31 -0500
committerAndreas Kling <kling@serenityos.org>2022-01-09 00:19:47 +0100
commitc2b3bab984865796dee3c385b50cb601f84f7363 (patch)
tree87759e9e111678b503f8a12f369fabe3d1adfe92 /Userland
parentb3e47f0bd501e5cef590fdde8e3e7b3502aa0a95 (diff)
downloadserenity-c2b3bab984865796dee3c385b50cb601f84f7363.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Applications/PixelPaint/ImageEditor.cpp33
-rw-r--r--Userland/Applications/PixelPaint/ImageEditor.h5
-rw-r--r--Userland/Applications/PixelPaint/MainWidget.cpp33
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 <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;
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<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();
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);