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

filter: Statementstmt 0.0% 0 / 232 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 17 Podpod n/a totaltotal 0.0% 0 / 411 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::Bar;
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::Box;
11 use Term::Fabulous::Widget::Tabs::Button;
12 use Term::Fabulous::Widget::Tabs::Line;
13  
14 our $VERSION = '0.02';
15  
16 class Term::Fabulous::Widget::Tabs::Bar
17 :isa(Term::Fabulous::Widget::Box)
18 :strict(params)
19 {
20 use Clay::UI::Enum::Result;
21 use Clay::XS qw(
22 sizing_grow sizing_fit CLAY_LEFT_TO_RIGHT CLAY_TOP_TO_BOTTOM
23 CLAY_ALIGN_X_LEFT CLAY_ALIGN_X_CENTER CLAY_ALIGN_X_RIGHT CLAY_ALIGN_Y_TOP CLAY_ALIGN_Y_CENTER CLAY_ALIGN_Y_BOTTOM
24 );
25 use List::Util qw(first);
26 use Scalar::Util qw(blessed refaddr weaken);
27 use Term::Fabulous::Check qw(boolean describe non_negative_integer one_of);
28 use Term::Fabulous::Roving qw(roving_target);
29 use Term::Fabulous::Enum::BorderStyle;
30 use Term::Fabulous::Event::Select;
31  
32 use constant BUTTON_CLASS => 'Term::Fabulous::Widget::Tabs::Button';
33  
34 my %DEFAULT_LOOK = Term::Fabulous::Widget::Tabs::Button->default_look;
35  
36 # The geometry of each side the bar can be on: whether the tabs run
37 # from left to right, and the directions toward the page and away from
38 # it, as the arms of Term::Fabulous::Enum::BorderStyle->junction.
39 my %SIDE = (
40 top => { horizontal => 1, toward_page => 'down', away => 'up' },
41 bottom => { horizontal => 1, toward_page => 'up', away => 'down' },
42 left => { horizontal => 0, toward_page => 'right', away => 'left' },
43 right => { horizontal => 0, toward_page => 'left', away => 'right' },
44 );
45 my @ORIENTATIONS = qw(horizontal vertical);
46 my %ALIGN_X = ( start => CLAY_ALIGN_X_LEFT, center => CLAY_ALIGN_X_CENTER, end => CLAY_ALIGN_X_RIGHT );
47 my %ALIGN_Y = ( start => CLAY_ALIGN_Y_TOP, center => CLAY_ALIGN_Y_CENTER, end => CLAY_ALIGN_Y_BOTTOM );
48  
49 field $side :param = $DEFAULT_LOOK{side};
50 field $orientation :param = $DEFAULT_LOOK{orientation};
51 field $tab_alignment :param = 'start';
52 field $tab_gap :param = 1;
53 field $tab_margin :param = 1;
54 field $tab_padding :param = $DEFAULT_LOOK{tab_padding};
55 field $active_bold :param = $DEFAULT_LOOK{active_bold};
56 field $page_border :param = 0;
57  
58 # The row of tabs and the line toward the page; the active tab; the
59 # sizing the layout asked for, which wins over the side's.
60 field $_row;
61 field $_line;
62 field $_active;
63 field $_sizing_wish;
64  
65 # The Tabs the bar belongs to, which shows the page of the active tab.
66 field $_owner;
67  
68 ADJUST {
69 $side = one_of( $self, side => $side, keys %SIDE );
70 $orientation = one_of( $self, orientation => $orientation, @ORIENTATIONS );
71 $tab_alignment = one_of( $self, tab_alignment => $tab_alignment, keys %ALIGN_X );
72 $tab_gap = non_negative_integer( $self, tab_gap => $tab_gap );
73 $tab_margin = non_negative_integer( $self, tab_margin => $tab_margin );
74 $tab_padding = non_negative_integer( $self, tab_padding => $tab_padding );
75 $active_bold = boolean( $self, active_bold => $active_bold );
76 $page_border = boolean( $self, page_border => $page_border );
77  
78 $_sizing_wish = $self->layout->{sizing} // {};
79 $_row = Term::Fabulous::Widget::Box->new;
80 $_line = Term::Fabulous::Widget::Tabs::Line->new;
81 $self->_arrange;
82  
83 weaken( my $weak_self = $self );
84 $self->on( KeyPress => sub ($event) { return $weak_self && $weak_self->_handle_key($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE } );
85 }
86  
87 # ---------------------------------------------------------------------
88 # The theme: the tabs family
89 # ---------------------------------------------------------------------
90  
91 method theme_family :common () {
92 return 'tabs';
93 }
94  
95 # The colors and the line style come from the theme's tabs family
96 # unless given.
97 method themed_params :common () {
98 return (
99 $class->SUPER::themed_params,
100 line_style => [ 'line.style', 'normal', 'grid_border_style' ],
101 line_color => [ 'line.color', 'normal', 'color' ],
102 text_color => [ 'text', 'normal', 'color' ],
103 active_text_color => [ 'text', 'active', 'color' ],
104 disabled_color => [ 'text', 'disabled', 'color' ],
105 hover_background_color => [ 'hover_background', 'normal', 'color' ],
106 focus_border_color => [ 'focus_border', 'normal', 'optional_color' ],
107 );
108 }
109  
110 # The tabs and the line read the looks; they paint again
111 # (Term::Fabulous::Role::Themed).
112 method looks_changed (@names) {
113 $self->_restyle;
114 return;
115 }
116  
117 # ---------------------------------------------------------------------
118 # Geometry
119 # ---------------------------------------------------------------------
120  
121 method is_horizontal () {
122 return $SIDE{$side}{horizontal};
123 }
124  
125 method toward_page () {
126 return $SIDE{$side}{toward_page};
127 }
128  
129 method away_from_page () {
130 return $SIDE{$side}{away};
131 }
132  
133 # Lays the bar out for its side: the tabs in a row (or a column) that
134 # runs along the bar, aligned toward the page and along the bar as
135 # tab_alignment says, and the line between them and the page.
136 method _arrange () {
137 my $horizontal = $self->is_horizontal;
138 my ( $along, $across ) = $horizontal ? qw(width height) : qw(height width);
139 my %sizing = ( $along => sizing_grow(), $across => sizing_fit(), %$_sizing_wish );
140 $self->SUPER::layout( { %{ $self->layout }, layout_direction => $horizontal ? CLAY_TOP_TO_BOTTOM : CLAY_LEFT_TO_RIGHT, sizing => \%sizing } );
141  
142 my %alignment
143 = $horizontal
144 ? ( x => $ALIGN_X{$tab_alignment}, y => $side eq 'top' ? CLAY_ALIGN_Y_BOTTOM : CLAY_ALIGN_Y_TOP )
145 : ( y => $ALIGN_Y{$tab_alignment}, x => $side eq 'left' ? CLAY_ALIGN_X_RIGHT : CLAY_ALIGN_X_LEFT );
146 my %margin = $horizontal ? ( left => $tab_margin, right => $tab_margin ) : ( top => $tab_margin, bottom => $tab_margin );
147 $_row->layout(
148 {
149 layout_direction => $horizontal ? CLAY_LEFT_TO_RIGHT : CLAY_TOP_TO_BOTTOM,
150 child_gap => $tab_gap,
151 child_alignment => \%alignment,
152 padding => \%margin,
153 sizing => { $along => sizing_grow(), $across => sizing_fit() },
154 }
155 );
156  
157 my @parts = $side eq 'top' || $side eq 'left' ? ( $_row, $_line ) : ( $_line, $_row );
158 $self->_reattach(@parts) unless join( ' ', map { refaddr $_ } @parts ) eq join( ' ', map { refaddr $_ } @{ $self->children } );
159 return;
160 }
161  
162 # Puts the parts in this order; a tab that had the focus keeps it.
163 method _reattach (@parts) {
164 my $focused = first { $_->is_focused } $self->buttons;
165 $self->SUPER::clear_children;
166 $self->SUPER::add_child(@parts);
167 $focused->focus if defined $focused;
168 return;
169 }
170  
171 # A sizing written later wins over the side's as well.
172 method layout :override (@new) {
173 return $self->SUPER::layout unless @new;
174 my $layout = $self->SUPER::layout(@new);
175 $_sizing_wish = $layout->{sizing} // {};
176 return $layout;
177 }
178  
179 # ---------------------------------------------------------------------
180 # Tabs
181 # ---------------------------------------------------------------------
182  
183 method add_child :override (@kids) {
184 foreach my $kid (@kids) {
185 die ref($self) . ": a tab bar holds only Term::Fabulous::Widget::Tabs::Button widgets, got " . ( blessed $kid ? ref $kid : describe($kid) )
186 unless blessed $kid && $kid->isa(BUTTON_CLASS);
187 }
188 $_row->add_child(@kids);
189 my $first_enabled = first { $_->is_enabled } @kids;
190 $self->_activate($first_enabled) if !defined $_active && defined $first_enabled;
191 $self->_restyle;
192 return $self;
193 }
194  
195 method remove_child :override (@kids) {
196 $_row->remove_child(@kids);
197 $self->_dropped;
198 return $self;
199 }
200  
201 method remove_child_with_id :override ($target_id) {
202 $_row->remove_child_with_id($target_id);
203 $self->_dropped;
204 return $self;
205 }
206  
207 method remove_children_with :override ($predicate) {
208 $_row->remove_children_with($predicate);
209 $self->_dropped;
210 return $self;
211 }
212  
213 method clear_children :override () {
214 $_row->clear_children;
215 $self->_dropped;
216 return $self;
217 }
218  
219 # After tabs left the bar: when the active one did, the first enabled
220 # tab left takes its place.
221 method _dropped () {
222 $self->_activate( first { $_->is_enabled } $self->buttons ) if defined $_active && !$_row->has_child($_active);
223 $self->_restyle;
224 return;
225 }
226  
227 method buttons () {
228 return @{ $_row->children };
229 }
230  
231 method button ($index) {
232 my @buttons = $self->buttons;
233 die ref($self) . ": button needs an index in 0.." . $#buttons . ", got " . describe($index) unless defined $index && !ref $index && $index =~ /\A[0-9]+\z/ && $index < @buttons;
234 return $buttons[$index];
235 }
236  
237 # A tab or its index: the tab.
238 method _button_of ($which) {
239 return $self->button($which) unless blessed $which;
240 die ref($self) . ": not a tab of this bar" unless defined $self->index_of($which);
241 return $which;
242 }
243  
244 method index_of ($button) {
245 my @buttons = $self->buttons;
246 return first { refaddr( $buttons[$_] ) == refaddr($button) } 0 .. $#buttons;
247 }
248  
249 method active () {
250 return $_active;
251 }
252  
253 method active_index () {
254 return defined $_active ? $self->index_of($_active) : undef;
255 }
256  
257 # Makes a tab the active one from the program; undef makes none
258 # active. Fires nothing.
259 method select ($which) {
260 $self->_activate( defined $which ? $self->_button_of($which) : undef );
261 return $self;
262 }
263  
264 # Chooses a tab as the user does: fires Select when the active tab
265 # changes, and focuses it. A disabled tab changes nothing.
266 method choose ($which) {
267 my $button = $self->_button_of($which);
268 return $self unless $button->is_enabled;
269 $self->_chosen($button);
270 return $self;
271 }
272  
273 sub _same ( $one, $other ) {
274 return defined $one ? defined $other && refaddr($one) == refaddr($other) : !defined $other;
275 }
276  
277 method _activate ($button) {
278 return if _same( $_active, $button );
279 my $had_focus = defined $_active && $_active->is_focused;
280 $_active = $button;
281 $self->_restyle;
282 $self->_focus_active if $had_focus;
283 $_owner->_sync if defined $_owner;
284 return;
285 }
286  
287 method _chosen ($button) {
288 my $changed = !_same( $_active, $button );
289 $self->_activate($button);
290 $self->_focus_active;
291 $self->fire_event( Term::Fabulous::Event::Select->new( item => $button, index => $self->index_of($button) ) ) if $changed;
292 return;
293 }
294  
295 method _focus_active () {
296 my $ui = $self->ui;
297 return unless defined $_active && defined $ui && $ui->interaction->can_take_focus($_active);
298 $_active->focus unless $_active->is_focused;
299 return;
300 }
301  
302 # The user activated a tab (a click, Enter or Space).
303 method _button_activated ($button) {
304 $self->_chosen($button) if $button->is_enabled;
305 return;
306 }
307  
308 # The Tabs that owns the bar, told when the active tab changes.
309 method _set_owner ($owner) {
310 $_owner = $owner;
311 weaken $_owner if defined $_owner;
312 return;
313 }
314  
315 # ---------------------------------------------------------------------
316 # Look
317 # ---------------------------------------------------------------------
318  
319 # Only the active tab takes the focus: Tab stops at the bar once, and
320 # the arrow keys move between the tabs.
321 method _restyle () {
322 foreach my $button ( $self->buttons ) {
323 my $is_active = _same( $_active, $button );
324 $button->_set_active($is_active);
325 $button->can_focus($is_active);
326 }
327 $_line->mark_changed;
328 $self->mark_changed;
329 return;
330 }
331  
332 method _set ( $field_ref, $value, $arranges = 0 ) {
333 $$field_ref = $value;
334 $self->_arrange if $arranges;
335 $self->_restyle;
336 return $$field_ref;
337 }
338  
339 method side (@new) { return @new ? $self->_set( \$side, one_of( $self, side => $new[0], keys %SIDE ), 1 ) : $side }
340 method orientation (@new) { return @new ? $self->_set( \$orientation, one_of( $self, orientation => $new[0], @ORIENTATIONS ), 1 ) : $orientation }
341 method tab_alignment (@new) { return @new ? $self->_set( \$tab_alignment, one_of( $self, tab_alignment => $new[0], keys %ALIGN_X ), 1 ) : $tab_alignment }
342 method tab_gap (@new) { return @new ? $self->_set( \$tab_gap, non_negative_integer( $self, tab_gap => $new[0] ), 1 ) : $tab_gap }
343 method tab_margin (@new) { return @new ? $self->_set( \$tab_margin, non_negative_integer( $self, tab_margin => $new[0] ), 1 ) : $tab_margin }
344 method tab_padding (@new) { return @new ? $self->_set( \$tab_padding, non_negative_integer( $self, tab_padding => $new[0] ) ) : $tab_padding }
345 method active_bold (@new) { return @new ? $self->_set( \$active_bold, boolean( $self, active_bold => $new[0] ) ) : $active_bold }
346 method page_border (@new) { return @new ? $self->_set( \$page_border, boolean( $self, page_border => $new[0] ) ) : $page_border }
347  
348 method line_style (@new) {
349 return @new ? $self->set_look( line_style => $new[0] ) : $self->look_value('line_style'); # set_look and the theme both check for joints
350 }
351 method line_color (@new) { return @new ? $self->set_look( line_color => $new[0] ) : $self->look_value('line_color') }
352 method text_color (@new) { return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color') }
353 method active_text_color (@new) { return @new ? $self->set_look( active_text_color => $new[0] ) : $self->look_value('active_text_color') }
354 method hover_background_color (@new) { return @new ? $self->set_look( hover_background_color => $new[0] ) : $self->look_value('hover_background_color') }
355 method focus_border_color (@new) { return @new ? $self->set_look( focus_border_color => $new[0] ) : $self->look_value('focus_border_color') }
356 method disabled_color (@new) { return @new ? $self->set_look( disabled_color => $new[0] ) : $self->look_value('disabled_color') }
357  
358 method layout_properties :common () {
359 return (
360 $class->SUPER::layout_properties,
361 ( map { $_ => 'scalar' } qw(side orientation tab_alignment tab_gap tab_margin tab_padding) ),
362 ( map { $_ => 'boolean' } qw(active_bold page_border) ),
363 );
364 }
365  
366 # ---------------------------------------------------------------------
367 # Keys: the arrows, Home and End choose another tab while a tab of the
368 # bar has the focus.
369 # ---------------------------------------------------------------------
370  
371 method _handle_key ($event) {
372 my @buttons = $self->buttons;
373 return 0 unless grep { $_->is_focused } @buttons;
374 my @enabled = grep { $buttons[$_]->is_enabled } 0 .. $#buttons;
375 my $target = roving_target( \@enabled, $self->active_index, $event->main_key_name ) // return 0;
376 $self->_chosen( $buttons[$target] );
377 return 1;
378 }
379 }
380  
381 1;
382  
383 __END__