#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use Encode;
use Parallel::ForkManager;
use Gtk3 -init;

my $pm = new Parallel::ForkManager(2);
my $window = Gtk3::Window->new('toplevel');
$window->signal_connect(delete_event => sub { Gtk3->main_quit() });

sub print_statement {
	my ( $self, $attr ) = @_;
	my @pid;
	my $flag = 0;

	$pm->run_on_start(
		sub {
			my ( $pid, $id ) = @_;
			print "run_on_start\n";
			$$attr{'spinner'}->show;
			$$attr{'spinner'}->start;
		} # sub
	); # run_on_start
	$pm->run_on_finish(
		sub {
			my ( $pid, $exit_code, $id ) = @_;
			print "run_on_finish\n";
			if ( $exit_code == 1 ){
				$$attr{'spinner'}->stop;
				#----------------------
				# if I uncomment this line, spinner does not show at all!!!
				#----------------------
				$$attr{'spinner'}->hide;
			}
		} # sub
	); # run_on_finish

	# print statements
	$pid[0] = $pm->start;
		sleep 2;
	$pm->finish(1);

	#$pm->wait_all_children;

}

my $grid = Gtk3::Grid->new();
my $spinner = Gtk3::Spinner->new;

my $start_button = Gtk3::Button->new_with_label("Start Spin");
$start_button->signal_connect( clicked => \&print_statement,
									      { 'spinner' => $spinner, }		  
); # signal_connect
#$spinner->show;
#	$spinner->start;
#	print $spinner->get_visible,"\n";
#	$spinner->set_visible(1);
#my $stop_button = Gtk3::Button->new_with_label("Stop Spin");
#$stop_button->signal_connect( clicked => sub {
#		#	$spinner->hide;
#			$spinner->stop;
#			print $spinner->get_visible,"\n";
#			$spinner->set_visible(0);
#	} # sub
#); # signal_connect
$grid->attach($start_button,1,1,1,1);
#$grid->attach($stop_button,1,2,2,1);
$grid->attach($spinner,1,3,3,1);
$window->add($grid);
$window->show_all();
$spinner->set_visible(0);

Gtk3->main();

