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

filter: Statementstmt 0.0% 0 / 224 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 23 Podpod n/a totaltotal 0.0% 0 / 372 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::DocumentTabs;
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::DocumentTabs
15 :isa(Term::Fabulous::Widget::Display)
16 :strict(params)
17 {
18 use Clay::UI::Enum::Result;
19 use Clay::XS qw(sizing_grow);
20 use List::Util qw(sum0);
21 use Term::Fabulous::Check qw(boolean describe non_negative_integer optional string);
22 use Term::Fabulous::Event::Select;
23 use Term::Fabulous::Event::TabAdd;
24 use Term::Fabulous::Event::TabClose;
25 use Term::Fabulous::Termbox qw(TB_BOLD TB_KEY_MOUSE_LEFT TB_KEY_MOUSE_MIDDLE TB_MOD_MOTION);
26 use Term::Fabulous::Unicode qw(fit_columns string_columns);
27  
28 use constant MODIFIED => "\x{25CF}"; # a dot: unsaved changes
29 use constant ERROR => '!';
30 use constant CLOSE => "\x{00D7}";
31 use constant NEW => ' + ';
32 use constant MORE_LEFT => "\x{25C2}";
33 use constant MORE_RIGHT => "\x{25B8}";
34  
35 # The columns of a tab besides its title: " title mark " and, when it
36 # can be closed, "x ".
37 use constant FIXED_COLUMNS => 4;
38 use constant CLOSE_COLUMNS => 2;
39  
40 my $CONTINUE = Clay::UI::Enum::Result->CONTINUE;
41 my $HANDLED = Clay::UI::Enum::Result->HANDLED;
42  
43 field $active :param = undef;
44 field $new_button :param = 1;
45  
46 field $_tabs; # [ checked tab, ... ], or a code reference that returns tabs
47 field @_shown_tabs; # the tabs the next paint draws, read by paint_key
48 field @_regions; # what the last paint put where: [ from, to, action, index ]
49  
50 ADJUST :params ( :$tabs = [] ) {
51 $_tabs = $self->_checked_tabs($tabs);
52 $active = $self->_checked_active($active);
53 $new_button = boolean( $self, new_button => $new_button );
54 $self->on( Mouse => sub ( $self, $event ) { return $self->_on_mouse($event) }, owner => $self );
55 }
56  
57 method theme_family :common () {
58 return 'document_tabs';
59 }
60  
61 method themed_params :common () {
62 return (
63 $class->SUPER::themed_params,
64 active_background_color => [ 'background', 'active', 'cell_color' ],
65 text_color => [ 'text', 'normal', 'cell_color' ],
66 active_text_color => [ 'text', 'active', 'cell_color' ],
67 modified_color => [ 'modified', 'normal', 'cell_color' ],
68 error_color => [ 'error', 'normal', 'cell_color' ],
69 button_color => [ 'button', 'normal', 'cell_color' ],
70 );
71 }
72  
73 # ---------------------------------------------------------------------
74 # The tabs
75 # ---------------------------------------------------------------------
76  
77 # The tabs as given to new or tabs: an array reference of tabs, or a
78 # code reference that returns them whenever a frame is drawn.
79 method _checked_tabs ($tabs) {
80 return $tabs if ref $tabs eq 'CODE';
81 die ref($self) . ": tabs must be an array reference or a code reference, got " . describe($tabs) unless ref $tabs eq 'ARRAY';
82 return [ map { $self->_checked_tab($_) } @$tabs ];
83 }
84  
85 # A tab is a hash with a title; its flags become 0 or 1, and keys of
86 # the program's own stay.
87 method _checked_tab ($tab) {
88 die ref($self) . ": a tab must be a hash reference with a title, got " . describe($tab) unless ref $tab eq 'HASH';
89 return {
90 %$tab,
91 title => _one_row( string( $self, 'the title of a tab' => $tab->{title} ) ),
92 modified => boolean( $self, 'modified' => $tab->{modified} ),
93 error => boolean( $self, 'error' => $tab->{error} ),
94 closable => boolean( $self, 'closable' => $tab->{closable} // 1 ),
95 };
96 }
97  
98 method _checked_active ($index) {
99 return optional( \&non_negative_integer, $self, active => $index );
100 }
101  
102 # A tab is one row: line breaks and runs of white space become one
103 # space, and white space at the ends goes.
104 sub _one_row ($text) {
105 return $text =~ s/\s+/ /gr =~ s/\A | \z//gr;
106 }
107  
108 method tabs (@new) {
109 return ref $_tabs eq 'CODE' ? $_tabs : [
110 map {
111 { %$_ }
112 } @$_tabs
113 ]
114 unless @new;
115 $_tabs = $self->_checked_tabs( $new[0] );
116 $self->mark_changed;
117 return $self->tabs;
118 }
119  
120 # The tabs to draw now, checked: a code reference is called.
121 method _current_tabs () {
122 return map {
123 { %$_ }
124 } @$_tabs unless ref $_tabs eq 'CODE';
125 return map { $self->_checked_tab($_) } $_tabs->();
126 }
127  
128 method tab ($index) {
129 $index = non_negative_integer( $self, index => $index );
130 my @tabs = $self->_current_tabs;
131 return $tabs[$index];
132 }
133  
134 method count () {
135 return scalar( my @tabs = $self->_current_tabs );
136 }
137  
138 method active (@new) {
139 return $active unless @new;
140 $active = $self->_checked_active( $new[0] );
141 $self->mark_changed;
142 return $active;
143 }
144  
145 method new_button (@new) {
146 return $new_button unless @new;
147 $new_button = boolean( $self, new_button => $new[0] );
148 $self->mark_changed;
149 return $new_button;
150 }
151  
152 # ---------------------------------------------------------------------
153 # Colors
154 # ---------------------------------------------------------------------
155  
156 method active_background_color (@new) { return @new ? $self->set_look( active_background_color => $new[0] ) : $self->look_value('active_background_color') }
157 method text_color (@new) { return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color') }
158 method active_text_color (@new) { return @new ? $self->set_look( active_text_color => $new[0] ) : $self->look_value('active_text_color') }
159 method modified_color (@new) { return @new ? $self->set_look( modified_color => $new[0] ) : $self->look_value('modified_color') }
160 method error_color (@new) { return @new ? $self->set_look( error_color => $new[0] ) : $self->look_value('error_color') }
161 method button_color (@new) { return @new ? $self->set_look( button_color => $new[0] ) : $self->look_value('button_color') }
162  
163 # The termbox2 attributes paint draws with.
164 method _attrs () {
165 return {
166 background => $self->color_attr( $self->background_color ),
167 active_background => $self->color_attr( $self->active_background_color ),
168 text => $self->color_attr( $self->text_color ),
169 active_text => $self->color_attr( $self->active_text_color ) | TB_BOLD,
170 modified => $self->color_attr( $self->modified_color ),
171 error => $self->color_attr( $self->error_color ),
172 button => $self->color_attr( $self->button_color ),
173 };
174 }
175  
176 # ---------------------------------------------------------------------
177 # Painting
178 # ---------------------------------------------------------------------
179  
180 method natural_size () {
181 return ( sizing_grow(), 1 );
182 }
183  
184 # The tabs are read here, once per frame, and painted from what was
185 # read.
186 method paint_key :override () {
187 @_shown_tabs = $self->_current_tabs;
188 return ( $self->SUPER::paint_key, $active, $new_button, map { @{$_}{qw(title modified error closable)} } @_shown_tabs );
189 }
190  
191 # The tab paint shows as active: none when active is undef or past the
192 # end.
193 method _shown_active () {
194 return defined $active && $active <= $#_shown_tabs ? $active : undef;
195 }
196  
197 # The tabs from the left, scrolled so that the active one fits, with an
198 # arrow at an end that has hidden tabs and the + button after them.
199 # Every tab ends a column before the + button, which leaves room for
200 # the arrow.
201 method paint () {
202 my $width = $self->columns;
203 my $attrs = $self->_attrs;
204 my $tabs_end = $width - 1 - ( $new_button ? length NEW : 0 );
205 my $room = $tabs_end - 1;
206 my @titles = map { fit_columns( $_->{title}, $room - _columns_besides_title($_) ) } @_shown_tabs;
207 my @labels = map { _label( $_shown_tabs[$_], $titles[$_] ) } 0 .. $#_shown_tabs;
208 my @widths = map { string_columns($_) } @labels;
209 my $shown_active = $self->_shown_active;
210 my $first = _first_shown( \@widths, $shown_active // 0, $room );
211 @_regions = ();
212 $self->fill_attrs( 0, 0, $width, ' ', $attrs->{text}, $attrs->{background} );
213  
214 my $x = 0;
215 if ( $first > 0 ) {
216 $self->put_attrs( 0, 0, MORE_LEFT, $attrs->{button}, $attrs->{background} );
217 push @_regions, [ 0, 0, 'arrow', $first - 1 ];
218 $x = 1;
219 }
220 foreach my $index ( $first .. $#_shown_tabs ) {
221 last if $x + $widths[$index] > $tabs_end;
222 my $is_active = defined $shown_active && $index == $shown_active;
223 $x = $self->_paint_tab( $x, $index, $titles[$index], $labels[$index], $is_active, $attrs );
224 next unless $index < $#_shown_tabs && $x + $widths[ $index + 1 ] > $tabs_end;
225 $self->put_attrs( $x, 0, MORE_RIGHT, $attrs->{button}, $attrs->{background} );
226 push @_regions, [ $x, $x, 'arrow', $index + 1 ];
227 $x++;
228 last;
229 }
230 return unless $new_button;
231 $self->paint_text( $x, 0, NEW, $attrs->{button}, $attrs->{background} );
232 push @_regions, [ $x, $x + length(NEW) - 1, 'new', undef ];
233 return;
234 }
235  
236 # Paints one tab from column $x and records its regions; returns the
237 # column after it.
238 method _paint_tab ( $x, $index, $title, $label, $is_active, $attrs ) {
239 my $tab = $_shown_tabs[$index];
240 my ( $fg, $bg ) = $is_active ? @{$attrs}{qw(active_text active_background)} : @{$attrs}{qw(text background)};
241 $self->paint_text( $x, 0, $label, $fg, $bg );
242 my $mark_x = $x + 1 + string_columns($title) + 1;
243 $self->put_attrs( $mark_x, 0, _mark($tab), $tab->{error} ? $attrs->{error} : $attrs->{modified}, $bg ) if $tab->{error} || $tab->{modified};
244 my $end = $x + string_columns($label) - 1;
245 push @_regions, $tab->{closable} ? ( [ $x, $mark_x + 1, 'tab', $index ], [ $mark_x + 2, $mark_x + 2, 'close', $index ], [ $mark_x + 3, $end, 'tab', $index ] ) : [ $x, $end, 'tab', $index ];
246 return $end + 1;
247 }
248  
249 # The mark after the title: an error wins over unsaved changes.
250 sub _mark ($tab) {
251 return $tab->{error} ? ERROR : $tab->{modified} ? MODIFIED : ' ';
252 }
253  
254 sub _label ( $tab, $title ) {
255 return " $title " . _mark($tab) . ' ' . ( $tab->{closable} ? CLOSE . ' ' : '' );
256 }
257  
258 sub _columns_besides_title ($tab) {
259 return FIXED_COLUMNS + ( $tab->{closable} ? CLOSE_COLUMNS : 0 );
260 }
261  
262 # The first tab shown: the strip scrolls so that the tab at $target
263 # fits, the arrow on the left counted.
264 sub _first_shown ( $widths, $target, $room ) {
265 my $first = 0;
266 $first++ while $first < $target && sum0( @{$widths}[ $first .. $target ] ) + ( $first > 0 ? 1 : 0 ) > $room;
267 return $first;
268 }
269  
270 # ---------------------------------------------------------------------
271 # The mouse
272 # ---------------------------------------------------------------------
273  
274 method _on_mouse ($event) {
275 return $CONTINUE if $event->modifiers & TB_MOD_MOTION;
276 my $key = $event->key;
277 return $CONTINUE unless $key == TB_KEY_MOUSE_LEFT || $key == TB_KEY_MOUSE_MIDDLE;
278 my ($column) = $self->cell_at($event);
279 return $CONTINUE unless defined $column;
280 my ($region) = grep { $column >= $_->[0] && $column <= $_->[1] } @_regions;
281 return $HANDLED unless defined $region;
282  
283 my ( undef, undef, $action, $index ) = @$region;
284 return $self->_middle_click( $action, $index ) if $key == TB_KEY_MOUSE_MIDDLE;
285 return $self->_fire_close($index) if $action eq 'close';
286 return $self->_fire_add if $action eq 'new';
287 return $self->_fire_select($index);
288 }
289  
290 # A middle click closes a tab that can be closed.
291 method _middle_click ( $action, $index ) {
292 return $HANDLED unless ( $action eq 'tab' || $action eq 'close' ) && $_shown_tabs[$index]{closable};
293 return $self->_fire_close($index);
294 }
295  
296 method _fire_select ($index) {
297 $self->fire_event( Term::Fabulous::Event::Select->new( item => { %{ $_shown_tabs[$index] } }, index => $index ) );
298 return $HANDLED;
299 }
300  
301 method _fire_close ($index) {
302 $self->fire_event( Term::Fabulous::Event::TabClose->new( item => { %{ $_shown_tabs[$index] } }, index => $index ) );
303 return $HANDLED;
304 }
305  
306 method _fire_add () {
307 $self->fire_event( Term::Fabulous::Event::TabAdd->new );
308 return $HANDLED;
309 }
310 }
311  
312 1;
313  
314 __END__