Adding a confirmation dialog to be able to open the restored file.

This commit is contained in:
Sergiotarxz 2024-03-04 04:24:22 +01:00
parent 185fe331a6
commit 8fd91b1e08
2 changed files with 69 additions and 15 deletions

View File

@ -33,6 +33,11 @@ has app => (
required => 1,
);
has main_window => (
is => 'ro',
required => 1,
);
has _win => ( is => 'rw', );
sub start {
@ -46,11 +51,11 @@ sub start {
}
sub fill_logs_win {
my $self = shift;
my $win = $self->_win;
my $logs = GEmeTool::Log->new->get_logs;
my $self = shift;
my $win = $self->_win;
my $logs = GEmeTool::Log->new->get_logs;
my $grid_log = Gtk4::Grid->new;
my $i = 0;
my $i = 0;
for my $log_message (@$logs) {
my $date = $log_message->{date};
my $message = $log_message->{message};
@ -68,7 +73,7 @@ sub fill_logs_win {
}
my $label = Gtk4::Label->new($message);
$label->set_halign('start');
$grid_log->attach($label, 0, $i, 10, 1);
$grid_log->attach( $label, 0, $i, 10, 1 );
$self->create_button_restore( $grid_log, 11, $i, 'Restore input file',
$backup_input_file, $input_file );
$self->create_button_restore( $grid_log, 12, $i, 'Restore output file',
@ -77,7 +82,7 @@ sub fill_logs_win {
}
my $scroll = Gtk4::ScrolledWindow->new;
$scroll->set_propagate_natural_width(1);
$scroll->set_policy('never', 'automatic');
$scroll->set_policy( 'never', 'automatic' );
$scroll->set_child($grid_log);
$win->set_child($scroll);
}
@ -88,7 +93,8 @@ sub open_save {
my $dest_file = shift;
my $file_dialog = Gtk4::FileDialog->new;
if ( defined $dest_file ) {
$file_dialog->set_initial_folder( Glib::IO::File::new_for_path($dest_file->parent ));
$file_dialog->set_initial_folder(
Glib::IO::File::new_for_path( $dest_file->parent ) );
$file_dialog->set_initial_name( $dest_file->basename );
}
$file_dialog->save(
@ -102,17 +108,54 @@ sub open_save {
my $dest_file = $dialog->save_finish($res);
return if !defined $dest_file;
$dest_file = path( $dest_file->get_path );
logger()->msg('Restoring backup...', $file, $dest_file);
logger()->msg( 'Restoring backup...', $file, $dest_file );
$dest_file->spew_raw( $file->slurp_raw );
$self->offer_to_edit_the_new_file($dest_file);
}
);
}
sub offer_to_edit_the_new_file {
my $self = shift;
my $file = shift;
my $main_window = $self->main_window;
my $confirm = Gtk4::Window->new;
my $box_window = Gtk4::Box->new('vertical', 30);
my $label_question = Gtk4::Label->new("Do you want to open the restored file?");
my $label_warning = Gtk4::Label->new("Any unsaved changes in the save editor will be lost forever");
my $box_options = Gtk4::Box->new('horizontal', 0);
my $cancel_button = Gtk4::Button->new_with_label('Do nothing');
my $confirm_button = Gtk4::Button->new_with_label('Load new file');
$box_options->set_property('hexpand', 1);
$cancel_button->set_property('halign', 'start');
$confirm_button->set_property('halign', 'end');
$confirm_button->set_property('hexpand', 1);
$cancel_button->signal_connect('clicked' => sub {
$confirm->close;
});
$confirm_button->signal_connect('clicked' => sub {
$main_window->open_file($file);
$confirm->close;
});
$confirm->set_titlebar(Gtk4::Label->new('Open the restored file'));
$box_window->append($label_question);
$box_window->append($label_warning);
$box_options->append($cancel_button);
$box_options->append($confirm_button);
$box_window->append($box_options);
$confirm->set_child($box_window);
$confirm->present;
}
sub create_button_restore {
my $self = shift;
my $grid_log = shift;
my $column = shift;
my $row = shift;
my $column = shift;
my $row = shift;
my $restore_label = shift;
my $file = shift;
my $dest_file = shift;
@ -130,6 +173,6 @@ sub create_button_restore {
$self->open_save( $file, $dest_file );
}
);
$grid_log->attach($button_restore_backup, $column, $row, 1, 1);
$grid_log->attach( $button_restore_backup, $column, $row, 1, 1 );
}
1;

View File

@ -75,7 +75,6 @@ sub activate {
path(__FILE__)->parent->parent->child('resources/icons')->absolute );
Gtk4::Window::set_default_icon_name('gemetool');
my $win;
my $menu = Glib::IO::Menu->new;
my $about = Glib::IO::SimpleAction->new( 'about', undef );
@ -104,9 +103,11 @@ sub activate {
);
$logs->signal_connect(
activate => sub {
GEmeTool::View::LogWindow->new(app => $self->_app)->start;
my $win = $self->_win;
GEmeTool::View::LogWindow->new(app => $self->_app, main_window => $self)->start;
}
);
$about->signal_connect( activate => \&activate_about, );
my $about_menu_item = Glib::IO::MenuItem->new( 'About', 'app.about' );
@ -127,7 +128,7 @@ sub activate {
$box->append(
Gtk4::Label->new('You have to load a Pokemon Emerald Save.') );
$win = Gtk4::ApplicationWindow->new($app);
my $win = Gtk4::ApplicationWindow->new($app);
$self->_win($win);
$win->set_title('GEmeTool');
$win->set_show_menubar(1);
@ -198,11 +199,21 @@ sub save {
$self->_save->save($file);
}
sub open_file {
my $self = shift;
my $file = shift;
if (!-e $file) {
return;
}
$file = path( $file );
$self->_file($file);
$self->start_editing_file;
}
sub start_editing_file {
my $self = shift;
my $file = $self->_file;
my $save_as = $self->_save_as;
my (@saves_raw);
my $box_app = Gtk4::Box->new( 'vertical', 0 );
$self->_save(GEmeTool::Save->instance($file));
my $save_obj = $self->_save;