Widget/LinearBox.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 171 CC 1 SCAR 6.9
CC
1
Cov
0%
BUILDARGS
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, 2009-2023 -- leonerd@leonerd.org.uk
5  
6 use v5.20;
7 use warnings;
8 use Object::Pad 0.807;
9  
10 package Tickit::Widget::LinearBox 0.55;
11 class Tickit::Widget::LinearBox :strict(params);
12  
13 inherit Tickit::ContainerWidget;
14  
15 use experimental 'postderef';
16  
17 use Tickit::RenderBuffer;
18  
19 use Carp;
20 use Syntax::Keyword::Dynamically;
21  
22 use Tickit::Utils qw( distribute );
23  
24 use List::Util qw( sum );
25  
26 =head1 NAME
27  
28 C<Tickit::Widget::LinearBox> - abstract base class for C<HBox> and C<VBox>
29  
30 =head1 DESCRIPTION
31  
32 This class is a base class for both L<Tickit::Widget::HBox> and
33 L<Tickit::Widget::VBox>. It is not intended to be used directly.
34  
35 It maintains an ordered list of child widgets, and implements the following
36 child widget options:
37  
38 =over 8
39  
40 =item expand => NUM
41  
42 A number used to control how extra space is distributed among child widgets,
43 if the window containing this widget has more space available to it than the
44 children need. The actual value is unimportant, but extra space will be
45 distributed among the children in proportion with their C<expand> value.
46  
47 For example, if all the children have a C<expand> value of 1, extra space is
48 distributed evenly. If one child has a value of 2, it will gain twice as much
49 extra space as its siblings. Any child with a value of 0 will obtain no extra
50 space.
51  
52 =item force_size => NUM
53  
54 If provided, forces the size of this child widget, overriding the value
55 returned by C<get_child_base>.
56  
57 =back
58  
59 =cut
60  
61 =head1 CONSTRUCTOR
62  
63 =cut
64  
65 =head2 new
66  
67 $widget = Tickit::Widget::LinearBox->new( %args );
68  
69 Returns a new C<Tickit::Widget::LinearBox>.
70  
71 =cut
72  
73 field @_children;
74  
75 field $_suppress_redistribute;
76  
77 sub BUILDARGS
78 {
79 my $class = shift;
80 my %args = @_;
81  
82 exists $args{$_} and $args{style}{$_} = delete $args{$_} for qw( spacing );
83  
84 return $class->SUPER::BUILDARGS( %args );
85 }
86  
87 =head1 METHODS
88  
89 =cut
90  
91 =head2 children
92  
93 @children = $widget->children;
94  
95 In scalar context, returns the number of contained children. In list context,
96 returns a list of all the child widgets.
97  
98 =cut
99  
100 method children
101 {
102 return @_children;
103 }
104  
105 method _any2index
106 {
107 my ( $target ) = @_;
108 if( ref $target ) {
109 my $child = $target;
110 $_children[$_] == $child and return $_ for 0 .. $#_children;
111 croak "Unable to find child $child";
112 }
113 else {
114 my $index = $target;
115 return $index if $index >= 0 and $index < scalar @_children;
116 croak "Index $index out of bounds";
117 }
118 }
119  
120 =head2 child_opts
121  
122 %opts = $widget->child_opts( $child_or_index );
123  
124 Returns the options currently set for the given child, specified either by
125 reference or by index.
126  
127 =cut
128  
129 method child_opts
130 {
131 my ( $target ) = @_;
132 my $child = ref $target ? $target : $_children[$target];
133  
134 return unless $child;
135  
136 return $self->SUPER::child_opts( $child );
137 }
138  
139 =head2 set_child
140  
141 $widget->set_child( $index, $child );
142  
143 Replaces the child widget at the given index with the given new one;
144 preserving any options that are set on it.
145  
146 =cut
147  
148 method set_child
149 {
150 my ( $index, $child ) = @_;
151  
152 my $old_child = $_children[$index];
153  
154 my %opts;
155 if( $old_child ) {
156 %opts = $self->child_opts( $old_child );
157  
158 dynamically $_suppress_redistribute = 1;
159 $self->SUPER::remove( $old_child );
160 }
161  
162 $_children[$index] = $child;
163  
164 $self->SUPER::add( $child, %opts );
165 }
166  
167 =head2 set_child_opts
168  
169 $widget->set_child_opts( $child_or_index, %newopts );
170  
171 Sets new options on the given child, specified either by reference or by
172 index. Any options whose value is given as C<undef> are deleted.
173  
174 =cut
175  
176 method set_child_opts
177 {
178 my ( $target, @opts ) = @_;
179 my $child = ref $target ? $target : $_children[$target];
180  
181 return unless $child;
182  
183 return $self->SUPER::set_child_opts( $child, @opts );
184 }
185  
186 method render_to_rb
187 {
188 my ( $rb, $rect ) = @_;
189  
190 $rb->eraserect( $rect );
191  
192 if( $self->get_style_values( "spacing" ) > 0 and @_children and
193 my $line_style = $self->get_style_values( "line_style" ) ) {
194 my $line_pen = $self->get_style_pen( "line" );
195 my $prev_win = $_children[0]->window;
196  
197 foreach my $idx ( 1 .. $#_children ) {
198 my $next_win = $_children[$idx]->window
199 or next;
200  
201 $self->render_dividing_line( $rb, $prev_win, $next_win,
202 $line_style, $line_pen ) if $prev_win;
203  
204 $prev_win = $next_win;
205 }
206 }
207 }
208  
209 =head2 add
210  
211 $widget->add( $child, %opts );
212  
213 Adds the widget as a new child of this one, with the given options.
214  
215 This method returns the container widget instance itself making it suitable to
216 use as a chaining mutator; e.g.
217  
218 my $container = Tickit::Widget::LinearBox->new( ... )
219 ->add( Tickit::Widget::Static->new( ... ) )
220 ->add( Tickit::Widget::Static->new( ... ) );
221  
222 =cut
223  
224 method add
225 {
226 my ( $child, %opts ) = @_;
227  
228 push @_children, $child;
229  
230 $self->SUPER::add( $child,
231 expand => $opts{expand} || 0,
232 force_size => $opts{force_size},
233 );
234  
235 return $self;
236 }
237  
238 =head2 add_children
239  
240 $widget->add_children( @children );
241  
242 Adds each of the given widgets as a new child of this one. Each element of the
243 list should either be a widget object reference directly, or an unblessed hash
244 reference containing additional options. (See
245 L<Tickit::Widget/split_widget_opts>).
246  
247 This method returns the container widget instance itself making it suitable to
248 use as a chaining mutator.
249  
250 =cut
251  
252 sub add_children
253 {
254 my $self = shift;
255  
256 foreach my $arg ( @_ ) {
257 $self->add( Tickit::Widget::split_widget_opts $arg );
258 }
259  
260 return $self;
261 }
262  
263 =head2 remove
264  
265 $widget->remove( $child_or_index );
266  
267 Removes the given child widget if present, by reference or index
268  
269 =cut
270  
271 method remove
272 {
273 my $index = $self->_any2index( shift );
274  
275 my ( $child ) = splice @_children, $index, 1, ();
276  
277 $self->SUPER::remove( $child ) if $child;
278 }
279  
280 method reshape
281 {
282 $_suppress_redistribute and return;
283  
284 my $window = $self->window;
285  
286 return unless $self->children;
287  
288 my $spacing = $self->get_style_values( "spacing" );
289  
290 my @buckets;
291 foreach my $child ( $self->children ) {
292 my %opts = $self->child_opts( $child );
293  
294 push @buckets, {
295 fixed => $spacing,
296 } if @buckets; # gap
297  
298 my $base = defined $opts{force_size} ? $opts{force_size}
299 : $self->get_child_base( $child );
300 warn "Child $child did not define a base size for $self\n", $base = 0
301 unless defined $base;
302  
303 push @buckets, {
304 base => $base,
305 expand => $opts{expand},
306 child => $child,
307 };
308 }
309  
310 distribute( $self->get_total_quota( $window ), @buckets );
311  
312 foreach my $b ( @buckets ) {
313 my $child = $b->{child} or next;
314  
315 $self->set_child_window( $child, $b->{start}, $b->{value}, $window );
316 }
317  
318 $self->redraw;
319 }
320  
321 =head1 AUTHOR
322  
323 Paul Evans <leonerd@leonerd.org.uk>
324  
325 =cut
326  
327 0x55AA;