« blib/lib/Term/Fabulous/Widget/Display.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/DocumentTabs.pm »
| 1 | package Term::Fabulous::Widget::Divider; | ||
| 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::Divider | ||
| 15 | :isa(Term::Fabulous::Widget::Display) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use Clay::XS qw(sizing_grow); | ||
| 19 | use List::Util qw(max); | ||
| 20 | use Term::Fabulous::Check qw(boolean non_negative_integer one_of optional string); | ||
| 21 | use Term::Fabulous::Enum::BorderStyle; | ||
| 22 | use Term::Fabulous::Termbox qw(TB_BOLD); | ||
| 23 | use Term::Fabulous::Unicode qw(grapheme_clusters cluster_columns string_columns); | ||
| 24 | |||
| 25 | # The glyphs of a border style, by index: the top side runs along a | ||
| 26 | # horizontal line, the left side along a vertical one. | ||
| 27 | use constant HORIZONTAL_GLYPH => 1; | ||
| 28 | use constant VERTICAL_GLYPH => 3; | ||
| 29 | |||
| 30 | my @POSITIONS = qw(start center end); | ||
| 31 | |||
| 32 | field $vertical :param = 0; | ||
| 33 | field $text :param = ''; | ||
| 34 | field $text_position :param = 'center'; | ||
| 35 | field $text_margin :param = 1; | ||
| 36 | field $text_padding :param = 1; | ||
| 37 | field $glyph :param = undef; | ||
| 38 | field $bold :param = 0; | ||
| 39 | |||
| 40 | ADJUST { | ||
| 41 | $vertical = boolean( $self, vertical => $vertical ); | ||
| 42 | $text = string( $self, text => $text ); | ||
| 43 | $text_position = one_of( $self, text_position => $text_position, @POSITIONS ); | ||
| 44 | $text_margin = non_negative_integer( $self, text_margin => $text_margin ); | ||
| 45 | $text_padding = non_negative_integer( $self, text_padding => $text_padding ); | ||
| 46 | $glyph = $self->_checked_glyph($glyph); | ||
| 47 | $bold = boolean( $self, bold => $bold ); | ||
| 48 | } | ||
| 49 | |||
| 50 | method theme_family :common () { | ||
| 51 | return 'divider'; | ||
| 52 | } | ||
| 53 | |||
| 54 | method themed_params :common () { | ||
| 55 | return ( | ||
| 56 | $class->SUPER::themed_params, line_style => [ 'line.style', 'normal', 'border_style' ], color => [ 'line.color', 'normal', 'cell_color' ], | ||
| 57 | text_color => [ 'text', 'normal', 'cell_color' ] | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | |||
| 61 | method _checked_glyph ($value) { | ||
| 62 | return optional( \&Term::Fabulous::Check::glyph, $self, glyph => $value ); | ||
| 63 | } | ||
| 64 | |||
| 65 | # --------------------------------------------------------------------- | ||
| 66 | # Accessors | ||
| 67 | # --------------------------------------------------------------------- | ||
| 68 | |||
| 69 | method _set ( $field_ref, $value ) { | ||
| 70 | $$field_ref = $value; | ||
| 71 | $self->mark_changed; | ||
| 72 | return $$field_ref; | ||
| 73 | } | ||
| 74 | |||
| 75 | method vertical (@new) { return @new ? $self->_set( \$vertical, boolean( $self, vertical => $new[0] ) ) : $vertical } | ||
| 76 | method text (@new) { return @new ? $self->_set( \$text, string( $self, text => $new[0] ) ) : $text } | ||
| 77 | method text_position (@new) { return @new ? $self->_set( \$text_position, one_of( $self, text_position => $new[0], @POSITIONS ) ) : $text_position } | ||
| 78 | method text_margin (@new) { return @new ? $self->_set( \$text_margin, non_negative_integer( $self, text_margin => $new[0] ) ) : $text_margin } | ||
| 79 | method text_padding (@new) { return @new ? $self->_set( \$text_padding, non_negative_integer( $self, text_padding => $new[0] ) ) : $text_padding } | ||
| 80 | method glyph (@new) { return @new ? $self->_set( \$glyph, $self->_checked_glyph( $new[0] ) ) : $glyph } | ||
| 81 | method bold (@new) { return @new ? $self->_set( \$bold, boolean( $self, bold => $new[0] ) ) : $bold } | ||
| 82 | |||
| 83 | method line_style (@new) { | ||
| 84 | return @new ? $self->set_look( line_style => $new[0] ) : $self->look_value('line_style') // Term::Fabulous::Enum::BorderStyle->Solid; | ||
| 85 | } | ||
| 86 | method color (@new) { return @new ? $self->set_look( color => $new[0] ) : $self->look_value('color') } | ||
| 87 | method text_color (@new) { return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color') } | ||
| 88 | |||
| 89 | method layout_properties :common () { | ||
| 90 | return ( | ||
| 91 | $class->SUPER::layout_properties, | ||
| 92 | ( map { $_ => 'boolean' } qw(vertical bold) ), | ||
| 93 | ( map { $_ => 'scalar' } qw(text text_position text_margin text_padding glyph) ), | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | |||
| 97 | # --------------------------------------------------------------------- | ||
| 98 | # Painting | ||
| 99 | # --------------------------------------------------------------------- | ||
| 100 | |||
| 101 | # The glyph the line is drawn with. | ||
| 102 | method line_glyph () { | ||
| 103 | return $glyph // $self->line_style->glyphs->[ $vertical ? VERTICAL_GLYPH : HORIZONTAL_GLYPH ]; | ||
| 104 | } | ||
| 105 | |||
| 106 | # The clusters of the text, each with its columns; a vertical divider | ||
| 107 | # shows them one below the other. | ||
| 108 | method _text_clusters () { | ||
| 109 | return map { [ $_, cluster_columns($_) ] } grapheme_clusters($text); | ||
| 110 | } | ||
| 111 | |||
| 112 | # The cells the text takes along the line, padding included; 0 without | ||
| 113 | # a text. | ||
| 114 | method _text_length () { | ||
| 115 | return 0 unless length $text; | ||
| 116 | return 2 * $text_padding + ( $vertical ? scalar( $self->_text_clusters ) : string_columns($text) ); | ||
| 117 | } | ||
| 118 | |||
| 119 | # The cells the line needs at least: the text with a cell of line on | ||
| 120 | # each side, and the margin where the text sits at an end. | ||
| 121 | method _min_length () { | ||
| 122 | my $length = $self->_text_length; | ||
| 123 | return 0 unless $length; | ||
| 124 | return $length + 2 + ( $text_position eq 'center' ? 0 : $text_margin ); | ||
| 125 | } | ||
| 126 | |||
| 127 | # The length of the line, which grows along it, and its thickness: one | ||
| 128 | # cell, or the widest cluster of a vertical text. | ||
| 129 | method natural_size () { | ||
| 130 | my $thickness = $vertical ? max( 1, map { $_->[1] } $self->_text_clusters ) : 1; | ||
| 131 | my $length = sizing_grow( $self->_min_length ); | ||
| 132 | return $vertical ? ( $thickness, $length ) : ( $length, $thickness ); | ||
| 133 | } | ||
| 134 | |||
| 135 | # Where the text starts along the line, or undef when it does not fit. | ||
| 136 | method _text_start ($length) { | ||
| 137 | my $text_length = $self->_text_length or return undef; | ||
| 138 | return undef if $text_length > $length; | ||
| 139 | return $text_margin < $length - $text_length ? $text_margin : 0 if $text_position eq 'start'; | ||
| 140 | return $length - $text_length - ( $text_margin < $length - $text_length ? $text_margin : 0 ) if $text_position eq 'end'; | ||
| 141 | return int( ( $length - $text_length ) / 2 ); | ||
| 142 | } | ||
| 143 | |||
| 144 | method paint () { | ||
| 145 | my ( $columns, $rows ) = ( $self->columns, $self->rows ); | ||
| 146 | my $line_fg = $self->color_attr( $self->color ); | ||
| 147 | my $text_fg = ( $self->color_attr( $self->text_color ) // 0 ) | ( $bold ? TB_BOLD : 0 ); | ||
| 148 | my $line = $self->line_glyph; | ||
| 149 | |||
| 150 | if ($vertical) { | ||
| 151 | my $x = int( ( $columns - 1 ) / 2 ); | ||
| 152 | my $start = $self->_text_start($rows); | ||
| 153 | $self->put_attrs( $x, $_, $line, $line_fg, undef ) foreach 0 .. $rows - 1; | ||
| 154 | return unless defined $start; | ||
| 155 | my @clusters = $self->_text_clusters; | ||
| 156 | my $y = $start + $text_padding; | ||
| 157 | $self->put_attrs( $x, $_, ' ', undef, undef ) foreach $start .. $start + $self->_text_length - 1; | ||
| 158 | $self->put_attrs( $x, $y++, $_->[0], $text_fg, undef ) foreach @clusters; | ||
| 159 | return; | ||
| 160 | } | ||
| 161 | |||
| 162 | my $y = int( ( $rows - 1 ) / 2 ); | ||
| 163 | my $start = $self->_text_start($columns); | ||
| 164 | $self->fill_attrs( 0, $y, $columns, $line, $line_fg, undef ); | ||
| 165 | return unless defined $start; | ||
| 166 | $self->fill_attrs( $start, $y, $self->_text_length, ' ', undef, undef ); | ||
| 167 | $self->paint_text( $start + $text_padding, $y, $text, $text_fg, undef ); | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | 1; | ||
| 173 | |||
| 174 | __END__ |