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

filter: Statementstmt 0.0% 0 / 210 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 19 Podpod n/a totaltotal 0.0% 0 / 338 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;
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::Role::Searchable qw(searchables_in);
11 use Term::Fabulous::Role::SearchContainer;
12 use Term::Fabulous::Widget::Box;
13 use Term::Fabulous::Widget::Tabs::Bar;
14 use Term::Fabulous::Widget::Tabs::Button;
15 use Term::Fabulous::Widget::Tabs::Page;
16  
17 our $VERSION = '0.05';
18  
19 class Term::Fabulous::Widget::Tabs
20 :isa(Term::Fabulous::Widget::Box)
21 :does(Term::Fabulous::Role::SearchContainer)
22 :strict(params)
23 {
24 use Clay::UI::Enum::Result;
25 use Clay::XS qw(sizing_grow CLAY_LEFT_TO_RIGHT CLAY_TOP_TO_BOTTOM);
26 use List::Util qw(first);
27 use Scalar::Util qw(blessed refaddr);
28 use Term::Fabulous::Check qw(describe);
29 use Term::Fabulous::Roving qw(roving_target);
30 use Term::Fabulous::Event::Select;
31  
32 use constant PAGE_CLASS => 'Term::Fabulous::Widget::Tabs::Page';
33  
34 # The look is the bar's; these constructor parameters go to it. The
35 # themed ones live on the bar (see forwarded_looks).
36 my @BAR_LOOKS = qw(line_style line_color text_color active_text_color hover_background_color focus_border_color disabled_color);
37 my @BAR_PARAMS = ( qw(side orientation tab_alignment tab_gap tab_margin tab_padding active_padding active_bold page_border), @BAR_LOOKS );
38  
39 # The bar, the slot that shows the active page, and the pages, in the
40 # order of the bar's tabs.
41 field $_bar;
42 field $_slot;
43 field @_pages;
44  
45 ADJUSTPARAMS($params) {
46 my %look = ( page_border => 1, map { $_ => delete $params->{$_} } grep { exists $params->{$_} } @BAR_PARAMS );
47 $_bar = Term::Fabulous::Widget::Tabs::Bar->new(%look);
48 $_slot = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow(), height => sizing_grow() } } );
49 $_bar->_set_owner($self);
50  
51 my $layout = $self->layout;
52 $self->layout( { %$layout, sizing => { width => sizing_grow(), height => sizing_grow(), %{ $layout->{sizing} // {} } } } );
53 $self->_arrange;
54  
55 $_bar->on( Select => sub ( $self, $event ) { $self->_tab_chosen($event); return }, owner => $self );
56 $self->on( KeyPress => sub ( $self, $event ) { return $self->_handle_key($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE }, owner => $self );
57 }
58  
59 # ---------------------------------------------------------------------
60 # Layout: the bar and the slot, the bar on its side.
61 # ---------------------------------------------------------------------
62  
63 method _arrange () {
64 my $side = $_bar->side;
65 $self->layout( { %{ $self->layout }, layout_direction => $_bar->is_horizontal ? CLAY_TOP_TO_BOTTOM : CLAY_LEFT_TO_RIGHT } );
66 my @parts = $side eq 'top' || $side eq 'left' ? ( $_bar, $_slot ) : ( $_slot, $_bar );
67 $self->_reattach(@parts) unless join( ' ', map { refaddr $_ } @parts ) eq join( ' ', map { refaddr $_ } @{ $self->children } );
68 $self->_restyle_pages;
69 return;
70 }
71  
72 # Puts the parts in this order; a tab that had the focus keeps it.
73 method _reattach (@parts) {
74 my $focused = first { $_->is_focused } $_bar->buttons;
75 $self->SUPER::clear_children;
76 $self->SUPER::add_child(@parts);
77 $focused->focus if defined $focused;
78 return;
79 }
80  
81 # ---------------------------------------------------------------------
82 # Pages
83 # ---------------------------------------------------------------------
84  
85 method add_child :override (@kids) {
86 foreach my $kid (@kids) {
87 die ref($self) . ": a Tabs holds only Term::Fabulous::Widget::Tabs::Page widgets, got " . ( blessed $kid ? ref $kid : describe($kid) )
88 unless blessed $kid && $kid->isa(PAGE_CLASS);
89 die ref($self) . ": the page is part of a Tabs already" if defined $kid->tabs;
90 }
91 foreach my $page (@kids) {
92 my $button = Term::Fabulous::Widget::Tabs::Button->new( title => $page->title, icon => $page->icon, disabled => $page->disabled );
93 push @_pages, $page;
94 $page->_set_tabs($self);
95 $_bar->add_child($button);
96 $_bar->select($button) if $page->_wants_active;
97 }
98 $self->_restyle_pages;
99 $self->_sync;
100 return $self;
101 }
102  
103 method remove_child :override (@kids) {
104 foreach my $kid (@kids) {
105 die ref($self) . ": remove_child takes pages, got " . describe($kid) . "; remove a page by its id with remove_child_with_id"
106 unless blessed $kid;
107 }
108 my %given = map { refaddr($_) => 1 } @kids;
109 $self->_remove_pages( grep { $given{ refaddr $_ } } @_pages );
110 return $self;
111 }
112  
113 method remove_child_with_id :override ($target_id) {
114 $self->_remove_pages( grep { defined $_->id && $_->id eq $target_id } @_pages );
115 return $self;
116 }
117  
118 method remove_children_with :override ($predicate) {
119 $self->_remove_pages( grep { $predicate->($_) } @_pages );
120 return $self;
121 }
122  
123 method clear_children :override () {
124 $self->_remove_pages(@_pages);
125 return $self;
126 }
127  
128 # Takes pages and their tabs out; the bar picks a new active tab when
129 # the active one leaves, and _sync shows its page.
130 method _remove_pages (@gone) {
131 foreach my $page (@gone) {
132 my $index = $self->index_of($page);
133 my $button = $_bar->button($index);
134 splice @_pages, $index, 1;
135 $page->_set_tabs(undef);
136 $_bar->remove_child($button);
137 }
138 $self->_sync;
139 return;
140 }
141  
142 method pages () {
143 return @_pages;
144 }
145  
146 method page ($index) {
147 die ref($self) . ": page needs an index in 0.." . $#_pages . ", got " . describe($index) unless defined $index && !ref $index && $index =~ /\A[0-9]+\z/ && $index < @_pages;
148 return $_pages[$index];
149 }
150  
151 # A page or its index: the page.
152 method _page_of ($which) {
153 return $self->page($which) unless blessed $which;
154 die ref($self) . ": not a page of this Tabs" unless defined $self->index_of($which);
155 return $which;
156 }
157  
158 method index_of ($page) {
159 return first { refaddr( $_pages[$_] ) == refaddr($page) } 0 .. $#_pages;
160 }
161  
162 method _button_for ($page) {
163 return $_bar->button( $self->index_of($page) );
164 }
165  
166 method active () {
167 my $index = $_bar->active_index // return undef;
168 return $_pages[$index];
169 }
170  
171 method active_index () {
172 return $_bar->active_index;
173 }
174  
175 method bar () {
176 return $_bar;
177 }
178  
179 # Shows a page from the program; undef shows none. Fires nothing.
180 method select ($which) {
181 $_bar->select( defined $which ? $self->_button_for( $self->_page_of($which) ) : undef );
182 return $self;
183 }
184  
185 # Chooses a page as the user does: fires Select when the active page
186 # changes. A disabled page changes nothing.
187 method choose ($which) {
188 $_bar->choose( $self->_button_for( $self->_page_of($which) ) );
189 return $self;
190 }
191  
192 # The bar's Select names its tab; the Tabs fires its own with the page
193 # and keeps the bar's from bubbling on.
194 method _tab_chosen ($event) {
195 $self->_sync;
196 $self->fire_event( Term::Fabulous::Event::Select->new( item => $_pages[ $event->index ], index => $event->index ) );
197 return;
198 }
199  
200 # Puts the page of the active tab into the slot, in place of whatever
201 # is there. Called by the bar when its active tab changes.
202 method _sync () {
203 my $page = $self->active;
204 my ($shown) = @{ $_slot->children };
205 return if defined $page ? defined $shown && refaddr($shown) == refaddr($page) : !defined $shown;
206 $_slot->clear_children if defined $shown;
207 $_slot->add_child($page) if defined $page;
208 return;
209 }
210  
211 # Who takes the focus when the focus inside the Tabs is lost, for
212 # example with the page that held it (Clay::UI::Interaction's focus
213 # heirs): the active tab, so Ctrl+PageUp and Ctrl+PageDown keep
214 # working.
215 method focus_heir () {
216 return $_bar->active;
217 }
218  
219 # A page changed its title, icon or disabled flag: its tab follows.
220 method _page_changed ($page) {
221 my $button = $self->_button_for($page);
222 $button->title( $page->title );
223 $button->icon( $page->icon );
224 $button->disabled( $page->disabled );
225 $_bar->_restyle;
226 return;
227 }
228  
229 # ---------------------------------------------------------------------
230 # Look: the bar's, and the border of the pages
231 # ---------------------------------------------------------------------
232  
233 # A page has a border around it, or none, as page_border says; its
234 # style and color follow the bar (see
235 # Term::Fabulous::Widget::Tabs::Page).
236 method _restyle_pages () {
237 my $on = $_bar->page_border ? 1 : 0;
238 foreach my $page (@_pages) {
239 $page->bordered($on) unless $page->has_look_override('bordered') && $page->bordered->{top} == $on;
240 }
241 return;
242 }
243  
244 method _forward ( $name, $arranges, $restyles_pages, @new ) {
245 my $value = $_bar->$name(@new);
246 return $value unless @new;
247 $self->_arrange if $arranges;
248 $self->_restyle_pages if $restyles_pages;
249 return $value;
250 }
251  
252 method side (@new) { return $self->_forward( side => 1, 1, @new ) }
253 method orientation (@new) { return $self->_forward( orientation => 0, 0, @new ) }
254 method tab_alignment (@new) { return $self->_forward( tab_alignment => 0, 0, @new ) }
255 method tab_gap (@new) { return $self->_forward( tab_gap => 0, 0, @new ) }
256 method tab_margin (@new) { return $self->_forward( tab_margin => 0, 0, @new ) }
257 method tab_padding (@new) { return $self->_forward( tab_padding => 0, 0, @new ) }
258 method active_padding (@new) { return $self->_forward( active_padding => 0, 0, @new ) }
259 method active_bold (@new) { return $self->_forward( active_bold => 0, 0, @new ) }
260 method page_border (@new) { return $self->_forward( page_border => 0, 1, @new ) }
261  
262 # The looks live on the bar (Term::Fabulous::Role::Themed): the bar
263 # takes them from the constructor, and reset_look returns them to the
264 # theme there.
265 method forwarded_looks :common () {
266 return ( bar => [ 'Term::Fabulous::Widget::Tabs::Bar', @BAR_LOOKS ] );
267 }
268  
269 method line_style (@new) { return @new ? $self->set_look( line_style => $new[0] ) : $_bar->line_style }
270 method line_color (@new) { return @new ? $self->set_look( line_color => $new[0] ) : $self->look_value('line_color') }
271 method text_color (@new) { return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color') }
272 method active_text_color (@new) { return @new ? $self->set_look( active_text_color => $new[0] ) : $self->look_value('active_text_color') }
273 method hover_background_color (@new) { return @new ? $self->set_look( hover_background_color => $new[0] ) : $self->look_value('hover_background_color') }
274 method focus_border_color (@new) { return @new ? $self->set_look( focus_border_color => $new[0] ) : $self->look_value('focus_border_color') }
275 method disabled_color (@new) { return @new ? $self->set_look( disabled_color => $new[0] ) : $self->look_value('disabled_color') }
276  
277 method layout_properties :common () {
278 return (
279 $class->SUPER::layout_properties,
280 ( map { $_ => 'scalar' } qw(side orientation tab_alignment tab_gap tab_margin tab_padding active_padding) ),
281 ( map { $_ => 'boolean' } qw(active_bold page_border) ),
282 );
283 }
284  
285 # ---------------------------------------------------------------------
286 # Keys: Ctrl+PageUp and Ctrl+PageDown turn the pages from anywhere in
287 # the Tabs, as in a browser.
288 # ---------------------------------------------------------------------
289  
290 method _handle_key ($event) {
291 my @enabled = grep { $_pages[$_]->is_enabled } 0 .. $#_pages;
292 my $target = roving_target( \@enabled, $self->active_index, $event->main_key_name, keys => 'pages' ) // return 0;
293 $self->choose($target);
294 return 1;
295 }
296  
297 # ---------------------------------------------------------------------
298 # Searching (Term::Fabulous::Role::SearchContainer): the texts of every
299 # page, shown or not; revealing one activates its page
300 # ---------------------------------------------------------------------
301  
302 method searchable_parts () {
303 return map { searchables_in($_) } @_pages;
304 }
305  
306 method reveal_part ($part) {
307 my $page = first { $_->contains($part) } @_pages;
308 $self->select($page) if defined $page;
309 return;
310 }
311 }
312  
313 1;
314  
315 __END__