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