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

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