« blib/lib/Term/Fabulous/ThemeEditor/KdlArea.pm
↑ Summary
blib/lib/Term/Fabulous/ThemeEditor/SettingsList.pm »
| 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.05'; | ||
| 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 Term::Fabulous::Scrolling qw(scroll_by_key); | ||
| 20 | use Term::Fabulous::Theme; | ||
| 21 | use Term::Fabulous::Widget::Accordion; | ||
| 22 | use Term::Fabulous::Widget::Accordion::Item; | ||
| 23 | use Term::Fabulous::Widget::Button; | ||
| 24 | use Term::Fabulous::Widget::Checkbox; | ||
| 25 | use Term::Fabulous::Widget::ColorPicker; | ||
| 26 | use Term::Fabulous::Widget::Dialog; | ||
| 27 | use Term::Fabulous::Widget::Divider; | ||
| 28 | use Term::Fabulous::Widget::DocumentTabs; | ||
| 29 | use Term::Fabulous::Widget::Dropdown; | ||
| 30 | use Term::Fabulous::Widget::Image; | ||
| 31 | use Term::Fabulous::Widget::Menu; | ||
| 32 | use Term::Fabulous::Widget::MenuBar; | ||
| 33 | use Term::Fabulous::Widget::ProgressBar; | ||
| 34 | use Term::Fabulous::Widget::RadioButton; | ||
| 35 | use Term::Fabulous::Widget::RadioGroup; | ||
| 36 | use Term::Fabulous::Widget::Text; | ||
| 37 | use Term::Fabulous::Widget::ScrollBox; | ||
| 38 | use Term::Fabulous::Widget::SegmentedControl; | ||
| 39 | use Term::Fabulous::Widget::Slider; | ||
| 40 | use Term::Fabulous::Widget::Spinner; | ||
| 41 | use Term::Fabulous::Widget::StarRating; | ||
| 42 | use Term::Fabulous::Widget::StatusBar; | ||
| 43 | use Term::Fabulous::Widget::Table; | ||
| 44 | use Term::Fabulous::Widget::Tabs; | ||
| 45 | use Term::Fabulous::Widget::Tabs::Page; | ||
| 46 | use Term::Fabulous::Widget::TextArea; | ||
| 47 | use Term::Fabulous::Widget::TextField; | ||
| 48 | use Term::Fabulous::Widget::Toast; | ||
| 49 | |||
| 50 | use constant GALLERY_ID => 'theme-editor-preview'; | ||
| 51 | |||
| 52 | # A picture of 12 by 8 pixels, a ring of colors, for the image family, | ||
| 53 | # as TGA: Imager reads it without a plugin, so the preview does not | ||
| 54 | # depend on the PNG support Imager was built with. | ||
| 55 | use constant PICTURE => | ||
| 56 | '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=='; | ||
| 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 | $self->on( KeyPress => sub ( $self, $event ) { return $self->_scroll_key($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE }, owner => $self ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _column (@kids) { | ||
| 141 | my $box = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow() } } ); | ||
| 142 | $box->add_child(@kids); | ||
| 143 | return $box; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _row ( $gap, @kids ) { | ||
| 147 | my $box = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_LEFT_TO_RIGHT_WRAP, sizing => { width => sizing_grow() }, child_gap => $gap } ); | ||
| 148 | $box->add_child(@kids); | ||
| 149 | return $box; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _button ( $label, $classes = [], %parameters ) { | ||
| 153 | my $button = Term::Fabulous::Widget::Button->new( classes => $classes, layout => { padding => { left => 1, right => 1 } }, %parameters ); | ||
| 154 | $button->add_child( Term::Fabulous::Widget::Text->new( text => $label ) ); | ||
| 155 | return $button; | ||
| 156 | } | ||
| 157 | |||
| 158 | # A box has no border.enabled: the program frames it. | ||
| 159 | sub _framed_box ( $text, $classes = [] ) { | ||
| 160 | my $box = Term::Fabulous::Widget::Box->new( bordered => 1, classes => $classes, layout => { sizing => { width => sizing_grow() }, padding => { left => 1, right => 1 } } ); | ||
| 161 | $box->add_child( Term::Fabulous::Widget::Text->new( text => $text ) ); | ||
| 162 | return $box; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub _tabs ( $classes = [] ) { | ||
| 166 | my $tabs = Term::Fabulous::Widget::Tabs->new( classes => $classes, layout => { sizing => { width => sizing_grow() } } ); | ||
| 167 | foreach my $page ( [ General => 'The general settings.' ], [ Colors => 'The colors.' ], [ Locked => 'Not available.', 1 ] ) { | ||
| 168 | my $tab = Term::Fabulous::Widget::Tabs::Page->new( title => $page->[0], disabled => $page->[2] // 0 ); | ||
| 169 | $tab->add_child( Term::Fabulous::Widget::Text->new( text => $page->[1] ) ); | ||
| 170 | $tabs->add_child($tab); | ||
| 171 | } | ||
| 172 | return $tabs; | ||
| 173 | } | ||
| 174 | |||
| 175 | # Tabs of documents: one with unsaved changes, one with errors, one | ||
| 176 | # that cannot be closed. A click shows a tab; closing does nothing. | ||
| 177 | sub _document_tabs ( $classes = [] ) { | ||
| 178 | my $tabs = Term::Fabulous::Widget::DocumentTabs->new( | ||
| 179 | classes => $classes, | ||
| 180 | active => 0, | ||
| 181 | tabs => [ { title => 'notes' }, { title => 'draft', modified => 1 }, { title => 'broken', error => 1 }, { title => 'pinned', closable => 0 } ], | ||
| 182 | layout => { sizing => { width => sizing_grow() } }, | ||
| 183 | ); | ||
| 184 | $tabs->on( Select => sub ( $tabs, $event ) { $tabs->active( $event->index ); return }, owner => $tabs ); | ||
| 185 | return $tabs; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub _accordion ( $classes = [] ) { | ||
| 189 | my $accordion = Term::Fabulous::Widget::Accordion->new( classes => $classes, layout => { sizing => { width => sizing_grow() } } ); | ||
| 190 | foreach my $item ( [ 'Account' => 'Name, e-mail and password.' ], [ 'Notifications' => 'What reaches you, and how.' ], [ 'Privacy' => 'Who sees what.' ] ) { | ||
| 191 | my $section = Term::Fabulous::Widget::Accordion::Item->new( title => $item->[0] ); | ||
| 192 | $section->add_child( Term::Fabulous::Widget::Text->new( text => $item->[1] ) ); | ||
| 193 | $accordion->add_child($section); | ||
| 194 | } | ||
| 195 | $accordion->open(0); | ||
| 196 | return $accordion; | ||
| 197 | } | ||
| 198 | |||
| 199 | # --------------------------------------------------------------------- | ||
| 200 | # The sections | ||
| 201 | # --------------------------------------------------------------------- | ||
| 202 | |||
| 203 | method _build_palette () { | ||
| 204 | my $grid = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_LEFT_TO_RIGHT_WRAP, sizing => { width => sizing_grow() }, child_gap => 1 } ); | ||
| 205 | foreach my $token ( Term::Fabulous::Theme::tokens() ) { | ||
| 206 | my $color = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fixed(2), height => sizing_fixed(1) } } ); | ||
| 207 | my $entry = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fixed(19) }, child_gap => 1 } ); | ||
| 208 | $entry->add_child( $color, Term::Fabulous::Widget::Text->new( text => $token, wrap_mode => CLAY_TEXT_WRAP_NONE ) ); | ||
| 209 | $swatch{$token} = $color; | ||
| 210 | $grid->add_child($entry); | ||
| 211 | } | ||
| 212 | return $grid; | ||
| 213 | } | ||
| 214 | |||
| 215 | method _build_text () { | ||
| 216 | my $links = Term::Fabulous::Widget::Text->new( | ||
| 217 | 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.', | ||
| 218 | ); | ||
| 219 | return _column( Term::Fabulous::Widget::Text->new( text => 'Plain text, as most widgets draw it.' ), $links ); | ||
| 220 | } | ||
| 221 | |||
| 222 | method _build_box () { | ||
| 223 | my $plain = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow() } } ); | ||
| 224 | $plain->add_child( Term::Fabulous::Widget::Text->new( text => 'A box without a border shows its background only.' ) ); | ||
| 225 | return _column( _framed_box('A box with a border: box.border.color and box.border.style.'), $plain ); | ||
| 226 | } | ||
| 227 | |||
| 228 | # The buttons as the theme draws them, and with a border of their own, | ||
| 229 | # so that button.border.color and button.border.style show either way. | ||
| 230 | method _build_button () { | ||
| 231 | my $themed = Term::Fabulous::Widget::Text->new( text => 'As the theme draws them; button.border.enabled gives them a border:' ); | ||
| 232 | my $bordered = Term::Fabulous::Widget::Text->new( text => 'With a border of their own, in button.border.color and button.border.style:' ); | ||
| 233 | return _column( $themed, _buttons(), $bordered, _buttons( bordered => 1 ) ); | ||
| 234 | } | ||
| 235 | |||
| 236 | sub _buttons (%border) { | ||
| 237 | return _row( 1, _button( 'Save', ['primary'], %border ), _button( 'Cancel', [], %border ), _button( 'Disabled', [], disabled => 1, %border ) ); | ||
| 238 | } | ||
| 239 | |||
| 240 | method _build_fields () { | ||
| 241 | my $invalid = Term::Fabulous::Widget::TextField->new( value => 'not an e-mail address', validator => 'email', layout => { sizing => { width => sizing_grow() } } ); | ||
| 242 | my $area = Term::Fabulous::Widget::TextArea->new( | ||
| 243 | 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.' ), | ||
| 244 | layout => { sizing => { width => sizing_grow(), height => sizing_fixed(3) } }, | ||
| 245 | ); | ||
| 246 | return _column( | ||
| 247 | Term::Fabulous::Widget::TextField->new( placeholder => 'A placeholder', layout => { sizing => { width => sizing_grow() } } ), | ||
| 248 | Term::Fabulous::Widget::TextField->new( value => 'Text you can select and edit', layout => { sizing => { width => sizing_grow() } } ), | ||
| 249 | $invalid, | ||
| 250 | Term::Fabulous::Widget::TextField->new( value => 'Disabled', disabled => 1, layout => { sizing => { width => sizing_grow() } } ), | ||
| 251 | $area, | ||
| 252 | ); | ||
| 253 | } | ||
| 254 | |||
| 255 | method _build_choices () { | ||
| 256 | my $size = Term::Fabulous::Widget::RadioGroup->new( value => 'm', layout => { layout_direction => CLAY_LEFT_TO_RIGHT, child_gap => 2 } ); | ||
| 257 | $size->add_child( Term::Fabulous::Widget::RadioButton->new( label => $_->[0], value => $_->[1] ) ) foreach [ Small => 's' ], [ Medium => 'm' ], [ Large => 'l' ]; | ||
| 258 | return _column( | ||
| 259 | _row( | ||
| 260 | 2, Term::Fabulous::Widget::Checkbox->new( label => 'Checked', checked => 1 ), Term::Fabulous::Widget::Checkbox->new( label => 'Not checked' ), | ||
| 261 | Term::Fabulous::Widget::Checkbox->new( label => 'Disabled', disabled => 1 ) | ||
| 262 | ), | ||
| 263 | $size, | ||
| 264 | Term::Fabulous::Widget::SegmentedControl->new( options => [qw(Day Week Month)], value => 'Week' ), | ||
| 265 | Term::Fabulous::Widget::Slider->new( min => 0, max => 100, value => 65, value_format => '%d%%', layout => { sizing => { width => sizing_grow( 0, 40 ) } } ), | ||
| 266 | Term::Fabulous::Widget::StarRating->new( value => 3.5, half => 1, show_value => 1 ), | ||
| 267 | Term::Fabulous::Widget::Dropdown->new( options => [ 'Red', 'Green', 'Blue', 'Cyan', 'Magenta' ], value => 'Green' ), | ||
| 268 | ); | ||
| 269 | } | ||
| 270 | |||
| 271 | # A color picker with a few swatches; the color picker has no family | ||
| 272 | # of its own, its parts draw in theirs. | ||
| 273 | method _build_color_picker () { | ||
| 274 | return Term::Fabulous::Widget::ColorPicker->new( | ||
| 275 | id => 'theme-editor-preview-color', | ||
| 276 | value => 'sky', | ||
| 277 | list_rows => 5, | ||
| 278 | swatches => [ [ 'rose', 'Rose', '#e06c75' ], [ 'amber', 'Amber', '#e5c07b' ], [ 'leaf', 'Leaf', '#98c379' ], [ 'sky', 'Sky', '#61afef' ], [ 'none', 'None' ] ], | ||
| 279 | ); | ||
| 280 | } | ||
| 281 | |||
| 282 | method _build_tabs () { | ||
| 283 | return _tabs(); | ||
| 284 | } | ||
| 285 | |||
| 286 | method _build_document_tabs () { | ||
| 287 | return _document_tabs(); | ||
| 288 | } | ||
| 289 | |||
| 290 | method _build_accordion () { | ||
| 291 | return _accordion(); | ||
| 292 | } | ||
| 293 | |||
| 294 | method _build_table () { | ||
| 295 | my @people = ( | ||
| 296 | [ 'Ada', 'Engines', 96 ], [ 'Grace', 'Compilers', 91 ], [ 'Alan', 'Engines', 88 ], [ 'Edsger', 'Compilers', 85 ], | ||
| 297 | [ 'Barbara', 'Languages', 93 ], [ 'Donald', 'Languages', 99 ], [ 'Margaret', 'Engines', 94 ], | ||
| 298 | ); | ||
| 299 | return Term::Fabulous::Widget::Table->new( | ||
| 300 | id => 'theme-editor-preview-table', | ||
| 301 | row_id => 'name', | ||
| 302 | group_by => 'team', | ||
| 303 | selection => 'single', | ||
| 304 | hover => 1, | ||
| 305 | filter_row => 1, | ||
| 306 | page_size => 5, | ||
| 307 | columns => [ { key => 'name', title => 'Name' }, { key => 'team', title => 'Team' }, { key => 'score', title => 'Score', type => 'number' } ], | ||
| 308 | rows => [ map { { name => $_->[0], team => $_->[1], score => $_->[2] } } @people ], | ||
| 309 | layout => { sizing => { width => sizing_grow() } }, | ||
| 310 | ); | ||
| 311 | } | ||
| 312 | |||
| 313 | method _build_progress () { | ||
| 314 | return _column( | ||
| 315 | Term::Fabulous::Widget::ProgressBar->new( value => 65, layout => { sizing => { width => sizing_grow( 0, 40 ) } } ), | ||
| 316 | Term::Fabulous::Widget::ProgressBar->new( value => 30, value_position => 'inside', layout => { sizing => { width => sizing_grow( 0, 40 ) } } ), | ||
| 317 | Term::Fabulous::Widget::Spinner->new( label => 'A spinner, stopped', running => 0 ), | ||
| 318 | ); | ||
| 319 | } | ||
| 320 | |||
| 321 | method _build_toast () { | ||
| 322 | my $show = _button('Show a toast'); | ||
| 323 | $show->on( Activate => sub ( $self, $event ) { $self->_show_toast; return }, owner => $self ); | ||
| 324 | return _column( | ||
| 325 | ( | ||
| 326 | map { Term::Fabulous::Widget::Toast->new( kind => $_->[0], title => $_->[1], message => $_->[2], closable => 0 ) } [ info => 'Info', 'Good to know.' ], | ||
| 327 | [ success => 'Saved', 'The theme was written.' ], [ warning => 'Careful', 'It cannot be undone.' ] | ||
| 328 | ), | ||
| 329 | Term::Fabulous::Widget::Toast->new( kind => 'danger', title => 'Important', message => 'Filled with its color.', important => 1, closable => 0 ), | ||
| 330 | $show, | ||
| 331 | ); | ||
| 332 | } | ||
| 333 | |||
| 334 | # A menu bar whose titles open short menus, a menu with every kind of | ||
| 335 | # item, and a status bar for each kind of message. | ||
| 336 | method _build_menus () { | ||
| 337 | return _column( | ||
| 338 | _menu_bar( [] ), | ||
| 339 | _row( 0, _menu( [] ) ), | ||
| 340 | Term::Fabulous::Widget::StatusBar->new( message => 'An info message.', parts => [ [ 'accent', 'accent' ], [ 'dim', 'dim' ] ] ), | ||
| 341 | Term::Fabulous::Widget::StatusBar->new( message => 'Saved the theme.', kind => 'success' ), | ||
| 342 | Term::Fabulous::Widget::StatusBar->new( message => 'It cannot be undone.', kind => 'warning' ), | ||
| 343 | Term::Fabulous::Widget::StatusBar->new( message => 'Could not write it.', kind => 'danger' ), | ||
| 344 | ); | ||
| 345 | } | ||
| 346 | |||
| 347 | sub _menu_bar ($classes) { | ||
| 348 | return Term::Fabulous::Widget::MenuBar->new( | ||
| 349 | caption => 'caption', | ||
| 350 | classes => $classes, | ||
| 351 | menus => [ map { { title => $_, items => [ { label => 'New' }, { label => 'Open', keys => ['Ctrl+O'] } ] } } qw(File Edit View) ], | ||
| 352 | ); | ||
| 353 | } | ||
| 354 | |||
| 355 | # Highlighted, with keys, a separator, a toggle that is on and one that | ||
| 356 | # is disabled. | ||
| 357 | sub _menu ($classes) { | ||
| 358 | return Term::Fabulous::Widget::Menu->new( | ||
| 359 | classes => $classes, | ||
| 360 | items => [ | ||
| 361 | { label => 'Highlighted', keys => ['Ctrl+H'] }, | ||
| 362 | { label => 'With keys', keys => ['F5'] }, | ||
| 363 | '-', | ||
| 364 | { label => 'Checked', checked => sub { 1 } }, | ||
| 365 | { label => 'Disabled', enabled => sub { 0 } }, | ||
| 366 | ], | ||
| 367 | ); | ||
| 368 | } | ||
| 369 | |||
| 370 | method _build_dialog () { | ||
| 371 | my $open = _button('Open a dialog'); | ||
| 372 | $open->on( Activate => sub ( $self, $event ) { $self->_open_dialog; return }, owner => $self ); | ||
| 373 | return $open; | ||
| 374 | } | ||
| 375 | |||
| 376 | method _build_divider () { | ||
| 377 | return _column( | ||
| 378 | Term::Fabulous::Widget::Divider->new, | ||
| 379 | Term::Fabulous::Widget::Divider->new( text => 'A divider with text' ), | ||
| 380 | ); | ||
| 381 | } | ||
| 382 | |||
| 383 | method _build_image () { | ||
| 384 | return _row( | ||
| 385 | 2, Term::Fabulous::Widget::Image->new( base64 => PICTURE ), | ||
| 386 | Term::Fabulous::Widget::Text->new( text => 'Without the Imager module, a notice in image.notice is shown instead of the picture.' ) | ||
| 387 | ); | ||
| 388 | } | ||
| 389 | |||
| 390 | method _build_variants () { | ||
| 391 | return Term::Fabulous::Widget::Text->new( text => 'The theme defines no variants.', id => 'theme-editor-no-variants' ); | ||
| 392 | } | ||
| 393 | |||
| 394 | # --------------------------------------------------------------------- | ||
| 395 | # Live parts: a toast and a dialog in the preview theme | ||
| 396 | # --------------------------------------------------------------------- | ||
| 397 | |||
| 398 | method _show_toast () { | ||
| 399 | my $ui = $self->ui // return; | ||
| 400 | my $toast = Term::Fabulous::Widget::Toast->new( kind => 'success', title => 'A toast', message => 'It closes by itself after a few seconds.' ); | ||
| 401 | $ui->add_preview_root($toast) if $ui->can('add_preview_root'); | ||
| 402 | $toast->show($ui); | ||
| 403 | return; | ||
| 404 | } | ||
| 405 | |||
| 406 | method _open_dialog () { | ||
| 407 | my $ui = $self->ui // return; | ||
| 408 | my $dialog = Term::Fabulous::Widget::Dialog->new( | ||
| 409 | layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_fixed(44) }, padding => { left => 2, right => 2, top => 1, bottom => 1 }, child_gap => 1 } ); | ||
| 410 | my $close = _button( 'Close', ['primary'] ); | ||
| 411 | $close->on( Activate => sub ($event) { $dialog->close; return } ); | ||
| 412 | $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 ); | ||
| 413 | $ui->add_preview_root($dialog) if $ui->can('add_preview_root'); | ||
| 414 | $dialog->open($ui); | ||
| 415 | return; | ||
| 416 | } | ||
| 417 | |||
| 418 | # --------------------------------------------------------------------- | ||
| 419 | # Following the theme | ||
| 420 | # --------------------------------------------------------------------- | ||
| 421 | |||
| 422 | # Shows a theme: the palette swatches and a sample of each variant (the | ||
| 423 | # widgets read their looks themselves; the editor paints the screen | ||
| 424 | # background behind them). | ||
| 425 | # The variants come as [ family, name ] pairs. | ||
| 426 | method show_theme ( $new, @variants ) { | ||
| 427 | $theme = $new; | ||
| 428 | $swatch{$_}->background_color( $theme->token($_) ) foreach keys %swatch; | ||
| 429 | $self->_show_variants(@variants); | ||
| 430 | return $self; | ||
| 431 | } | ||
| 432 | |||
| 433 | method _show_variants (@variants) { | ||
| 434 | my $list = join ',', map { "$_->[0].$_->[1]" } @variants; | ||
| 435 | return if $list eq $variant_list; | ||
| 436 | $variant_list = $list; | ||
| 437 | my @kids = @{ $variant_box->children }; | ||
| 438 | $variant_box->remove_child( @kids[ 1 .. $#kids ] ); | ||
| 439 | return $variant_box->add_child( $self->_build_variants ) unless @variants; | ||
| 440 | foreach my $variant (@variants) { | ||
| 441 | my ( $family, $name ) = @$variant; | ||
| 442 | my $sample = $SAMPLE{$family}; | ||
| 443 | $variant_box->add_child( | ||
| 444 | _column( | ||
| 445 | Term::Fabulous::Widget::Text->new( text => "$family\[$name]" ), | ||
| 446 | defined $sample ? $sample->( [$name] ) : Term::Fabulous::Widget::Text->new( text => "The $family family has no sample of its own here; see its section above." ), | ||
| 447 | ) | ||
| 448 | ); | ||
| 449 | } | ||
| 450 | return; | ||
| 451 | } | ||
| 452 | |||
| 453 | # --------------------------------------------------------------------- | ||
| 454 | # Scrolling | ||
| 455 | # --------------------------------------------------------------------- | ||
| 456 | |||
| 457 | method section_of_family ($family) { | ||
| 458 | return $SECTION_OF_FAMILY{$family}; | ||
| 459 | } | ||
| 460 | |||
| 461 | # Scrolls the gallery so that a section starts at its top. | ||
| 462 | method show_section ($id) { | ||
| 463 | my $box = $section{$id} // return $self; | ||
| 464 | $gallery->reveal( $box, align => 'top' ); | ||
| 465 | return $self; | ||
| 466 | } | ||
| 467 | |||
| 468 | # PageUp, PageDown, Ctrl+Home and Ctrl+End scroll the gallery when the | ||
| 469 | # focused widget does not use them. | ||
| 470 | method _scroll_key ($event) { | ||
| 471 | my $ui = $self->ui // return 0; | ||
| 472 | return defined scroll_by_key( $ui, $gallery, $event->main_key_name, keys => 'pager' ) ? 1 : 0; | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | 1; | ||
| 477 | |||
| 478 | __END__ |