« blib/lib/Term/Fabulous/Widget/Table/Borders.pm
↑ Summary
blib/lib/Term/Fabulous/Chart/Scale/Category.pm »
| 1 | package Term::Fabulous::Widget::Table::Frame; | ||
| 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 Exporter 'import'; | ||
| 11 | our @EXPORT_OK = qw(framed); | ||
| 12 | |||
| 13 | use Clay::UI::Enum::Result; | ||
| 14 | use Clay::XS qw(sizing_fixed sizing_grow); | ||
| 15 | use Scalar::Util qw(weaken); | ||
| 16 | use Term::Fabulous::Widget::Box; | ||
| 17 | use Term::Fabulous::Widget::ScrollBox; | ||
| 18 | |||
| 19 | # A table in a frame of its own (a table's own frame scrolls with its | ||
| 20 | # rows), drawn in the accent while the table has the focus. | ||
| 21 | sub framed ( $table, $rows, $min_rows = $rows ) { | ||
| 22 | die "Term::Fabulous::Widget::Table::Frame: min_rows $min_rows is more than rows $rows" if $min_rows > $rows; | ||
| 23 | my $frame = $min_rows == $rows ? _fixed_frame( $table, $rows ) : _shrinking_frame( $table, $rows, $min_rows ); | ||
| 24 | weaken( my $weak_frame = $frame ); | ||
| 25 | weaken( my $weak_table = $table ); | ||
| 26 | $table->on( OnFocus => sub ($event) { $weak_frame->border_color( $weak_frame->family_look( input => 'accent' ) ) if $event->target == $weak_table; return Clay::UI::Enum::Result->CONTINUE } ); | ||
| 27 | $table->on( OnBlur => sub ($event) { $weak_frame->reset_look('border_color') if $event->target == $weak_table; return Clay::UI::Enum::Result->CONTINUE } ); | ||
| 28 | return $frame; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _fixed_frame ( $table, $rows ) { | ||
| 32 | my $frame = Term::Fabulous::Widget::Box->new( bordered => 1, layout => { sizing => { width => sizing_grow(), height => sizing_fixed( $rows + 2 ) } } ); | ||
| 33 | $frame->add_child($table); | ||
| 34 | return $frame; | ||
| 35 | } | ||
| 36 | |||
| 37 | # A frame that wants $rows but gives up rows down to $min_rows when its | ||
| 38 | # parent has no room. Clay sizes a frame by its content, and shrinks it | ||
| 39 | # only down to the content's minimum. So the table has a strut next to | ||
| 40 | # it: an empty column that wants $rows but clips its own content (a | ||
| 41 | # scroll box pinned at the top, which takes no minimum from what it | ||
| 42 | # holds) and so shrinks down to $min_rows. | ||
| 43 | sub _shrinking_frame ( $table, $rows, $min_rows ) { | ||
| 44 | my $frame = Term::Fabulous::Widget::Box->new( bordered => 1, layout => { sizing => { width => sizing_grow(), height => sizing_grow( $min_rows + 2, $rows + 2 ) } } ); | ||
| 45 | my $strut = Term::Fabulous::Widget::ScrollBox->new( | ||
| 46 | id => $table->id . '/strut', | ||
| 47 | scrollbar => 0, | ||
| 48 | vertical => 1, | ||
| 49 | child_offset => { x => 0, y => 0 }, | ||
| 50 | layout => { sizing => { width => sizing_fixed(0), height => sizing_grow( $min_rows, $rows ) } }, | ||
| 51 | ); | ||
| 52 | $strut->add_child( Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fixed(0), height => sizing_fixed($rows) } } ) ); | ||
| 53 | $frame->add_child( $table, $strut ); | ||
| 54 | return $frame; | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; | ||
| 58 | |||
| 59 | __END__ |