blib/lib/Term/Fabulous/ThemeEditor/SettingsList.pm untested

filter: Statementstmt 0.0% 0 / 348 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 149 MC/DCmcdc 0.0% 0 / 84 Subroutinesub 0.0% 0 / 25 Podpod n/a totaltotal 0.0% 0 / 750 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::ThemeEditor::SettingsList;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.03';
9  
10 use Object::Pad 0.825;
11  
12 use Clay::UI::Role::Interaction::Focusable;
13 use Term::Fabulous::Widget::Box;
14 use Term::Fabulous::Widget::Display;
15  
16 # The table of the form mode: rows of settings in sections that open and
17 # close, with a swatch, the setting, its value and where the value comes
18 # from. Only the rows near the viewport are widgets (it is a
19 # Term::Fabulous::Widget::VirtualList inside), so it stays fast with
20 # every section open.
21 class Term::Fabulous::ThemeEditor::SettingsList
22 :isa(Term::Fabulous::Widget::Box)
23 :does(Clay::UI::Role::Interaction::Focusable)
24 :strict(params)
25 {
26 use Clay::UI::Enum::Result;
27 use Clay::XS qw(sizing_fixed sizing_grow CLAY_TOP_TO_BOTTOM);
28 use List::Util qw(any first max min);
29 use Scalar::Util qw(weaken);
30 use Term::Fabulous::Widget::VirtualList;
31 use Term::Fabulous::Unicode qw(string_columns);
32 use Time::HiRes ();
33  
34 use constant DOUBLE_CLICK_SECONDS => 0.4;
35 use constant SWATCH_COLUMNS => 4;
36 use constant ORIGIN_COLUMNS => 8;
37  
38 field $on_activate :param; # code: Enter or a double click on a setting row ( $id )
39 field $on_cursor :param; # code: the cursor moved ( $id )
40  
41 field @tree; # the rows: hashes, sections with children
42 field %row; # id => row
43 field %parent; # id => the id of its section
44 field %expanded;
45 field @shown; # the rows the list shows now, in order, each { row, depth }
46 field %index_of; # id => its index in @shown
47 field $search = '';
48 field %filter;
49 field $cursor;
50 field $hover;
51 field $value_columns = 10;
52 field $header;
53 field $body;
54 field @last_click; # [ time, index ]
55  
56 ADJUST {
57 my $id = $self->id // die "Term::Fabulous::ThemeEditor::SettingsList: an id is required";
58 $self->layout( { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow(), height => sizing_grow() } } );
59 weaken( my $weak_self = $self );
60 $header = Term::Fabulous::ThemeEditor::SettingsList::Row->new( list => $self, index => -1, layout => { sizing => { width => sizing_grow(), height => sizing_fixed(1) } } );
61 $body = Term::Fabulous::Widget::VirtualList->new(
62 id => "$id/rows",
63 build => sub ($index) {
64 Term::Fabulous::ThemeEditor::SettingsList::Row->new( list => $weak_self, index => $index, layout => { sizing => { width => sizing_grow(), height => sizing_fixed(1) } } );
65 },
66 layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow(), height => sizing_grow() } },
67 );
68 $self->add_child( $header, $body );
69 $self->on( KeyPress => sub ($event) { return $weak_self->_key($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE } );
70 }
71  
72 # ---------------------------------------------------------------------
73 # Rows
74 # ---------------------------------------------------------------------
75  
76 # Replaces every row: a list of hashes with an id, the shown texts
77 # (setting, value, origin, swatch), a color, and for a section
78 # `section => 1` and its rows under `children`. Every section starts
79 # closed; the cursor stays on its row when the row is still there.
80 method rows ($rows) {
81 @tree = @$rows;
82 %row = %parent = %expanded = ();
83 my @queue = map { [ $_, undef ] } @tree;
84 while ( my $entry = shift @queue ) {
85 my ( $item, $above ) = @$entry;
86 $row{ $item->{id} } = $item;
87 $parent{ $item->{id} } = $above;
88 push @queue, map { [ $_, $item->{id} ] } @{ $item->{children} // [] };
89 }
90 $cursor = undef unless defined $cursor && exists $row{$cursor};
91 $self->_reshape;
92 return $self;
93 }
94  
95 method has_row ($row_id) {
96 return defined $row_id && exists $row{$row_id} ? 1 : 0;
97 }
98  
99 method row ($row_id) {
100 return $row{$row_id};
101 }
102  
103 method parent_of ($row_id) {
104 return $parent{$row_id};
105 }
106  
107 method children_of ($row_id) {
108 my $item = $row{$row_id} // return ();
109 return map { $_->{id} } @{ $item->{children} // [] };
110 }
111  
112 method row_ids () {
113 return keys %row;
114 }
115  
116 # Changes texts of a row; the row paints again by itself.
117 method update_row ( $row_id, $changes ) {
118 my $item = $row{$row_id} // die "Term::Fabulous::ThemeEditor::SettingsList: no row '$row_id'";
119 @{$item}{ keys %$changes } = values %$changes;
120 return $self;
121 }
122  
123 # Shows the rows again after changes with update_row: a search or a
124 # filter may take other rows now, and the values may be wider.
125 method refresh () {
126 $self->_reshape;
127 return $self;
128 }
129  
130 # ---------------------------------------------------------------------
131 # Sections
132 # ---------------------------------------------------------------------
133  
134 method is_expanded ($row_id) {
135 return $expanded{$row_id} ? 1 : 0;
136 }
137  
138 method expand (@ids) {
139 $expanded{$_} = 1 foreach grep { $row{$_} && $row{$_}{section} } @ids;
140 $self->_reshape;
141 return $self;
142 }
143  
144 method collapse (@ids) {
145 delete @expanded{@ids};
146 $self->_reshape;
147 return $self;
148 }
149  
150 method expand_all () {
151 return $self->expand( grep { $row{$_}{section} } keys %row );
152 }
153  
154 method collapse_all () {
155 %expanded = ();
156 $self->_reshape;
157 return $self;
158 }
159  
160 method toggle ($row_id) {
161 return $self->is_expanded($row_id) ? $self->collapse($row_id) : $self->expand($row_id);
162 }
163  
164 # ---------------------------------------------------------------------
165 # Search and filters
166 # ---------------------------------------------------------------------
167  
168 # Accessor for the search text: rows whose setting, value or origin
169 # holds it (in any case) are shown with the sections above them.
170 method search (@new) {
171 return $search unless @new;
172 $search = $new[0] // '';
173 $self->_reshape;
174 return $self;
175 }
176  
177 # A named filter: code that gets a row and returns true to show it, or
178 # undef to remove it.
179 method filter ( $name, $code ) {
180 defined $code ? ( $filter{$name} = $code ) : delete $filter{$name};
181 $self->_reshape;
182 return $self;
183 }
184  
185 method _matches ($item) {
186 return 0 if $item->{section};
187 return 0 if any { !$_->($item) } values %filter;
188 return 1 unless length $search;
189 my $wanted = fc $search;
190 return ( any { index( fc( $_ // '' ), $wanted ) >= 0 } @{$item}{qw(setting value origin)}, $item->{id} ) ? 1 : 0;
191 }
192  
193 # The ids of the rows a search or a filter leaves, in order, sections
194 # included; every row without either.
195 method filtered_row_ids () {
196 return map { $_->{row}{id} } @shown;
197 }
198  
199 # Works out the rows shown: a section's rows while it is open; with a
200 # search or a filter, the rows that match and their sections, open.
201 method _reshape () {
202 my $narrowed = length $search || %filter;
203 @shown = ();
204 my $walk;
205 $walk = sub ( $items, $depth ) {
206 my @out;
207 foreach my $item (@$items) {
208 if ( !$item->{section} ) {
209 push @out, { row => $item, depth => $depth } if !$narrowed || $self->_matches($item);
210 next;
211 }
212 my @inside = $walk->( $item->{children} // [], $depth + 1 );
213 next if $narrowed && !@inside;
214 push @out, { row => $item, depth => $depth };
215 push @out, @inside if $narrowed || $expanded{ $item->{id} };
216 }
217 return @out;
218 };
219 @shown = $walk->( \@tree, 0 );
220 undef $walk;
221 %index_of = map { $shown[$_]{row}{id} => $_ } 0 .. $#shown;
222 $body->count( scalar @shown );
223 $self->_measure_values;
224 my $before = $cursor // '';
225 $cursor = @shown ? $shown[0]{row}{id} : undef if !defined $cursor || !exists $index_of{$cursor};
226 $self->mark_changed;
227 $on_cursor->($cursor) if defined $cursor && $cursor ne $before;
228 return;
229 }
230  
231 # The widest value of all rows, so that the columns stay where they are
232 # whatever a search or a section shows.
233 method _measure_values () {
234 $value_columns = max( 5, min( 24, max( 0, map { string_columns( $_->{value} // '' ) } values %row ) ) + 1 );
235 return;
236 }
237  
238 # ---------------------------------------------------------------------
239 # The cursor
240 # ---------------------------------------------------------------------
241  
242 # Accessor for the row under the cursor; setting one that a closed
243 # section hides opens it, one a search or filter hides dies.
244 method cursor (@new) {
245 return $cursor unless @new;
246 my $row_id = $new[0];
247 die "Term::Fabulous::ThemeEditor::SettingsList: no row '$row_id'" unless $self->has_row($row_id);
248 my @closed;
249 for ( my $above = $parent{$row_id}; defined $above; $above = $parent{$above} ) {
250 push @closed, $above unless $expanded{$above};
251 }
252 $self->expand(@closed) if @closed;
253 die "Term::Fabulous::ThemeEditor::SettingsList: the row '$row_id' is not shown (a search or filter hides it)" unless exists $index_of{$row_id};
254 $self->_move_to( $index_of{$row_id} );
255 return $self;
256 }
257  
258 method shown_count () {
259 return scalar @shown;
260 }
261  
262 method shown_row ($index) {
263 return $index >= 0 && $index < @shown ? $shown[$index] : undef;
264 }
265  
266 method cursor_index () {
267 return defined $cursor ? $index_of{$cursor} : undef;
268 }
269  
270 method hover_index () {
271 return $hover;
272 }
273  
274 method value_columns () {
275 return $value_columns;
276 }
277  
278 method _move_to ($index) {
279 return unless @shown;
280 $index = max( 0, min( $#shown, $index ) );
281 my $old = $cursor // '';
282 $cursor = $shown[$index]{row}{id};
283 $self->_reveal($index);
284 $self->mark_changed;
285 $on_cursor->($cursor) if $cursor ne $old;
286 return;
287 }
288  
289 # Scrolls so that the row at an index is inside the viewport.
290 method _reveal ($index) {
291 my $visible = $body->visible_items;
292 my $rows = $self->_viewport_rows;
293 if ( !defined $visible ) {
294 $body->scroll_to_item( max( 0, $index - $rows + 1 ) ) if $index >= $rows;
295 return;
296 }
297 return $body->scroll_to_item($index) if $index < $visible->{first};
298 return $body->scroll_to_item( max( 0, $index - $rows + 1 ) ) if $index > $visible->{first} + $rows - 1;
299 return;
300 }
301  
302 method _viewport_rows () {
303 my $ui = $self->ui // return 10;
304 my $box = $ui->bounding_box($body) // return 10;
305 return max( 1, int( $box->{height} + 0.5 ) );
306 }
307  
308 # ---------------------------------------------------------------------
309 # Keys and mouse
310 # ---------------------------------------------------------------------
311  
312 method _key ($event) {
313 return 0 unless $self->is_focused;
314 my $name = $event->main_key_name // return 0;
315 my $index = $self->cursor_index // 0;
316 my $page = max( 1, $self->_viewport_rows - 1 );
317 my %move = ( Up => $index - 1, Down => $index + 1, PageUp => $index - $page, PageDown => $index + $page, Home => 0, End => $#shown, 'Ctrl+Home' => 0, 'Ctrl+End' => $#shown );
318 if ( exists $move{$name} ) {
319 $self->_move_to( $move{$name} );
320 return 1;
321 }
322 my $item = defined $cursor ? $row{$cursor} : undef;
323 return 0 unless defined $item;
324 my $text = $event->text // '';
325 if ( $name eq 'Right' || $text eq '+' ) {
326 return 1 unless $item->{section};
327 if ( !$self->is_expanded($cursor) && !length $search && !%filter ) {
328 $self->expand($cursor);
329 return 1;
330 }
331 $self->_move_to( $index + 1 ) if $name eq 'Right';
332 return 1;
333 }
334 if ( $name eq 'Left' || $text eq '-' ) {
335 if ( $item->{section} && $self->is_expanded($cursor) && !length $search && !%filter ) {
336 $self->collapse($cursor);
337 return 1;
338 }
339 $self->_move_to( $index_of{ $parent{$cursor} } ) if defined $parent{$cursor} && exists $index_of{ $parent{$cursor} };
340 return 1;
341 }
342 if ( $name eq 'Enter' || $name eq 'Space' ) {
343 $self->activate($cursor);
344 return 1;
345 }
346 return 0;
347 }
348  
349 # A section opens or closes; a setting is edited.
350 method activate ($row_id) {
351 my $item = $row{$row_id} // return;
352 return $self->toggle($row_id) if $item->{section};
353 $on_activate->($row_id);
354 return;
355 }
356  
357 # A click puts the cursor on a row; on a section's marker it opens or
358 # closes the section, and a second click on the same row activates it.
359 method click ( $index, $column ) {
360 return unless defined $self->shown_row($index);
361 my $entry = $self->shown_row($index);
362 $self->_move_to($index);
363 my $now = Time::HiRes::time();
364 if ( $entry->{row}{section} && $column >= SWATCH_COLUMNS + 2 * $entry->{depth} && $column < SWATCH_COLUMNS + 2 * $entry->{depth} + 2 ) {
365 @last_click = ();
366 $self->toggle( $entry->{row}{id} );
367 return;
368 }
369 if ( @last_click && $last_click[1] == $index && $now - $last_click[0] <= DOUBLE_CLICK_SECONDS ) {
370 @last_click = ();
371 $self->activate( $entry->{row}{id} );
372 return;
373 }
374 @last_click = ( $now, $index );
375 return;
376 }
377  
378 method hover ($index) {
379 return if ( $hover // -2 ) == ( $index // -2 );
380 $hover = $index;
381 $self->mark_changed;
382 return;
383 }
384 }
385  
386 # One row of the list (index -1: the titles), painted from the list's
387 # data: the swatch, the setting with its indentation and marker, the
388 # value, where it comes from.
389 class Term::Fabulous::ThemeEditor::SettingsList::Row :isa(Term::Fabulous::Widget::Display) :strict(params) {
390 use Clay::UI::Enum::Result;
391 use List::Util qw(max min);
392 use Scalar::Util qw(weaken);
393 use Term::Fabulous::Termbox qw(TB_BOLD TB_KEY_MOUSE_LEFT TB_MOD_MOTION);
394 use Term::Fabulous::Unicode qw(string_columns);
395  
396 use constant OPEN => "\x{25BE} ";
397 use constant CLOSED => "\x{25B8} ";
398  
399 field $list :param :weak;
400 field $index :param;
401  
402 ADJUST {
403 weaken( my $weak_self = $self );
404 $self->on( Mouse => sub ($event) { $weak_self->_mouse($event); return Clay::UI::Enum::Result->CONTINUE } );
405 $self->on( MouseMove => sub ($event) { $weak_self->_moved; return Clay::UI::Enum::Result->CONTINUE } );
406 $self->on( OnHoverStopped => sub ($event) { $weak_self->_left; return Clay::UI::Enum::Result->CONTINUE } );
407 }
408  
409 method natural_size () {
410 return ( 0, 1 );
411 }
412  
413 # A text cut to a number of columns, a ~ marking the cut.
414 sub _fit ( $text, $columns ) {
415 return $text if string_columns($text) <= $columns;
416 return '' if $columns < 1;
417 my $cut = $text;
418 chop $cut while length $cut && string_columns($cut) > $columns - 1;
419 return "$cut~";
420 }
421  
422 method _mouse ($event) {
423 return if $index < 0 || !defined $list;
424 return unless $event->key == TB_KEY_MOUSE_LEFT && !( $event->modifiers & TB_MOD_MOTION );
425 my ($column) = $self->cell_at($event);
426 $list->click( $index, $column // 0 );
427 return;
428 }
429  
430 method _moved () {
431 $list->hover( $index >= 0 ? $index : undef ) if defined $list;
432 return;
433 }
434  
435 method _left () {
436 $list->hover(undef) if defined $list && ( $list->hover_index // -1 ) == $index;
437 return;
438 }
439  
440 method paint_key :override () {
441 return $self->SUPER::paint_key unless defined $list;
442 return ( $self->SUPER::paint_key, $list->value_columns, $list->is_focused ) if $index < 0;
443 my $entry = $list->shown_row($index) // return ( $self->SUPER::paint_key, 'none' );
444 my $row = $entry->{row};
445 return (
446 $self->SUPER::paint_key, $list->value_columns, $entry->{depth}, $list->is_focused,
447 ( $list->cursor_index // -1 ) == $index, ( $list->hover_index // -1 ) == $index,
448 $list->is_expanded( $row->{id} ),
449 map { ref $_ ? join( ',', @$_ ) : $_ // '' } @{$row}{qw(setting value origin swatch color set section)}
450 );
451 }
452  
453 method _look ( $slot, $state = 'normal' ) {
454 return $self->color_attr( $self->family_look( table => $slot, $state ) );
455 }
456  
457 method paint () {
458 return unless defined $list;
459 my $width = $self->columns;
460 my $origin = Term::Fabulous::ThemeEditor::SettingsList::ORIGIN_COLUMNS;
461 my $swatch = Term::Fabulous::ThemeEditor::SettingsList::SWATCH_COLUMNS;
462 my $values = min( $list->value_columns, max( 6, int( ( $width - $swatch - $origin ) * 0.4 ) ) );
463 my $label = max( 1, $width - $swatch - $values - $origin );
464 if ( $index < 0 ) {
465 my ( $fg, $bg ) = ( $self->_look('header.text') | TB_BOLD, $self->_look('header.background') );
466 $self->fill_attrs( 0, 0, $width, ' ', $fg, $bg );
467 $self->paint_text( $swatch, 0, 'Setting', $fg, $bg );
468 $self->paint_text( $swatch + $label, 0, 'Value', $fg, $bg );
469 $self->paint_text( $swatch + $label + $values, 0, 'From', $fg, $bg );
470 return;
471 }
472 my $entry = $list->shown_row($index) // return;
473 my $row = $entry->{row};
474 my $is_cursor = ( $list->cursor_index // -1 ) == $index;
475 my $bg
476 = $is_cursor && $list->is_focused ? $self->_look('cursor')
477 : $is_cursor ? $self->_look('selected')
478 : ( $list->hover_index // -1 ) == $index ? $self->_look('hover')
479 : $row->{section} ? $self->_look('group.background')
480 : undef;
481 my $fg = $row->{section} ? $self->_look('group.text') | TB_BOLD : $row->{set} ? $self->_look('text') | TB_BOLD : $self->_look('muted');
482 $self->fill_attrs( 0, 0, $width, ' ', $fg, $bg );
483 if ( defined $row->{color} ) {
484 $self->fill_attrs( 0, 0, $swatch - 1, ' ', $fg, $self->color_attr( $row->{color} ) );
485 }
486 elsif ( length( $row->{swatch} // '' ) ) {
487 $self->paint_text( 0, 0, $row->{swatch}, $self->_look('text'), $bg, $swatch - 1 );
488 }
489 my $indent = ' ' x $entry->{depth};
490 my $marker = !$row->{section} ? ' ' : ( $list->is_expanded( $row->{id} ) || length $list->search ) ? OPEN : CLOSED;
491 $self->paint_text( $swatch, 0, _fit( $indent . $marker . $row->{setting}, $label - 1 ), $fg, $bg );
492 $self->paint_text( $swatch + $label, 0, _fit( $row->{value} // '', $values - 1 ), $row->{section} ? $self->_look('muted') : $fg, $bg );
493 $self->paint_text( $swatch + $label + $values, 0, $row->{origin} // '', $row->{set} ? $self->_look('group.text') : $self->_look('muted'), $bg );
494 return;
495 }
496 }
497  
498 1;
499  
500 __END__