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

filter: Statementstmt 0.0% 0 / 287 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 61 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 18 Podpod n/a totaltotal 0.0% 0 / 476 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::Form;
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 Term::Fabulous::Widget::Box;
13  
14 # The form mode of the theme editor: every setting of a theme in a list,
15 # grouped into sections (the theme node, the palette, one per family, one
16 # per variant), with a search field, a filter for the settings the theme
17 # sets and a line that explains the setting under the cursor.
18 class Term::Fabulous::ThemeEditor::Form :isa(Term::Fabulous::Widget::Box) :strict(params) {
19 use Clay::UI::Enum::Result;
20 use Clay::XS qw(sizing_grow sizing_fixed CLAY_TOP_TO_BOTTOM CLAY_ALIGN_Y_CENTER);
21 use List::Util qw(any first);
22 use Scalar::Util qw(refaddr weaken);
23 use Term::Fabulous::Enum::BorderStyle;
24 use Term::Fabulous::Theme;
25 use Term::Fabulous::Theme::Document;
26 use Term::Fabulous::Widget::Checkbox;
27 use Term::Fabulous::Widget::Text;
28 use Term::Fabulous::ThemeEditor::SettingsList;
29 use Term::Fabulous::Widget::TextField;
30  
31 use constant LIST_ID => 'theme-editor-settings';
32  
33 field $on_activate :param; # code: the user wants to edit a setting (its key)
34 field $on_cursor :param; # code: the cursor moved (the row's id)
35  
36 field $search :reader;
37 field $only_set :reader;
38 field $list :reader;
39 field $banner;
40 field $details;
41 field %shown; # id => the row data last given to the list
42 field $variant_list = '';
43 field $shown_tab; # the address of the tab the rows are of
44 field $read_only = 0;
45  
46 ADJUST {
47 $self->layout( { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow(), height => sizing_grow() } } );
48 $search = Term::Fabulous::Widget::TextField->new( placeholder => 'Search (Ctrl+F)', layout => { sizing => { width => sizing_grow() } } );
49 $only_set = Term::Fabulous::Widget::Checkbox->new( label => 'Only set' );
50 my $tools = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow() }, child_gap => 2, padding => { bottom => 1 } } );
51 $tools->add_child( $search, $only_set );
52  
53 $banner = Term::Fabulous::Widget::Text->new( text => '' );
54 $details = Term::Fabulous::Widget::Text->new( text => '' );
55 my $details_box = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow(), height => sizing_fixed(2) }, padding => { top => 0 } } );
56 $details_box->add_child($details);
57  
58 weaken( my $weak_self = $self ); # for the list's callbacks, which it keeps
59 $list = Term::Fabulous::ThemeEditor::SettingsList->new(
60 id => LIST_ID,
61 on_activate => sub ($id) { $weak_self->_activate($id) },
62 on_cursor => sub ($id) { $weak_self->_cursor_moved($id) },
63 );
64 $self->add_child( $tools, $list, $details_box );
65  
66 $search->on( Change => sub ( $self, $event ) { $self->list->search( $event->value ); return Clay::UI::Enum::Result->CONTINUE }, owner => $self );
67 $search->on( Submit => sub ( $self, $event ) { $self->focus_list; return }, owner => $self );
68 $search->on(
69 KeyPress => sub ( $self, $event ) {
70 return Clay::UI::Enum::Result->CONTINUE unless ( $event->main_key_name // '' ) eq 'Down';
71 $self->focus_list;
72 return Clay::UI::Enum::Result->HANDLED;
73 },
74 owner => $self
75 );
76 $only_set->on( Change => sub ( $self, $event ) { $self->_filter_only_set( $event->value ); return Clay::UI::Enum::Result->CONTINUE }, owner => $self );
77 }
78  
79 method _filter_only_set ($on) {
80 $list->filter( only_set => $on ? sub ($row) { $row->{set} || $row->{has_set} } : undef );
81 return;
82 }
83  
84 method _activate ($id) {
85 return unless defined $id;
86 my $row = $shown{$id} // return;
87 return if $row->{section};
88 $on_activate->( $row->{key} );
89 return;
90 }
91  
92 method _cursor_moved ($id) {
93 $self->_explain($id);
94 $on_cursor->($id);
95 return;
96 }
97  
98 # ---------------------------------------------------------------------
99 # Rows
100 # ---------------------------------------------------------------------
101  
102 # The keys of a family's slots in every state, in the order the form
103 # lists them: slots by name, each in the normal state first.
104 sub slot_keys ( $family, $variant = undef ) {
105 my %states_of = Term::Fabulous::Theme::slots($family);
106 my @keys;
107 foreach my $slot ( sort keys %states_of ) {
108 push @keys, map { Term::Fabulous::Theme::Document::key_for( family => $family, variant => $variant, slot => $slot, state => $_ ) } 'normal', @{ $states_of{$slot} };
109 }
110 return @keys;
111 }
112  
113 # The id of a section row.
114 sub section_id ( $kind, @names ) {
115 return join ':', "#$kind", @names;
116 }
117  
118 # A section row knows what it is: its kind (theme, palette, family or
119 # variant), and the family and variant it is about.
120 method _section_row ( $place, $label, @children ) {
121 my ( $kind, $family, $variant ) = @{$place}{qw(kind family variant)};
122 return {
123 id => section_id( $kind, grep { defined } $family, $variant ),
124 kind => $kind,
125 family => $family,
126 variant => $variant,
127 setting => $label,
128 value => '', origin => '', swatch => '', section => 1, children => \@children,
129 };
130 }
131  
132 method _setting_row ( $key, $label ) {
133 return { id => $key, key => $key, setting => $label, value => '', origin => '', swatch => '' };
134 }
135  
136 # The rows of a document's settings, with the variants it defines and
137 # those the user added without settings yet.
138 method _rows ( $document, @extra_variants ) {
139 my @rows = (
140 $self->_section_row( { kind => 'theme' }, 'Theme', map { $self->_setting_row( "theme.$_", $_ ) } qw(name extends) ),
141 $self->_section_row( { kind => 'palette' }, 'Palette', map { $self->_setting_row( "palette.$_", $_ ) } Term::Fabulous::Theme::tokens() ),
142 );
143 my %variants;
144 push @{ $variants{ $_->[0] } }, $_->[1] foreach $document->variants, @extra_variants;
145 foreach my $family ( Term::Fabulous::Theme::families() ) {
146 my @slots = map { $self->_setting_row( $_, $_ =~ s/\A\Q$family\E\.//r ) } slot_keys($family);
147 my %seen;
148 my @variant_rows = map {
149 my $variant = $_;
150 $self->_section_row(
151 { kind => 'variant', family => $family, variant => $variant }, "variant $variant",
152 map { $self->_setting_row( $_, $_ =~ s/\A\Q$family\E\[\Q$variant\E\]\.//r ) } slot_keys( $family, $variant )
153 )
154 } grep { !$seen{$_}++ } @{ $variants{$family} // [] };
155 push @rows, $self->_section_row( { kind => 'family', family => $family }, $family, @slots, @variant_rows );
156 }
157 return @rows;
158 }
159  
160 # Shows a tab: its rows (rebuilt when the variants changed) with their
161 # values, and the cursor and open sections the form had for it.
162 method show ( $tab, $document, @extra_variants ) {
163 my $state = $tab->form_state;
164 my $variants = join ',', map { "$_->[0]\[$_->[1]]" } $document->variants, @extra_variants;
165 my $other = !defined $shown_tab || $shown_tab != refaddr $tab;
166 if ( $other || $variants ne $variant_list ) {
167 my @expanded = $other ? @{ $state->{expanded} // [ section_id('theme'), section_id('palette') ] } : $self->_expanded;
168 my $cursor = $other ? $state->{cursor} : $list->cursor;
169 $self->_rebuild( $document, @extra_variants );
170 $variant_list = $variants;
171 $shown_tab = refaddr $tab;
172 $list->expand( grep { $list->has_row($_) } @expanded );
173 $self->_place_cursor($cursor) if defined $cursor;
174 }
175 $self->_fill($document);
176 return $self;
177 }
178  
179 method _expanded () {
180 return grep { $list->has_row($_) && $list->is_expanded($_) } map { $_->{id} } grep { $_->{section} } values %shown;
181 }
182  
183 # Remembers the cursor and the open sections for a tab before the form
184 # shows another one.
185 method keep_state ($tab) {
186 return $self unless defined $shown_tab && $shown_tab == refaddr $tab;
187 my $state = $tab->form_state;
188 $state->{cursor} = $list->cursor;
189 $state->{expanded} = [ $self->_expanded ];
190 return $self;
191 }
192  
193 method _rebuild ( $document, @extra_variants ) {
194 my @rows = $self->_rows( $document, @extra_variants );
195 %shown = ();
196 my @queue = @rows;
197 while ( my $row = shift @queue ) {
198 $shown{ $row->{id} } = $row;
199 push @queue, @{ $row->{children} // [] };
200 }
201 $list->rows( \@rows );
202 return;
203 }
204  
205 # The values of every row from a document and its theme; only rows
206 # whose looks changed are given to the list.
207 method _fill ($document) {
208 my $theme = $document->theme;
209 my %has_set;
210 foreach my $id ( sort keys %shown ) {
211 my $row = $shown{$id};
212 next if $row->{section};
213 my %values = $self->_values( $document, $theme, $row->{key} );
214 if ( $values{set} ) {
215 my $section = $list->parent_of($id);
216 while ( defined $section ) {
217 $has_set{$section}++;
218 $section = $list->parent_of($section);
219 }
220 }
221 next unless any { ( $values{$_} // '' ) ne ( $row->{$_} // '' ) } keys %values;
222 $list->update_row( $id, \%values );
223 @{$row}{ keys %values } = values %values;
224 }
225 foreach my $row ( grep { $_->{section} } values %shown ) {
226 my $count = $has_set{ $row->{id} } // 0;
227 my $text = $count ? "$count set" : '';
228 next if $text eq $row->{value} && ( $row->{has_set} // 0 ) == ( $count ? 1 : 0 );
229 $list->update_row( $row->{id}, { value => $text, has_set => $count ? 1 : 0 } );
230 @{$row}{qw(value has_set)} = ( $text, $count ? 1 : 0 );
231 }
232 $list->refresh;
233 return;
234 }
235  
236 # What a row shows for a key: the value, where it comes from, the
237 # swatch (a color, or the glyphs of a border style) and whether the
238 # theme sets it.
239 method _values ( $document, $theme, $key ) {
240 my $effective = $document->effective_setting($key);
241 my $spec = $effective->{spec};
242 my %origin = ( set => 'set', default => 'default', normal => 'normal', family => 'family' );
243 my %values = (
244 value => $spec // '',
245 origin => $origin{ $effective->{origin} } // $effective->{origin},
246 set => $effective->{origin} eq 'set' ? 1 : 0,
247 swatch => '',
248 color => undef,
249 );
250 return %values unless defined $theme;
251 my $parsed = Term::Fabulous::Theme::Document::parse_key($key);
252 if ( $parsed->{section} eq 'palette' ) {
253 $values{color} = $theme->token( $parsed->{token} );
254 return %values;
255 }
256 return %values unless $parsed->{section} eq 'slot';
257 my $look = $theme->look( @{$parsed}{qw(family slot state)}, defined $parsed->{variant} ? [ $parsed->{variant} ] : [] );
258 if ( ref $look eq 'ARRAY' ) {
259 $values{color} = $look;
260 }
261 elsif ( ref $look ) {
262 my ( $corner, $edge, $other ) = $look->get_top_glyphs;
263 $values{swatch} = "$corner$edge$other";
264 }
265 return %values;
266 }
267  
268 # ---------------------------------------------------------------------
269 # Reading and moving
270 # ---------------------------------------------------------------------
271  
272 # The row under the cursor: { id, key or section }, or undef.
273 method current () {
274 my $id = $list->cursor // return undef;
275 return $shown{$id};
276 }
277  
278 method current_key () {
279 my $row = $self->current // return undef;
280 return $row->{key};
281 }
282  
283 # The section a row belongs to (a section row is its own).
284 method section_of ($id) {
285 my $row = $shown{$id} // return undef;
286 return $row->{section} ? $id : $list->parent_of($id);
287 }
288  
289 # What a section is about: { kind, family, variant }, undef for an id
290 # that is no section.
291 method section_info ($id) {
292 my $row = $shown{ $id // '' } // return undef;
293 return undef unless $row->{section};
294 return { map { $_ => $row->{$_} } qw(kind family variant) };
295 }
296  
297 # The keys of the settings in a section (a family's include its
298 # variants').
299 method keys_in ($section) {
300 my @keys;
301 my @queue = $list->children_of($section);
302 while ( defined( my $id = shift @queue ) ) {
303 my $row = $shown{$id};
304 push @keys, $row->{key} if defined $row->{key};
305 push @queue, $list->children_of($id) if $row->{section};
306 }
307 return @keys;
308 }
309  
310 method has_row ($id) {
311 return exists $shown{$id} ? 1 : 0;
312 }
313  
314 # Puts the cursor on a row, opening the sections above it and clearing
315 # a search or filter that hides it.
316 method _place_cursor ($id) {
317 return 0 unless $list->has_row($id);
318 my @path;
319 for ( my $parent = $list->parent_of($id); defined $parent; $parent = $list->parent_of($parent) ) {
320 unshift @path, $parent;
321 }
322 $list->expand(@path) if @path;
323 unless ( grep { $_ eq $id } $list->filtered_row_ids ) {
324 $search->value('');
325 $list->search('');
326 $only_set->checked(0);
327 $self->_filter_only_set(0);
328 }
329 $list->cursor($id);
330 $self->_explain($id);
331 return 1;
332 }
333  
334 # Gives the list the focus, the cursor on the first row a search
335 # left.
336 method focus_list () {
337 my $ui = $self->ui // return $self;
338 my @settings = grep { !$shown{$_}{section} } $list->filtered_row_ids;
339 my $current = $list->cursor // '';
340 $list->cursor( $settings[0] ) if @settings && !grep { $_ eq $current } @settings;
341 $ui->interaction->set_focused_widget($list);
342 $self->_explain( $list->cursor );
343 return $self;
344 }
345  
346 method go_to ($id) {
347 return $self->_place_cursor($id);
348 }
349  
350 # ---------------------------------------------------------------------
351 # Read-only while the text has errors
352 # ---------------------------------------------------------------------
353  
354 method read_only (@new) {
355 return $read_only unless @new;
356 $read_only = $new[0] ? 1 : 0;
357 return $read_only;
358 }
359  
360 method show_banner ($markup) {
361 $banner->markup($markup);
362 $self->insert_children( 1, $banner ) unless $self->has_child($banner);
363 return $self;
364 }
365  
366 method hide_banner () {
367 $self->remove_child($banner);
368 return $self;
369 }
370  
371 # ---------------------------------------------------------------------
372 # The line under the list
373 # ---------------------------------------------------------------------
374  
375 field $explain_with; # code: ( $row ) -> markup of the details line
376  
377 method explain_with ($code) {
378 $explain_with = $code;
379 return $self;
380 }
381  
382 method _explain ($id) {
383 my $row = defined $id ? $shown{$id} : undef;
384 $details->markup( defined $row && defined $explain_with ? $explain_with->($row) : '' );
385 return;
386 }
387  
388 method explain_current () {
389 $self->_explain( $list->cursor );
390 return $self;
391 }
392 }
393  
394 1;
395  
396 __END__