blib/lib/Term/Fabulous/Widget/Display.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 158 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::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.03';
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 Time::HiRes ();
20 use Term::Fabulous::Render::Attr qw(cell_color_attr);
21 use Term::Fabulous::Theme;
22 use Term::Fabulous::Unicode qw(grapheme_clusters cluster_columns);
23  
24 # Counts mark_changed calls, the changes of the widget's own state; with
25 # the rest of the paint key, what the cells were last painted for.
26 field $_change_count = 0;
27 field $_painted_key;
28  
29 # The content size a subclass asks for when the layout gives none.
30 method natural_size;
31  
32 # Draws the widget into the cleared buffer.
33 method paint;
34  
35 # ---------------------------------------------------------------------
36 # Painting
37 # ---------------------------------------------------------------------
38  
39 method mark_changed :override () {
40 $_change_count++;
41 return $self->SUPER::mark_changed;
42 }
43  
44 # Paints the cells again when anything paint reads changed since they
45 # were last painted.
46 method refresh :override () {
47 return unless $self->columns > 0 && $self->rows > 0;
48 my $key = join "\x{1F}", map { $_ // "\x{0}" } $self->paint_key;
49 return if defined $_painted_key && $key eq $_painted_key;
50 $_painted_key = $key;
51 $self->clear;
52 $self->paint;
53 return;
54 }
55  
56 # What paint reads: the size, the widget's own state (every setter
57 # calls mark_changed) and the theme. A subclass that paints from the
58 # state of other objects adds it.
59 method paint_key () {
60 return ( $self->columns, $self->rows, $_change_count, Term::Fabulous::Theme::generation() );
61 }
62  
63 # The termbox2 attribute of a color, undef for none.
64 method color_attr ($color) {
65 return cell_color_attr( color => $color );
66 }
67  
68 # Paints a character string from ($x, $y) with termbox2 attributes,
69 # stopping before a cluster that would cross column $limit; returns the
70 # column after the last painted cluster.
71 method paint_text ( $x, $y, $text, $fg, $bg, $limit = $self->columns ) {
72 foreach my $cluster ( grapheme_clusters($text) ) {
73 my $columns = cluster_columns($cluster);
74 last if $x + $columns > $limit;
75 $self->put_attrs( $x, $y, $cluster, $fg, $bg );
76 $x += $columns;
77 }
78 return $x;
79 }
80  
81 method fill_attrs ( $x, $y, $width, $glyph, $fg, $bg ) {
82 $self->put_attrs( $_, $y, $glyph, $fg, $bg ) foreach $x .. $x + $width - 1;
83 return;
84 }
85  
86 # ---------------------------------------------------------------------
87 # Animation: frames from the application's clock, which a test or the
88 # screenshot harness can move; the system clock outside a UI.
89 # ---------------------------------------------------------------------
90  
91 method now () {
92 my $ui = $self->ui;
93 return $ui->now if defined $ui && $ui->can('now');
94 return Time::HiRes::time();
95 }
96  
97 # Asks the UI for a frame at a time on the clock; a static page draws
98 # once and ignores it.
99 method request_frame_at ($time) {
100 my $ui = $self->ui;
101 $ui->request_frame_at($time) if defined $ui && $ui->can('request_frame_at');
102 return $self;
103 }
104  
105 # The frame of an animation that shows $count frames, $interval seconds
106 # each, at the current time, and asks for a frame when the next one is
107 # due. Called from paint_key, it keeps the animation going.
108 method animation_frame ( $interval, $count ) {
109 my $tick = int( $self->now / $interval );
110 $self->request_frame_at( ( $tick + 1 ) * $interval );
111 return $tick % $count;
112 }
113  
114 # ---------------------------------------------------------------------
115 # Natural size: fills the sizing axes the layout leaves open. Clay::UI
116 # runs the contributors in alphabetical order, so this one runs after
117 # contribute_layout and contribute_layout_inset and sees the final
118 # padding, border included. An axis of the natural size is a number of
119 # cells, made a fixed sizing with the padding added, or a sizing hash
120 # of Clay::XS, whose limits get the padding added.
121 # ---------------------------------------------------------------------
122  
123 method contribute_layout_size ($config) {
124 my $layout = $config->{layout} // {};
125 my $sizing = $layout->{sizing} // {};
126 my @open = grep { !defined $sizing->{$_} } qw(width height);
127 return unless @open;
128  
129 my $padding = $layout->{padding} // {};
130 my ( $columns, $rows ) = $self->natural_size;
131 my %natural = ( width => $columns, height => $rows );
132 my %inset = (
133 width => ( $padding->{left} // 0 ) + ( $padding->{right} // 0 ),
134 height => ( $padding->{top} // 0 ) + ( $padding->{bottom} // 0 ),
135 );
136 $config->{layout} = { %$layout, sizing => { %$sizing, map { $_ => _with_inset( $natural{$_}, $inset{$_} ) } @open } };
137 return;
138 }
139  
140 # A natural axis as a sizing of the whole widget: a sizing hash with
141 # the inset added to its limits (a max of 0 is no limit), or the cells
142 # as a fixed sizing.
143 sub _with_inset ( $natural, $inset ) {
144 return sizing_fixed( $natural + $inset ) unless ref $natural;
145 return { %$natural, min => $natural->{min} + $inset, ( $natural->{max} > 0 ? ( max => $natural->{max} + $inset ) : () ) };
146 }
147 }
148  
149 1;
150  
151 __END__