blib/lib/Term/Fabulous/Theme.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Chart/Curve.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Table/Filter.pm »
1 package Term::Fabulous::Theme;
2  
3 3 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.05';
9  
10 3 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 class Term::Fabulous::Theme :strict(params) {
13 3 ▶ use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶ use Text::KDL::XS qw(parse_kdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶ use Term::Fabulous::Check qw(border_style color describe);
Subroutine BEGIN: called CC=1 SCAR=2.6
17 use Term::Fabulous::Enum::BorderStyle;
18  
19 # ---------------------------------------------------------------------
20 # The vocabulary: palette tokens, families with their slots and states.
21 # ---------------------------------------------------------------------
22  
23 my @STATES = qw(hovered focused pressed disabled selected active invalid);
24 my %IS_STATE = map { $_ => 1 } 'normal', @STATES;
25  
26 # Every token, with its value in the built-in dark and light themes.
27 # The dark values are the colors the widgets were drawn in before
28 # themes existed.
29 my %PALETTE = (
30 background => { dark => [ 22, 22, 34, 255 ], light => [ 250, 250, 247, 255 ] },
31 surface => { dark => [ 28, 33, 45, 255 ], light => [ 240, 241, 245, 255 ] },
32 surface_raised => { dark => [ 36, 40, 50, 255 ], light => [ 228, 231, 238, 255 ] },
33 surface_field => { dark => [ 36, 40, 48, 255 ], light => [ 255, 255, 255, 255 ] },
34 surface_low => { dark => [ 30, 33, 40, 255 ], light => [ 236, 238, 242, 255 ] },
35 group_background => { dark => [ 28, 32, 41, 255 ], light => [ 232, 235, 241, 255 ] },
36 border => { dark => [ 70, 85, 110, 255 ], light => [ 160, 170, 190, 255 ] },
37 line => { dark => [ 88, 96, 112, 255 ], light => [ 190, 196, 208, 255 ] },
38 outline => { dark => [ 90, 96, 110, 255 ], light => [ 175, 182, 196, 255 ] },
39 text => { dark => [ 220, 223, 228, 255 ], light => [ 30, 34, 42, 255 ] },
40 text_bright => { dark => [ 235, 238, 243, 255 ], light => [ 10, 12, 16, 255 ] },
41 text_muted => { dark => [ 140, 146, 158, 255 ], light => [ 110, 118, 132, 255 ] },
42 text_dim => { dark => [ 150, 160, 180, 255 ], light => [ 100, 108, 124, 255 ] },
43 text_inverse => { dark => [ 16, 18, 22, 255 ], light => [ 250, 250, 250, 255 ] },
44 placeholder => { dark => [ 120, 126, 138, 255 ], light => [ 140, 146, 158, 255 ] },
45 disabled => { dark => [ 108, 112, 120, 255 ], light => [ 160, 164, 172, 255 ] },
46 accent => { dark => [ 97, 175, 239, 255 ], light => [ 30, 110, 200, 255 ] },
47 focus_background => { dark => [ 52, 58, 72, 255 ], light => [ 220, 228, 244, 255 ] },
48 hover_background => { dark => [ 40, 45, 58, 255 ], light => [ 230, 234, 242, 255 ] },
49 hover_low => { dark => [ 38, 42, 52, 255 ], light => [ 234, 237, 243, 255 ] },
50 control_hover => { dark => [ 60, 66, 80, 255 ], light => [ 214, 220, 232, 255 ] },
51 selected => { dark => [ 38, 62, 92, 255 ], light => [ 200, 216, 240, 255 ] },
52 selection => { dark => [ 38, 79, 120, 255 ], light => [ 190, 210, 240, 255 ] },
53 track => { dark => [ 58, 63, 75, 255 ], light => [ 215, 219, 228, 255 ] },
54 track_scroll => { dark => [ 70, 76, 90, 255 ], light => [ 205, 209, 218, 255 ] },
55 button_face => { dark => [ 44, 49, 60, 255 ], light => [ 222, 226, 234, 255 ] },
56 backdrop => { dark => [ 0, 0, 0, 128 ], light => [ 0, 0, 0, 96 ] },
57 success => { dark => [ 152, 195, 121, 255 ], light => [ 60, 140, 50, 255 ] },
58 warning => { dark => [ 229, 192, 123, 255 ], light => [ 190, 130, 20, 255 ] },
59 danger => { dark => [ 224, 108, 117, 255 ], light => [ 200, 50, 60, 255 ] },
60 );
61  
62 # A slot: its kind (color, style or boolean), its default for the
63 # normal state and for every state it has. A state default of 'normal'
64 # means the state looks like the normal state unless a theme says
65 # otherwise. Flags added to a slot: reverse (it may be 'reverse'),
66 # required (it cannot be 'none', its widget draws with the value) and
67 # grid (a style with joints). A boolean slot has no states, because
68 # its widget would change size when its state changes, and is always
69 # required.
70 sub _color_slot ( $default, %by_state ) {
71 return { kind => 'color', default => { normal => $default, %by_state } };
72 }
73  
74 sub _style_slot ($default) {
75 return { kind => 'style', default => { normal => $default } };
76 }
77  
78 sub _boolean_slot ($default) {
79 return { kind => 'boolean', required => 1, default => { normal => $default } };
80 }
81  
82 # The values a boolean slot takes, and the spec each one is stored as.
83 my %BOOLEAN_SPEC = ( true => 'true', 1 => 'true', false => 'false', 0 => 'false' );
84  
85 sub _required ( $slot, %flags ) {
86 return { %$slot, required => 1, %flags };
87 }
88  
89 my %FAMILY = (
90 text => {
91 slots => {
92 'color' => _color_slot('text'),
93 'link' => _color_slot( 'accent', hovered => 'text_bright', selected => 'text_inverse' ),
94 'link.background' => _color_slot( 'none', hovered => 'hover_background', selected => 'accent' ),
95 }
96 },
97 box => {
98 slots => {
99 'background' => _color_slot('none'),
100 'border.color' => _color_slot('border'),
101 'border.style' => _style_slot('Round'),
102 }
103 },
104 button => {
105 extends => 'box',
106 slots => {
107 'background' => { %{ _color_slot( 'none', hovered => 'normal', focused => 'normal', pressed => 'reverse', disabled => 'normal' ) }, reverse => 1 },
108 'border.color' => _color_slot( 'border', hovered => 'normal', focused => 'accent', pressed => 'normal', disabled => 'disabled' ),
109 'border.enabled' => _boolean_slot('false'),
110 'text' => _color_slot( 'text', hovered => 'normal', focused => 'normal', pressed => 'normal', disabled => 'disabled' ),
111 }
112 },
113 input => {
114 extends => 'box',
115 slots => {
116 'background' => _color_slot( 'none', focused => 'focus_background', disabled => 'normal' ),
117 'border.color' => _color_slot( 'border', focused => 'normal', disabled => 'normal', invalid => 'danger' ),
118 'border.enabled' => _boolean_slot('false'),
119 'text' => _color_slot( 'text', disabled => 'disabled', invalid => 'danger' ),
120 'accent' => _color_slot('accent'),
121 'star' => _required( _color_slot('warning') ),
122 'placeholder' => _color_slot('placeholder'),
123 'selection' => _color_slot('selection'),
124 'track' => _color_slot('outline'),
125 'inactive' => _required( _color_slot('outline') ),
126 'half' => _color_slot('none'),
127 'separator' => _color_slot('outline'),
128 'selected_text' => _color_slot('text_inverse'),
129 'hover_background' => _color_slot('control_hover'),
130 }
131 },
132 text_input => {
133 extends => 'input',
134 slots => { 'background' => _color_slot( 'surface_field', focused => 'focus_background', disabled => 'normal' ) },
135 },
136 dropdown => {
137 extends => 'input',
138 slots => {
139 'background' => _color_slot( 'surface_field', focused => 'focus_background', disabled => 'normal' ),
140 'list.background' => _color_slot('surface_low'),
141 'list.border.color' => _color_slot('accent'),
142 'list.border.style' => _style_slot('Round'),
143 'highlight.text' => _color_slot('text_inverse'),
144 'highlight.background' => _color_slot('accent'),
145 }
146 },
147 menu => {
148 extends => 'input',
149 slots => {
150 'background' => _color_slot( 'surface_low', focused => 'normal', disabled => 'normal' ),
151 'border.color' => _color_slot( 'accent', focused => 'normal', disabled => 'normal', invalid => 'normal' ),
152 'border.style' => _style_slot('Round'),
153 'border.enabled' => _boolean_slot('true'),
154 'keys' => _color_slot('placeholder'),
155 'separator' => _color_slot('placeholder'),
156 'highlight.text' => _color_slot('text_inverse'),
157 'highlight.background' => _color_slot('accent'),
158 }
159 },
160 menu_bar => {
161 extends => 'input',
162 slots => {
163 'background' => _color_slot( 'surface_low', focused => 'normal', disabled => 'normal' ),
164 'caption' => _color_slot('placeholder'),
165 'highlight.text' => _color_slot('text_inverse'),
166 'highlight.background' => _color_slot('accent'),
167 }
168 },
169 table => {
170 extends => 'box',
171 slots => {
172 'text' => _required( _color_slot('text') ),
173 'header.text' => _required( _color_slot('text_bright') ),
174 'header.background' => _color_slot('surface_raised'),
175 'group.text' => _color_slot('accent'),
176 'group.background' => _color_slot('group_background'),
177 'cursor' => _required( _color_slot('focus_background') ),
178 'selected' => _color_slot('selected'),
179 'hover' => _color_slot('hover_low'),
180 'filter.background' => _color_slot('surface_low'),
181 'error' => _color_slot('danger'),
182 'muted' => _required( _color_slot('text_muted') ),
183 'line.color' => _required( _color_slot('line') ),
184 'stripe' => _color_slot('none'),
185 'pager.button' => _required( _color_slot('button_face') ),
186 }
187 },
188 scrollbar => { extends => 'box', slots => { track => _required( _color_slot('track_scroll') ), thumb => _required( _color_slot('accent') ) } },
189 tabs => {
190 extends => 'box',
191 slots => {
192 'line.color' => _color_slot('outline'),
193 'line.style' => _required( _style_slot('Round'), grid => 1 ),
194 'text' => _color_slot( 'text_dim', active => 'text', disabled => 'disabled' ),
195 'hover_background' => _color_slot('hover_background'),
196 'focus_border' => _color_slot('accent'),
197 }
198 },
199 document_tabs => {
200 extends => 'box',
201 slots => {
202 'background' => _color_slot( 'surface_low', active => 'focus_background' ),
203 'text' => _color_slot( 'text_dim', active => 'text' ),
204 'modified' => _color_slot('accent'),
205 'error' => _color_slot('danger'),
206 'button' => _color_slot('accent'),
207 }
208 },
209 accordion => {
210 extends => 'box',
211 slots => {
212 'title' => _required( _color_slot('text') ),
213 'accent' => _required( _color_slot('accent') ),
214 'header.background' => _color_slot( 'none', focused => 'focus_background', hovered => 'hover_background' ),
215 'disabled' => _required( _color_slot('disabled') ),
216 'border.color' => _color_slot('disabled'),
217 'border.style' => _style_slot('Round'),
218 'border.enabled' => _boolean_slot('false'),
219 }
220 },
221 dialog => {
222 extends => 'box',
223 slots => {
224 'background' => _color_slot('surface'),
225 'border.color' => _color_slot('accent'),
226 'border.style' => _style_slot('Round'),
227 'border.enabled' => _boolean_slot('true'),
228 'backdrop' => _color_slot('backdrop'),
229 }
230 },
231 toast => {
232 extends => 'box',
233 slots => {
234 'background' => _color_slot('surface'),
235 'border.style' => _style_slot('Round'),
236 'border.enabled' => _boolean_slot('true'),
237 'text' => _required( _color_slot('text') ),
238 'important_text' => _required( _color_slot('text_inverse') ),
239 'info' => _required( _color_slot('accent') ),
240 'success' => _required( _color_slot('success') ),
241 'warning' => _required( _color_slot('warning') ),
242 'danger' => _required( _color_slot('danger') ),
243 }
244 },
245 status_bar => {
246 extends => 'box',
247 slots => {
248 'background' => _color_slot('surface_low'),
249 'text' => _color_slot('text'),
250 'dim' => _color_slot('placeholder'),
251 'accent' => _color_slot('accent'),
252 'success' => _color_slot('success'),
253 'warning' => _color_slot('warning'),
254 'danger' => _color_slot('danger'),
255 }
256 },
257 progress => {
258 extends => 'box',
259 slots => {
260 'color' => _color_slot('accent'),
261 'track' => _color_slot('track'),
262 'text' => _color_slot('text'),
263 'inside_text' => _color_slot('text_inverse'),
264 'border.enabled' => _boolean_slot('false'),
265 }
266 },
267 spinner => { extends => 'box', slots => { color => _color_slot('accent'), label => _color_slot('text'), 'border.enabled' => _boolean_slot('false') } },
268 image => { extends => 'box', slots => { notice => _color_slot('text_dim'), 'border.enabled' => _boolean_slot('false') } },
269 divider => {
270 extends => 'box',
271 slots => {
272 'line.color' => _color_slot('outline'),
273 'line.style' => _style_slot('Solid'),
274 'text' => _color_slot('text_dim'),
275 }
276 },
277 );
278  
279 # The slots of a family, its ancestors' included; the family's own
280 # definitions win.
281 my %slots_of_family;
282  
283 sub _slots_of ($family) {
284 return $slots_of_family{$family} //= do {
285 my $definition = $FAMILY{$family};
286 my $parent = $definition->{extends};
287 +{ ( defined $parent ? %{ _slots_of($parent) } : () ), %{ $definition->{slots} } };
288 };
289 }
290  
291 sub families () {
292 my @names = sort keys %FAMILY;
293 return @names;
294 }
295  
296 sub tokens () {
297 my @names = sort keys %PALETTE;
298 return @names;
299 }
300  
301 sub has_token ($name) {
302 return defined $name && !ref $name && exists $PALETTE{$name} ? 1 : 0;
303 }
304  
305 sub states () {
306 return ( 'normal', @STATES );
307 }
308  
309 sub is_family ($name) {
310 return defined $name && !ref $name && exists $FAMILY{$name} ? 1 : 0;
311 }
312  
313 # The slot names of a family, each with the states it has.
314 sub slots ($family) {
315 die "Term::Fabulous::Theme: unknown family " . describe($family) . " (known: " . join( ', ', families() ) . ")" unless is_family($family);
316 my $slots = _slots_of($family);
317 my %states_of;
318 foreach my $slot ( keys %$slots ) {
319 $states_of{$slot} = [ grep { $_ ne 'normal' } sort keys %{ $slots->{$slot}{default} } ];
320 }
321 return %states_of;
322 }
323  
324 sub has_slot ( $family, $slot, $state = 'normal' ) {
325 return 0 unless is_family($family) && defined $slot && defined $state && !ref $slot && !ref $state;
326 my $definition = _slots_of($family)->{$slot} // return 0;
327 return exists $definition->{default}{$state} ? 1 : 0;
328 }
329  
330 # What the vocabulary says about a slot: its kind, the values it takes
331 # besides colors or styles, its states and its defaults.
332 sub slot_info ( $family, $slot ) {
333 die "Term::Fabulous::Theme: unknown family " . describe($family) . " (known: " . join( ', ', families() ) . ")" unless is_family($family);
334 my $slots = _slots_of($family);
335 my $definition = defined $slot && !ref $slot ? $slots->{$slot} : undef;
336 die "Term::Fabulous::Theme: the family $family has no slot " . describe($slot) . " (known: " . join( ', ', sort keys %$slots ) . ")" unless defined $definition;
337 return {
338 kind => $definition->{kind},
339 required => $definition->{required} ? 1 : 0,
340 reverse => $definition->{reverse} ? 1 : 0,
341 grid => $definition->{grid} ? 1 : 0,
342 states => [ grep { $_ ne 'normal' } sort keys %{ $definition->{default} } ],
343 default => { %{ $definition->{default} } },
344 };
345 }
346  
347 # A value a theme may give a slot in a state, as the theme would store
348 # it; dies with the reason when the slot cannot take it.
349 sub check_value ( $family, $slot, $state, $value ) {
350 die "Term::Fabulous::Theme: the family " . describe($family) . " has no slot " . describe($slot) . " with the state " . describe($state) unless has_slot( $family, $slot, $state );
351 return _checked_spec( $family, $slot, $state, $value );
352 }
353  
354 # ---------------------------------------------------------------------
355 # The generation: a process-wide counter that every UI bumps when its
356 # theme is set, so that widgets know when to look their looks up again.
357 # ---------------------------------------------------------------------
358  
359 my $generation = 0;
360  
361 sub generation () {
362 return $generation;
363 }
364  
365 sub bump_generation () {
366 return ++$generation;
367 }
368  
369 # ---------------------------------------------------------------------
370 # Construction
371 # ---------------------------------------------------------------------
372  
373 my %builtin;
374  
375 field $name :param :reader = undef;
376 field $extends :param = 'dark';
377  
378 # What the theme sets itself: tokens as [r, g, b, a], and the raw specs
379 # (a token name, a color, a style, 'none', 'reverse') of its slot and
380 # variant overrides, under "family" and "family.variant".
381 field %_palette;
382 field %_own_slot;
383 field %_own_variant;
384  
385 # The result of the compile step: every slot and state of every family
386 # and variant, as values the widgets draw with.
387 field %_table;
388 field %_variant_table;
389 field %_look_table_cache;
390  
391 # The constructor parameters, read once in ADJUST and then dropped.
392 field $palette :param = {};
393 field $slots :param = {};
394 field $variants :param = {};
395 field $_builtin :param(_builtin) = undef;
396  
397 ADJUST {
398 die "Term::Fabulous::Theme: name must be a string, got " . describe($name) if defined $name && ref $name;
399 $extends = defined $_builtin ? undef : _theme_of( extends => $extends );
400 $name //= $_builtin;
401  
402 my %own_palette = _checked_palette( $palette, $_builtin );
403 %_palette = ( ( defined $extends ? %{ $extends->_palette_copy } : () ), %own_palette );
404  
405 %_own_slot = _checked_slots($slots);
406 %_own_variant = _checked_variants($variants);
407 $self->_compile;
408 ( $palette, $slots, $variants ) = ( undef, undef, undef );
409 }
410  
411 sub _theme_of ( $what, $value ) {
412 return $value if blessed $value && $value->isa(__PACKAGE__);
413 die "Term::Fabulous::Theme: $what must be a Term::Fabulous::Theme or the name of a built-in theme, got " . describe($value)
414 unless defined $value && !ref $value;
415 my $theme = __PACKAGE__->builtin($value);
416 die "Term::Fabulous::Theme: $what names an unknown built-in theme '$value' (known: " . join( ', ', builtin_names() ) . ")" unless defined $theme;
417 return $theme;
418 }
419  
420 # The palette a theme sets: every token for a built-in theme, any
421 # subset otherwise.
422 sub _checked_palette ( $palette, $builtin_name ) {
423 die "Term::Fabulous::Theme: palette must be a hash reference of token names and colors, got " . describe($palette) unless ref $palette eq 'HASH';
424 my @unknown = grep { !exists $PALETTE{$_} } sort keys %$palette;
425 die "Term::Fabulous::Theme: palette does not know the token" . ( @unknown > 1 ? 's' : '' ) . " @unknown (known: " . join( ', ', tokens() ) . ")" if @unknown;
426 return map { $_ => $PALETTE{$_}{$builtin_name} } tokens() if defined $builtin_name;
427 return map { $_ => color( __PACKAGE__, "palette token $_", $palette->{$_} ) } sort keys %$palette;
428 }
429  
430 # Slot overrides as { family => { "slot.state" => raw spec } }. A key is
431 # "family.slot" or "family.slot.state".
432 sub _checked_slots ($slots) {
433 die "Term::Fabulous::Theme: slots must be a hash reference, got " . describe($slots) unless ref $slots eq 'HASH';
434 my %by_family;
435 foreach my $key ( sort keys %$slots ) {
436 my ( $family, $slot, $state ) = _split_slot_key($key);
437 $by_family{$family}{"$slot.$state"} = _checked_spec( $family, $slot, $state, $slots->{$key} );
438 }
439 return %by_family;
440 }
441  
442 # Variant overrides as { "family.variant" => { "slot.state" => raw spec } }.
443 sub _checked_variants ($variants) {
444 die "Term::Fabulous::Theme: variants must be a hash reference, got " . describe($variants) unless ref $variants eq 'HASH';
445 my %by_variant;
446 foreach my $key ( sort keys %$variants ) {
447 my ( $family, $variant ) = $key =~ /\A([a-z_]+)\.([A-Za-z0-9_-]+)\z/
448 or die "Term::Fabulous::Theme: a variant key must be 'family.variant', got " . describe($key);
449 die "Term::Fabulous::Theme: variant '$key' names an unknown family '$family' (known: " . join( ', ', families() ) . ")" unless is_family($family);
450 my $overrides = $variants->{$key};
451 die "Term::Fabulous::Theme: variant '$key' must be a hash reference of slots, got " . describe($overrides) unless ref $overrides eq 'HASH';
452 foreach my $slot_key ( sort keys %$overrides ) {
453 my ( $slot, $state ) = _split_slot_name( $family, $slot_key, "variant '$key'" );
454 $by_variant{$key}{"$slot.$state"} = _checked_spec( $family, $slot, $state, $overrides->{$slot_key} );
455 }
456 }
457 return %by_variant;
458 }
459  
460 sub _split_slot_key ($key) {
461 my ( $family, $rest ) = ( $key // '' ) =~ /\A([a-z_]+)\.(.+)\z/
462 or die "Term::Fabulous::Theme: a slot key must be 'family.slot' or 'family.slot.state', got " . describe($key);
463 die "Term::Fabulous::Theme: slot '$key' names an unknown family '$family' (known: " . join( ', ', families() ) . ")" unless is_family($family);
464 return ( $family, _split_slot_name( $family, $rest, "slot '$key'" ) );
465 }
466  
467 # ( slot, state ) of "slot" or "slot.state" within a family.
468 sub _split_slot_name ( $family, $name, $what ) {
469 my $slots = _slots_of($family);
470 return ( $name, 'normal' ) if exists $slots->{$name};
471 if ( my ( $slot, $state ) = $name =~ /\A(.+)\.([a-z]+)\z/ ) {
472 if ( exists $slots->{$slot} ) {
473 my @states = grep { $_ ne 'normal' } sort keys %{ $slots->{$slot}{default} };
474 die "Term::Fabulous::Theme: $what: the slot $family.$slot has no state '$state' (" . ( @states ? 'its states: ' . join( ', ', @states ) : 'it has the normal state only' ) . ")"
475 unless exists $slots->{$slot}{default}{$state};
476 return ( $slot, $state );
477 }
478 }
479 die "Term::Fabulous::Theme: $what: the family $family has no slot '$name' (known: " . join( ', ', sort keys %$slots ) . ")";
480 }
481  
482 # A raw spec a theme may give: undef or 'none' (no color or style)
483 # unless the slot is required, a token name, 'reverse' where the slot
484 # allows it, a style name or item for a style slot (one with joints for
485 # a grid slot), true, false, 1 or 0 for a boolean slot, any color for
486 # a color slot. Returned normalized; a boolean as 'true' or 'false'.
487 sub _checked_spec ( $family, $slot, $state, $value ) {
488 my $definition = _slots_of($family)->{$slot};
489 my $what = "$family.$slot" . ( $state eq 'normal' ? '' : ".$state" );
490 if ( !defined $value || ( !ref $value && $value eq 'none' ) ) {
491 die "Term::Fabulous::Theme: $what cannot be 'none'" if $definition->{required};
492 return 'none';
493 }
494 if ( !ref $value && $value eq 'reverse' ) {
495 die "Term::Fabulous::Theme: $what cannot be 'reverse' (only button.background can)" unless $definition->{reverse};
496 return 'reverse';
497 }
498 if ( $definition->{kind} eq 'style' ) {
499 my %allowed = ( ( $definition->{required} ? () : ( none => 'none' ) ), ( $definition->{grid} ? ( grid => 1 ) : () ) );
500 return border_style( __PACKAGE__, $what, $value, %allowed );
501 }
502 if ( $definition->{kind} eq 'boolean' ) {
503 die "Term::Fabulous::Theme: $what must be true, false, 1 or 0, got " . describe($value) unless !ref $value && exists $BOOLEAN_SPEC{$value};
504 return $BOOLEAN_SPEC{$value};
505 }
506 return $value if !ref $value && exists $PALETTE{$value};
507 return color( __PACKAGE__, $what, $value );
508 }
509  
510 # ---------------------------------------------------------------------
511 # The compile step
512 # ---------------------------------------------------------------------
513  
514 method _palette_copy () {
515 return { map { $_ => [ @{ $_palette{$_} } ] } keys %_palette };
516 }
517  
518 # The raw spec of a slot and state: this theme's own, the parent's, or
519 # the vocabulary's default.
520 method _raw ( $family, $key ) {
521 return $_own_slot{$family}{$key} if exists $_own_slot{$family}{$key};
522 return $extends->_raw( $family, $key ) if defined $extends;
523 my ( $slot, $state ) = $key =~ /\A(.+)\.([a-z]+)\z/;
524 return _slots_of($family)->{$slot}{default}{$state};
525 }
526  
527 # The raw spec a variant sets for a slot and state, in this theme or
528 # an ancestor; undef when none does.
529 method _raw_variant ( $variant_key, $key ) {
530 return $_own_variant{$variant_key}{$key} if exists $_own_variant{$variant_key}{$key};
531 return $extends->_raw_variant( $variant_key, $key ) if defined $extends;
532 return undef;
533 }
534  
535 method _variant_keys () {
536 my %seen = ( ( defined $extends ? map { $_ => 1 } $extends->_variant_keys : () ), map { $_ => 1 } keys %_own_variant );
537 my @keys = sort keys %seen;
538 return @keys;
539 }
540  
541 method _compile () {
542 foreach my $family ( families() ) {
543 my $slots = _slots_of($family);
544 my %raw;
545 foreach my $slot ( keys %$slots ) {
546 $raw{"$slot.$_"} = $self->_raw( $family, "$slot.$_" ) foreach keys %{ $slots->{$slot}{default} };
547 }
548 $_table{$family} = $self->_resolved_table( $family, \%raw );
549 }
550  
551 foreach my $variant_key ( $self->_variant_keys ) {
552 my ($family) = split /\./, $variant_key;
553 my $slots = _slots_of($family);
554 my %raw;
555 foreach my $slot ( keys %$slots ) {
556 foreach my $state ( keys %{ $slots->{$slot}{default} } ) {
557 my $own = $self->_raw_variant( $variant_key, "$slot.$state" ) // next;
558 $raw{"$slot.$state"} = $own;
559 }
560 }
561 $_variant_table{$variant_key} = $self->_resolved_table( $family, \%raw );
562 }
563 return;
564 }
565  
566 # The values of a raw table. A state whose spec is 'normal' gets no
567 # entry: readers fall back to the slot's normal value, which lets an
568 # explicit normal color of a widget show in such a state too.
569 method _resolved_table ( $family, $raw ) {
570 my %table;
571 foreach my $key ( keys %$raw ) {
572 my $spec = $raw->{$key};
573 next if !ref $spec && $spec eq 'normal';
574 $table{$key} = $self->_resolved_value( $family, $key, $spec );
575 }
576 return \%table;
577 }
578  
579 method _resolved_value ( $family, $key, $spec ) {
580 return $spec if ref $spec; # a color as [r, g, b, a] or a BorderStyle item, checked when it was given
581 return undef if $spec eq 'none';
582 return 'reverse' if $spec eq 'reverse';
583 return $_palette{$spec} if exists $PALETTE{$spec};
584 my ($slot) = $key =~ /\A(.+)\.[a-z]+\z/;
585 my $kind = _slots_of($family)->{$slot}{kind};
586 return Term::Fabulous::Enum::BorderStyle->from_name($spec) if $kind eq 'style';
587 return $spec eq 'true' ? 1 : 0 if $kind eq 'boolean';
588 die "Term::Fabulous::Theme: internal: no value for $family.$key from '$spec'";
589 }
590  
591 # ---------------------------------------------------------------------
592 # Reading
593 # ---------------------------------------------------------------------
594  
595 method extends () {
596 return $extends;
597 }
598  
599 method palette () {
600 return $self->_palette_copy;
601 }
602  
603 method token ($token) {
604 die "Term::Fabulous::Theme: unknown token " . describe($token) . " (known: " . join( ', ', tokens() ) . ")" unless defined $token && !ref $token && exists $_palette{$token};
605 return [ @{ $_palette{$token} } ];
606 }
607  
608 method has_variant ( $family, $variant ) {
609 return exists $_variant_table{"$family.$variant"} ? 1 : 0;
610 }
611  
612 # The looks of a family for a widget with the given classes: the
613 # family's table, overlaid with the variant of every class that has
614 # one, in class order. The result is shared and must not be changed.
615 method look_table ( $family, $classes = [] ) {
616 die "Term::Fabulous::Theme: unknown family " . describe($family) . " (known: " . join( ', ', families() ) . ")" unless is_family($family);
617 die "Term::Fabulous::Theme: classes must be an array reference, got " . describe($classes) unless ref $classes eq 'ARRAY';
618 my @variants = grep { exists $_variant_table{$_} } map { "$family.$_" } @$classes;
619 return $_table{$family} unless @variants;
620  
621 my $cache_key = join "\x{1F}", @variants;
622 return $_look_table_cache{$cache_key} //= { %{ $_table{$family} }, map { %{ $_variant_table{$_} } } @variants };
623 }
624  
625 method look ( $family, $slot, $state = 'normal', $classes = [] ) {
626 die "Term::Fabulous::Theme: the family " . ( $family // 'undef' ) . " has no slot " . describe($slot) . " with the state " . describe($state) unless has_slot( $family, $slot, $state );
627 my $table = $self->look_table( $family, $classes );
628 return exists $table->{"$slot.$state"} ? $table->{"$slot.$state"} : $table->{"$slot.normal"};
629 }
630  
631 # ---------------------------------------------------------------------
632 # Built-in themes
633 # ---------------------------------------------------------------------
634  
635 sub builtin_names () {
636 return qw(dark light);
637 }
638  
639 method builtin :common ($theme_name) {
640 return undef unless defined $theme_name && !ref $theme_name && grep { $_ eq $theme_name } builtin_names();
641 return $builtin{$theme_name} //= $class->new( _builtin => $theme_name );
642 }
643  
644 method default :common () {
645 return $class->builtin('dark');
646 }
647  
648 # ---------------------------------------------------------------------
649 # Theme files
650 # ---------------------------------------------------------------------
651  
652 method from_file :common ($path) {
653 die "Term::Fabulous::Theme: from_file needs a file name, got " . describe($path) unless defined $path && !ref $path && length $path;
654 return _from_source( _open_file($path), "theme file '$path'" );
655 }
656  
657 # Text::KDL::XS reads a filehandle as UTF-8 bytes, a string as characters.
658 sub _open_file ($path) {
659 open my $handle, '<:raw', $path or die "Term::Fabulous::Theme: cannot open '$path': $!";
660 return $handle;
661 }
662  
663 method from_string :common ($kdl) {
664 die "Term::Fabulous::Theme: from_string needs a string, got " . describe($kdl) unless defined $kdl && !ref $kdl;
665 return _from_source( $kdl, 'theme' );
666 }
667  
668 sub _from_source ( $source, $where ) {
669 my $document;
670 try {
671 $document = parse_kdl($source);
672 }
673 catch ($error) {
674 die "Term::Fabulous::Theme: $where: failed to parse KDL: $error";
675 }
676 my $theme;
677 try {
678 $theme = __PACKAGE__->new( _parse_document($document) );
679 }
680 catch ($error) {
681 chomp $error;
682 $error =~ s/\ATerm::Fabulous::Theme: //;
683 $error =~ s/ at \S+ line \d+\.?\z//;
684 die "Term::Fabulous::Theme: $where: $error\n";
685 }
686 return $theme;
687 }
688  
689 # The constructor parameters a theme document describes.
690 sub _parse_document ($document) {
691 my ( %params, %slots, %variants, $saw_theme, $saw_palette );
692 foreach my $node ( $document->nodes->@* ) {
693 my $node_name = $node->name;
694 if ( $node_name eq 'theme' ) {
695 die "the 'theme' node may appear only once" if $saw_theme++;
696 %params = ( %params, _parse_theme_node($node) );
697 next;
698 }
699 if ( $node_name eq 'palette' ) {
700 die "the 'palette' node may appear only once" if $saw_palette++;
701 $params{palette} = _parse_palette_node($node);
702 next;
703 }
704 die "unknown top-level node '$node_name' (known: theme, palette and the families " . join( ', ', families() ) . ")" unless is_family($node_name);
705 die "$node_name: a family node takes no arguments or properties" if $node->args->@* || $node->props->@*;
706 _parse_block( $node->children, $node_name, 'normal', \%slots, \%variants );
707 }
708 return ( %params, slots => \%slots, variants => \%variants );
709 }
710  
711 sub _parse_theme_node ($node) {
712 my @args = $node->args->@*;
713 die "the 'theme' node takes at most one argument, the theme's name" if @args > 1;
714 die "the theme's name must be a string" if @args && !$args[0]->is_string;
715 die "the 'theme' node has no children" if $node->children->@*;
716 my %params = ( @args ? ( name => $args[0]->value ) : () );
717 foreach my $prop ( $node->props->@* ) {
718 my ( $key, $value ) = @$prop;
719 die "the 'theme' node knows only the property 'extends', got '$key'" unless $key eq 'extends';
720 die "extends must be the name of a built-in theme" unless $value->is_string;
721 $params{extends} = $value->value;
722 }
723 return %params;
724 }
725  
726 sub _parse_palette_node ($node) {
727 die "the 'palette' node takes no arguments or properties" if $node->args->@* || $node->props->@*;
728 my %palette;
729 foreach my $kid ( $node->children->@* ) {
730 my $token = $kid->name;
731 die "palette: unknown token '$token' (known: " . join( ', ', tokens() ) . ")" unless exists $PALETTE{$token};
732 die "palette: the token '$token' is set twice" if exists $palette{$token};
733 $palette{$token} = _single_string( $kid, "palette token '$token'", childless => 1 );
734 }
735 return \%palette;
736 }
737  
738 # The nodes of a family block, or of a state or variant block in it:
739 # state blocks, variant blocks and slot nodes. A slot set twice in one
740 # document is a mistake (usually a copy), not an override.
741 sub _parse_block ( $nodes, $family, $state, $slots, $variants, $variant = undef ) {
742 foreach my $kid (@$nodes) {
743 my $kid_name = $kid->name;
744 if ( $kid_name eq 'variant' ) {
745 die "$family: a variant cannot be inside a state or another variant" if $state ne 'normal' || defined $variant;
746 my $variant_name = _single_string( $kid, "$family: a variant" );
747 die "$family: a variant name must be a word" unless $variant_name =~ /\A[A-Za-z0-9_-]+\z/;
748 _parse_block( $kid->children, $family, 'normal', $slots, $variants, $variant_name );
749 next;
750 }
751 if ( $IS_STATE{$kid_name} && $kid_name ne 'normal' ) {
752 die "$family: a state cannot be inside another state" if $state ne 'normal';
753 die "$family: a state node takes no arguments or properties" if $kid->args->@* || $kid->props->@*;
754 _parse_block( $kid->children, $family, $kid_name, $slots, $variants, $variant );
755 next;
756 }
757 foreach my $pair ( _slot_values( $kid, $family ) ) {
758 my ( $slot, $value ) = @$pair;
759 my $key = $state eq 'normal' ? $slot : "$slot.$state";
760 my $target = defined $variant ? ( $variants->{"$family.$variant"} //= {} ) : $slots;
761 my $name = defined $variant ? $key : "$family.$key";
762 die "$family: " . ( defined $variant ? "variant '$variant': " : '' ) . "the slot '$key' is set twice" if exists $target->{$name};
763 $target->{$name} = $value;
764 }
765 }
766 return;
767 }
768  
769 # [ slot, value ] pairs of a slot node: `name "value"` sets the slot
770 # name, `name key="value"` sets name.key for every property.
771 sub _slot_values ( $kid, $family ) {
772 my $kid_name = $kid->name;
773 my @args = $kid->args->@*;
774 my @props = $kid->props->@*;
775 die "$family: the node '$kid_name' has no children" if $kid->children->@*;
776 die "$family: the node '$kid_name' needs either one value or key=value properties" if ( @args && @props ) || ( !@args && !@props ) || @args > 1;
777 return ( [ $kid_name, _kdl_spec( $args[0], $family, $kid_name ) ] ) if @args;
778 return map { [ "$kid_name.$_->[0]", _kdl_spec( $_->[1], $family, "$kid_name.$_->[0]" ) ] } @props;
779 }
780  
781 # The raw spec of a KDL value: #null for none, #true or #false for a
782 # boolean slot, a string for any other slot. Checked like any spec
783 # when the theme is built.
784 sub _kdl_spec ( $value, $family, $slot ) {
785 return undef if $value->is_null;
786 my $definition = _slots_of($family)->{$slot};
787 if ( defined $definition && $definition->{kind} eq 'boolean' ) {
788 die "$family.$slot must be #true or #false" unless $value->is_bool;
789 return $value->as_perl ? 'true' : 'false';
790 }
791 die "$family.$slot must be a string or #null" unless $value->is_string;
792 return $value->value;
793 }
794  
795 # The one string argument of a node without properties (and, for a
796 # palette token, without children).
797 sub _single_string ( $kid, $what, %options ) {
798 my @args = $kid->args->@*;
799 die "$what needs exactly one string argument" unless @args == 1 && $args[0]->is_string && !$kid->props->@* && !( $options{childless} && $kid->children->@* );
800 return $args[0]->value;
801 }
802 }
803  
804 1;
805  
806 __END__
« blib/lib/Term/Fabulous/Chart/Curve.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Table/Filter.pm »