| 1 | package Term::Fabulous::Render; | ||
| 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 | # Loaded first: dies with a clear message when termbox2 lacks truecolor, | ||
| 13 | # before the TB_OUTPUT_TRUECOLOR import below could fail obscurely. | ||
| 14 | use Term::Fabulous::Render::Attr (); | ||
| 15 | |||
| 16 | use Term::Fabulous::Render::Rectangle; | ||
| 17 | use Term::Fabulous::Render::Border; | ||
| 18 | use Term::Fabulous::Render::Text; | ||
| 19 | use Term::Fabulous::Render::Canvas; | ||
| 20 | |||
| 21 | role Term::Fabulous::Render | ||
| 22 | :does(Term::Fabulous::Render::Rectangle) | ||
| 23 | :does(Term::Fabulous::Render::Border) | ||
| 24 | :does(Term::Fabulous::Render::Text) | ||
| 25 | :does(Term::Fabulous::Render::Canvas) | ||
| 26 | { | ||
| 27 | use Clay::XS qw( | ||
| 28 | CLAY_RENDER_COMMAND_TYPE_RECTANGLE | ||
| 29 | CLAY_RENDER_COMMAND_TYPE_BORDER | ||
| 30 | CLAY_RENDER_COMMAND_TYPE_TEXT | ||
| 31 | CLAY_RENDER_COMMAND_TYPE_CUSTOM | ||
| 32 | ); | ||
| 33 | use Clay::UI::Revision qw(bump_revision); | ||
| 34 | use Clay::UI::Role::Layout::HasParent qw(is_widget); | ||
| 35 | use Feature::Compat::Try; | ||
| 36 | use Scalar::Util qw(blessed looks_like_number refaddr weaken); | ||
| 37 | use Term::Fabulous::Check qw(describe positive_integer string); | ||
| 38 | use Term::Fabulous::Termbox qw(TB_OUTPUT_TRUECOLOR TB_DEFAULT); | ||
| 39 | use Term::Fabulous::Render::Attr qw(color_attr); | ||
| 40 | use Term::Fabulous::Render::Frame; | ||
| 41 | use Term::Fabulous::Role::Themed; | ||
| 42 | use Term::Fabulous::Theme; | ||
| 43 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 44 | |||
| 45 | # Scissors paint nothing: the Frame has turned them into clip rects. | ||
| 46 | my %handler_by_command_type = ( | ||
| 47 | CLAY_RENDER_COMMAND_TYPE_RECTANGLE() => 'render_rectangle', | ||
| 48 | CLAY_RENDER_COMMAND_TYPE_BORDER() => 'render_border', | ||
| 49 | CLAY_RENDER_COMMAND_TYPE_TEXT() => 'render_text', | ||
| 50 | CLAY_RENDER_COMMAND_TYPE_CUSTOM() => 'render_custom', | ||
| 51 | ); | ||
| 52 | |||
| 53 | # Clay_UpdateScrollContainers moves a scroll container by ten layout | ||
| 54 | # units, here cells, per unit of scroll delta. | ||
| 55 | use constant CELLS_PER_CLAY_SCROLL_UNIT => 10; | ||
| 56 | |||
| 57 | field $output_mode :param :reader = TB_OUTPUT_TRUECOLOR; | ||
| 58 | |||
| 59 | # The theme the widgets draw with (Term::Fabulous::Theme), set at | ||
| 60 | # construction and through the theme accessor. | ||
| 61 | field $theme :param = undef; | ||
| 62 | |||
| 63 | # Background attribute of every cell painted this frame, indexed [y][x]. | ||
| 64 | field $buffer = []; | ||
| 65 | |||
| 66 | # The attribute of the screen background the frame being painted lies | ||
| 67 | # on, TB_DEFAULT when the terminal's own background shows. | ||
| 68 | field $_screen_background_attr = TB_DEFAULT; | ||
| 69 | field $_last_frame; | ||
| 70 | |||
| 71 | # The paint-order index of the command being painted, undef between | ||
| 72 | # commands. | ||
| 73 | field $_painting_index; | ||
| 74 | |||
| 75 | # The work to do after frames, in the order it was registered (see | ||
| 76 | # after_draw and each_draw): { code, owner, bound, key, repeat, | ||
| 77 | # frames, calls, done }. An owner is held weakly. | ||
| 78 | field @_frame_work; | ||
| 79 | |||
| 80 | my %FRAME_WORK_OPTIONS = ( after_draw => [qw(owner)], each_draw => [qw(owner name frames)] ); | ||
| 81 | |||
| 82 | # Provided by Clay::UI. | ||
| 83 | method render; | ||
| 84 | method widget_for; | ||
| 85 | method measure_text; | ||
| 86 | method owns; | ||
| 87 | |||
| 88 | # Provided by the consumer: undef, or { x, y, down } of the pointer. | ||
| 89 | method pointer_state; | ||
| 90 | |||
| 91 | # Provided by the consumer: the Term::Fabulous::Color the screen is | ||
| 92 | # painted in before the widgets, or undef to leave the terminal's own | ||
| 93 | # background (see SCREEN BACKGROUND). | ||
| 94 | method screen_background; | ||
| 95 | |||
| 96 | # Provided by the consumer: the cell target the frames are painted | ||
| 97 | # into (see CELL TARGET). | ||
| 98 | method cell_target; | ||
| 99 | |||
| 100 | ADJUST { | ||
| 101 | die "Term::Fabulous::Render: output_mode must be TB_OUTPUT_TRUECOLOR (" . TB_OUTPUT_TRUECOLOR . "), got '$output_mode'" | ||
| 102 | unless looks_like_number($output_mode) && $output_mode == TB_OUTPUT_TRUECOLOR; | ||
| 103 | |||
| 104 | $theme = _checked_theme( $theme // 'dark' ); | ||
| 105 | Term::Fabulous::Theme::bump_generation(); # widgets built before this UI look their looks up again | ||
| 106 | Term::Fabulous::Role::Themed::forget_tree_looks( $self->root ); | ||
| 107 | $self->measure_text( \&_measure_text ); | ||
| 108 | } | ||
| 109 | |||
| 110 | # A Term::Fabulous::Theme, or one of the built-in themes by name. | ||
| 111 | sub _checked_theme ($value) { | ||
| 112 | return $value if blessed $value && $value->isa('Term::Fabulous::Theme'); | ||
| 113 | my $builtin = defined $value && !ref $value ? Term::Fabulous::Theme->builtin($value) : undef; | ||
| 114 | die "Term::Fabulous::Render: theme must be a Term::Fabulous::Theme or the name of a built-in theme (" . join( ', ', Term::Fabulous::Theme::builtin_names() ) . "), got " . describe($value) | ||
| 115 | unless defined $builtin; | ||
| 116 | return $builtin; | ||
| 117 | } | ||
| 118 | |||
| 119 | # Setting a theme makes every widget look its looks up again, tells | ||
| 120 | # the widgets that copy looks into their parts (a Table), and draws a | ||
| 121 | # frame. | ||
| 122 | method theme (@new) { | ||
| 123 | return $theme unless @new; | ||
| 124 | $theme = _checked_theme( $new[0] ); | ||
| 125 | $self->refresh_looks; | ||
| 126 | return $theme; | ||
| 127 | } | ||
| 128 | |||
| 129 | # The theme a widget of the tree draws with (Term::Fabulous::Role::Themed | ||
| 130 | # asks): the UI's. A subclass may give parts of its tree another theme; | ||
| 131 | # it calls refresh_looks when what it returns changes. | ||
| 132 | method theme_for ($widget) { | ||
| 133 | return $theme; | ||
| 134 | } | ||
| 135 | |||
| 136 | # Every widget looks its looks up again in the next frame. | ||
| 137 | method refresh_looks () { | ||
| 138 | Term::Fabulous::Theme::bump_generation(); | ||
| 139 | Term::Fabulous::Role::Themed::forget_tree_looks( $self->root ); | ||
| 140 | bump_revision(); | ||
| 141 | return $self; | ||
| 142 | } | ||
| 143 | |||
| 144 | # Clay measures single words and single lines, so the height is one cell. | ||
| 145 | sub _measure_text ( $text, $config, $userdata ) { | ||
| 146 | return { width => string_columns($text), height => 1 }; | ||
| 147 | } | ||
| 148 | |||
| 149 | method last_frame () { | ||
| 150 | return $_last_frame // Term::Fabulous::Render::Frame->new( commands => [], width => $self->width, height => $self->height ); | ||
| 151 | } | ||
| 152 | |||
| 153 | method clip_rect () { | ||
| 154 | die "Term::Fabulous::Render: clip_rect is only known while a render command is painted" unless defined $_painting_index; | ||
| 155 | return $_last_frame->clip_rect($_painting_index); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub _clay_scroll_delta ($scroll_cells) { | ||
| 159 | die "Term::Fabulous::Render: scroll_cells must be [columns, rows] of numbers" | ||
| 160 | unless ref $scroll_cells eq 'ARRAY' && @$scroll_cells == 2 && !grep { !looks_like_number($_) } @$scroll_cells; | ||
| 161 | my ( $columns, $rows ) = @$scroll_cells; | ||
| 162 | return { x => $columns / CELLS_PER_CLAY_SCROLL_UNIT, y => $rows / CELLS_PER_CLAY_SCROLL_UNIT }; | ||
| 163 | } | ||
| 164 | |||
| 165 | # Clay counts the right and bottom edges of a box as inside it, so a | ||
| 166 | # cell's corner would also lie in the boxes left of and above it; its | ||
| 167 | # center lies in the box of the cell only. | ||
| 168 | sub _clay_pointer ($pointer) { | ||
| 169 | return { %$pointer, x => $pointer->{x} + 0.5, y => $pointer->{y} + 0.5 }; | ||
| 170 | } | ||
| 171 | |||
| 172 | # clip_rect answers for the command being painted, and for no other | ||
| 173 | # time, even when a handler dies. | ||
| 174 | method _paint_commands ($frame) { | ||
| 175 | my @commands = $frame->commands; | ||
| 176 | try { | ||
| 177 | foreach my $index ( 0 .. $#commands ) { | ||
| 178 | my $command = $commands[$index]; | ||
| 179 | my $handler = $handler_by_command_type{ $command->{commandType} } // next; | ||
| 180 | $_painting_index = $index; | ||
| 181 | $self->$handler( $command, $self->widget_for( $command->{userData} ), $buffer ); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | catch ($error) { | ||
| 185 | $_painting_index = undef; | ||
| 186 | die $error; | ||
| 187 | } | ||
| 188 | $_painting_index = undef; | ||
| 189 | return; | ||
| 190 | } | ||
| 191 | |||
| 192 | method draw (%args) { | ||
| 193 | my @unknown = grep { $_ ne 'scroll_cells' } sort keys %args; | ||
| 194 | die "Term::Fabulous::Render: draw got unknown argument(s): @unknown" if @unknown; | ||
| 195 | |||
| 196 | my $pointer = $self->pointer_state; | ||
| 197 | my $commands = $self->render( | ||
| 198 | ( defined $pointer ? ( pointer_state => _clay_pointer($pointer) ) : () ), | ||
| 199 | ( defined $args{scroll_cells} ? ( scroll_delta => _clay_scroll_delta( $args{scroll_cells} ) ) : () ), | ||
| 200 | ); | ||
| 201 | |||
| 202 | # Complete before painting starts, so hit-testing sees the whole | ||
| 203 | # frame even when painting dies partway. | ||
| 204 | $_last_frame = Term::Fabulous::Render::Frame->new( commands => $commands, width => $self->width, height => $self->height ); | ||
| 205 | |||
| 206 | my $screen_background = $self->screen_background; | ||
| 207 | $_screen_background_attr = defined $screen_background ? color_attr($screen_background) : TB_DEFAULT; | ||
| 208 | |||
| 209 | my $target = $self->cell_target; | ||
| 210 | $target->begin_frame( $self->plan_canvases($_last_frame) ); | ||
| 211 | $buffer = []; | ||
| 212 | $self->_paint_screen_background($target) if defined $screen_background; | ||
| 213 | $self->_paint_commands($_last_frame); | ||
| 214 | $target->show_sixels( $self->sixel_placements( $_last_frame, $target ) ) if $target->DOES('Term::Fabulous::Render::Target::Sixel'); | ||
| 215 | $target->end_frame; | ||
| 216 | $self->finish_canvases; | ||
| 217 | |||
| 218 | $self->_run_frame_work; | ||
| 219 | return; | ||
| 220 | } | ||
| 221 | |||
| 222 | # Runs the work that was registered when the frame was drawn; work | ||
| 223 | # registered meanwhile (a callback may queue more) waits for the next | ||
| 224 | # frame. Every due callback runs even if an earlier one dies, a dying | ||
| 225 | # one ends, and the first error is rethrown afterwards. | ||
| 226 | method _run_frame_work () { | ||
| 227 | my $first_error; | ||
| 228 | my @due = grep { !$_->{done} } @_frame_work; # copies: callbacks change the list | ||
| 229 | foreach my $entry (@due) { | ||
| 230 | next if $entry->{done}; # replaced by a callback that ran before it | ||
| 231 | try { $self->_run_frame_entry($entry) } | ||
| 232 | catch ($error) { | ||
| 233 | $entry->{done} = 1; | ||
| 234 | $first_error //= $error; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | @_frame_work = grep { !$_->{done} } @_frame_work; | ||
| 238 | die $first_error if defined $first_error; | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | |||
| 242 | # Work bound to an owner ends unseen once the owner is gone or no | ||
| 243 | # longer in this UI; it is called with the owner. | ||
| 244 | method _run_frame_entry ($entry) { | ||
| 245 | my @arguments; | ||
| 246 | if ( $entry->{bound} ) { | ||
| 247 | my $owner = $entry->{owner}; | ||
| 248 | if ( !defined $owner || !$self->owns($owner) ) { | ||
| 249 | $entry->{done} = 1; | ||
| 250 | return; | ||
| 251 | } | ||
| 252 | @arguments = ($owner); | ||
| 253 | } | ||
| 254 | $entry->{calls}++; | ||
| 255 | my $finished = $entry->{code}->(@arguments); | ||
| 256 | $entry->{done} = 1 if !$entry->{repeat} || $finished || ( defined $entry->{frames} && $entry->{calls} >= $entry->{frames} ); | ||
| 257 | return; | ||
| 258 | } | ||
| 259 | |||
| 260 | # Fills the viewport with spaces in the screen background and records | ||
| 261 | # it as the background below every cell, so that the widgets, borders | ||
| 262 | # and translucent boxes of the frame are painted over it instead of | ||
| 263 | # over the terminal's own background. The target leaves the cells a | ||
| 264 | # canvas keeps from the last frame as they are. | ||
| 265 | method _paint_screen_background ($target) { | ||
| 266 | my $width = $self->width; | ||
| 267 | foreach my $y ( 0 .. $self->height - 1 ) { | ||
| 268 | $buffer->[$y] = [ ($_screen_background_attr) x $width ]; | ||
| 269 | $target->fill_row( 0, $y, $width, $_screen_background_attr ); | ||
| 270 | } | ||
| 271 | return; | ||
| 272 | } | ||
| 273 | |||
| 274 | method after_draw ( $code, %options ) { | ||
| 275 | push @_frame_work, $self->_frame_work_entry( after_draw => $code, %options ); | ||
| 276 | return $self; | ||
| 277 | } | ||
| 278 | |||
| 279 | # A registration with the owner and name of one still queued replaces | ||
| 280 | # it: the old one ends, the new one is queued last. | ||
| 281 | method each_draw ( $code, %options ) { | ||
| 282 | my $entry = $self->_frame_work_entry( each_draw => $code, %options ); | ||
| 283 | $entry->{repeat} = 1; | ||
| 284 | if ( defined $entry->{key} ) { | ||
| 285 | $_->{done} = 1 foreach grep { defined $_->{key} && $_->{key} eq $entry->{key} } @_frame_work; | ||
| 286 | @_frame_work = grep { !$_->{done} } @_frame_work; | ||
| 287 | } | ||
| 288 | push @_frame_work, $entry; | ||
| 289 | return $self; | ||
| 290 | } | ||
| 291 | |||
| 292 | method _frame_work_entry ( $method, $code, %options ) { | ||
| 293 | my %known = map { $_ => 1 } @{ $FRAME_WORK_OPTIONS{$method} }; | ||
| 294 | my @unknown = grep { !$known{$_} } sort keys %options; | ||
| 295 | die "Term::Fabulous::Render: $method does not take @unknown (known: @{ $FRAME_WORK_OPTIONS{$method} })" if @unknown; | ||
| 296 | die "Term::Fabulous::Render: $method needs a code reference, got " . describe($code) unless ref $code eq 'CODE'; | ||
| 297 | my $bound = exists $options{owner}; | ||
| 298 | die "Term::Fabulous::Render: $method takes a widget as its owner, got " . describe( $options{owner} ) if $bound && !is_widget( $options{owner} ); | ||
| 299 | my $name = exists $options{name} ? string( "Term::Fabulous::Render: $method", name => $options{name} ) : undef; | ||
| 300 | my $frames = exists $options{frames} ? positive_integer( "Term::Fabulous::Render: $method", frames => $options{frames} ) : undef; | ||
| 301 | my $entry = { | ||
| 302 | code => $code, | ||
| 303 | bound => $bound, | ||
| 304 | owner => $options{owner}, | ||
| 305 | key => defined $name ? ( $bound ? refaddr( $options{owner} ) : '' ) . "\0$name" : undef, | ||
| 306 | frames => $frames, | ||
| 307 | calls => 0, | ||
| 308 | }; | ||
| 309 | weaken $entry->{owner} if $bound; | ||
| 310 | return $entry; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | 1; | ||
| 315 | |||
| 316 | __END__ |