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

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