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

filter: Statementstmt 0.0% 0 / 242 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 24 Podpod n/a totaltotal 0.0% 0 / 362 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::ValueDialog;
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 Term::Fabulous::Widget::Dialog;
13  
14 # The dialog that edits one setting of a theme: a color picker with the
15 # palette's tokens as swatches for a color, a list of border styles or
16 # other choices, or a field for the name. Every change shows in the
17 # preview at once; Apply writes it to the theme, Cancel takes it back.
18 class Term::Fabulous::ThemeEditor::ValueDialog :isa(Term::Fabulous::Widget::Dialog) :strict(params) {
19 use Clay::UI::Enum::Result;
20 use Clay::XS qw(sizing_grow CLAY_TOP_TO_BOTTOM CLAY_ATTACH_TO_ROOT CLAY_ATTACH_POINT_RIGHT_CENTER);
21 use Feature::Compat::Try;
22 use List::Util qw(any min);
23 use Scalar::Util qw(weaken);
24 use Term::Fabulous::Check qw(color);
25 use Term::Fabulous::Enum::BorderStyle;
26 use Term::Fabulous::Theme;
27 use Term::Fabulous::Theme::Document;
28 use Term::Fabulous::Widget::Box;
29 use Term::Fabulous::Widget::Button;
30 use Term::Fabulous::Widget::ColorPicker;
31 use Term::Fabulous::Widget::Table;
32 use Term::Fabulous::Widget::Table::Frame qw(framed);
33 use Term::Fabulous::Widget::Text;
34 use Term::Fabulous::Widget::TextField;
35  
36 use constant LIST_ID => 'theme-editor-value-list';
37 use constant PICKER_ID => 'theme-editor-color';
38  
39 field $key :param :reader;
40 field $current :param; # the value the theme has now (effective)
41 field $is_set :param = 0; # whether the theme sets it
42 field $default :param = undef; # what the key falls back to, for the hint
43 field $palette :param; # token => [r, g, b, a], for the swatches
44 field $on_preview :param; # code: shows a value (undef: the theme as it is)
45 field $on_apply :param; # code: writes a value
46 field $on_reset :param; # code: removes the setting
47 field $list_rows :param = 12; # how many rows the list shows
48  
49 field $kind; # color, palette, style, boolean, name, base
50 field $value; # the value chosen now
51 field $list;
52 field $picker;
53 field $message;
54  
55 ADJUST {
56 my $parsed = Term::Fabulous::Theme::Document::parse_key($key);
57 $kind
58 = $parsed->{section} eq 'palette' ? 'palette'
59 : $parsed->{section} eq 'theme' ? ( $parsed->{field} eq 'name' ? 'name' : 'base' )
60 : Term::Fabulous::Theme::slot_info( @{$parsed}{qw(family slot)} )->{kind};
61 $value = $current;
62  
63 # At the right edge of the screen and without dimming it, so the
64 # preview on the left shows the value being chosen in its true
65 # colors.
66 $self->backdrop_color( [ 0, 0, 0, 0 ] );
67 $self->floating(
68 {
69 attach_to => CLAY_ATTACH_TO_ROOT,
70 attach_points => { element => CLAY_ATTACH_POINT_RIGHT_CENTER, parent => CLAY_ATTACH_POINT_RIGHT_CENTER },
71 offset => { x => -1, y => 0 },
72 z_index => $self->z_index + 1,
73 }
74 );
75 $self->layout( { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow( 0, 64 ) }, padding => { left => 2, right => 2, top => 1, bottom => 1 }, child_gap => 1 } );
76  
77 $self->add_child( Term::Fabulous::Widget::Text->new( text => "Edit $key", bold => 1 ) );
78 $self->add_child( Term::Fabulous::Widget::Text->new( text => $self->_hint($parsed) ) );
79 $self->add_child( $self->_chooser($parsed) );
80 $message = Term::Fabulous::Widget::Text->new( text => '' );
81 $self->add_child( $message, $self->_buttons );
82  
83 weaken( my $weak_self = $self );
84 $self->on( Close => sub ($event) { $weak_self->_closed; return Clay::UI::Enum::Result->CONTINUE } );
85 }
86  
87 method value () {
88 return $value;
89 }
90  
91 # What the setting is now and what Reset gives it; tokens are named as
92 # tokens (a token may be called like a state: disabled).
93 method _hint ($parsed) {
94 my $now
95 = $is_set
96 ? 'The theme sets it to ' . $self->_words($current) . '.'
97 : 'The theme does not set it' . ( defined $current ? '; it is ' . $self->_words($current) . ' now.' : '.' );
98 return $now unless defined $default;
99 return "$now That is its default, too." if $is_set && defined $current && $current eq $default;
100 return "$now Reset gives it back to " . $self->_words($default) . '.';
101 }
102  
103 method _words ($spec) {
104 return exists $palette->{$spec} ? qq{the token "$spec"} : $spec;
105 }
106  
107 # ---------------------------------------------------------------------
108 # What the dialog offers
109 # ---------------------------------------------------------------------
110  
111 method _chooser ($parsed) {
112 return $self->_name_field if $kind eq 'name';
113 return $self->_list( map { [ $_, $_ ] } Term::Fabulous::Theme::builtin_names() ) if $kind eq 'base';
114 return $self->_style_list($parsed) if $kind eq 'style';
115 return $self->_list( [ 'true', 'true' ], [ 'false', 'false' ] ) if $kind eq 'boolean';
116 return $self->_color_picker if $kind eq 'palette'; # a token's value must be a color
117 my $info = Term::Fabulous::Theme::slot_info( @{$parsed}{qw(family slot)} );
118 my @swatches = map { [ $_, $_, $palette->{$_} ] } Term::Fabulous::Theme::tokens();
119 push @swatches, [ 'none', 'none (no color)' ] unless $info->{required};
120 push @swatches, [ 'reverse', 'reverse video' ] if $info->{reverse} && $parsed->{state} eq 'pressed';
121 return $self->_color_picker(@swatches);
122 }
123  
124 method _name_field () {
125 my $field = Term::Fabulous::Widget::TextField->new( value => $current // '', placeholder => 'The theme\'s name', layout => { sizing => { width => sizing_grow() } } );
126 $field->editor->select_all;
127 weaken( my $weak_self = $self );
128 $field->on( Change => sub ($event) { $weak_self->_choose( $event->value ); return Clay::UI::Enum::Result->CONTINUE } );
129 $field->on( Submit => sub ($event) { $weak_self->apply; return } );
130 return $field;
131 }
132  
133 method _style_list ($parsed) {
134 my $info = Term::Fabulous::Theme::slot_info( @{$parsed}{qw(family slot)} );
135 my @styles = $info->{grid} ? Term::Fabulous::Enum::BorderStyle->get_grid_styles : Term::Fabulous::Enum::BorderStyle->values;
136 my @choices = map {
137 my ( $corner, $edge, $other ) = $_->get_top_glyphs;
138 [ $_->name, $_->name, "$corner$edge$edge$other" ]
139 } @styles;
140 push @choices, [ 'none', 'none (no border style)' ] unless $info->{required};
141 return $self->_list(@choices);
142 }
143  
144 # A list of [ value, label, sample ] choices; the cursor on one
145 # chooses it.
146 method _list (@choices) {
147 $list = Term::Fabulous::Widget::Table->new(
148 id => LIST_ID,
149 row_id => 'value',
150 selection => 'single',
151 header => 0,
152 border => 'none',
153 column_lines => 'none',
154 columns => [
155 { key => 'sample', width => 'fixed(6)' },
156 { key => 'label', width => 'grow' },
157 ],
158 rows => [ map { { value => $_->[0], label => $_->[1], sample => $_->[2] // '' } } @choices ],
159 layout => { sizing => { width => sizing_grow(), height => sizing_grow() } },
160 );
161 my ($chosen) = grep { $choices[$_][0] eq ( $current // '' ) } 0 .. $#choices;
162 if ( defined $chosen ) {
163 my $below = min( $#choices, $chosen + int( $list_rows / 2 ) );
164 $list->cursor( $choices[$below][0] ); # scrolls so that the chosen value is near the middle
165 $list->cursor( $choices[$chosen][0] );
166 $list->set_selection( $choices[$chosen][0] );
167 }
168 weaken( my $weak_self = $self );
169 $list->on( CursorMove => sub ($event) { $weak_self->_choose( $event->row_id ) if defined $event->row_id; return Clay::UI::Enum::Result->CONTINUE } );
170 $list->on( RowActivate => sub ($event) { $weak_self->apply; return } );
171 return framed( $list, $list_rows ); # no header
172 }
173  
174 # The color picker, with the swatches given ([ value, label, color ]):
175 # its changes show in the preview, Enter applies.
176 method _color_picker (@swatches) {
177 $picker = Term::Fabulous::Widget::ColorPicker->new(
178 id => PICKER_ID,
179 value => _pickable( $current, @swatches ),
180 swatches => @swatches ? \@swatches : undef,
181 list_rows => $list_rows,
182 );
183 weaken( my $weak_self = $self );
184 $picker->on( Change => sub ($event) { $weak_self->_choose( $event->value ); return Clay::UI::Enum::Result->CONTINUE } );
185 $picker->on( Submit => sub ($event) { $weak_self->apply; return } );
186 return $picker;
187 }
188  
189 # The value the picker starts with: the current one when the picker
190 # can show it (a swatch's value or a color), else none.
191 sub _pickable ( $spec, @swatches ) {
192 return undef unless defined $spec;
193 return $spec if any { $_->[0] eq $spec } @swatches;
194 try {
195 color( __PACKAGE__, value => $spec );
196 }
197 catch ($error) {
198 return undef;
199 }
200 return $spec;
201 }
202  
203 method _buttons () {
204 my $row = Term::Fabulous::Widget::Box->new( layout => { child_gap => 2 } );
205 weaken( my $weak_self = $self );
206 my @buttons = ( [ 'Apply', ['primary'], sub { $weak_self->apply } ], [ 'Reset to default', [], sub { $weak_self->reset } ], [ 'Cancel', [], sub { $weak_self->cancel } ] );
207 foreach my $definition (@buttons) {
208 my ( $label, $classes, $action ) = @$definition;
209 next if $label =~ /\AReset/ && !$is_set;
210 my $button = Term::Fabulous::Widget::Button->new( bordered => 1, classes => $classes, layout => { padding => { left => 1, right => 1 } } );
211 $button->add_child( Term::Fabulous::Widget::Text->new( text => $label ) );
212 $button->on( Activate => sub ($event) { $action->(); return } );
213 $row->add_child($button);
214 }
215 return $row;
216 }
217  
218 # ---------------------------------------------------------------------
219 # Choosing
220 # ---------------------------------------------------------------------
221  
222 # A value was chosen in the list, the picker or the field; the preview
223 # shows it.
224 method _choose ($chosen) {
225 $value = $chosen;
226 $message->text('');
227 $on_preview->( $self->_checked($chosen) ? $chosen : undef );
228 return;
229 }
230  
231 # Whether the key can take a value; the message says why not.
232 method _checked ($spec) {
233 try {
234 Term::Fabulous::Theme::Document->parse('')->with_setting( $key, $spec );
235 }
236 catch ($error) {
237 $error =~ s/\ATerm::Fabulous::Theme::Document: //;
238 $error =~ s/\n\z//;
239 $message->text($error);
240 return 0;
241 }
242 return 1;
243 }
244  
245 # ---------------------------------------------------------------------
246 # Leaving
247 # ---------------------------------------------------------------------
248  
249 field $finished = 0;
250  
251 # A text in the picker's field that is no color is not applied; the
252 # message line says what the field wants.
253 method apply () {
254 if ( defined $picker && !$picker->is_valid ) {
255 $message->text( $picker->error );
256 return $self;
257 }
258 return $self unless defined $value && length $value && $self->_checked($value);
259 $finished = 1;
260 $self->close;
261 $on_apply->($value);
262 return $self;
263 }
264  
265 method reset () {
266 $finished = 1;
267 $self->close;
268 $on_reset->();
269 return $self;
270 }
271  
272 method cancel () {
273 $self->close;
274 return $self;
275 }
276  
277 # Closed without Apply or Reset (Cancel, Escape): the preview shows the
278 # theme as it is again.
279 method _closed () {
280 $on_preview->(undef) unless $finished;
281 return;
282 }
283 }
284  
285 1;
286  
287 __END__