From 042fef3cab692782867e4c24b0b457e0c567f658 Mon Sep 17 00:00:00 2001 From: Sergiotarxz Date: Thu, 17 Oct 2024 07:21:48 +0200 Subject: [PATCH] Adding save as. --- lib/Exd/Gui.pm | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/Exd/Gui.pm b/lib/Exd/Gui.pm index d0fcaef..3ebf659 100644 --- a/lib/Exd/Gui.pm +++ b/lib/Exd/Gui.pm @@ -89,6 +89,8 @@ EOF } ); +has _save_path => ( is => 'rw' ); + sub _tempdir_previews($self) { if ( !defined $self->_tempdir_previews_guts ) { $self->_tempdir_previews_guts( Path::Tiny->tempdir ); @@ -125,6 +127,26 @@ sub start($self) { } sub _save_action($self) { + my $file_format = $self->_file_format; + my $dialog = Gtk4::FileDialog->new; + if (defined $self->_save_path) { + my $zip = $file_format->to_zip; + $zip->writeToFileNamed(''.$self->_save_path); + return; + } + $dialog->set_initial_name('project.exd'); + $dialog->save($self->window, undef, sub ($source, $res, $data) { + eval { + my $file = $dialog->save_finish($res); + $file = $file->get_path; + $self->_save_path($file); + my $zip = $file_format->to_zip; + $zip->writeToFileNamed(''.$file); + }; + }); +} + +sub _save_as_action($self) { my $file_format = $self->_file_format; my $dialog = Gtk4::FileDialog->new; $dialog->set_initial_name('project.exd'); @@ -142,10 +164,13 @@ sub _open_action($self) { my $file_format = $self->_file_format; my $dialog = Gtk4::FileDialog->new; $dialog->set_initial_name('project.exd'); + my $window = $self->window; $dialog->open($self->window, undef, sub ($source, $res, $data) { eval { my $file = $dialog->open_finish($res); $file = $file->get_path; + $self->_save_path($file); + $window->set_title("Exd (Thermal Printer) ". path($self->_save_path)->basename); $self->_file_format(Exd::FileFormat->from_zip_file($file)); $self->_update_editor_buffer; }; @@ -158,16 +183,22 @@ sub _startup($self) { my $file_menu = Glib::IO::Menu->new; my $open_action = Glib::IO::SimpleAction->new('open', undef); my $save_action = Glib::IO::SimpleAction->new('save', undef); + my $save_as_action = Glib::IO::SimpleAction->new('save-as', undef); $open_action->signal_connect('activate', sub { $self->_open_action; }); $save_action->signal_connect('activate', sub { $self->_save_action; }); + $save_as_action->signal_connect('activate', sub { + $self->_save_as_action; + }); $app->add_action($open_action); $app->add_action($save_action); + $app->add_action($save_as_action); $file_menu->append('Open', 'app.open'); $file_menu->append('Save', 'app.save'); + $file_menu->append('Save as', 'app.save-as'); $menu_model->append_submenu('File', $file_menu); $app->set_menubar($menu_model); } @@ -402,7 +433,7 @@ sub _populate_preview( $self, $box_editor_preview ) { sub _open_gallery($self) { my $window = Gtk4::Window->new; $window->set_transient_for( $self->window ); - $window->set_default_size( 600, 600 ); + $window->set_default_size( 650, 650 ); $window->set_title('Image gallery'); my $box = Gtk4::Box->new( 'vertical', 1 ); my $box_upper_menu = Gtk4::Box->new( 'horizontal', 10 );