blib/lib/Term/Fabulous/Role/Themed.pm untested

filter: Statementstmt 0.0% 0 / 165 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 357 CC 11 SCAR 27.4
CC
11
Cov
0%
_themed_params_of
30.0
_forwarded_of
30.0
_kind_of
12.0
CRAP 15.5
1 package Term::Fabulous::Role::Themed;
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 role Term::Fabulous::Role::Themed {
13 use Scalar::Util qw(blessed);
14 use Term::Fabulous::Check qw(border_sides border_style cell_color color describe optional);
15 use Term::Fabulous::Theme;
16  
17 # The family of the class (a class method): button, input, ...
18 method theme_family;
19  
20 # The themed parameters of the class (a class method), as
21 # name => [ slot, state ] or name => [ slot, state, kind ]; a subclass
22 # adds its own to its parent's. A kind says how a value is checked
23 # (see %KIND); the role takes the parameters of a kind from the
24 # constructor and declares them as layout properties. A slot of undef
25 # (with a kind) is a parameter the family has no slot for: only an
26 # explicit value counts.
27 method themed_params;
28  
29 # The state the widget shows now, from the states its family's slots
30 # have: 'normal', 'focused', ...
31 method look_state;
32  
33 # Called by reset_look for a parameter whose explicit value the class
34 # keeps outside the role (a Clay::UI field, say): clears it.
35 method look_reset ($name);
36  
37 # The class names of the widget (an array reference), which select the
38 # theme's variants.
39 method classes;
40  
41 # Explicit values, under the parameter name; the theme fills the rest.
42 field %_override;
43  
44 # The looks of the family for the widget's classes, from the theme of
45 # the UI the widget is in, as of a theme generation.
46 field $_look_table;
47 field $_look_generation = -1;
48  
49 # Kind => how a value is checked (Term::Fabulous::Check), what a
50 # message calls a value, whether undef is a value (none) rather than a
51 # mistake, and the kind of its layout property.
52 my %KIND = (
53 color => { check => \&color, noun => 'a color', property => 'color' },
54 cell_color => { check => \&cell_color, noun => 'a color', property => 'color' },
55 optional_color => { check => _optional( \&color ), takes_undef => 1, property => 'scalar' },
56 optional_cell_color => { check => _optional( \&cell_color ), takes_undef => 1, property => 'scalar' },
57 border_style => { check => \&border_style, noun => 'a border style or its name', property => 'scalar' },
58 grid_border_style => {
59 check => sub ( $owner, $name, $value ) { border_style( $owner, $name, $value, grid => 1 ) },
60 noun => 'a border style with joints or its name',
61 property => 'scalar',
62 },
63 border_sides => { check => \&border_sides, noun => 'a boolean or a hash reference of sides', property => 'border_sides' },
64 );
65  
66 sub _optional ($check) {
67 return sub ( $owner, $name, $value ) { optional( $check, $owner, $name, $value ) };
68 }
69  
70 # name => { slot, state, and with a kind: check, noun, takes_undef,
71 # property } and "slot.state" => name per class, checked against the
72 # family once.
73 my %themed_params_of;
74 my %param_of_look_of;
75 my %forwarded_of;
76  
77 sub _themed_params_of ($class) {
78 return $themed_params_of{$class} //= do {
79 my %params = $class->themed_params;
80 my $family = $class->theme_family;
81 my %checked;
82 foreach my $name ( sort keys %params ) {
83 my $mapping = $params{$name};
84 die "$class: themed parameter '$name' must map to [ slot, state ] or [ slot, state, kind ], got " . describe($mapping)
85 unless ref $mapping eq 'ARRAY' && ( @$mapping == 2 || @$mapping == 3 );
86 my ( $slot, $state, $kind ) = @$mapping;
87 die "$class: themed parameter '$name' maps to no slot, so it needs a kind" unless defined $slot || defined $kind;
88 die "$class: themed parameter '$name' maps to $slot" . ( $state eq 'normal' ? '' : ".$state" ) . ", which the family $family does not have"
89 unless !defined $slot || Term::Fabulous::Theme::has_slot( $family, $slot, $state );
90 $checked{$name} = { slot => $slot, state => $state, _kind_of( $class, $name, $kind ) };
91 }
92 \%checked;
93 };
94 }
95  
96 # What a parameter's kind says: one of %KIND, or a code reference that
97 # checks like the functions of Term::Fabulous::Check (undef included).
98 # No kind: the class keeps and checks the value itself.
99 sub _kind_of ( $class, $name, $kind ) {
100 return () unless defined $kind;
101 return ( check => $kind, takes_undef => 1, property => 'scalar' ) if ref $kind eq 'CODE';
102 my $known = $KIND{$kind} // die "$class: themed parameter '$name' has an unknown kind " . describe($kind) . " (known: " . join( ', ', sort keys %KIND ) . ", or a code reference)";
103 return %$known;
104 }
105  
106 sub _param_of_look_of ($class) {
107 return $param_of_look_of{$class} //= do {
108 my $params = _themed_params_of($class);
109 my %name_of_look = map { join( '.', @{ $params->{$_} }{qw(slot state)} ) => $_ } grep { defined $params->{$_}{slot} } keys %$params;
110 \%name_of_look;
111 };
112 }
113  
114 # name => { method, param } for the looks the class keeps on its parts:
115 # the method that returns the parts, and the parameter as the part's
116 # class declares it.
117 sub _forwarded_of ($class) {
118 return $forwarded_of{$class} //= do {
119 my %parts = $class->can('forwarded_looks') ? $class->forwarded_looks : ();
120 my $own = _themed_params_of($class);
121 my %forwarded;
122 foreach my $method ( sort keys %parts ) {
123 my ( $part_class, @names ) = @{ $parts{$method} };
124 foreach my $name (@names) {
125 my $param = _themed_params_of($part_class)->{$name};
126 die "$class: forwarded look '$name' is not a themed parameter of $part_class with a kind" unless defined $param && defined $param->{check};
127 die "$class: forwarded look '$name' is a themed parameter of $class as well" if $own->{$name};
128 die "$class: forwarded look '$name' goes to two kinds of parts" if $forwarded{$name};
129 $forwarded{$name} = { method => $method, param => $param };
130 }
131 }
132 \%forwarded;
133 };
134 }
135  
136 # The parts that keep a forwarded look, or the empty list.
137 method _parts_of ($name) {
138 my $forward = _forwarded_of( ref $self )->{$name} // return ();
139 my $method = $forward->{method};
140 return $self->$method;
141 }
142  
143 # The layout properties of the themed parameters of a kind and of the
144 # forwarded looks (a class method), for Term::Fabulous::Role::CanParseLayout.
145 method themed_layout_properties :common () {
146 my $params = _themed_params_of($class);
147 my $forwarded = _forwarded_of($class);
148 return (
149 ( map { $_ => $params->{$_}{property} } grep { defined $params->{$_}{property} } keys %$params ),
150 ( map { $_ => $forwarded->{$_}{param}{property} } keys %$forwarded ),
151 );
152 }
153  
154 # The constructor records the given themed parameters of a kind,
155 # checked; a parameter left out stays with the theme. It runs before
156 # the class's own fields and ADJUST blocks, so it calls no accessor
157 # and no looks_changed: the class reads its looks when it builds its
158 # parts. A forwarded look is the business of the class that builds
159 # the part.
160 ADJUSTPARAMS($params) {
161 my $themed = _themed_params_of( ref $self );
162 _forwarded_of( ref $self ); # checks the declaration on first use
163 foreach my $name ( sort grep { exists $params->{$_} && defined $themed->{$_}{check} } keys %$themed ) {
164 $_override{$name} = $self->_checked_look( $name, $themed->{$name}, delete $params->{$name} );
165 }
166 }
167  
168 # A value checked by its parameter's kind; undef for a kind that does
169 # not take it names the way back to the theme.
170 method _checked_look ( $name, $param, $value ) {
171 return $value unless defined $param->{check};
172 die ref($self) . ": $name must be $param->{noun}, got undef (reset_look('$name') returns it to the theme)"
173 unless defined $value || $param->{takes_undef};
174 return $param->{check}->( $self, $name, $value );
175 }
176  
177 # The looks of the widget, fetched again after a theme change.
178 method _look_table () {
179 my $generation = Term::Fabulous::Theme::generation();
180 return $_look_table if $_look_generation == $generation;
181  
182 $_look_table = $self->drawing_theme->look_table( ref($self)->theme_family, $self->classes );
183 $_look_generation = $generation;
184 return $_look_table;
185 }
186  
187 # The theme the UI the widget is in gives it, or the default one
188 # outside a UI.
189 method drawing_theme () {
190 my $ui = $self->ui;
191 return $ui->theme_for($self) if defined $ui && $ui->can('theme_for');
192 return defined $ui && $ui->can('theme') ? $ui->theme : Term::Fabulous::Theme->default;
193 }
194  
195 # Tells the widget which looks may have changed, when it has the hook.
196 method _looks_changed (@names) {
197 $self->looks_changed(@names) if @names && $self->can('looks_changed');
198 return;
199 }
200  
201 # Every look the widget has: its themed parameters and the forwarded
202 # ones.
203 method _all_look_names () {
204 my @names = sort( keys %{ _themed_params_of( ref $self ) }, keys %{ _forwarded_of( ref $self ) } );
205 return @names;
206 }
207  
208 # Forgets the fetched looks of the widget and of everything below it,
209 # for when the theme of its UI or the widget's classes change; in a UI,
210 # every look may have changed.
211 method forget_looks () {
212 forget_tree_looks($self);
213 return;
214 }
215  
216 # Forgets the fetched looks of the widget alone: the tree_changed hook
217 # of Term::Fabulous::Widget and ::Text calls it on every widget of a
218 # subtree that joined or left a tree.
219 method _forget_own_looks () {
220 $_look_generation = -1;
221 $self->_looks_changed( $self->_all_look_names ) if $self->_in_themed_ui;
222 return;
223 }
224  
225 # True in a UI that gives the widget its looks: a Clay::UI without
226 # themes (the default theme), or a Term::Fabulous whose theme is a
227 # theme. Clay::UI announces a new UI to its widgets from its own
228 # constructor, before Term::Fabulous has turned its theme argument into
229 # a theme; Term::Fabulous tells them itself once it has.
230 method _in_themed_ui () {
231 my $ui = $self->ui // return 0;
232 return 1 unless $ui->can('theme');
233 my $theme = $ui->theme;
234 return blessed $theme && $theme->isa('Term::Fabulous::Theme') ? 1 : 0;
235 }
236  
237 # A node and every themed widget below it forget their looks; the walk
238 # goes on below nodes that are not themed (the rows of a grid).
239 sub forget_tree_looks ($node) {
240 $_->_forget_own_looks foreach grep { $_->DOES(__PACKAGE__) } $node->subtree;
241 return;
242 }
243  
244 # The theme's value of a slot in a state: a state the theme gives no
245 # value of its own looks like the normal state.
246 method look ( $slot, $state = 'normal' ) {
247 my $table = $_look_generation == Term::Fabulous::Theme::generation() ? $_look_table : $self->_look_table;
248 return exists $table->{"$slot.$state"} ? $table->{"$slot.$state"} : $table->{"$slot.normal"};
249 }
250  
251 method family_look ( $family, $slot, $state = 'normal' ) {
252 return $self->drawing_theme->look( $family, $slot, $state );
253 }
254  
255 # The value a slot has in a state, given what it has in the normal
256 # state (an explicit value or the theme's): the explicit value of the
257 # state's parameter, else the theme's own value for the state, else
258 # the normal value.
259 method themed_value ( $slot, $state, $normal_value ) {
260 return $normal_value if $state eq 'normal';
261 my $name = _param_of_look_of( ref $self )->{"$slot.$state"};
262 return $_override{$name} if defined $name && exists $_override{$name};
263 my $table = $_look_generation == Term::Fabulous::Theme::generation() ? $_look_table : $self->_look_table;
264 return exists $table->{"$slot.$state"} ? $table->{"$slot.$state"} : $normal_value;
265 }
266  
267 method _unknown_look ( $what, $name ) {
268 die ref($self) . ": $what does not know " . describe($name) . " (known: " . join( ', ', $self->_all_look_names ) . ")";
269 }
270  
271 # What a themed parameter is worth: its explicit value, or the theme's.
272 method look_value ($name) {
273 my ($part) = $self->_parts_of($name);
274 return $part->look_value($name) if defined $part;
275 return $_override{$name} if exists $_override{$name};
276 my $param = _themed_params_of( ref $self )->{$name} // $self->_unknown_look( look_value => $name );
277 return undef unless defined $param->{slot};
278 return $self->look( @{$param}{qw(slot state)} );
279 }
280  
281 method has_look_override ($name) {
282 my ($part) = $self->_parts_of($name);
283 return $part->has_look_override($name) if defined $part;
284 return exists $_override{$name} ? 1 : 0;
285 }
286  
287 # Records an explicit value, checked by the parameter's kind (a
288 # parameter without one the caller has checked); a forwarded look is
289 # checked here, in the owner's name, and goes to every part that keeps
290 # it.
291 method set_look ( $name, $value ) {
292 my $forward = _forwarded_of( ref $self )->{$name};
293 if ( defined $forward ) {
294 $value = $self->_checked_look( $name, $forward->{param}, $value );
295 $_->set_look( $name => $value ) foreach $self->_parts_of($name);
296 }
297 else {
298 my $param = _themed_params_of( ref $self )->{$name} // $self->_unknown_look( set_look => $name );
299 $value = $self->_checked_look( $name, $param, $value );
300 $_override{$name} = $value;
301 }
302 $self->mark_changed;
303 $self->_looks_changed($name);
304 return $value;
305 }
306  
307 method reset_look (@names) {
308 my $params = _themed_params_of( ref $self );
309 my $forwarded = _forwarded_of( ref $self );
310 foreach my $name (@names) {
311 $self->_unknown_look( reset_look => $name ) unless defined $name && !ref $name && ( exists $params->{$name} || exists $forwarded->{$name} );
312 }
313 foreach my $name (@names) {
314 if ( $forwarded->{$name} ) {
315 $_->reset_look($name) foreach $self->_parts_of($name);
316 next;
317 }
318 delete $_override{$name};
319 $self->look_reset($name);
320 }
321 $self->mark_changed;
322 $self->_looks_changed(@names);
323 return $self;
324 }
325 }
326  
327 1;
328  
329 __END__