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

filter: Statementstmt 0.0% 0 / 190 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 309 CC 2 SCAR 8.4
CC
2
Cov
0%
BUILDARGS
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.3
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.05';
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 fires Change for
17 # the preview at once; the answer apply carries the value, reset asks to
18 # remove the setting, and every other answer takes the preview back.
19 class Term::Fabulous::ThemeEditor::ValueDialog
20 :isa(Term::Fabulous::Widget::Dialog)
21 :strict(params)
22 {
23 use Clay::UI::Enum::Result;
24 use Clay::XS qw(sizing_grow CLAY_TOP_TO_BOTTOM);
25 use Feature::Compat::Try;
26 use List::Util qw(any min);
27 use Term::Fabulous::Check qw(color);
28 use Term::Fabulous::Enum::BorderStyle;
29 use Term::Fabulous::Event::Change;
30 use Term::Fabulous::Text::Markup qw(escape_markup);
31 use Term::Fabulous::Theme;
32 use Term::Fabulous::Theme::Document;
33 use Term::Fabulous::Widget::ColorPicker;
34 use Term::Fabulous::Widget::Table;
35 use Term::Fabulous::Widget::Table::Frame qw(framed);
36 use Term::Fabulous::Widget::Text;
37 use Term::Fabulous::Widget::TextField;
38  
39 use constant LIST_ID => 'theme-editor-value-list';
40 use constant PICKER_ID => 'theme-editor-color';
41  
42 field $key :param :reader;
43 field $current :param; # the value the theme has now (effective)
44 field $is_set :param = 0; # whether the theme sets it
45 field $default :param = undef; # what the key falls back to, for the hint
46 field $palette :param; # token => [r, g, b, a], for the swatches
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  
54 # At the right edge of the screen, titled with the key, with Reset only
55 # for a setting the theme has; the line above the buttons keeps its
56 # place.
57 sub BUILDARGS ( $class, %params ) {
58 my $key = $params{key} // die "$class: a key is required";
59 return $class->SUPER::BUILDARGS(
60 %params,
61 title => "Edit $key",
62 placement => 'right',
63 notice_line => 'kept',
64 buttons => [ { id => 'apply', label => 'Apply', primary => 1 }, ( $params{is_set} ? { id => 'reset', label => 'Reset to default' } : () ), { id => 'cancel', label => 'Cancel' } ],
65 );
66 }
67  
68 ADJUST {
69 my $parsed = Term::Fabulous::Theme::Document::parse_key($key);
70 $kind
71 = $parsed->{section} eq 'palette' ? 'palette'
72 : $parsed->{section} eq 'theme' ? ( $parsed->{field} eq 'name' ? 'name' : 'base' )
73 : Term::Fabulous::Theme::slot_info( @{$parsed}{qw(family slot)} )->{kind};
74 $value = $current;
75  
76 # Without dimming the screen, so the preview on the left shows the
77 # value being chosen in its true colors.
78 $self->backdrop_color( [ 0, 0, 0, 0 ] );
79 $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 } );
80  
81 $self->add_child( Term::Fabulous::Widget::Text->new( text => $self->_hint($parsed) ) );
82 $self->add_child( $self->_chooser($parsed) );
83 }
84  
85 method value () {
86 return $value;
87 }
88  
89 # What the setting is now and what Reset gives it; tokens are named as
90 # tokens (a token may be called like a state: disabled).
91 method _hint ($parsed) {
92 my $now
93 = $is_set
94 ? 'The theme sets it to ' . $self->_words($current) . '.'
95 : 'The theme does not set it' . ( defined $current ? '; it is ' . $self->_words($current) . ' now.' : '.' );
96 return $now unless defined $default;
97 return "$now That is its default, too." if $is_set && defined $current && $current eq $default;
98 return "$now Reset gives it back to " . $self->_words($default) . '.';
99 }
100  
101 method _words ($spec) {
102 return exists $palette->{$spec} ? qq{the token "$spec"} : $spec;
103 }
104  
105 # ---------------------------------------------------------------------
106 # What the dialog offers
107 # ---------------------------------------------------------------------
108  
109 method _chooser ($parsed) {
110 return $self->_name_field if $kind eq 'name';
111 return $self->_list( map { [ $_, $_ ] } Term::Fabulous::Theme::builtin_names() ) if $kind eq 'base';
112 return $self->_style_list($parsed) if $kind eq 'style';
113 return $self->_list( [ 'true', 'true' ], [ 'false', 'false' ] ) if $kind eq 'boolean';
114 return $self->_color_picker if $kind eq 'palette'; # a token's value must be a color
115 my $info = Term::Fabulous::Theme::slot_info( @{$parsed}{qw(family slot)} );
116 my @swatches = map { [ $_, $_, $palette->{$_} ] } Term::Fabulous::Theme::tokens();
117 push @swatches, [ 'none', 'none (no color)' ] unless $info->{required};
118 push @swatches, [ 'reverse', 'reverse video' ] if $info->{reverse} && $parsed->{state} eq 'pressed';
119 return $self->_color_picker(@swatches);
120 }
121  
122 method _name_field () {
123 my $field = Term::Fabulous::Widget::TextField->new( value => $current // '', placeholder => 'The theme\'s name', layout => { sizing => { width => sizing_grow() } } );
124 $field->editor->select_all;
125 $field->on( Change => sub ( $self, $event ) { $self->_choose( $event->value ); return Clay::UI::Enum::Result->HANDLED }, owner => $self );
126 $field->on( Submit => sub ( $self, $event ) { $self->apply; return }, owner => $self );
127 return $field;
128 }
129  
130 method _style_list ($parsed) {
131 my $info = Term::Fabulous::Theme::slot_info( @{$parsed}{qw(family slot)} );
132 my @styles = $info->{grid} ? Term::Fabulous::Enum::BorderStyle->get_grid_styles : Term::Fabulous::Enum::BorderStyle->values;
133 my @choices = map {
134 my ( $corner, $edge, $other ) = $_->get_top_glyphs;
135 [ $_->name, $_->name, "$corner$edge$edge$other" ]
136 } @styles;
137 push @choices, [ 'none', 'none (no border style)' ] unless $info->{required};
138 return $self->_list(@choices);
139 }
140  
141 # A list of [ value, label, sample ] choices; the cursor on one
142 # chooses it.
143 method _list (@choices) {
144 $list = Term::Fabulous::Widget::Table->new(
145 id => LIST_ID,
146 row_id => 'value',
147 selection => 'single',
148 header => 0,
149 border => 'none',
150 column_lines => 'none',
151 columns => [
152 { key => 'sample', width => 'fixed(6)' },
153 { key => 'label', width => 'grow' },
154 ],
155 rows => [ map { { value => $_->[0], label => $_->[1], sample => $_->[2] // '' } } @choices ],
156 layout => { sizing => { width => sizing_grow(), height => sizing_grow() } },
157 );
158 my ($chosen) = grep { $choices[$_][0] eq ( $current // '' ) } 0 .. $#choices;
159 if ( defined $chosen ) {
160 my $below = min( $#choices, $chosen + int( $list_rows / 2 ) );
161 $list->cursor( $choices[$below][0] ); # scrolls so that the chosen value is near the middle
162 $list->cursor( $choices[$chosen][0] );
163 $list->set_selection( $choices[$chosen][0] );
164 }
165 $list->on( CursorMove => sub ( $self, $event ) { $self->_choose( $event->row_id ) if defined $event->row_id; return Clay::UI::Enum::Result->CONTINUE }, owner => $self );
166 $list->on( RowActivate => sub ( $self, $event ) { $self->apply; return }, owner => $self );
167 return framed( $list, $list_rows ); # no header
168 }
169  
170 # The color picker, with the swatches given ([ value, label, color ]):
171 # its changes show in the preview, Enter applies.
172 method _color_picker (@swatches) {
173 $picker = Term::Fabulous::Widget::ColorPicker->new(
174 id => PICKER_ID,
175 value => _pickable( $current, @swatches ),
176 swatches => @swatches ? \@swatches : undef,
177 list_rows => $list_rows,
178 );
179 $picker->on( Change => sub ( $self, $event ) { $self->_choose( $event->value ); return Clay::UI::Enum::Result->HANDLED }, owner => $self );
180 $picker->on( Submit => sub ( $self, $event ) { $self->apply; return }, owner => $self );
181 return $picker;
182 }
183  
184 # The value the picker starts with: the current one when the picker
185 # can show it (a swatch's value or a color), else none.
186 sub _pickable ( $spec, @swatches ) {
187 return undef unless defined $spec;
188 return $spec if any { $_->[0] eq $spec } @swatches;
189 try {
190 color( __PACKAGE__, value => $spec );
191 }
192 catch ($error) {
193 return undef;
194 }
195 return $spec;
196 }
197  
198 # ---------------------------------------------------------------------
199 # Choosing
200 # ---------------------------------------------------------------------
201  
202 # A value was chosen in the list, the picker or the field: the dialog
203 # fires Change with it for the preview, or with undef for a value the
204 # key cannot take (the preview shows the theme as it is).
205 method _choose ($chosen) {
206 $value = $chosen;
207 $self->notice('');
208 $self->fire_event( Term::Fabulous::Event::Change->new( value => $self->_checked($chosen) ? $chosen : undef ) );
209 return;
210 }
211  
212 # Whether the key can take a value; the message says why not.
213 method _checked ($spec) {
214 try {
215 Term::Fabulous::Theme::Document->parse('')->with_setting( $key, $spec );
216 }
217 catch ($error) {
218 $error =~ s/\ATerm::Fabulous::Theme::Document: //;
219 $error =~ s/\n\z//;
220 $self->notice( escape_markup($error) );
221 return 0;
222 }
223 return 1;
224 }
225  
226 # ---------------------------------------------------------------------
227 # Leaving
228 # ---------------------------------------------------------------------
229  
230 method press_button :override ($id) {
231 return $self->apply if $id eq 'apply';
232 return $self->SUPER::press_button($id);
233 }
234  
235 # A text in the picker's field that is no color is not applied; the
236 # notice line says what the field wants.
237 method apply () {
238 if ( defined $picker && !$picker->is_valid ) {
239 $self->notice( escape_markup( $picker->error ) );
240 return $self;
241 }
242 return $self unless defined $value && length $value && $self->_checked($value);
243 return $self->answer( apply => value => $value );
244 }
245 }
246  
247 1;
248  
249 __END__