diff --git a/scripts/TUI.pm b/scripts/TUI.pm index 805a5de..3686506 100644 --- a/scripts/TUI.pm +++ b/scripts/TUI.pm @@ -33,11 +33,13 @@ sub select_from_list { : map { $_ => $_ } @items; my @selection; + my @selection_order; # multiselect only: items in toggle order my $cui = init_cui(); my $win = $cui->add( 'root', 'Window', ); - my $listbox = $win->add( + my $listbox; + $listbox = $win->add( 'item_list', 'Listbox', -title => $title, @@ -47,7 +49,20 @@ sub select_from_list { -multi => $multiselect == 1, -radio => $multiselect == 0, -padbottom => 1, + -onchange => sub { + return unless $multiselect; + my %now = map { $_ => 1 } $listbox->get(); + # Append newly selected items in toggle order + for my $item (@items) { + if ( $now{$item} && !grep { $_ eq $item } @selection_order ) { + push @selection_order, $item; + } + } + + # Drop deselected items + @selection_order = grep { $now{$_} } @selection_order; + }, ); $win->add( @@ -64,6 +79,9 @@ sub select_from_list { if ( $multiselect && grep { $_ eq '__ALL__' } @picked ) { @selection = @items; } + elsif ($multiselect) { + @selection = @selection_order; + } else { @selection = @picked; }