Widget/Box.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 122 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2012-2023 -- leonerd@leonerd.org.uk
5  
6 use v5.20;
7 use warnings;
8 use Object::Pad 0.807;
9  
10 package Tickit::Widget::Box 0.60;
11 class Tickit::Widget::Box :strict(params);
12  
13 inherit Tickit::ContainerWidget;
14  
15 apply Tickit::WidgetRole::SingleChildContainer;
16  
17 use Carp;
18  
19 use Tickit::Style;
20 use Tickit::RenderBuffer;
21  
22 use Tickit::Utils qw( bound );
23  
24 use constant WIDGET_PEN_FROM_STYLE => 1;
25  
26 =head1 NAME
27  
28 C<Tickit::Widget::Box> - apply spacing and positioning to a widget
29  
30 =head1 SYNOPSIS
31  
32 use Tickit;
33 use Tickit::Widget::Box;
34 use Tickit::Widget::Static;
35  
36 my $box = Tickit::Widget::Box->new(
37 bg => "green",
38 child_lines => "80%",
39 child_cols => "80%",
40 )
41 ->set_child(
42 Tickit::Widget::Static->new(
43 text => "Hello, world!",
44 bg => "black",
45 align => "centre",
46 valign => "middle",
47 )
48 );
49  
50 Tickit->new( root => $box )->run;
51  
52 =head1 DESCRIPTION
53  
54 This subclass of L<Tickit::SingleChildWidget> can apply spacing around the
55 outside of a given child widget. The size of the Box is controlled by the size
56 of the child widget bounded by the given limits, allowing it to enforce a
57 given minimum or maximum size in each of the horizontal and vertical
58 directions. By setting both the minimum and maximum size to the same value,
59 the exact size of the child widget can be controlled.
60  
61 Limits can be specified either as absolute values, or as a percentage of the
62 maxmium available space.
63  
64 If the Box is given more space to use than the child widget will consume, the
65 child will be placed somewhere within the space, at a position that is
66 controllable using the C<align> and C<valign> properties, as defined by
67 L<Tickit::WidgetRole::Alignable>.
68  
69 =head1 STYLE
70  
71 The default style pen is used as the widget pen.
72  
73 Note that while the widget pen is mutable and changes to it will result in
74 immediate redrawing, any changes made will be lost if the widget style is
75 changed.
76  
77 =cut
78  
79 =head1 CONSTRUCTOR
80  
81 =cut
82  
83 =head2 new
84  
85 $box = Tickit::Widget::Box->new( %args );
86  
87 In addition to the constructor arguments allowed by C<Tickit::Widget> and
88 C<Tickit::SingleChildWidget>, this constructor also recognises the following
89 named arguments:
90  
91 =over 8
92  
93 =item child_{lines,cols}_{min,max} => NUM or STRING
94  
95 Initial values for size limit options.
96  
97 =item child_{lines,cols} => NUM or STRING
98  
99 Initial values for size forcing options.
100  
101 =item align => NUM or STRING
102  
103 =item valign => NUM or STRING
104  
105 Initial values for alignment options.
106  
107 =back
108  
109 =cut
110  
111 ADJUST :params (
112 :$align = 0.5,
113 :$valign = 0.5,
114 %params, # easier than declaring 6 named ones
115 ) {
116 foreach (qw( child_lines_min child_lines_max child_cols_min child_cols_max
117 child_lines child_cols )) {
118 my $val = delete $params{$_};
119 $self->${\"set_$_"}( $val ) if defined $val;
120 }
121  
122 $self->set_align( $align );
123 $self->set_valign( $valign );
124 }
125  
126 method lines
127 {
128 my $child = $self->child;
129 return $child ? bound( $self->child_lines_min, $child->requested_lines, $self->child_lines_max ) : 1;
130 }
131  
132 method cols
133 {
134 my $child = $self->child;
135 return $child ? bound( $self->child_cols_min, $child->requested_cols, $self->child_cols_max ) : 1;
136 }
137  
138 method render_to_rb
139 {
140 my ( $rb, $rect ) = @_;
141  
142 $rb->eraserect( $rect );
143 }
144  
145 =head1 METHODS
146  
147 The following methods all accept either absolute sizes, specified in integers,
148 or percentages, specified in strings of the form C<10%>. If a percentage is
149 given it specifies a size that is a fraction of the total amount that is
150 available to the Box.
151  
152 =head2 child_lines_min
153  
154 =head2 set_child_lines_min
155  
156 =head2 child_cols_min
157  
158 =head2 set_child_cols_min
159  
160 $min = $box->child_lines_min;
161  
162 $box->set_child_lines_min( $min );
163  
164 $min = $box->child_cols_min;
165  
166 $box->set_child_cols_min( $min );
167  
168 Accessors for the child size minimum limits. If the child widget requests a
169 size smaller than these limits, the allocated Window will be resized up to at
170 least these sizes.
171  
172 =head2 child_lines_max
173  
174 =head2 set_child_lines_max
175  
176 =head2 child_cols_max
177  
178 =head2 set_child_cols_max
179  
180 $max = $box->child_lines_max;
181  
182 $box->set_child_lines_max( $max );
183  
184 $max = $box->child_cols_max;
185  
186 $box->set_child_cols_max( $max );
187  
188 Accessors for the child size maximum limits. If the child widget requests a
189 size larger than these limits, the allocated Window will be resized down to at
190 most these sizes.
191  
192 =head2 set_child_lines
193  
194 =head2 set_child_cols
195  
196 $box->set_child_lines( $size );
197  
198 $box->set_child_cols( $size );
199  
200 Convenient shortcut mutators that set both the minimum and maximum limit to
201 the same value. This has the effect of forcing the size of the child widget.
202  
203 =cut
204  
205 field $_child_lines_max; method _child_lines_max :lvalue { $_child_lines_max }
206 field $_child_lines_min; method _child_lines_min :lvalue { $_child_lines_min }
207 field $_child_cols_max; method _child_cols_max :lvalue { $_child_cols_max }
208 field $_child_cols_min; method _child_cols_min :lvalue { $_child_cols_min }
209  
210 # Because I hate copying code 4 times
211 BEGIN {
212 use Object::Pad 0.800 ':experimental(mop)';
213  
214 my $meta = Object::Pad::MOP::Class->for_caller;
215  
216 foreach my $dir (qw( lines cols )) {
217 foreach my $lim (qw( max min )) {
218 my $fieldmeth = "_child_${dir}_${lim}";
219 my $name = "child_${dir}_${lim}";
220  
221 $meta->add_method( $name => method {
222 my $value = $self->$fieldmeth;
223 if( !defined $value ) {
224 return undef;
225 }
226 elsif( $value =~ m/^(.+)%$/ ) {
227 my $win = $self->window;
228 return $win ? int( $1 * $win->$dir / 100 ) : undef;
229 }
230 else {
231 return $value;
232 }
233 } );
234  
235 $meta->add_method( "set_$name" => method {
236 ( $self->$fieldmeth ) = @_;
237 $self->resized;
238 } );
239 }
240  
241 my $set_min = "set_child_${dir}_min";
242 my $set_max = "set_child_${dir}_max";
243  
244 $meta->add_method( "set_child_$dir" => method {
245 my ( $value ) = @_;
246 $self->$set_min( $value );
247 $self->$set_max( $value );
248 } );
249 }
250 }
251  
252 use Tickit::WidgetRole::Alignable name => "align", dir => "h", reshape => 1;
253 use Tickit::WidgetRole::Alignable name => "valign", dir => "v", reshape => 1;
254  
255 method reshape
256 {
257 my $window = $self->window or return;
258 my $child = $self->child or return;
259  
260 my ( $top, $lines ) = $self->_valign_allocation( $self->lines, $window->lines );
261 my ( $left, $cols ) = $self->_align_allocation ( $self->cols, $window->cols );
262 my @geom = ( $top, $left, $lines, $cols );
263  
264 if( my $childwin = $child->window ) {
265 $childwin->change_geometry( @geom );
266 }
267 else {
268 $child->set_window( $window->make_sub( @geom ) );
269 }
270  
271 $self->redraw;
272 }
273  
274 method window_lost
275 {
276 my $child = $self->child or return;
277 $child->set_window( undef );
278 }
279  
280 =head1 AUTHOR
281  
282 Paul Evans <leonerd@leonerd.org.uk>
283  
284 =cut
285  
286 0x55AA;