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

filter: Statementstmt 0.0% 0 / 387 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 42 Podpod n/a totaltotal 0.0% 0 / 511 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::ThemeEditor::Preview;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.03';
9  
10 use Object::Pad 0.825;
11  
12 use Term::Fabulous::Widget::Box;
13  
14 # The left side of the theme editor: a widget of every family, which the
15 # user can try out, drawn in the theme being edited.
16 class Term::Fabulous::ThemeEditor::Preview :isa(Term::Fabulous::Widget::Box) :strict(params) {
17 use Clay::UI::Enum::Result;
18 use Clay::XS qw(sizing_fixed sizing_grow CLAY_LEFT_TO_RIGHT CLAY_TOP_TO_BOTTOM CLAY_LEFT_TO_RIGHT_WRAP CLAY_TEXT_WRAP_NONE);
19 use List::Util qw(max min);
20 use Scalar::Util qw(blessed refaddr weaken);
21 use Term::Fabulous::Theme;
22 use Term::Fabulous::Widget::Accordion;
23 use Term::Fabulous::Widget::Accordion::Item;
24 use Term::Fabulous::Widget::Button;
25 use Term::Fabulous::Widget::Checkbox;
26 use Term::Fabulous::Widget::ColorPicker;
27 use Term::Fabulous::Widget::Dialog;
28 use Term::Fabulous::Widget::Divider;
29 use Term::Fabulous::Widget::DocumentTabs;
30 use Term::Fabulous::Widget::Dropdown;
31 use Term::Fabulous::Widget::Image;
32 use Term::Fabulous::Widget::Menu;
33 use Term::Fabulous::Widget::MenuBar;
34 use Term::Fabulous::Widget::ProgressBar;
35 use Term::Fabulous::Widget::RadioButton;
36 use Term::Fabulous::Widget::RadioGroup;
37 use Term::Fabulous::Widget::RichText;
38 use Term::Fabulous::Widget::ScrollBox;
39 use Term::Fabulous::Widget::SegmentedControl;
40 use Term::Fabulous::Widget::Slider;
41 use Term::Fabulous::Widget::Spinner;
42 use Term::Fabulous::Widget::StarRating;
43 use Term::Fabulous::Widget::StatusBar;
44 use Term::Fabulous::Widget::Table;
45 use Term::Fabulous::Widget::Tabs;
46 use Term::Fabulous::Widget::Tabs::Page;
47 use Term::Fabulous::Widget::Text;
48 use Term::Fabulous::Widget::TextArea;
49 use Term::Fabulous::Widget::TextField;
50 use Term::Fabulous::Widget::Toast;
51  
52 use constant GALLERY_ID => 'theme-editor-preview';
53  
54 # A picture of 12 by 8 pixels, a ring of colors, for the image family,
55 # as TGA: Imager reads it without a plugin, so the preview does not
56 # depend on the PNG support Imager was built with.
57 use constant PICTURE =>
58 'AAAKAAAAAAAAAAAADAAIACAoggAAAAAFVPKj/1TyVP+j8lT/8vJU//KjVP/yVFT/ggAAAAALAAAAAAAAAABU8tj/VPKJ/2/yVP+98lT/8thU//KJVP/yVG//8lS9/wAAAAAAAAAACwAAAABU2PL/VPK9/1Tyb/+J8lT/2PJU//K9VP/yb1T/8lSJ//JU2P+9VPL/AAAAAAtUo/L/VPLy/1Tyo/9U8lT/o/JU//LyVP/yo1T/8lRU//JUo//yVPL/o1Ty/1RU8v8LVL3y/1Ty2P9U8on/b/JU/73yVP/y2FT/8olU//JUb//yVL3/2FTy/4lU8v9Ub/L/CwAAAABU8r3/VPJv/4nyVP/Y8lT/8r1U//JvVP/yVIn/8lTY/71U8v9vVPL/AAAAAAsAAAAAAAAAAFTyVP+j8lT/8vJU//KjVP/yVFT/8lSj//JU8v+jVPL/AAAAAAAAAACCAAAAAAW98lT/8thU//KJVP/yVG//8lS9/9hU8v+CAAAAAA==';
59  
60 # The sections of the gallery, in order: id, title, and the families
61 # they show.
62 my @SECTIONS = (
63 [ palette => 'Palette', [] ],
64 [ text => 'Text', ['text'] ],
65 [ box => 'Boxes', ['box'] ],
66 [ button => 'Buttons', ['button'] ],
67 [ fields => 'Fields', [ 'text_input', 'scrollbar' ] ],
68 [ choices => 'Choices', [ 'input', 'dropdown' ] ],
69 [ color_picker => 'Color picker', [] ],
70 [ tabs => 'Tabs', ['tabs'] ],
71 [ document_tabs => 'Document tabs', ['document_tabs'] ],
72 [ accordion => 'Accordion', ['accordion'] ],
73 [ table => 'Table', ['table'] ],
74 [ progress => 'Progress', [ 'progress', 'spinner' ] ],
75 [ toast => 'Messages', ['toast'] ],
76 [ menus => 'Menus', [ 'menu_bar', 'menu', 'status_bar' ] ],
77 [ dialog => 'Dialog', ['dialog'] ],
78 [ divider => 'Dividers', ['divider'] ],
79 [ image => 'Image', ['image'] ],
80 [ variants => 'Variants', [] ],
81 );
82 my %SECTION_OF_FAMILY = map {
83 my $section = $_;
84 map { $_ => $section->[0] } @{ $section->[2] }
85 } @SECTIONS;
86  
87 # The families whose variants the gallery can show, with how it makes a
88 # sample of one.
89 my %SAMPLE = (
90 text => sub ($classes) { Term::Fabulous::Widget::Text->new( text => 'Text of this variant', classes => $classes ) },
91 box => sub ($classes) { _framed_box( 'A box of this variant', $classes ) },
92 button => sub ($classes) { _button( 'Button', $classes ) },
93 input => sub ($classes) { Term::Fabulous::Widget::Checkbox->new( label => 'Check box', checked => 1, classes => $classes ) },
94 text_input => sub ($classes) { Term::Fabulous::Widget::TextField->new( value => 'Text field', classes => $classes, layout => { sizing => { width => sizing_grow( 0, 30 ) } } ) },
95 dropdown => sub ($classes) { Term::Fabulous::Widget::Dropdown->new( options => [qw(First Second Third)], value => 'First', classes => $classes ) },
96 progress => sub ($classes) { Term::Fabulous::Widget::ProgressBar->new( value => 40, classes => $classes ) },
97 spinner => sub ($classes) { Term::Fabulous::Widget::Spinner->new( label => 'Spinner', running => 0, classes => $classes ) },
98 divider => sub ($classes) { Term::Fabulous::Widget::Divider->new( text => 'Divider', classes => $classes ) },
99 toast => sub ($classes) { Term::Fabulous::Widget::Toast->new( kind => 'info', message => 'A message of this variant.', closable => 0, classes => $classes ) },
100 menu_bar => sub ($classes) { _menu_bar($classes) },
101 menu => sub ($classes) { _menu($classes) },
102 status_bar => sub ($classes) { Term::Fabulous::Widget::StatusBar->new( message => 'A status bar of this variant.', parts => [ [ 'dim', 'dim' ] ], classes => $classes ) },
103 tabs => sub ($classes) { _tabs($classes) },
104 document_tabs => sub ($classes) { _document_tabs($classes) },
105 accordion => sub ($classes) { _accordion($classes) },
106 );
107  
108 field %section; # id => the section's box
109 field %swatch; # token => the box that shows its color
110 field $gallery;
111 field $variant_box;
112 field $variant_list = "\x{0}";
113 field $theme;
114  
115 ADJUST {
116 $self->layout( { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow(), height => sizing_grow() } } );
117 $gallery = Term::Fabulous::Widget::ScrollBox->new(
118 id => GALLERY_ID,
119 layout => {
120 layout_direction => CLAY_TOP_TO_BOTTOM,
121 sizing => { width => sizing_grow(), height => sizing_grow() },
122 padding => { left => 1, right => 1 },
123 child_gap => 1,
124 },
125 );
126 $self->add_child($gallery);
127 foreach my $definition (@SECTIONS) {
128 my ( $id, $title ) = @$definition;
129 my $box = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow() }, child_gap => 1 } );
130 my $families = @{ $definition->[2] } ? ' (' . join( ', ', @{ $definition->[2] } ) . ')' : '';
131 $box->add_child( Term::Fabulous::Widget::Divider->new( text => "$title$families", text_position => 'start', bold => 1 ) );
132 my $build = "_build_$id";
133 $box->add_child( $self->$build );
134 $section{$id} = $box;
135 $gallery->add_child($box);
136 }
137 $variant_box = $section{variants};
138  
139 weaken( my $weak_self = $self );
140 $self->on( OnFocus => sub ($event) { $weak_self->_reveal( $event->target ); return Clay::UI::Enum::Result->CONTINUE } );
141 $self->on( KeyPress => sub ($event) { return $weak_self->_scroll_key($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE } );
142 }
143  
144 sub _column (@kids) {
145 my $box = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow() } } );
146 $box->add_child(@kids);
147 return $box;
148 }
149  
150 sub _row ( $gap, @kids ) {
151 my $box = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_LEFT_TO_RIGHT_WRAP, sizing => { width => sizing_grow() }, child_gap => $gap } );
152 $box->add_child(@kids);
153 return $box;
154 }
155  
156 sub _button ( $label, $classes = [], %parameters ) {
157 my $button = Term::Fabulous::Widget::Button->new( classes => $classes, layout => { padding => { left => 1, right => 1 } }, %parameters );
158 $button->add_child( Term::Fabulous::Widget::Text->new( text => $label ) );
159 return $button;
160 }
161  
162 # A box has no border.enabled: the program frames it.
163 sub _framed_box ( $text, $classes = [] ) {
164 my $box = Term::Fabulous::Widget::Box->new( bordered => 1, classes => $classes, layout => { sizing => { width => sizing_grow() }, padding => { left => 1, right => 1 } } );
165 $box->add_child( Term::Fabulous::Widget::Text->new( text => $text ) );
166 return $box;
167 }
168  
169 sub _tabs ( $classes = [] ) {
170 my $tabs = Term::Fabulous::Widget::Tabs->new( classes => $classes, layout => { sizing => { width => sizing_grow() } } );
171 foreach my $page ( [ General => 'The general settings.' ], [ Colors => 'The colors.' ], [ Locked => 'Not available.', 1 ] ) {
172 my $tab = Term::Fabulous::Widget::Tabs::Page->new( title => $page->[0], disabled => $page->[2] // 0 );
173 $tab->add_child( Term::Fabulous::Widget::Text->new( text => $page->[1] ) );
174 $tabs->add_child($tab);
175 }
176 return $tabs;
177 }
178  
179 # Tabs of documents: one with unsaved changes, one with errors, one
180 # that cannot be closed. A click shows a tab; closing does nothing.
181 sub _document_tabs ( $classes = [] ) {
182 my $tabs = Term::Fabulous::Widget::DocumentTabs->new(
183 classes => $classes,
184 active => 0,
185 tabs => [ { title => 'notes' }, { title => 'draft', modified => 1 }, { title => 'broken', error => 1 }, { title => 'pinned', closable => 0 } ],
186 layout => { sizing => { width => sizing_grow() } },
187 );
188 weaken( my $weak_tabs = $tabs );
189 $tabs->on( Select => sub ($event) { $weak_tabs->active( $event->index ); return } );
190 return $tabs;
191 }
192  
193 sub _accordion ( $classes = [] ) {
194 my $accordion = Term::Fabulous::Widget::Accordion->new( classes => $classes, layout => { sizing => { width => sizing_grow() } } );
195 foreach my $item ( [ 'Account' => 'Name, e-mail and password.' ], [ 'Notifications' => 'What reaches you, and how.' ], [ 'Privacy' => 'Who sees what.' ] ) {
196 my $section = Term::Fabulous::Widget::Accordion::Item->new( title => $item->[0] );
197 $section->add_child( Term::Fabulous::Widget::Text->new( text => $item->[1] ) );
198 $accordion->add_child($section);
199 }
200 $accordion->open(0);
201 return $accordion;
202 }
203  
204 # ---------------------------------------------------------------------
205 # The sections
206 # ---------------------------------------------------------------------
207  
208 method _build_palette () {
209 my $grid = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_LEFT_TO_RIGHT_WRAP, sizing => { width => sizing_grow() }, child_gap => 1 } );
210 foreach my $token ( Term::Fabulous::Theme::tokens() ) {
211 my $color = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fixed(2), height => sizing_fixed(1) } } );
212 my $entry = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fixed(19) }, child_gap => 1 } );
213 $entry->add_child( $color, Term::Fabulous::Widget::Text->new( text => $token, wrap_mode => CLAY_TEXT_WRAP_NONE ) );
214 $swatch{$token} = $color;
215 $grid->add_child($entry);
216 }
217 return $grid;
218 }
219  
220 method _build_text () {
221 my $links = Term::Fabulous::Widget::RichText->new(
222 markup => 'Body text in the text color, with [link=one]a link[/link] and [link=two]another one[/link]. Point at a link to see it hovered; Tab to the text and Right to select one.',
223 );
224 return _column( Term::Fabulous::Widget::Text->new( text => 'Plain text, as most widgets draw it.' ), $links );
225 }
226  
227 method _build_box () {
228 my $plain = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow() } } );
229 $plain->add_child( Term::Fabulous::Widget::Text->new( text => 'A box without a border shows its background only.' ) );
230 return _column( _framed_box('A box with a border: box.border.color and box.border.style.'), $plain );
231 }
232  
233 # The buttons as the theme draws them, and with a border of their own,
234 # so that button.border.color and button.border.style show either way.
235 method _build_button () {
236 my $themed = Term::Fabulous::Widget::Text->new( text => 'As the theme draws them; button.border.enabled gives them a border:' );
237 my $bordered = Term::Fabulous::Widget::Text->new( text => 'With a border of their own, in button.border.color and button.border.style:' );
238 return _column( $themed, _buttons(), $bordered, _buttons( bordered => 1 ) );
239 }
240  
241 sub _buttons (%border) {
242 return _row( 1, _button( 'Save', ['primary'], %border ), _button( 'Cancel', [], %border ), _button( 'Disabled', [], disabled => 1, %border ) );
243 }
244  
245 method _build_fields () {
246 my $invalid = Term::Fabulous::Widget::TextField->new( value => 'not an e-mail address', validator => 'email', layout => { sizing => { width => sizing_grow() } } );
247 my $area = Term::Fabulous::Widget::TextArea->new(
248 value => join( "\n", 'A text area with more lines', 'than it shows, so its scrollbar', 'appears at the right.', 'Scroll it with the wheel', 'or move through it', 'with the arrow keys.' ),
249 layout => { sizing => { width => sizing_grow(), height => sizing_fixed(3) } },
250 );
251 return _column(
252 Term::Fabulous::Widget::TextField->new( placeholder => 'A placeholder', layout => { sizing => { width => sizing_grow() } } ),
253 Term::Fabulous::Widget::TextField->new( value => 'Text you can select and edit', layout => { sizing => { width => sizing_grow() } } ),
254 $invalid,
255 Term::Fabulous::Widget::TextField->new( value => 'Disabled', disabled => 1, layout => { sizing => { width => sizing_grow() } } ),
256 $area,
257 );
258 }
259  
260 method _build_choices () {
261 my $size = Term::Fabulous::Widget::RadioGroup->new( value => 'm', layout => { layout_direction => CLAY_LEFT_TO_RIGHT, child_gap => 2 } );
262 $size->add_child( Term::Fabulous::Widget::RadioButton->new( label => $_->[0], value => $_->[1] ) ) foreach [ Small => 's' ], [ Medium => 'm' ], [ Large => 'l' ];
263 return _column(
264 _row(
265 2, Term::Fabulous::Widget::Checkbox->new( label => 'Checked', checked => 1 ), Term::Fabulous::Widget::Checkbox->new( label => 'Not checked' ),
266 Term::Fabulous::Widget::Checkbox->new( label => 'Disabled', disabled => 1 )
267 ),
268 $size,
269 Term::Fabulous::Widget::SegmentedControl->new( options => [qw(Day Week Month)], value => 'Week' ),
270 Term::Fabulous::Widget::Slider->new( min => 0, max => 100, value => 65, value_format => '%d%%', layout => { sizing => { width => sizing_grow( 0, 40 ) } } ),
271 Term::Fabulous::Widget::StarRating->new( value => 3.5, half => 1, show_value => 1 ),
272 Term::Fabulous::Widget::Dropdown->new( options => [ 'Red', 'Green', 'Blue', 'Cyan', 'Magenta' ], value => 'Green' ),
273 );
274 }
275  
276 # A color picker with a few swatches; the color picker has no family
277 # of its own, its parts draw in theirs.
278 method _build_color_picker () {
279 return Term::Fabulous::Widget::ColorPicker->new(
280 id => 'theme-editor-preview-color',
281 value => 'sky',
282 list_rows => 5,
283 swatches => [ [ 'rose', 'Rose', '#e06c75' ], [ 'amber', 'Amber', '#e5c07b' ], [ 'leaf', 'Leaf', '#98c379' ], [ 'sky', 'Sky', '#61afef' ], [ 'none', 'None' ] ],
284 );
285 }
286  
287 method _build_tabs () {
288 return _tabs();
289 }
290  
291 method _build_document_tabs () {
292 return _document_tabs();
293 }
294  
295 method _build_accordion () {
296 return _accordion();
297 }
298  
299 method _build_table () {
300 my @people = (
301 [ 'Ada', 'Engines', 96 ], [ 'Grace', 'Compilers', 91 ], [ 'Alan', 'Engines', 88 ], [ 'Edsger', 'Compilers', 85 ],
302 [ 'Barbara', 'Languages', 93 ], [ 'Donald', 'Languages', 99 ], [ 'Margaret', 'Engines', 94 ],
303 );
304 return Term::Fabulous::Widget::Table->new(
305 id => 'theme-editor-preview-table',
306 row_id => 'name',
307 group_by => 'team',
308 selection => 'single',
309 hover => 1,
310 filter_row => 1,
311 page_size => 5,
312 columns => [ { key => 'name', title => 'Name' }, { key => 'team', title => 'Team' }, { key => 'score', title => 'Score', type => 'number' } ],
313 rows => [ map { { name => $_->[0], team => $_->[1], score => $_->[2] } } @people ],
314 layout => { sizing => { width => sizing_grow() } },
315 );
316 }
317  
318 method _build_progress () {
319 return _column(
320 Term::Fabulous::Widget::ProgressBar->new( value => 65, layout => { sizing => { width => sizing_grow( 0, 40 ) } } ),
321 Term::Fabulous::Widget::ProgressBar->new( value => 30, value_position => 'inside', layout => { sizing => { width => sizing_grow( 0, 40 ) } } ),
322 Term::Fabulous::Widget::Spinner->new( label => 'A spinner, stopped', running => 0 ),
323 );
324 }
325  
326 method _build_toast () {
327 my $show = _button('Show a toast');
328 weaken( my $weak_self = $self );
329 $show->on( Activate => sub ($event) { $weak_self->_show_toast; return } );
330 return _column(
331 (
332 map { Term::Fabulous::Widget::Toast->new( kind => $_->[0], title => $_->[1], message => $_->[2], closable => 0 ) } [ info => 'Info', 'Good to know.' ],
333 [ success => 'Saved', 'The theme was written.' ], [ warning => 'Careful', 'It cannot be undone.' ]
334 ),
335 Term::Fabulous::Widget::Toast->new( kind => 'danger', title => 'Important', message => 'Filled with its color.', important => 1, closable => 0 ),
336 $show,
337 );
338 }
339  
340 # A menu bar whose titles open short menus, a menu with every kind of
341 # item, and a status bar for each kind of message.
342 method _build_menus () {
343 return _column(
344 _menu_bar( [] ),
345 _row( 0, _menu( [] ) ),
346 Term::Fabulous::Widget::StatusBar->new( message => 'An info message.', parts => [ [ 'accent', 'accent' ], [ 'dim', 'dim' ] ] ),
347 Term::Fabulous::Widget::StatusBar->new( message => 'Saved the theme.', kind => 'success' ),
348 Term::Fabulous::Widget::StatusBar->new( message => 'It cannot be undone.', kind => 'warning' ),
349 Term::Fabulous::Widget::StatusBar->new( message => 'Could not write it.', kind => 'danger' ),
350 );
351 }
352  
353 sub _menu_bar ($classes) {
354 return Term::Fabulous::Widget::MenuBar->new(
355 caption => 'caption',
356 classes => $classes,
357 menus => [ map { { title => $_, items => [ { label => 'New' }, { label => 'Open', keys => ['Ctrl+O'] } ] } } qw(File Edit View) ],
358 );
359 }
360  
361 # Highlighted, with keys, a separator, a toggle that is on and one that
362 # is disabled.
363 sub _menu ($classes) {
364 return Term::Fabulous::Widget::Menu->new(
365 classes => $classes,
366 items => [
367 { label => 'Highlighted', keys => ['Ctrl+H'] },
368 { label => 'With keys', keys => ['F5'] },
369 '-',
370 { label => 'Checked', checked => sub { 1 } },
371 { label => 'Disabled', enabled => sub { 0 } },
372 ],
373 );
374 }
375  
376 method _build_dialog () {
377 my $open = _button('Open a dialog');
378 weaken( my $weak_self = $self );
379 $open->on( Activate => sub ($event) { $weak_self->_open_dialog; return } );
380 return $open;
381 }
382  
383 method _build_divider () {
384 return _column(
385 Term::Fabulous::Widget::Divider->new,
386 Term::Fabulous::Widget::Divider->new( text => 'A divider with text' ),
387 );
388 }
389  
390 method _build_image () {
391 return _row(
392 2, Term::Fabulous::Widget::Image->new( base64 => PICTURE ),
393 Term::Fabulous::Widget::Text->new( text => 'Without the Imager module, a notice in image.notice is shown instead of the picture.' )
394 );
395 }
396  
397 method _build_variants () {
398 return Term::Fabulous::Widget::Text->new( text => 'The theme defines no variants.', id => 'theme-editor-no-variants' );
399 }
400  
401 # ---------------------------------------------------------------------
402 # Live parts: a toast and a dialog in the preview theme
403 # ---------------------------------------------------------------------
404  
405 method _show_toast () {
406 my $ui = $self->ui // return;
407 my $toast = Term::Fabulous::Widget::Toast->new( kind => 'success', title => 'A toast', message => 'It closes by itself after a few seconds.' );
408 $ui->add_preview_root($toast) if $ui->can('add_preview_root');
409 $toast->show($ui);
410 return;
411 }
412  
413 method _open_dialog () {
414 my $ui = $self->ui // return;
415 my $dialog = Term::Fabulous::Widget::Dialog->new(
416 layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_fixed(44) }, padding => { left => 2, right => 2, top => 1, bottom => 1 }, child_gap => 1 } );
417 my $close = _button( 'Close', ['primary'] );
418 $close->on( Activate => sub ($event) { $dialog->close; return } );
419 $dialog->add_child( Term::Fabulous::Widget::Text->new( text => 'A dialog in the preview theme, over its backdrop. Escape or the button closes it.' ), $close );
420 $ui->add_preview_root($dialog) if $ui->can('add_preview_root');
421 $dialog->open($ui);
422 return;
423 }
424  
425 # ---------------------------------------------------------------------
426 # Following the theme
427 # ---------------------------------------------------------------------
428  
429 # Shows a theme: the palette swatches and a sample of each variant (the
430 # widgets read their looks themselves; the editor paints the screen
431 # background behind them).
432 # The variants come as [ family, name ] pairs.
433 method show_theme ( $new, @variants ) {
434 $theme = $new;
435 $swatch{$_}->background_color( $theme->token($_) ) foreach keys %swatch;
436 $self->_show_variants(@variants);
437 return $self;
438 }
439  
440 method _show_variants (@variants) {
441 my $list = join ',', map { "$_->[0].$_->[1]" } @variants;
442 return if $list eq $variant_list;
443 $variant_list = $list;
444 my @kids = @{ $variant_box->children };
445 $variant_box->remove_child( @kids[ 1 .. $#kids ] );
446 return $variant_box->add_child( $self->_build_variants ) unless @variants;
447 foreach my $variant (@variants) {
448 my ( $family, $name ) = @$variant;
449 my $sample = $SAMPLE{$family};
450 $variant_box->add_child(
451 _column(
452 Term::Fabulous::Widget::Text->new( text => "$family\[$name]" ),
453 defined $sample ? $sample->( [$name] ) : Term::Fabulous::Widget::Text->new( text => "The $family family has no sample of its own here; see its section above." ),
454 )
455 );
456 }
457 return;
458 }
459  
460 # ---------------------------------------------------------------------
461 # Scrolling
462 # ---------------------------------------------------------------------
463  
464 method section_of_family ($family) {
465 return $SECTION_OF_FAMILY{$family};
466 }
467  
468 # Scrolls the gallery so that a section starts at its top.
469 method show_section ($id) {
470 my $box = $section{$id} // return $self;
471 $self->_scroll_into_view( $box, 1 );
472 return $self;
473 }
474  
475 method _scroll_into_view ( $widget, $to_top = 0 ) {
476 my $ui = $self->ui // return;
477 my $view = $ui->bounding_box($gallery) // return;
478 my $target = $ui->bounding_box($widget) // return;
479 my $state = $ui->scroll_state($gallery) // return;
480 my $position = $state->{position}{y} // 0;
481 my $top = $view->{y};
482 my $bottom = $view->{y} + $view->{height};
483 my $wanted = $position;
484  
485 if ( $to_top || $target->{y} < $top ) {
486 $wanted = $position - ( $target->{y} - $top );
487 }
488 elsif ( $target->{y} + $target->{height} > $bottom ) {
489 $wanted = $position - ( $target->{y} + min( $target->{height}, $view->{height} ) - $bottom );
490 }
491 my $lowest = min( 0, $view->{height} - ( $state->{content}{height} // 0 ) );
492 $wanted = max( $lowest, min( 0, $wanted ) );
493 $ui->scroll_to( $gallery, { y => $wanted } ) if $wanted != $position;
494 return;
495 }
496  
497 method _reveal ($target) {
498 return unless blessed $target && $gallery->contains($target);
499 $self->_scroll_into_view($target);
500 return;
501 }
502  
503 # PageUp, PageDown, Ctrl+Home and Ctrl+End scroll the gallery when the
504 # focused widget does not use them.
505 method _scroll_key ($event) {
506 my $name = $event->main_key_name // return 0;
507 my $ui = $self->ui // return 0;
508 my $view = $ui->bounding_box($gallery) // return 0;
509 my $state = $ui->scroll_state($gallery) // return 0;
510 my $position = $state->{position}{y} // 0;
511 my $lowest = min( 0, $view->{height} - ( $state->{content}{height} // 0 ) );
512 my %target = ( PageUp => $position + $view->{height} - 1, PageDown => $position - $view->{height} + 1, 'Ctrl+Home' => 0, 'Ctrl+End' => $lowest );
513 return 0 unless exists $target{$name};
514 $ui->scroll_to( $gallery, { y => max( $lowest, min( 0, $target{$name} ) ) } );
515 return 1;
516 }
517 }
518  
519 1;
520  
521 __END__