1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:17:44 +00:00

PixelPaint: Use FileSystemAccessClient::try_* APIs

This commit is contained in:
Mustafa Quraish 2022-01-16 04:30:06 -05:00 committed by Andreas Kling
parent aae96af812
commit 1c3e93c6e0
11 changed files with 59 additions and 105 deletions

View file

@ -173,38 +173,28 @@ RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
}
ErrorOr<void> Image::export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel)
ErrorOr<void> Image::export_bmp_to_file(Core::File& file, bool preserve_alpha_channel)
{
auto file = Core::File::construct();
file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
if (file->has_error())
return Error::from_errno(file->error());
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
Gfx::BMPWriter dumper;
auto encoded_data = dumper.dump(bitmap);
if (!file->write(encoded_data.data(), encoded_data.size()))
return Error::from_errno(file->error());
if (!file.write(encoded_data.data(), encoded_data.size()))
return Error::from_errno(file.error());
return {};
}
ErrorOr<void> Image::export_png_to_fd_and_close(int fd, bool preserve_alpha_channel)
ErrorOr<void> Image::export_png_to_file(Core::File& file, bool preserve_alpha_channel)
{
auto file = Core::File::construct();
file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
if (file->has_error())
return Error::from_errno(file->error());
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
auto encoded_data = Gfx::PNGWriter::encode(*bitmap);
if (!file->write(encoded_data.data(), encoded_data.size()))
return Error::from_errno(file->error());
if (!file.write(encoded_data.data(), encoded_data.size()))
return Error::from_errno(file.error());
return {};
}

View file

@ -70,8 +70,8 @@ public:
void serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const;
ErrorOr<void> write_to_file(String const& file_path) const;
ErrorOr<void> export_bmp_to_fd_and_close(int fd, bool preserve_alpha_channel);
ErrorOr<void> export_png_to_fd_and_close(int fd, bool preserve_alpha_channel);
ErrorOr<void> export_bmp_to_file(Core::File&, bool preserve_alpha_channel);
ErrorOr<void> export_png_to_file(Core::File&, bool preserve_alpha_channel);
void move_layer_to_front(Layer&);
void move_layer_to_back(Layer&);

View file

@ -591,12 +591,12 @@ void ImageEditor::save_project()
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)
auto response = FileSystemAccessClient::Client::the().try_request_file(window(), path(), Core::OpenMode::Truncate | Core::OpenMode::WriteOnly);
if (response.is_error())
return;
auto result = save_project_to_fd_and_close(*response.fd);
auto result = save_project_to_file(*response.value());
if (result.is_error()) {
GUI::MessageBox::show_error(window(), String::formatted("Could not save {}: {}", *response.chosen_file, result.error()));
GUI::MessageBox::show_error(window(), String::formatted("Could not save {}: {}", path(), result.error()));
return;
}
undo_stack().set_current_unmodified();
@ -604,19 +604,20 @@ void ImageEditor::save_project()
void ImageEditor::save_project_as()
{
auto save_result = FileSystemAccessClient::Client::the().save_file(window()->window_id(), "untitled", "pp");
if (save_result.error != 0)
auto response = FileSystemAccessClient::Client::the().try_save_file(window(), "untitled", "pp");
if (response.is_error())
return;
auto result = save_project_to_fd_and_close(*save_result.fd);
auto file = response.value();
auto result = save_project_to_file(*file);
if (result.is_error()) {
GUI::MessageBox::show_error(window(), String::formatted("Could not save {}: {}", *save_result.chosen_file, result.error()));
GUI::MessageBox::show_error(window(), String::formatted("Could not save {}: {}", file->filename(), result.error()));
return;
}
set_path(*save_result.chosen_file);
set_path(file->filename());
undo_stack().set_current_unmodified();
}
Result<void, String> ImageEditor::save_project_to_fd_and_close(int fd) const
Result<void, String> ImageEditor::save_project_to_file(Core::File& file) const
{
StringBuilder builder;
JsonObjectSerializer json(builder);
@ -634,13 +635,8 @@ Result<void, String> ImageEditor::save_project_to_fd_and_close(int fd) const
json_guides.finish();
json.finish();
auto file = Core::File::construct();
file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
if (file->has_error())
return String { file->error_string() };
if (!file->write(builder.string_view()))
return String { file->error_string() };
if (!file.write(builder.string_view()))
return String { file.error_string() };
return {};
}

View file

@ -135,7 +135,7 @@ 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;
Result<void, String> save_project_to_file(Core::File&) const;
int calculate_ruler_step_size() const;
Gfx::IntRect mouse_indicator_rect_x() const;

View file

@ -131,11 +131,10 @@ void MainWidget::initialize_menubar(GUI::Window& window)
});
m_open_image_action = GUI::CommonActions::make_open_action([&](auto&) {
auto result = FileSystemAccessClient::Client::the().open_file(window.window_id());
if (result.error != 0)
auto response = FileSystemAccessClient::Client::the().try_open_file(&window);
if (response.is_error())
return;
open_image_fd(*result.fd, *result.chosen_file);
open_image(response.value());
});
m_save_image_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
@ -163,11 +162,11 @@ void MainWidget::initialize_menubar(GUI::Window& window)
"As &BMP", [&](auto&) {
auto* editor = current_image_editor();
VERIFY(editor);
auto save_result = FileSystemAccessClient::Client::the().save_file(window.window_id(), "untitled", "bmp");
if (save_result.error != 0)
auto response = FileSystemAccessClient::Client::the().try_save_file(&window, "untitled", "bmp");
if (response.is_error())
return;
auto preserve_alpha_channel = GUI::MessageBox::show(&window, "Do you wish to preserve transparency?", "Preserve transparency?", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
auto result = editor->image().export_bmp_to_fd_and_close(*save_result.fd, preserve_alpha_channel == GUI::MessageBox::ExecYes);
auto result = editor->image().export_bmp_to_file(response.value(), preserve_alpha_channel == GUI::MessageBox::ExecYes);
if (result.is_error())
GUI::MessageBox::show_error(&window, String::formatted("Export to BMP failed: {}", result.error()));
}));
@ -177,11 +176,12 @@ void MainWidget::initialize_menubar(GUI::Window& window)
"As &PNG", [&](auto&) {
auto* editor = current_image_editor();
VERIFY(editor);
auto save_result = FileSystemAccessClient::Client::the().save_file(window.window_id(), "untitled", "png");
if (save_result.error != 0)
// TODO: fix bmp on line below?
auto response = FileSystemAccessClient::Client::the().try_save_file(&window, "untitled", "png");
if (response.is_error())
return;
auto preserve_alpha_channel = GUI::MessageBox::show(&window, "Do you wish to preserve transparency?", "Preserve transparency?", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
auto result = editor->image().export_png_to_fd_and_close(*save_result.fd, preserve_alpha_channel == GUI::MessageBox::ExecYes);
auto result = editor->image().export_png_to_file(response.value(), preserve_alpha_channel == GUI::MessageBox::ExecYes);
if (result.is_error())
GUI::MessageBox::show_error(&window, String::formatted("Export to PNG failed: {}", result.error()));
}));
@ -306,11 +306,11 @@ void MainWidget::initialize_menubar(GUI::Window& window)
}));
m_edit_menu->add_action(GUI::Action::create(
"&Load Color Palette", [&](auto&) {
auto open_result = FileSystemAccessClient::Client::the().open_file(window.window_id(), "Load Color Palette");
if (open_result.error != 0)
auto response = FileSystemAccessClient::Client::the().try_open_file(&window, "Load Color Palette");
if (response.is_error())
return;
auto result = PixelPaint::PaletteWidget::load_palette_fd_and_close(*open_result.fd);
auto result = PixelPaint::PaletteWidget::load_palette_file(*response.value());
if (result.is_error()) {
GUI::MessageBox::show_error(&window, String::formatted("Loading color palette failed: {}", result.error()));
return;
@ -320,11 +320,11 @@ void MainWidget::initialize_menubar(GUI::Window& window)
}));
m_edit_menu->add_action(GUI::Action::create(
"Sa&ve Color Palette", [&](auto&) {
auto save_result = FileSystemAccessClient::Client::the().save_file(window.window_id(), "untitled", "palette");
if (save_result.error != 0)
auto response = FileSystemAccessClient::Client::the().try_save_file(&window, "untitled", "palette");
if (response.is_error())
return;
auto result = PixelPaint::PaletteWidget::save_palette_fd_and_close(m_palette_widget->colors(), *save_result.fd);
auto result = PixelPaint::PaletteWidget::save_palette_file(m_palette_widget->colors(), *response.value());
if (result.is_error())
GUI::MessageBox::show_error(&window, String::formatted("Writing color palette failed: {}", result.error()));
}));
@ -701,18 +701,18 @@ void MainWidget::set_actions_enabled(bool enabled)
m_zoom_combobox->set_enabled(enabled);
}
void MainWidget::open_image_fd(int fd, String const& path)
void MainWidget::open_image(Core::File& file)
{
auto try_load = m_loader.try_load_from_fd_and_close(fd, path);
auto try_load = m_loader.try_load_from_file(file);
if (try_load.is_error()) {
GUI::MessageBox::show_error(window(), String::formatted("Unable to open file: {}, {}", path, try_load.error()));
GUI::MessageBox::show_error(window(), String::formatted("Unable to open file: {}, {}", file.filename(), try_load.error()));
return;
}
auto& image = *m_loader.release_image();
auto& editor = create_new_editor(image);
editor.set_path(path);
editor.set_path(file.filename());
editor.undo_stack().set_current_unmodified();
m_layer_list_widget->set_image(&image);
}
@ -863,11 +863,10 @@ void MainWidget::drop_event(GUI::DropEvent& event)
if (url.protocol() != "file")
continue;
auto result = FileSystemAccessClient::Client::the().request_file(window()->window_id(), url.path(), Core::OpenMode::ReadOnly);
if (result.error != 0)
continue;
open_image_fd(*result.fd, *result.chosen_file);
auto response = FileSystemAccessClient::Client::the().try_request_file(window(), url.path(), Core::OpenMode::ReadOnly);
if (response.is_error())
return;
open_image(response.value());
}
}
}

View file

@ -35,7 +35,7 @@ public:
void initialize_menubar(GUI::Window&);
void open_image_fd(int fd, String const& path);
void open_image(Core::File&);
void create_default_image();
bool request_close();

View file

@ -255,16 +255,6 @@ Result<Vector<Color>, String> PaletteWidget::load_palette_file(Core::File& file)
return palette;
}
Result<Vector<Color>, String> PaletteWidget::load_palette_fd_and_close(int fd)
{
auto file = Core::File::construct();
file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes);
if (file->has_error())
return String { file->error_string() };
return load_palette_file(file);
}
Result<Vector<Color>, String> PaletteWidget::load_palette_path(String const& file_path)
{
auto file_or_error = Core::File::open(file_path, Core::OpenMode::ReadOnly);
@ -275,20 +265,12 @@ Result<Vector<Color>, String> PaletteWidget::load_palette_path(String const& fil
return load_palette_file(file);
}
Result<void, String> PaletteWidget::save_palette_fd_and_close(Vector<Color> palette, int fd)
Result<void, String> PaletteWidget::save_palette_file(Vector<Color> palette, Core::File& file)
{
auto file = Core::File::construct();
file->open(fd, Core::OpenMode::WriteOnly, Core::File::ShouldCloseFileDescriptor::Yes);
if (file->has_error())
return String { file->error_string() };
for (auto& color : palette) {
file->write(color.to_string_without_alpha());
file->write("\n");
file.write(color.to_string_without_alpha());
file.write("\n");
}
file->close();
return {};
}

View file

@ -30,16 +30,14 @@ public:
Vector<Color> colors();
static Result<Vector<Color>, String> load_palette_fd_and_close(int);
static Result<Vector<Color>, String> load_palette_file(Core::File&);
static Result<Vector<Color>, String> load_palette_path(String const&);
static Result<void, String> save_palette_fd_and_close(Vector<Color>, int);
static Result<void, String> save_palette_file(Vector<Color>, Core::File&);
static Vector<Color> fallback_colors();
void set_image_editor(ImageEditor*);
private:
static Result<Vector<Color>, String> load_palette_file(Core::File&);
explicit PaletteWidget();
ImageEditor* m_editor { nullptr };

View file

@ -16,30 +16,22 @@
namespace PixelPaint {
ErrorOr<void> ProjectLoader::try_load_from_fd_and_close(int fd, StringView path)
ErrorOr<void> ProjectLoader::try_load_from_file(Core::File& file)
{
auto file = Core::File::construct();
file->open(fd, Core::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::No);
if (file->has_error())
return Error::from_errno(file->error());
auto contents = file->read_all();
auto contents = file.read_all();
auto json_or_error = JsonValue::from_string(contents);
if (json_or_error.is_error()) {
m_is_raw_image = true;
auto mapped_file = TRY(Core::MappedFile::map_from_fd_and_close(fd, path));
// FIXME: Find a way to avoid the memory copy here.
auto bitmap = TRY(Image::try_decode_bitmap(mapped_file->bytes()));
auto bitmap = TRY(Image::try_decode_bitmap(contents));
auto image = TRY(Image::try_create_from_bitmap(move(bitmap)));
m_image = image;
return {};
}
close(fd);
auto& json = json_or_error.value().as_object();
auto image = TRY(Image::try_create_from_pixel_paint_json(json));

View file

@ -18,7 +18,7 @@ public:
ProjectLoader() = default;
~ProjectLoader() = default;
ErrorOr<void> try_load_from_fd_and_close(int fd, StringView path);
ErrorOr<void> try_load_from_file(Core::File&);
bool is_raw_image() const { return m_is_raw_image; }
bool has_image() const { return !m_image.is_null(); }

View file

@ -71,13 +71,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->show();
if (image_file) {
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window->window_id(), image_file);
if (response.error != 0) {
if (response.error != -1)
GUI::MessageBox::show_error(window, String::formatted("Opening \"{}\" failed: {}", *response.chosen_file, strerror(response.error)));
auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, image_file);
if (response.is_error())
return 1;
}
main_widget->open_image_fd(*response.fd, *response.chosen_file);
main_widget->open_image(response.value());
} else {
main_widget->create_default_image();
}