« blib/lib/Term/Fabulous/Widget/Dialog/Backdrop.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/Divider.pm »
| 1 | package Term::Fabulous::Widget::Display; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | use Object::Pad 0.825; | ||
| 9 | |||
| 10 | use Term::Fabulous::Widget::Canvas; | ||
| 11 | |||
| 12 | our $VERSION = '0.05'; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::Display | ||
| 15 | :isa(Term::Fabulous::Widget::Canvas) | ||
| 16 | :abstract | ||
| 17 | { | ||
| 18 | use Clay::XS qw(sizing_fixed); | ||
| 19 | use List::Util qw(max); | ||
| 20 | use Time::HiRes (); | ||
| 21 | use Term::Fabulous::Render::Attr qw(cell_color_attr); | ||
| 22 | |||
| 23 | # How far short of a frame boundary, in frames, a time still counts as | ||
| 24 | # the frame that starts there. | ||
| 25 | use constant TICK_TOLERANCE => 1e-9; | ||
| 26 | use Term::Fabulous::Theme; | ||
| 27 | use Term::Fabulous::Unicode qw(grapheme_clusters cluster_columns); | ||
| 28 | |||
| 29 | # Counts mark_changed calls, the changes of the widget's own state; with | ||
| 30 | # the rest of the paint key, what the cells were last painted for. | ||
| 31 | field $_change_count = 0; | ||
| 32 | field $_painted_key; | ||
| 33 | |||
| 34 | # The content size a subclass asks for when the layout gives none. | ||
| 35 | method natural_size; | ||
| 36 | |||
| 37 | # Draws the widget into the cleared buffer. | ||
| 38 | method paint; | ||
| 39 | |||
| 40 | # --------------------------------------------------------------------- | ||
| 41 | # Painting | ||
| 42 | # --------------------------------------------------------------------- | ||
| 43 | |||
| 44 | method mark_changed :override () { | ||
| 45 | $_change_count++; | ||
| 46 | return $self->SUPER::mark_changed; | ||
| 47 | } | ||
| 48 | |||
| 49 | # Paints the cells again when anything paint reads changed since they | ||
| 50 | # were last painted. | ||
| 51 | method refresh :override () { | ||
| 52 | return unless $self->columns > 0 && $self->rows > 0; | ||
| 53 | my $key = join "\x{1F}", map { $_ // "\x{0}" } $self->paint_key; | ||
| 54 | return if defined $_painted_key && $key eq $_painted_key; | ||
| 55 | $_painted_key = $key; | ||
| 56 | $self->clear; | ||
| 57 | $self->paint; | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | # What paint reads: the size, the widget's own state (every setter | ||
| 62 | # calls mark_changed) and the theme. A subclass that paints from the | ||
| 63 | # state of other objects adds it. | ||
| 64 | method paint_key () { | ||
| 65 | return ( $self->columns, $self->rows, $_change_count, Term::Fabulous::Theme::generation() ); | ||
| 66 | } | ||
| 67 | |||
| 68 | # The termbox2 attribute of a color, undef for none. | ||
| 69 | method color_attr ($color) { | ||
| 70 | return cell_color_attr( color => $color ); | ||
| 71 | } | ||
| 72 | |||
| 73 | # Paints a character string from ($x, $y) with termbox2 attributes, | ||
| 74 | # stopping before a cluster that would cross column $limit; returns the | ||
| 75 | # column after the last painted cluster. | ||
| 76 | method paint_text ( $x, $y, $text, $fg, $bg, $limit = $self->columns ) { | ||
| 77 | foreach my $cluster ( grapheme_clusters($text) ) { | ||
| 78 | my $columns = cluster_columns($cluster); | ||
| 79 | last if $x + $columns > $limit; | ||
| 80 | $self->put_attrs( $x, $y, $cluster, $fg, $bg ); | ||
| 81 | $x += $columns; | ||
| 82 | } | ||
| 83 | return $x; | ||
| 84 | } | ||
| 85 | |||
| 86 | method fill_attrs ( $x, $y, $width, $glyph, $fg, $bg ) { | ||
| 87 | $self->put_attrs( $_, $y, $glyph, $fg, $bg ) foreach $x .. $x + $width - 1; | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | |||
| 91 | # --------------------------------------------------------------------- | ||
| 92 | # Animation: frames from the application's clock, which a test or the | ||
| 93 | # screenshot harness can move; the system clock outside a UI. | ||
| 94 | # --------------------------------------------------------------------- | ||
| 95 | |||
| 96 | method now () { | ||
| 97 | my $ui = $self->ui; | ||
| 98 | return $ui->now if defined $ui && $ui->can('now'); | ||
| 99 | return Time::HiRes::time(); | ||
| 100 | } | ||
| 101 | |||
| 102 | # Asks the UI for a frame at a time on the clock; a static page draws | ||
| 103 | # once and ignores it. | ||
| 104 | method request_frame_at ($time) { | ||
| 105 | my $ui = $self->ui; | ||
| 106 | $ui->request_frame_at($time) if defined $ui && $ui->can('request_frame_at'); | ||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | # The frame of an animation that shows $count frames, $interval seconds | ||
| 111 | # each, at the current time, and asks for a frame when the next one is | ||
| 112 | # due. Called from paint_key, it keeps the animation going. | ||
| 113 | # A time a rounding error short of a frame boundary counts as the | ||
| 114 | # frame that starts there: which frame a clock on a whole boundary | ||
| 115 | # falls in would otherwise depend on the platform's floating point, | ||
| 116 | # and the frame asked for next could already be due. | ||
| 117 | method animation_frame ( $interval, $count ) { | ||
| 118 | my $tick = int( $self->now / $interval + TICK_TOLERANCE ); | ||
| 119 | $self->request_frame_at( ( $tick + 1 ) * $interval ); | ||
| 120 | return $tick % $count; | ||
| 121 | } | ||
| 122 | |||
| 123 | # --------------------------------------------------------------------- | ||
| 124 | # Natural size: fills the sizing axes the layout leaves open. Clay::UI | ||
| 125 | # runs the contributors in alphabetical order, so this one runs after | ||
| 126 | # contribute_layout and contribute_layout_inset and sees the final | ||
| 127 | # padding, border included. An axis of the natural size is a number of | ||
| 128 | # cells, made a fixed sizing with the padding added, or a sizing hash | ||
| 129 | # of Clay::XS, whose limits get the padding added. | ||
| 130 | # --------------------------------------------------------------------- | ||
| 131 | |||
| 132 | method contribute_layout_size ($config) { | ||
| 133 | my $layout = $config->{layout} // {}; | ||
| 134 | my $sizing = $layout->{sizing} // {}; | ||
| 135 | my @open = grep { !defined $sizing->{$_} } qw(width height); | ||
| 136 | return unless @open; | ||
| 137 | |||
| 138 | my $padding = $layout->{padding} // {}; | ||
| 139 | my ( $columns, $rows ) = $self->natural_size; | ||
| 140 | my %natural = ( width => $columns, height => $rows ); | ||
| 141 | my %inset = ( | ||
| 142 | width => ( $padding->{left} // 0 ) + ( $padding->{right} // 0 ), | ||
| 143 | height => ( $padding->{top} // 0 ) + ( $padding->{bottom} // 0 ), | ||
| 144 | ); | ||
| 145 | $config->{layout} = { %$layout, sizing => { %$sizing, map { $_ => _with_inset( $natural{$_}, $inset{$_} ) } @open } }; | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | |||
| 149 | # The rows of the natural size, when it is a number of rows; a display | ||
| 150 | # that grows with its box (a sizing) takes at least the sizing's | ||
| 151 | # minimum, else one row. | ||
| 152 | method estimated_content_rows :override ($columns) { | ||
| 153 | my ( undef, $rows ) = $self->natural_size; | ||
| 154 | return $rows unless ref $rows; | ||
| 155 | return max( 1, $rows->{min} // 0 ); | ||
| 156 | } | ||
| 157 | |||
| 158 | # A natural axis as a sizing of the whole widget: a sizing hash with | ||
| 159 | # the inset added to its limits (a max of 0 is no limit), or the cells | ||
| 160 | # as a fixed sizing. | ||
| 161 | sub _with_inset ( $natural, $inset ) { | ||
| 162 | return sizing_fixed( $natural + $inset ) unless ref $natural; | ||
| 163 | return { %$natural, min => $natural->{min} + $inset, ( $natural->{max} > 0 ? ( max => $natural->{max} + $inset ) : () ) }; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | 1; | ||
| 168 | |||
| 169 | __END__ |