| 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.05'; | ||
| 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 Term::Fabulous::Widget::Box; | ||
| 16 | use Term::Fabulous::Widget::ScrollBox; | ||
| 17 | |||
| 18 | # A table in a frame of its own, drawn in the accent while the table has | ||
| 19 | # the focus, and of a height that gives way to the widgets around it. | ||
| 20 | sub framed ( $table, $rows, $min_rows = $rows ) { | ||
| 21 | die "Term::Fabulous::Widget::Table::Frame: min_rows $min_rows is more than rows $rows" if $min_rows > $rows; | ||
| 22 | my $frame = $min_rows == $rows ? _fixed_frame( $table, $rows ) : _shrinking_frame( $table, $rows, $min_rows ); | ||
| 23 | $table->on( | ||
| 24 | OnFocus => sub ( $frame, $event ) { | ||
| 25 | $frame->border_color( $frame->family_look( input => 'accent' ) ) if $event->target == $event->current_target; | ||
| 26 | return Clay::UI::Enum::Result->CONTINUE; | ||
| 27 | }, | ||
| 28 | owner => $frame, | ||
| 29 | ); | ||
| 30 | $table->on( | ||
| 31 | OnBlur => sub ( $frame, $event ) { | ||
| 32 | $frame->reset_look('border_color') if $event->target == $event->current_target; | ||
| 33 | return Clay::UI::Enum::Result->CONTINUE; | ||
| 34 | }, | ||
| 35 | owner => $frame, | ||
| 36 | ); | ||
| 37 | return $frame; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _fixed_frame ( $table, $rows ) { | ||
| 41 | my $frame = Term::Fabulous::Widget::Box->new( bordered => 1, layout => { sizing => { width => sizing_grow(), height => sizing_fixed( $rows + 2 ) } } ); | ||
| 42 | $frame->add_child($table); | ||
| 43 | return $frame; | ||
| 44 | } | ||
| 45 | |||
| 46 | # A frame that wants $rows but gives up rows down to $min_rows when its | ||
| 47 | # parent has no room. Clay sizes a frame by its content, and shrinks it | ||
| 48 | # only down to the content's minimum. So the table has a strut next to | ||
| 49 | # it: an empty column that wants $rows but clips its own content (a | ||
| 50 | # scroll box pinned at the top, which takes no minimum from what it | ||
| 51 | # holds) and so shrinks down to $min_rows. | ||
| 52 | sub _shrinking_frame ( $table, $rows, $min_rows ) { | ||
| 53 | my $frame = Term::Fabulous::Widget::Box->new( bordered => 1, layout => { sizing => { width => sizing_grow(), height => sizing_grow( $min_rows + 2, $rows + 2 ) } } ); | ||
| 54 | my $strut = Term::Fabulous::Widget::ScrollBox->new( | ||
| 55 | id => $table->id . '/strut', | ||
| 56 | scrollbar => 0, | ||
| 57 | vertical => 1, | ||
| 58 | child_offset => { x => 0, y => 0 }, | ||
| 59 | layout => { sizing => { width => sizing_fixed(0), height => sizing_grow( $min_rows, $rows ) } }, | ||
| 60 | ); | ||
| 61 | $strut->add_child( Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fixed(0), height => sizing_fixed($rows) } } ) ); | ||
| 62 | $frame->add_child( $table, $strut ); | ||
| 63 | return $frame; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | __END__ |