« blib/lib/Term/Fabulous/Widget/Tabs/Button.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/Tabs/Page.pm »
| 1 | package Term::Fabulous::Widget::Tabs::Line; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | use Object::Pad 0.825; | ||
| 9 | |||
| 10 | use Term::Fabulous::Widget::Display; | ||
| 11 | |||
| 12 | our $VERSION = '0.05'; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::Tabs::Line | ||
| 15 | :isa(Term::Fabulous::Widget::Display) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use Clay::XS qw(sizing_grow); | ||
| 19 | use Scalar::Util qw(blessed); | ||
| 20 | use Term::Fabulous::Enum::BorderStyle; | ||
| 21 | use Term::Fabulous::Render::Geometry qw(cell_rect); | ||
| 22 | |||
| 23 | # On its own, the line draws with the theme's tabs looks. | ||
| 24 | method theme_family :common () { | ||
| 25 | return 'tabs'; | ||
| 26 | } | ||
| 27 | |||
| 28 | # The bar the line belongs to, if any. | ||
| 29 | method bar () { | ||
| 30 | my $parent = $self->parent; | ||
| 31 | return blessed $parent && $parent->isa('Term::Fabulous::Widget::Tabs::Bar') ? $parent : undef; | ||
| 32 | } | ||
| 33 | |||
| 34 | method _is_horizontal () { | ||
| 35 | my $bar = $self->bar; | ||
| 36 | return !defined $bar || $bar->is_horizontal; | ||
| 37 | } | ||
| 38 | |||
| 39 | # One cell thick, as long as the bar. | ||
| 40 | method natural_size () { | ||
| 41 | return $self->_is_horizontal ? ( sizing_grow(), 1 ) : ( 1, sizing_grow() ); | ||
| 42 | } | ||
| 43 | |||
| 44 | # The first and the last cell of a tab along the line, as the frame | ||
| 45 | # being drawn lays it out; nothing when the tab is not laid out. | ||
| 46 | method _span ($tab) { | ||
| 47 | my $ui = $self->ui // return (); | ||
| 48 | my $box = $ui->bounding_box($tab) // return (); | ||
| 49 | my ( $x0, $y0, $x1, $y1 ) = cell_rect($box); | ||
| 50 | my ( $origin_x, $origin_y ) = $self->content_origin; | ||
| 51 | return $self->_is_horizontal ? ( $x0 - $origin_x, $x1 - 1 - $origin_x ) : ( $y0 - $origin_y, $y1 - 1 - $origin_y ); | ||
| 52 | } | ||
| 53 | |||
| 54 | # The tabs' places, states and the side change the picture without | ||
| 55 | # touching this widget; the bar marks it changed for everything else. | ||
| 56 | method paint_key :override () { | ||
| 57 | my $bar = $self->bar // return $self->SUPER::paint_key; | ||
| 58 | return ( $self->SUPER::paint_key, $bar->side, map { ( join( ',', $self->_span($_) ), $_->is_active, $_->is_focused, $_->is_enabled ) } $bar->buttons ); | ||
| 59 | } | ||
| 60 | |||
| 61 | # The glyph where the arms meet, in the line's style. | ||
| 62 | sub _joint ( $style, @arms ) { | ||
| 63 | return Term::Fabulous::Enum::BorderStyle->junction( map { $_ => $style } @arms ); | ||
| 64 | } | ||
| 65 | |||
| 66 | method paint () { | ||
| 67 | my $bar = $self->bar; | ||
| 68 | my $horizontal = $self->_is_horizontal; | ||
| 69 | my $length = $horizontal ? $self->columns : $self->rows; | ||
| 70 | my $style = defined $bar ? $bar->line_style : $self->look('line.style'); | ||
| 71 | my $line_attr = $self->color_attr( defined $bar ? $bar->line_color : $self->look('line.color') ); | ||
| 72 | my ( $start, $end ) = $horizontal ? qw(left right) : qw(up down); | ||
| 73 | |||
| 74 | my @glyphs = ( _joint( $style, $start, $end ) ) x $length; | ||
| 75 | my @attrs = ($line_attr) x $length; | ||
| 76 | my @open; # the cells of the active tab's opening, left as they are | ||
| 77 | |||
| 78 | if ( defined $bar ) { | ||
| 79 | my ( $toward, $away ) = ( $bar->toward_page, $bar->away_from_page ); | ||
| 80 | if ( $bar->page_border && $length >= 2 ) { | ||
| 81 | $glyphs[0] = _joint( $style, $toward, $end ); | ||
| 82 | $glyphs[-1] = _joint( $style, $toward, $start ); | ||
| 83 | } | ||
| 84 | my $put = sub ( $at, $glyph, $attr ) { | ||
| 85 | return if $at < 0 || $at >= $length; | ||
| 86 | $glyphs[$at] = $glyph; | ||
| 87 | $attrs[$at] = $attr; | ||
| 88 | }; | ||
| 89 | foreach my $tab ( $bar->buttons ) { | ||
| 90 | my ( $from, $to ) = $self->_span($tab) or next; | ||
| 91 | next if $to < $from; | ||
| 92 | unless ( $tab->is_active ) { | ||
| 93 | my $tee = _joint( $style, $away, $start, $end ); | ||
| 94 | $put->( $_, $tee, $line_attr ) foreach $from, $to; | ||
| 95 | next; | ||
| 96 | } | ||
| 97 | my $attr | ||
| 98 | = !$tab->is_enabled ? $self->color_attr( $bar->disabled_color ) | ||
| 99 | : $tab->is_focused ? $self->color_attr( $bar->focus_border_color ) // $line_attr | ||
| 100 | : $line_attr; | ||
| 101 | $put->( $from, _joint( $style, $away, $start ), $attr ); | ||
| 102 | $put->( $to, _joint( $style, $away, $end ), $attr ); | ||
| 103 | $open[$_] = 1 foreach grep { $_ >= 0 && $_ < $length } $from + 1 .. $to - 1; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | foreach my $at ( grep { !$open[$_] } 0 .. $length - 1 ) { | ||
| 108 | my ( $x, $y ) = $horizontal ? ( $at, 0 ) : ( 0, $at ); | ||
| 109 | $self->put_attrs( $x, $y, $glyphs[$at], $attrs[$at], undef ); | ||
| 110 | } | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | __END__ |