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.02';
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 3 ▶ 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 states () {
302 return ( 'normal', @STATES );
303 }
304  
305 sub is_family ($name) {
306 return defined $name && !ref $name && exists $FAMILY{$name} ? 1 : 0;
307 }
308  
309 # The slot names of a family, each with the states it has.
310 sub slots ($family) {
311 die "Term::Fabulous::Theme: unknown family " . describe($family) . " (known: " . join( ', ', families() ) . ")" unless is_family($family);
312 my $slots = _slots_of($family);
313 my %states_of;
314 foreach my $slot ( keys %$slots ) {
315 $states_of{$slot} = [ grep { $_ ne 'normal' } sort keys %{ $slots->{$slot}{default} } ];
316 }
317 return %states_of;
318 }
319  
320 sub has_slot ( $family, $slot, $state = 'normal' ) {
321 return 0 unless is_family($family) && defined $slot && defined $state && !ref $slot && !ref $state;
322 my $definition = _slots_of($family)->{$slot} // return 0;
323 return exists $definition->{default}{$state} ? 1 : 0;
324 }
325  
326 # What the vocabulary says about a slot: its kind, the values it takes
327 # besides colors or styles, its states and its defaults.
328 sub slot_info ( $family, $slot ) {
329 die "Term::Fabulous::Theme: unknown family " . describe($family) . " (known: " . join( ', ', families() ) . ")" unless is_family($family);
330 my $slots = _slots_of($family);
331 my $definition = defined $slot && !ref $slot ? $slots->{$slot} : undef;
332 die "Term::Fabulous::Theme: the family $family has no slot " . describe($slot) . " (known: " . join( ', ', sort keys %$slots ) . ")" unless defined $definition;
333 return {
334 kind => $definition->{kind},
335 required => $definition->{required} ? 1 : 0,
336 reverse => $definition->{reverse} ? 1 : 0,
337 grid => $definition->{grid} ? 1 : 0,
338 states => [ grep { $_ ne 'normal' } sort keys %{ $definition->{default} } ],
339 default => { %{ $definition->{default} } },
340 };
341 }
342  
343 # A value a theme may give a slot in a state, as the theme would store
344 # it; dies with the reason when the slot cannot take it.
345 sub check_value ( $family, $slot, $state, $value ) {
346 die "Term::Fabulous::Theme: the family " . describe($family) . " has no slot " . describe($slot) . " with the state " . describe($state) unless has_slot( $family, $slot, $state );
347 return _checked_spec( $family, $slot, $state, $value );
348 }
349  
350 # ---------------------------------------------------------------------
351 # The generation: a process-wide counter that every UI bumps when its
352 # theme is set, so that widgets know when to look their looks up again.
353 # ---------------------------------------------------------------------
354  
355 my $generation = 0;
356  
357 sub generation () {
358 return $generation;
359 }
360  
361 sub bump_generation () {
362 return ++$generation;
363 }
364  
365 # ---------------------------------------------------------------------
366 # Construction
367 # ---------------------------------------------------------------------
368  
369 my %builtin;
370  
371 field $name :param :reader = undef;
372 field $extends :param = 'dark';
373  
374 # What the theme sets itself: tokens as [r, g, b, a], and the raw specs
375 # (a token name, a color, a style, 'none', 'reverse') of its slot and
376 # variant overrides, under "family" and "family.variant".
377 field %_palette;
378 field %_own_slot;
379 field %_own_variant;
380  
381 # The result of the compile step: every slot and state of every family
382 # and variant, as values the widgets draw with.
383 field %_table;
384 field %_variant_table;
385 field %_look_table_cache;
386  
387 # The constructor parameters, read once in ADJUST and then dropped.
388 field $palette :param = {};
389 field $slots :param = {};
390 field $variants :param = {};
391 field $_builtin :param(_builtin) = undef;
392  
393 ADJUST {
394 die "Term::Fabulous::Theme: name must be a string, got " . describe($name) if defined $name && ref $name;
395 $extends = defined $_builtin ? undef : _theme_of( extends => $extends );
396 $name //= $_builtin;
397  
398 my %own_palette = _checked_palette( $palette, $_builtin );
399 %_palette = ( ( defined $extends ? %{ $extends->_palette_copy } : () ), %own_palette );
400  
401 %_own_slot = _checked_slots($slots);
402 %_own_variant = _checked_variants($variants);
403 $self->_compile;
404 ( $palette, $slots, $variants ) = ( undef, undef, undef );
405 }
406  
407 sub _theme_of ( $what, $value ) {
408 return $value if blessed $value && $value->isa(__PACKAGE__);
409 die "Term::Fabulous::Theme: $what must be a Term::Fabulous::Theme or the name of a built-in theme, got " . describe($value)
410 unless defined $value && !ref $value;
411 my $theme = __PACKAGE__->builtin($value);
412 die "Term::Fabulous::Theme: $what names an unknown built-in theme '$value' (known: " . join( ', ', builtin_names() ) . ")" unless defined $theme;
413 return $theme;
414 }
415  
416 # The palette a theme sets: every token for a built-in theme, any
417 # subset otherwise.
418 sub _checked_palette ( $palette, $builtin_name ) {
419 die "Term::Fabulous::Theme: palette must be a hash reference of token names and colors, got " . describe($palette) unless ref $palette eq 'HASH';
420 my @unknown = grep { !exists $PALETTE{$_} } sort keys %$palette;
421 die "Term::Fabulous::Theme: palette does not know the token" . ( @unknown > 1 ? 's' : '' ) . " @unknown (known: " . join( ', ', tokens() ) . ")" if @unknown;
422 return map { $_ => $PALETTE{$_}{$builtin_name} } tokens() if defined $builtin_name;
423 return map { $_ => color( __PACKAGE__, "palette token $_", $palette->{$_} ) } sort keys %$palette;
424 }
425  
426 # Slot overrides as { family => { "slot.state" => raw spec } }. A key is
427 # "family.slot" or "family.slot.state".
428 sub _checked_slots ($slots) {
429 die "Term::Fabulous::Theme: slots must be a hash reference, got " . describe($slots) unless ref $slots eq 'HASH';
430 my %by_family;
431 foreach my $key ( sort keys %$slots ) {
432 my ( $family, $slot, $state ) = _split_slot_key($key);
433 $by_family{$family}{"$slot.$state"} = _checked_spec( $family, $slot, $state, $slots->{$key} );
434 }
435 return %by_family;
436 }
437  
438 # Variant overrides as { "family.variant" => { "slot.state" => raw spec } }.
439 sub _checked_variants ($variants) {
440 die "Term::Fabulous::Theme: variants must be a hash reference, got " . describe($variants) unless ref $variants eq 'HASH';
441 my %by_variant;
442 foreach my $key ( sort keys %$variants ) {
443 my ( $family, $variant ) = $key =~ /\A([a-z_]+)\.([A-Za-z0-9_-]+)\z/
444 or die "Term::Fabulous::Theme: a variant key must be 'family.variant', got " . describe($key);
445 die "Term::Fabulous::Theme: variant '$key' names an unknown family '$family' (known: " . join( ', ', families() ) . ")" unless is_family($family);
446 my $overrides = $variants->{$key};
447 die "Term::Fabulous::Theme: variant '$key' must be a hash reference of slots, got " . describe($overrides) unless ref $overrides eq 'HASH';
448 foreach my $slot_key ( sort keys %$overrides ) {
449 my ( $slot, $state ) = _split_slot_name( $family, $slot_key, "variant '$key'" );
450 $by_variant{$key}{"$slot.$state"} = _checked_spec( $family, $slot, $state, $overrides->{$slot_key} );
451 }
452 }
453 return %by_variant;
454 }
455  
456 sub _split_slot_key ($key) {
457 my ( $family, $rest ) = ( $key // '' ) =~ /\A([a-z_]+)\.(.+)\z/
458 or die "Term::Fabulous::Theme: a slot key must be 'family.slot' or 'family.slot.state', got " . describe($key);
459 die "Term::Fabulous::Theme: slot '$key' names an unknown family '$family' (known: " . join( ', ', families() ) . ")" unless is_family($family);
460 return ( $family, _split_slot_name( $family, $rest, "slot '$key'" ) );
461 }
462  
463 # ( slot, state ) of "slot" or "slot.state" within a family.
464 sub _split_slot_name ( $family, $name, $what ) {
465 my $slots = _slots_of($family);
466 return ( $name, 'normal' ) if exists $slots->{$name};
467 if ( my ( $slot, $state ) = $name =~ /\A(.+)\.([a-z]+)\z/ ) {
468 if ( exists $slots->{$slot} ) {
469 my @states = grep { $_ ne 'normal' } sort keys %{ $slots->{$slot}{default} };
470 die "Term::Fabulous::Theme: $what: the slot $family.$slot has no state '$state' (" . ( @states ? 'its states: ' . join( ', ', @states ) : 'it has the normal state only' ) . ")"
471 unless exists $slots->{$slot}{default}{$state};
472 return ( $slot, $state );
473 }
474 }
475 die "Term::Fabulous::Theme: $what: the family $family has no slot '$name' (known: " . join( ', ', sort keys %$slots ) . ")";
476 }
477  
478 # A raw spec a theme may give: undef or 'none' (no color or style)
479 # unless the slot is required, a token name, 'reverse' where the slot
480 # allows it, a style name or item for a style slot (one with joints for
481 # a grid slot), true, false, 1 or 0 for a boolean slot, any color for
482 # a color slot. Returned normalized; a boolean as 'true' or 'false'.
483 sub _checked_spec ( $family, $slot, $state, $value ) {
484 my $definition = _slots_of($family)->{$slot};
485 my $what = "$family.$slot" . ( $state eq 'normal' ? '' : ".$state" );
486 if ( !defined $value || ( !ref $value && $value eq 'none' ) ) {
487 die "Term::Fabulous::Theme: $what cannot be 'none'" if $definition->{required};
488 return 'none';
489 }
490 if ( !ref $value && $value eq 'reverse' ) {
491 die "Term::Fabulous::Theme: $what cannot be 'reverse' (only button.background can)" unless $definition->{reverse};
492 return 'reverse';
493 }
494 if ( $definition->{kind} eq 'style' ) {
495 my %allowed = ( ( $definition->{required} ? () : ( none => 'none' ) ), ( $definition->{grid} ? ( grid => 1 ) : () ) );
496 return border_style( __PACKAGE__, $what, $value, %allowed );
497 }
498 if ( $definition->{kind} eq 'boolean' ) {
499 die "Term::Fabulous::Theme: $what must be true, false, 1 or 0, got " . describe($value) unless !ref $value && exists $BOOLEAN_SPEC{$value};
500 return $BOOLEAN_SPEC{$value};
501 }
502 return $value if !ref $value && exists $PALETTE{$value};
503 return color( __PACKAGE__, $what, $value );
504 }
505  
506 # ---------------------------------------------------------------------
507 # The compile step
508 # ---------------------------------------------------------------------
509  
510 method _palette_copy () {
511 return { map { $_ => [ @{ $_palette{$_} } ] } keys %_palette };
512 }
513  
514 # The raw spec of a slot and state: this theme's own, the parent's, or
515 # the vocabulary's default.
516 method _raw ( $family, $key ) {
517 return $_own_slot{$family}{$key} if exists $_own_slot{$family}{$key};
518 return $extends->_raw( $family, $key ) if defined $extends;
519 my ( $slot, $state ) = $key =~ /\A(.+)\.([a-z]+)\z/;
520 return _slots_of($family)->{$slot}{default}{$state};
521 }
522  
523 # The raw spec a variant sets for a slot and state, in this theme or
524 # an ancestor; undef when none does.
525 method _raw_variant ( $variant_key, $key ) {
526 return $_own_variant{$variant_key}{$key} if exists $_own_variant{$variant_key}{$key};
527 return $extends->_raw_variant( $variant_key, $key ) if defined $extends;
528 return undef;
529 }
530  
531 method _variant_keys () {
532 my %seen = ( ( defined $extends ? map { $_ => 1 } $extends->_variant_keys : () ), map { $_ => 1 } keys %_own_variant );
533 my @keys = sort keys %seen;
534 return @keys;
535 }
536  
537 method _compile () {
538 foreach my $family ( families() ) {
539 my $slots = _slots_of($family);
540 my %raw;
541 foreach my $slot ( keys %$slots ) {
542 $raw{"$slot.$_"} = $self->_raw( $family, "$slot.$_" ) foreach keys %{ $slots->{$slot}{default} };
543 }
544 $_table{$family} = $self->_resolved_table( $family, \%raw );
545 }
546  
547 foreach my $variant_key ( $self->_variant_keys ) {
548 my ($family) = split /\./, $variant_key;
549 my $slots = _slots_of($family);
550 my %raw;
551 foreach my $slot ( keys %$slots ) {
552 foreach my $state ( keys %{ $slots->{$slot}{default} } ) {
553 my $own = $self->_raw_variant( $variant_key, "$slot.$state" ) // next;
554 $raw{"$slot.$state"} = $own;
555 }
556 }
557 $_variant_table{$variant_key} = $self->_resolved_table( $family, \%raw );
558 }
559 return;
560 }
561  
562 # The values of a raw table. A state whose spec is 'normal' gets no
563 # entry: readers fall back to the slot's normal value, which lets an
564 # explicit normal color of a widget show in such a state too.
565 method _resolved_table ( $family, $raw ) {
566 my %table;
567 foreach my $key ( keys %$raw ) {
568 my $spec = $raw->{$key};
569 next if !ref $spec && $spec eq 'normal';
570 $table{$key} = $self->_resolved_value( $family, $key, $spec );
571 }
572 return \%table;
573 }
574  
575 method _resolved_value ( $family, $key, $spec ) {
576 return $spec if ref $spec; # a color as [r, g, b, a] or a BorderStyle item, checked when it was given
577 return undef if $spec eq 'none';
578 return 'reverse' if $spec eq 'reverse';
579 return $_palette{$spec} if exists $PALETTE{$spec};
580 my ($slot) = $key =~ /\A(.+)\.[a-z]+\z/;
581 my $kind = _slots_of($family)->{$slot}{kind};
582 return Term::Fabulous::Enum::BorderStyle->from_name($spec) if $kind eq 'style';
583 return $spec eq 'true' ? 1 : 0 if $kind eq 'boolean';
584 die "Term::Fabulous::Theme: internal: no value for $family.$key from '$spec'";
585 }
586  
587 # ---------------------------------------------------------------------
588 # Reading
589 # ---------------------------------------------------------------------
590  
591 method extends () {
592 return $extends;
593 }
594  
595 method palette () {
596 return $self->_palette_copy;
597 }
598  
599 method token ($token) {
600 die "Term::Fabulous::Theme: unknown token " . describe($token) . " (known: " . join( ', ', tokens() ) . ")" unless defined $token && !ref $token && exists $_palette{$token};
601 return [ @{ $_palette{$token} } ];
602 }
603  
604 method has_variant ( $family, $variant ) {
605 return exists $_variant_table{"$family.$variant"} ? 1 : 0;
606 }
607  
608 # The looks of a family for a widget with the given classes: the
609 # family's table, overlaid with the variant of every class that has
610 # one, in class order. The result is shared and must not be changed.
611 method look_table ( $family, $classes = [] ) {
612 die "Term::Fabulous::Theme: unknown family " . describe($family) . " (known: " . join( ', ', families() ) . ")" unless is_family($family);
613 die "Term::Fabulous::Theme: classes must be an array reference, got " . describe($classes) unless ref $classes eq 'ARRAY';
614 my @variants = grep { exists $_variant_table{$_} } map { "$family.$_" } @$classes;
615 return $_table{$family} unless @variants;
616  
617 my $cache_key = join "\x{1F}", @variants;
618 return $_look_table_cache{$cache_key} //= { %{ $_table{$family} }, map { %{ $_variant_table{$_} } } @variants };
619 }
620  
621 method look ( $family, $slot, $state = 'normal', $classes = [] ) {
622 die "Term::Fabulous::Theme: the family " . ( $family // 'undef' ) . " has no slot " . describe($slot) . " with the state " . describe($state) unless has_slot( $family, $slot, $state );
623 my $table = $self->look_table( $family, $classes );
624 return exists $table->{"$slot.$state"} ? $table->{"$slot.$state"} : $table->{"$slot.normal"};
625 }
626  
627 # ---------------------------------------------------------------------
628 # Built-in themes
629 # ---------------------------------------------------------------------
630  
631 sub builtin_names () {
632 return qw(dark light);
633 }
634  
635 method builtin :common ($theme_name) {
636 return undef unless defined $theme_name && !ref $theme_name && grep { $_ eq $theme_name } builtin_names();
637 return $builtin{$theme_name} //= $class->new( _builtin => $theme_name );
638 }
639  
640 method default :common () {
641 return $class->builtin('dark');
642 }
643  
644 # ---------------------------------------------------------------------
645 # Theme files
646 # ---------------------------------------------------------------------
647  
648 method from_file :common ($path) {
649 die "Term::Fabulous::Theme: from_file needs a file name, got " . describe($path) unless defined $path && !ref $path && length $path;
650 return _from_source( _open_file($path), "theme file '$path'" );
651 }
652  
653 # Text::KDL::XS reads a filehandle as UTF-8 bytes, a string as characters.
654 sub _open_file ($path) {
655 open my $handle, '<:raw', $path or die "Term::Fabulous::Theme: cannot open '$path': $!";
656 return $handle;
657 }
658  
659 method from_string :common ($kdl) {
660 die "Term::Fabulous::Theme: from_string needs a string, got " . describe($kdl) unless defined $kdl && !ref $kdl;
661 return _from_source( $kdl, 'theme' );
662 }
663  
664 sub _from_source ( $source, $where ) {
665 my $document;
666 try {
667 $document = parse_kdl($source);
668 }
669 catch ($error) {
670 die "Term::Fabulous::Theme: $where: failed to parse KDL: $error";
671 }
672 my $theme;
673 try {
674 $theme = __PACKAGE__->new( _parse_document($document) );
675 }
676 catch ($error) {
677 chomp $error;
678 $error =~ s/\ATerm::Fabulous::Theme: //;
679 $error =~ s/ at \S+ line \d+\.?\z//;
680 die "Term::Fabulous::Theme: $where: $error\n";
681 }
682 return $theme;
683 }
684  
685 # The constructor parameters a theme document describes.
686 sub _parse_document ($document) {
687 my ( %params, %slots, %variants, $saw_theme, $saw_palette );
688 foreach my $node ( $document->nodes->@* ) {
689 my $node_name = $node->name;
690 if ( $node_name eq 'theme' ) {
691 die "the 'theme' node may appear only once" if $saw_theme++;
692 %params = ( %params, _parse_theme_node($node) );
693 next;
694 }
695 if ( $node_name eq 'palette' ) {
696 die "the 'palette' node may appear only once" if $saw_palette++;
697 $params{palette} = _parse_palette_node($node);
698 next;
699 }
700 die "unknown top-level node '$node_name' (known: theme, palette and the families " . join( ', ', families() ) . ")" unless is_family($node_name);
701 die "$node_name: a family node takes no arguments or properties" if $node->args->@* || $node->props->@*;
702 _parse_block( $node->children, $node_name, 'normal', \%slots, \%variants );
703 }
704 return ( %params, slots => \%slots, variants => \%variants );
705 }
706  
707 sub _parse_theme_node ($node) {
708 my @args = $node->args->@*;
709 die "the 'theme' node takes at most one argument, the theme's name" if @args > 1;
710 die "the theme's name must be a string" if @args && !$args[0]->is_string;
711 die "the 'theme' node has no children" if $node->children->@*;
712 my %params = ( @args ? ( name => $args[0]->value ) : () );
713 foreach my $prop ( $node->props->@* ) {
714 my ( $key, $value ) = @$prop;
715 die "the 'theme' node knows only the property 'extends', got '$key'" unless $key eq 'extends';
716 die "extends must be the name of a built-in theme" unless $value->is_string;
717 $params{extends} = $value->value;
718 }
719 return %params;
720 }
721  
722 sub _parse_palette_node ($node) {
723 die "the 'palette' node takes no arguments or properties" if $node->args->@* || $node->props->@*;
724 my %palette;
725 foreach my $kid ( $node->children->@* ) {
726 my $token = $kid->name;
727 die "palette: unknown token '$token' (known: " . join( ', ', tokens() ) . ")" unless exists $PALETTE{$token};
728 die "palette: the token '$token' is set twice" if exists $palette{$token};
729 $palette{$token} = _single_string( $kid, "palette token '$token'" );
730 }
731 return \%palette;
732 }
733  
734 # The nodes of a family block, or of a state or variant block in it:
735 # state blocks, variant blocks and slot nodes. A slot set twice in one
736 # document is a mistake (usually a copy), not an override.
737 sub _parse_block ( $nodes, $family, $state, $slots, $variants, $variant = undef ) {
738 foreach my $kid (@$nodes) {
739 my $kid_name = $kid->name;
740 if ( $kid_name eq 'variant' ) {
741 die "$family: a variant cannot be inside a state or another variant" if $state ne 'normal' || defined $variant;
742 my $variant_name = _single_string( $kid, "$family: a variant" );
743 die "$family: a variant name must be a word" unless $variant_name =~ /\A[A-Za-z0-9_-]+\z/;
744 _parse_block( $kid->children, $family, 'normal', $slots, $variants, $variant_name );
745 next;
746 }
747 if ( $IS_STATE{$kid_name} && $kid_name ne 'normal' ) {
748 die "$family: a state cannot be inside another state" if $state ne 'normal';
749 die "$family: a state node takes no arguments or properties" if $kid->args->@* || $kid->props->@*;
750 _parse_block( $kid->children, $family, $kid_name, $slots, $variants, $variant );
751 next;
752 }
753 foreach my $pair ( _slot_values( $kid, $family ) ) {
754 my ( $slot, $value ) = @$pair;
755 my $key = $state eq 'normal' ? $slot : "$slot.$state";
756 my $target = defined $variant ? ( $variants->{"$family.$variant"} //= {} ) : $slots;
757 my $name = defined $variant ? $key : "$family.$key";
758 die "$family: " . ( defined $variant ? "variant '$variant': " : '' ) . "the slot '$key' is set twice" if exists $target->{$name};
759 $target->{$name} = $value;
760 }
761 }
762 return;
763 }
764  
765 # [ slot, value ] pairs of a slot node: `name "value"` sets the slot
766 # name, `name key="value"` sets name.key for every property.
767 sub _slot_values ( $kid, $family ) {
768 my $kid_name = $kid->name;
769 my @args = $kid->args->@*;
770 my @props = $kid->props->@*;
771 die "$family: the node '$kid_name' has no children" if $kid->children->@*;
772 die "$family: the node '$kid_name' needs either one value or key=value properties" if ( @args && @props ) || ( !@args && !@props ) || @args > 1;
773 return ( [ $kid_name, _kdl_spec( $args[0], $family, $kid_name ) ] ) if @args;
774 return map { [ "$kid_name.$_->[0]", _kdl_spec( $_->[1], $family, "$kid_name.$_->[0]" ) ] } @props;
775 }
776  
777 # The raw spec of a KDL value: #null for none, #true or #false for a
778 # boolean slot, a string for any other slot. Checked like any spec
779 # when the theme is built.
780 sub _kdl_spec ( $value, $family, $slot ) {
781 return undef if $value->is_null;
782 my $definition = _slots_of($family)->{$slot};
783 if ( defined $definition && $definition->{kind} eq 'boolean' ) {
784 die "$family.$slot must be #true or #false" unless $value->is_bool;
785 return $value->as_perl ? 'true' : 'false';
786 }
787 die "$family.$slot must be a string or #null" unless $value->is_string;
788 return $value->value;
789 }
790  
791 sub _single_string ( $kid, $what ) {
792 my @args = $kid->args->@*;
793 die "$what needs exactly one string argument" unless @args == 1 && $args[0]->is_string && !$kid->props->@*;
794 return $args[0]->value;
795 }
796 }
797  
798 1;
799  
800 __END__
« blib/lib/Term/Fabulous/Chart/Curve.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Table/Filter.pm »