blib/lib/Term/Fabulous/Widget/Table/ColumnChooser.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 13 Podpod n/a totaltotal 0.0% 0 / 145 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::Table::ColumnChooser;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.02';
9  
10 use Object::Pad 0.825;
11  
12 use Term::Fabulous::Widget::Box;
13 use Term::Fabulous::Widget::Checkbox;
14 use Term::Fabulous::Widget::Text;
15  
16 class Term::Fabulous::Widget::Table::ColumnChooser :isa(Term::Fabulous::Widget::Box) :strict(params) {
17 use Clay::UI::Enum::Result;
18 use Clay::XS qw(CLAY_TOP_TO_BOTTOM CLAY_ATTACH_TO_PARENT CLAY_ATTACH_POINT_RIGHT_TOP);
19 use Scalar::Util qw(weaken);
20 use Term::Fabulous::Enum::BorderStyle;
21  
22 # The highest z_index Clay has: the chooser belongs to the focused
23 # table and floats over everything.
24 use constant Z_INDEX => 32767;
25  
26 # [ key, title, visible ] per column, in order.
27 field $columns :param;
28 field $on_toggle :param; # sub ( $key, $visible )
29 field $on_close :param; # sub ()
30 field $text_color :param = [ 220, 223, 228, 255 ];
31 field $muted_color :param = [ 140, 146, 158, 255 ];
32 field @_boxes;
33  
34 ADJUST {
35 die "Term::Fabulous::Widget::Table::ColumnChooser: columns must be an array reference of [ key, title, visible ]"
36 unless ref $columns eq 'ARRAY' && !grep { ref $_ ne 'ARRAY' || @$_ != 3 } @$columns;
37 die "Term::Fabulous::Widget::Table::ColumnChooser: on_toggle and on_close must be code references"
38 unless ref $on_toggle eq 'CODE' && ref $on_close eq 'CODE';
39  
40 $self->layout( { layout_direction => CLAY_TOP_TO_BOTTOM, padding => { left => 1, right => 1 } } );
41 $self->floating( { attach_to => CLAY_ATTACH_TO_PARENT, attach_points => { element => CLAY_ATTACH_POINT_RIGHT_TOP, parent => CLAY_ATTACH_POINT_RIGHT_TOP }, z_index => Z_INDEX } );
42 $self->add_child( Term::Fabulous::Widget::Text->new( text => 'Columns', text_color => $text_color, bold => 1 ) );
43 foreach my $entry (@$columns) {
44 my ( $key, $title, $visible ) = @$entry;
45 my $box = Term::Fabulous::Widget::Checkbox->new( label => $title, checked => $visible, text_color => $text_color );
46 $box->on( Change => sub ($event) { $on_toggle->( $key, $event->value ); return } );
47 push @_boxes, $box;
48 }
49 $self->add_child( @_boxes, Term::Fabulous::Widget::Text->new( text => 'Esc closes', text_color => $muted_color ) );
50  
51 weaken( my $weak_self = $self );
52 $self->on(
53 KeyPress => sub ($event) {
54 my $name = $event->main_key_name // return Clay::UI::Enum::Result->CONTINUE;
55 return Clay::UI::Enum::Result->CONTINUE unless $name eq 'Escape';
56 $on_close->();
57 return;
58 }
59 );
60  
61 # Closes once the focus has left it; the new focus is only known after
62 # the OnBlur, so the check waits for the frame.
63 $self->on(
64 OnBlur => sub ($event) {
65 my $ui = $weak_self ? $weak_self->ui : undef;
66 $ui->after_draw( sub { $weak_self->_close_unless_focused if $weak_self } ) if defined $ui && $ui->can('after_draw');
67 return Clay::UI::Enum::Result->CONTINUE;
68 }
69 );
70 }
71  
72 method checkboxes () {
73 return @_boxes;
74 }
75  
76 method focus_first () {
77 my $ui = $self->ui // return;
78 $ui->interaction->set_focused_widget( $_boxes[0] ) if @_boxes;
79 return;
80 }
81  
82 method _close_unless_focused () {
83 my $ui = $self->ui // return;
84 return if $ui->interaction->has_focus_within($self);
85 $on_close->();
86 return;
87 }
88 }
89  
90 1;
91  
92 __END__