| 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.03'; | ||
| 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 Feature::Compat::Try; | ||
| 35 | use Scalar::Util qw(blessed looks_like_number); | ||
| 36 | use Term::Fabulous::Check qw(describe); | ||
| 37 | use Term::Fabulous::Termbox qw(TB_OUTPUT_TRUECOLOR TB_DEFAULT); | ||
| 38 | use Term::Fabulous::Render::Attr qw(color_attr); | ||
| 39 | use Term::Fabulous::Render::Frame; | ||
| 40 | use Term::Fabulous::Role::Themed; | ||
| 41 | use Term::Fabulous::Theme; | ||
| 42 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 43 | |||
| 44 | # Scissors paint nothing: the Frame has turned them into clip rects. | ||
| 45 | my %handler_by_command_type = ( | ||
| 46 | CLAY_RENDER_COMMAND_TYPE_RECTANGLE() => 'render_rectangle', | ||
| 47 | CLAY_RENDER_COMMAND_TYPE_BORDER() => 'render_border', | ||
| 48 | CLAY_RENDER_COMMAND_TYPE_TEXT() => 'render_text', | ||
| 49 | CLAY_RENDER_COMMAND_TYPE_CUSTOM() => 'render_custom', | ||
| 50 | ); | ||
| 51 | |||
| 52 | # Clay_UpdateScrollContainers moves a scroll container by ten layout | ||
| 53 | # units, here cells, per unit of scroll delta. | ||
| 54 | use constant CELLS_PER_CLAY_SCROLL_UNIT => 10; | ||
| 55 | |||
| 56 | field $output_mode :param :reader = TB_OUTPUT_TRUECOLOR; | ||
| 57 | |||
| 58 | # The theme the widgets draw with (Term::Fabulous::Theme), set at | ||
| 59 | # construction and through the theme accessor. | ||
| 60 | field $theme :param = undef; | ||
| 61 | |||
| 62 | # Background attribute of every cell painted this frame, indexed [y][x]. | ||
| 63 | field $buffer = []; | ||
| 64 | |||
| 65 | # The attribute of the screen background the frame being painted lies | ||
| 66 | # on, TB_DEFAULT when the terminal's own background shows. | ||
| 67 | field $_screen_background_attr = TB_DEFAULT; | ||
| 68 | field $_last_frame; | ||
| 69 | |||
| 70 | # The paint-order index of the command being painted, undef between | ||
| 71 | # commands. | ||
| 72 | field $_painting_index; | ||
| 73 | |||
| 74 | # Code references to call once the next frame has been drawn. | ||
| 75 | field @_after_draw; | ||
| 76 | |||
| 77 | # Provided by Clay::UI. | ||
| 78 | method render; | ||
| 79 | method widget_for; | ||
| 80 | method measure_text; | ||
| 81 | |||
| 82 | # Provided by the consumer: undef, or { x, y, down } of the pointer. | ||
| 83 | method pointer_state; | ||
| 84 | |||
| 85 | # Provided by the consumer: the Term::Fabulous::Color the screen is | ||
| 86 | # painted in before the widgets, or undef to leave the terminal's own | ||
| 87 | # background (see SCREEN BACKGROUND). | ||
| 88 | method screen_background; | ||
| 89 | |||
| 90 | # Provided by the consumer: the cell target the frames are painted | ||
| 91 | # into (see CELL TARGET). | ||
| 92 | method cell_target; | ||
| 93 | |||
| 94 | ADJUST { | ||
| 95 | die "Term::Fabulous::Render: output_mode must be TB_OUTPUT_TRUECOLOR (" . TB_OUTPUT_TRUECOLOR . "), got '$output_mode'" | ||
| 96 | unless looks_like_number($output_mode) && $output_mode == TB_OUTPUT_TRUECOLOR; | ||
| 97 | |||
| 98 | $theme = _checked_theme( $theme // 'dark' ); | ||
| 99 | Term::Fabulous::Theme::bump_generation(); # widgets built before this UI look their looks up again | ||
| 100 | Term::Fabulous::Role::Themed::forget_tree_looks( $self->root ); | ||
| 101 | $self->measure_text( \&_measure_text ); | ||
| 102 | } | ||
| 103 | |||
| 104 | # A Term::Fabulous::Theme, or one of the built-in themes by name. | ||
| 105 | sub _checked_theme ($value) { | ||
| 106 | return $value if blessed $value && $value->isa('Term::Fabulous::Theme'); | ||
| 107 | my $builtin = defined $value && !ref $value ? Term::Fabulous::Theme->builtin($value) : undef; | ||
| 108 | 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) | ||
| 109 | unless defined $builtin; | ||
| 110 | return $builtin; | ||
| 111 | } | ||
| 112 | |||
| 113 | # Setting a theme makes every widget look its looks up again, tells | ||
| 114 | # the widgets that copy looks into their parts (a Table), and draws a | ||
| 115 | # frame. | ||
| 116 | method theme (@new) { | ||
| 117 | return $theme unless @new; | ||
| 118 | $theme = _checked_theme( $new[0] ); | ||
| 119 | $self->refresh_looks; | ||
| 120 | return $theme; | ||
| 121 | } | ||
| 122 | |||
| 123 | # The theme a widget of the tree draws with (Term::Fabulous::Role::Themed | ||
| 124 | # asks): the UI's. A subclass may give parts of its tree another theme; | ||
| 125 | # it calls refresh_looks when what it returns changes. | ||
| 126 | method theme_for ($widget) { | ||
| 127 | return $theme; | ||
| 128 | } | ||
| 129 | |||
| 130 | # Every widget looks its looks up again in the next frame. | ||
| 131 | method refresh_looks () { | ||
| 132 | Term::Fabulous::Theme::bump_generation(); | ||
| 133 | Term::Fabulous::Role::Themed::forget_tree_looks( $self->root ); | ||
| 134 | bump_revision(); | ||
| 135 | return $self; | ||
| 136 | } | ||
| 137 | |||
| 138 | # Clay measures single words and single lines, so the height is one cell. | ||
| 139 | sub _measure_text ( $text, $config, $userdata ) { | ||
| 140 | return { width => string_columns($text), height => 1 }; | ||
| 141 | } | ||
| 142 | |||
| 143 | method last_frame () { | ||
| 144 | return $_last_frame // Term::Fabulous::Render::Frame->new( commands => [], width => $self->width, height => $self->height ); | ||
| 145 | } | ||
| 146 | |||
| 147 | method clip_rect () { | ||
| 148 | die "Term::Fabulous::Render: clip_rect is only known while a render command is painted" unless defined $_painting_index; | ||
| 149 | return $_last_frame->clip_rect($_painting_index); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _clay_scroll_delta ($scroll_cells) { | ||
| 153 | die "Term::Fabulous::Render: scroll_cells must be [columns, rows] of numbers" | ||
| 154 | unless ref $scroll_cells eq 'ARRAY' && @$scroll_cells == 2 && !grep { !looks_like_number($_) } @$scroll_cells; | ||
| 155 | my ( $columns, $rows ) = @$scroll_cells; | ||
| 156 | return { x => $columns / CELLS_PER_CLAY_SCROLL_UNIT, y => $rows / CELLS_PER_CLAY_SCROLL_UNIT }; | ||
| 157 | } | ||
| 158 | |||
| 159 | # Clay counts the right and bottom edges of a box as inside it, so a | ||
| 160 | # cell's corner would also lie in the boxes left of and above it; its | ||
| 161 | # center lies in the box of the cell only. | ||
| 162 | sub _clay_pointer ($pointer) { | ||
| 163 | return { %$pointer, x => $pointer->{x} + 0.5, y => $pointer->{y} + 0.5 }; | ||
| 164 | } | ||
| 165 | |||
| 166 | # clip_rect answers for the command being painted, and for no other | ||
| 167 | # time, even when a handler dies. | ||
| 168 | method _paint_commands ($frame) { | ||
| 169 | my @commands = $frame->commands; | ||
| 170 | try { | ||
| 171 | foreach my $index ( 0 .. $#commands ) { | ||
| 172 | my $command = $commands[$index]; | ||
| 173 | my $handler = $handler_by_command_type{ $command->{commandType} } // next; | ||
| 174 | $_painting_index = $index; | ||
| 175 | $self->$handler( $command, $self->widget_for( $command->{userData} ), $buffer ); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | catch ($error) { | ||
| 179 | $_painting_index = undef; | ||
| 180 | die $error; | ||
| 181 | } | ||
| 182 | $_painting_index = undef; | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | method draw (%args) { | ||
| 187 | my @unknown = grep { $_ ne 'scroll_cells' } sort keys %args; | ||
| 188 | die "Term::Fabulous::Render: draw got unknown argument(s): @unknown" if @unknown; | ||
| 189 | |||
| 190 | my $pointer = $self->pointer_state; | ||
| 191 | my $commands = $self->render( | ||
| 192 | ( defined $pointer ? ( pointer_state => _clay_pointer($pointer) ) : () ), | ||
| 193 | ( defined $args{scroll_cells} ? ( scroll_delta => _clay_scroll_delta( $args{scroll_cells} ) ) : () ), | ||
| 194 | ); | ||
| 195 | |||
| 196 | # Complete before painting starts, so hit-testing sees the whole | ||
| 197 | # frame even when painting dies partway. | ||
| 198 | $_last_frame = Term::Fabulous::Render::Frame->new( commands => $commands, width => $self->width, height => $self->height ); | ||
| 199 | |||
| 200 | my $screen_background = $self->screen_background; | ||
| 201 | $_screen_background_attr = defined $screen_background ? color_attr($screen_background) : TB_DEFAULT; | ||
| 202 | |||
| 203 | my $target = $self->cell_target; | ||
| 204 | $target->begin_frame( $self->plan_canvases($_last_frame) ); | ||
| 205 | $buffer = []; | ||
| 206 | $self->_paint_screen_background($target) if defined $screen_background; | ||
| 207 | $self->_paint_commands($_last_frame); | ||
| 208 | $target->show_sixels( $self->sixel_placements( $_last_frame, $target ) ) if $target->DOES('Term::Fabulous::Render::Target::Sixel'); | ||
| 209 | $target->end_frame; | ||
| 210 | $self->finish_canvases; | ||
| 211 | |||
| 212 | # A callback may queue another one for the frame after this one. | ||
| 213 | $_->() foreach splice @_after_draw; | ||
| 214 | return; | ||
| 215 | } | ||
| 216 | |||
| 217 | # Fills the viewport with spaces in the screen background and records | ||
| 218 | # it as the background below every cell, so that the widgets, borders | ||
| 219 | # and translucent boxes of the frame are painted over it instead of | ||
| 220 | # over the terminal's own background. The target leaves the cells a | ||
| 221 | # canvas keeps from the last frame as they are. | ||
| 222 | method _paint_screen_background ($target) { | ||
| 223 | my $width = $self->width; | ||
| 224 | foreach my $y ( 0 .. $self->height - 1 ) { | ||
| 225 | $buffer->[$y] = [ ($_screen_background_attr) x $width ]; | ||
| 226 | $target->fill_row( 0, $y, $width, $_screen_background_attr ); | ||
| 227 | } | ||
| 228 | return; | ||
| 229 | } | ||
| 230 | |||
| 231 | method after_draw ($callback) { | ||
| 232 | die "Term::Fabulous::Render: after_draw needs a code reference, got " . ( ref $callback || ( defined $callback ? "'$callback'" : 'undef' ) ) | ||
| 233 | unless ref $callback eq 'CODE'; | ||
| 234 | push @_after_draw, $callback; | ||
| 235 | return $self; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | 1; | ||
| 240 | |||
| 241 | __END__ |