Skip to content

Dialog Boxes

Saif Ahmed edited this page Dec 17, 2022 · 1 revision

Dialog boxes can inform, warn the user and get simple decisions from am user. An example may be to warn someone they are about to overwrite a file.

Like other Interact Widgets, these dialogs are created, added to the interact object, ad then started. This draws the widget on the terminal window. Once active, they capture key presses (the left right cursor and the enter key) and when submitted, the widget is closed, execute a callback is executed with some parameters (the button selected and any user defined parameters) propagated, and then the redraw function is called to remove the dialog widget.

sub confirmSave{  # if about to overwrite warn
	my ($param)=@_;
	my $file=$param->{selected};
	if (-e $file){   # file xists so need to warn
		my $dialog=new Term::Graille::Dialog(
			  redraw=>\&refreshScreen,
			  callback=>\&saveFile,      # this callback needs to handle both over-write and cancel
			  message=>"Are you sure you want to save this file? it will overwrite $file",
			  param=>{file=>$file,action=>"save"},  # the file must be propagated to the callback function
			  icon=>"warn",
			  title=>"Confirm Save",
			  buttons=>[qw/Overwrite Cancel/]
	   );
		my $dialogId=$io->addObject(object => $dialog);  # add the dialog object to the Interact object
		$io->start($dialogId);                           # activate the object
	}
	else{   # file does not exist so safe to save file;
		saveFile({file=>$file});
	}
}

image