« blib/lib/Term/Fabulous/Widget/Table/Column.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/Table/EdgeView.pm »
| 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.05'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | use Term::Fabulous::Role::Popup; | ||
| 13 | use Term::Fabulous::Widget::Box; | ||
| 14 | use Term::Fabulous::Widget::Checkbox; | ||
| 15 | use Term::Fabulous::Widget::Text; | ||
| 16 | |||
| 17 | class Term::Fabulous::Widget::Table::ColumnChooser | ||
| 18 | :isa(Term::Fabulous::Widget::Box) | ||
| 19 | :does(Term::Fabulous::Role::Popup) | ||
| 20 | :strict(params) | ||
| 21 | { | ||
| 22 | use Clay::UI::Enum::Result; | ||
| 23 | use Clay::XS qw(CLAY_TOP_TO_BOTTOM CLAY_ATTACH_TO_PARENT CLAY_ATTACH_POINT_RIGHT_TOP); | ||
| 24 | use Term::Fabulous::Enum::BorderStyle; | ||
| 25 | use Term::Fabulous::Placement qw(place_popup TOPMOST_Z_INDEX); | ||
| 26 | |||
| 27 | # [ key, title, visible ] per column, in order. | ||
| 28 | field $columns :param; | ||
| 29 | field $on_toggle :param; # sub ( $key, $visible ) | ||
| 30 | field $on_close :param; # sub () | ||
| 31 | field $text_color :param = [ 220, 223, 228, 255 ]; | ||
| 32 | field $muted_color :param = [ 140, 146, 158, 255 ]; | ||
| 33 | field @_boxes; | ||
| 34 | |||
| 35 | ADJUST { | ||
| 36 | die "Term::Fabulous::Widget::Table::ColumnChooser: columns must be an array reference of [ key, title, visible ]" | ||
| 37 | unless ref $columns eq 'ARRAY' && !grep { ref $_ ne 'ARRAY' || @$_ != 3 } @$columns; | ||
| 38 | die "Term::Fabulous::Widget::Table::ColumnChooser: on_toggle and on_close must be code references" | ||
| 39 | unless ref $on_toggle eq 'CODE' && ref $on_close eq 'CODE'; | ||
| 40 | |||
| 41 | $self->layout( { layout_direction => CLAY_TOP_TO_BOTTOM, padding => { left => 1, right => 1 } } ); | ||
| 42 | $self->floating( { attach_to => CLAY_ATTACH_TO_PARENT, attach_points => { element => CLAY_ATTACH_POINT_RIGHT_TOP, parent => CLAY_ATTACH_POINT_RIGHT_TOP }, z_index => TOPMOST_Z_INDEX } ); | ||
| 43 | $self->add_child( Term::Fabulous::Widget::Text->new( text => 'Columns', text_color => $text_color, bold => 1 ) ); | ||
| 44 | foreach my $entry (@$columns) { | ||
| 45 | my ( $key, $title, $visible ) = @$entry; | ||
| 46 | my $box = Term::Fabulous::Widget::Checkbox->new( label => $title, checked => $visible, text_color => $text_color ); | ||
| 47 | $box->on( Change => sub ($event) { $on_toggle->( $key, $event->value ); return } ); | ||
| 48 | push @_boxes, $box; | ||
| 49 | } | ||
| 50 | $self->add_child( @_boxes, Term::Fabulous::Widget::Text->new( text => 'Esc closes', text_color => $muted_color ) ); | ||
| 51 | |||
| 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 ( $self, $event ) { | ||
| 65 | $self->each_frame( sub ($chooser) { $chooser->_close_unless_focused; return 1 }, name => 'close unless focused' ); | ||
| 66 | return Clay::UI::Enum::Result->CONTINUE; | ||
| 67 | }, | ||
| 68 | owner => $self | ||
| 69 | ); | ||
| 70 | } | ||
| 71 | |||
| 72 | method contribute_floating :override ($config) { | ||
| 73 | $self->SUPER::contribute_floating($config); | ||
| 74 | $self->float_popup($config) if defined $config->{floating}; | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | # Where the chooser floats in this frame (Term::Fabulous::Placement, | ||
| 79 | # Term::Fabulous::Role::Popup): over the top right corner of the | ||
| 80 | # table, kept on the screen. Its size is known from its first frame | ||
| 81 | # on; in that frame it hangs from the table's corner as Clay attaches | ||
| 82 | # it. | ||
| 83 | method popup_position () { | ||
| 84 | my $ui = $self->ui // return undef; | ||
| 85 | my $table = $self->parent // return undef; | ||
| 86 | my ( $anchor, $own ) = ( $ui->bounding_box($table), $ui->bounding_box($self) ); | ||
| 87 | return undef unless defined $anchor && defined $own; | ||
| 88 | my $place = place_popup( anchor => $anchor, size => [ @{$own}{qw(width height)} ], screen => [ $ui->width, $ui->height ], prefer => 'at', align => 'right' ); | ||
| 89 | return { x => $place->{x}, y => $place->{y} }; | ||
| 90 | } | ||
| 91 | |||
| 92 | method checkboxes () { | ||
| 93 | return @_boxes; | ||
| 94 | } | ||
| 95 | |||
| 96 | method focus_first () { | ||
| 97 | my $ui = $self->ui // return; | ||
| 98 | $ui->interaction->set_focused_widget( $_boxes[0] ) if @_boxes; | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | method _close_unless_focused () { | ||
| 103 | my $ui = $self->ui // return; | ||
| 104 | return if $ui->interaction->has_focus_within($self); | ||
| 105 | $on_close->(); | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; | ||
| 111 | |||
| 112 | __END__ |