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

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