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

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