blib/lib/Term/Fabulous/Widget/Tabs/Button.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 14 Podpod n/a totaltotal 0.0% 0 / 219 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::Tabs::Button;
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::Theme;
11 use Term::Fabulous::Widget::Button;
12 use Term::Fabulous::Widget::Text;
13  
14 our $VERSION = '0.02';
15  
16 class Term::Fabulous::Widget::Tabs::Button
17 :isa(Term::Fabulous::Widget::Button)
18 :strict(params)
19 {
20 use Clay::UI::Enum::Result;
21 use Clay::XS qw(CLAY_LEFT_TO_RIGHT CLAY_TOP_TO_BOTTOM CLAY_TEXT_WRAP_NONE CLAY_TEXT_WRAP_NEWLINES);
22 use Scalar::Util qw(weaken);
23 use Term::Fabulous::Check qw(optional string);
24 use Term::Fabulous::Enum::BorderStyle;
25 use Term::Fabulous::Unicode qw(grapheme_clusters);
26  
27 # The settings a tab shows while it has no bar to take them from; the
28 # bar's defaults are these as well. The colors and the line style come
29 # from the theme's tabs family (see Term::Fabulous::Widget::Tabs::Bar).
30 my %DEFAULT_LOOK = (
31 side => 'top',
32 orientation => 'horizontal',
33 tab_padding => 1,
34 active_bold => 0,
35 );
36  
37 # The side of a tab that faces the page, by the side of the bar.
38 my %PAGE_SIDE = ( top => 'bottom', bottom => 'top', left => 'right', right => 'left' );
39  
40 field $title :param = '';
41 field $icon :param = undef;
42  
43 field $_icon_text;
44 field $_title_text;
45 field $_active = 0;
46  
47 method default_look :common () {
48 return %DEFAULT_LOOK;
49 }
50  
51 ADJUST {
52 $title = string( $self, title => $title );
53 $icon = optional( \&string, $self, icon => $icon );
54  
55 $self->bordered(1);
56 $self->pressed_background_color(undef);
57 $_icon_text = Term::Fabulous::Widget::Text->new( text => '' );
58 $_title_text = Term::Fabulous::Widget::Text->new( text => '' );
59  
60 weaken( my $weak_self = $self );
61 my $continue = Clay::UI::Enum::Result->CONTINUE;
62 $self->on( Activate => sub ($event) { $weak_self->_activated if $weak_self; return $continue } );
63 $self->on( $_ => sub ($event) { $weak_self->refresh_look if $weak_self; return $continue } ) foreach qw(OnFocus OnBlur OnHoverStart OnHoverStopped);
64 $self->refresh_look;
65 }
66  
67 # The bar the tab belongs to, if any: the tab's parent is the bar's row.
68 method bar () {
69 for ( my $node = $self->parent; defined $node; $node = $node->parent ) {
70 return $node if $node->isa('Term::Fabulous::Widget::Tabs::Bar');
71 }
72 return undef;
73 }
74  
75 method is_active () {
76 return $_active;
77 }
78  
79 # Set by the bar: the active tab is one cell larger toward the page.
80 method _set_active ($flag) {
81 $_active = $flag ? 1 : 0;
82 $self->refresh_look;
83 return;
84 }
85  
86 method title (@new) {
87 return $title unless @new;
88 $title = string( $self, title => $new[0] );
89 $self->refresh_look;
90 return $title;
91 }
92  
93 method icon (@new) {
94 return $icon unless @new;
95 $icon = optional( \&string, $self, icon => $new[0] );
96 $self->refresh_look;
97 return $icon;
98 }
99  
100 method focus () {
101 my $ui = $self->ui // die ref($self) . ": the tab is not part of a Term::Fabulous, so nothing can focus it";
102 $ui->interaction->set_focused_widget($self);
103 return $self;
104 }
105  
106 method layout_properties :common () {
107 return ( $class->SUPER::layout_properties, title => 'scalar', icon => 'scalar' );
108 }
109  
110 # The user activated the tab: the bar makes it the active one and fires
111 # Select. A tab on its own does nothing.
112 method _activated () {
113 my $bar = $self->bar;
114 $bar->_button_activated($self) if defined $bar;
115 return;
116 }
117  
118 # ---------------------------------------------------------------------
119 # Look
120 # ---------------------------------------------------------------------
121  
122 # One setting of the look: the bar's; without a bar, the default
123 # setting, or the default theme's tabs look.
124 method _look ($name) {
125 my $bar = $self->bar;
126 return $bar->$name if defined $bar;
127 return $DEFAULT_LOOK{$name} if exists $DEFAULT_LOOK{$name};
128 my %themed = Term::Fabulous::Widget::Tabs::Bar->themed_params;
129 return Term::Fabulous::Theme->default->look( 'tabs', @{ $themed{$name} }[ 0, 1 ] ); # slot and state
130 }
131  
132 # The tab's border and background follow the bar's colors and the
133 # tab's state, read when the frame is built, so a theme switch shows
134 # at once.
135 method contribute_look_theme :override ($config) {
136 my $border = $config->{border} // return;
137 my $color
138 = !$self->is_enabled ? $self->_look('disabled_color')
139 : $self->is_focused ? $self->_look('focus_border_color') // $self->_look('line_color')
140 : $self->_look('line_color');
141 $config->{border} = { %$border, color => $color };
142 $config->{background_color} = $self->_look('hover_background_color') if $self->is_enabled && !$_active && $self->is_hovered;
143 return;
144 }
145  
146 # A side without a style of its own is drawn in the line's style on
147 # the three sides away from the page and not at all on the page's
148 # side, read when the frame is drawn
149 # (Term::Fabulous::Role::HasBorderStyle).
150 method derived_border_style ($side) {
151 return $side eq $PAGE_SIDE{ $self->_look('side') } ? Term::Fabulous::Enum::BorderStyle->Hidden : $self->_look('line_style');
152 }
153  
154 # The labels are drawn in the bar's text colors.
155 method child_text_color :override ($explicit) {
156 return $self->_look( !$self->is_enabled ? 'disabled_color' : $_active ? 'active_text_color' : 'text_color' );
157 }
158  
159 # A label as a Text shows it: as it is, or one cluster per line for a
160 # vertical label.
161 sub _label_text ( $text, $vertical ) {
162 return $vertical ? join( "\n", grapheme_clusters($text) ) : $text;
163 }
164  
165 # Updates the borders, the padding and the label from the tab's state
166 # and the bar's settings. Called by the bar when they change.
167 method refresh_look () {
168 my $side = $self->_look('side');
169 my $vertical = $self->_look('orientation') eq 'vertical';
170 my $page_side = $PAGE_SIDE{$side};
171  
172 # The padding lies along the label; the active tab gets a cell more
173 # toward the page.
174 my $padding = $self->_look('tab_padding');
175 my %padding = $vertical ? ( top => $padding, bottom => $padding ) : ( left => $padding, right => $padding );
176 $padding{$page_side} = ( $padding{$page_side} // 0 ) + 1 if $_active;
177 $self->layout( { %{ $self->layout }, layout_direction => $vertical ? CLAY_TOP_TO_BOTTOM : CLAY_LEFT_TO_RIGHT, child_gap => 1, padding => \%padding } );
178  
179 my $wrap_mode = $vertical ? CLAY_TEXT_WRAP_NEWLINES : CLAY_TEXT_WRAP_NONE;
180 $_title_text->text( _label_text( $title, $vertical ) );
181 $_icon_text->text( _label_text( $icon // '', $vertical ) );
182 foreach my $text ( $_icon_text, $_title_text ) {
183 $text->wrap_mode($wrap_mode);
184 $text->bold( $_active && $self->_look('active_bold') ? 1 : 0 );
185 }
186 $self->clear_children;
187 $self->add_child( ( defined $icon ? $_icon_text : () ), $_title_text );
188 return $self;
189 }
190 }
191  
192 1;
193  
194 __END__