ContainerWidget.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Widget/VSplit.pm ↑ Summary Widget/Border.pm »
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 1 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Object::Pad 0.807;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Tickit::ContainerWidget 0.59;
11 class Tickit::ContainerWidget;
12  
13 0 ▶inherit Tickit::Widget;
Subroutine BEGIN: called CC=1 SCAR=2.6
14  
15 use Carp;
16  
17 use Scalar::Util qw( refaddr );
18  
19 =head1 NAME
20  
21 C<Tickit::ContainerWidget> - abstract base class for widgets that contain
22 other widgets
23  
24 =head1 SYNOPSIS
25  
26 TODO
27  
28 =head1 DESCRIPTION
29  
30 This class acts as an abstract base class for widgets that contain at leaast
31 one other widget object. It provides storage for a hash of "options"
32 associated with each child widget.
33  
34 =head1 STYLE
35  
36 The following style tags are used:
37  
38 =over 4
39  
40 =item :focus-child
41  
42 Set whenever a child widget within the container has the input focus.
43  
44 =back
45  
46 =cut
47  
48 =head1 CONSTRUCTOR
49  
50 =cut
51  
52 =head2 new
53  
54 $widget = Tickit::ContainerWidget->new( %args );
55  
56 Constructs a new C<Tickit::ContainerWidget> object. Must be called on a
57 subclass that implements the required methods; see the B<SUBCLASS METHODS>
58 section below.
59  
60 =cut
61  
62 field %_child_opts;
63  
64 # This class should probably be a role
65 ADJUST
66 {
67 my $class = ref $self;
68  
69 foreach my $method (qw( children )) {
70 $class->can( $method ) or
71 croak "$class cannot ->$method - do you subclass and implement it?";
72 }
73 }
74  
75 =head1 METHODS
76  
77 =cut
78  
79 =head2 add
80  
81 $widget->add( $child, %opts );
82  
83 Sets the child widget's parent, stores the options for the child, and calls
84 the C<children_changed> method. The concrete implementation will have to
85 implement storage of this child widget.
86  
87 Returns the container C<$widget> itself, for easy chaining.
88  
89 =cut
90  
91 method add
92 {
93 my ( $child, %opts ) = @_;
94  
95 $child and $child->isa( "Tickit::Widget" ) or
96 croak "Expected child to be a Tickit::Widget";
97  
98 $child->set_parent( $self );
99  
100 $_child_opts{refaddr $child} = \%opts;
101  
102 $self->children_changed;
103  
104 return $self;
105 }
106  
107 =head2 remove
108  
109 $widget->remove( $child_or_index );
110  
111 Removes the child widget's parent, and calls the C<children_changed> method.
112 The concrete implementation will have to remove this child from its storage.
113  
114 Returns the container C<$widget> itself, for easy chaining.
115  
116 =cut
117  
118 method remove
119 {
120 my ( $child ) = @_;
121  
122 $child->set_parent( undef );
123 $child->window->close if $child->window;
124 $child->set_window( undef );
125  
126 delete $_child_opts{refaddr $child};
127  
128 $self->children_changed;
129  
130 return $self;
131 }
132  
133 =head2 child_opts
134  
135 %opts = $widget->child_opts( $child );
136  
137 $opts = $widget->child_opts( $child );
138  
139 Returns the options currently set for the given child as a key/value list in
140 list context, or as a HASH reference in scalar context. The HASH reference in
141 scalar context is the actual hash used to store the options - modifications to
142 it will be preserved.
143  
144 =cut
145  
146 method child_opts
147 {
148 my ( $child ) = @_;
149  
150 my $opts = $_child_opts{refaddr $child};
151 return $opts if !wantarray;
152 return %$opts;
153 }
154  
155 =head2 set_child_opts
156  
157 $widget->set_child_opts( $child, %newopts );
158  
159 Sets new options on the given child. Any options whose value is given as
160 C<undef> are deleted.
161  
162 =cut
163  
164 method set_child_opts
165 {
166 my ( $child, %newopts ) = @_;
167  
168 my $opts = $_child_opts{refaddr $child};
169  
170 foreach ( keys %newopts ) {
171 defined $newopts{$_} ? ( $opts->{$_} = $newopts{$_} ) : ( delete $opts->{$_} );
172 }
173  
174 $self->children_changed;
175 }
176  
177 method child_resized
178 {
179 $self->reshape if $self->window;
180 $self->resized;
181 }
182  
183 method children_changed
184 {
185 $self->reshape if $self->window;
186 $self->resized;
187 }
188  
189 method window_gained
190 {
191 $self->SUPER::window_gained( @_ );
192  
193 $self->window->set_focus_child_notify( 1 );
194 }
195  
196 method window_lost
197 {
198 foreach my $child ( $self->children ) {
199 my $childwin = $child->window;
200 $childwin and $childwin->close;
201  
202 $child->set_window( undef );
203 }
204  
205 $self->SUPER::window_lost( @_ );
206 }
207  
208 method _on_win_focus
209 {
210 my ( $win, $evtype, $childwin ) = @_;
211 $self->SUPER::_on_win_focus( @_ );
212  
213 $self->set_style_tag( "focus-child" => $evtype ) if $childwin;
214 }
215  
216 =head2 find_child
217  
218 $child = $widget->find_child( $how, $other, %args );
219  
220 Returns a child widget. The C<$how> argument determines how this is done,
221 relative to the child widget given by C<$other>:
222  
223 =over 4
224  
225 =item first
226  
227 The first child returned by C<children> (C<$other> is ignored)
228  
229 =item last
230  
231 The last child returned by C<children> (C<$other> is ignored)
232  
233 =item before
234  
235 The child widget just before C<$other> in the order given by C<children>
236  
237 =item after
238  
239 The child widget just after C<$other> in the order given by C<children>
240  
241 =back
242  
243 Takes the following named arguments:
244  
245 =over 8
246  
247 =item where => CODE
248  
249 Optional. If defined, gives a filter function to filter the list of children
250 before searching for the required one. Will be invoked once per child, with
251 the child widget set as C<$_>; it should return a boolean value to indicate if
252 that child should be included in the search.
253  
254 =back
255  
256 =cut
257  
258 method find_child
259 {
260 my ( $how, $other, %args ) = @_;
261  
262 my $children = $args{children} // "children";
263 my @children = $self->$children;
264 if( my $where = $args{where} ) {
265 @children = grep { defined $other and $_ == $other or $where->() } @children;
266 }
267  
268 if( $how eq "first" ) {
269 return $children[0];
270 }
271 elsif( $how eq "last" ) {
272 return $children[-1];
273 }
274 elsif( $how eq "before" ) {
275 $children[$_] == $other and return $children[$_-1] for 1 .. $#children;
276 return undef;
277 }
278 elsif( $how eq "after" ) {
279 $children[$_] == $other and return $children[$_+1] for 0 .. $#children-1;
280 return undef;
281 }
282 else {
283 croak "Unrecognised ->find_child mode '$how'";
284 }
285 }
286  
287 use constant CONTAINER_OR_FOCUSABLE => sub {
288 $_->isa( "Tickit::ContainerWidget" ) or
289 $_->window && $_->window->is_visible && $_->CAN_FOCUS
290 };
291  
292 =head2 focus_next
293  
294 $widget->focus_next( $how, $other );
295  
296 Moves the input focus to the next widget in the widget tree, by searching in
297 the direction given by C<$how> relative to the widget given by C<$other>
298 (which must be an immediate child of C<$widget>).
299  
300 The direction C<$how> must be one of the following four values:
301  
302 =over 4
303  
304 =item first
305  
306 =item last
307  
308 Moves focus to the first or last child widget that can take focus. Recurses
309 into child widgets that are themselves containers. C<$other> is ignored.
310  
311 =item after
312  
313 =item before
314  
315 Moves focus to the next or previous child widget in tree order from the one
316 given by C<$other>. Recurses into child widgets that are themselves
317 containers, and out into parent containers.
318  
319 These searches will wrap around the widget tree; moving C<after> the last node
320 in the widget tree will move to the first, and vice versa.
321  
322 =back
323  
324 This differs from C<find_child> in that it performs a full tree search through
325 the widget tree, considering parents and children. If a C<before> or C<after>
326 search falls off the end of one node, it will recurse up to its parent and
327 search within the next child, and so on.
328  
329 Usually this would be used via the widget itself:
330  
331 $self->parent->focus_next( $how => $self );
332  
333 =cut
334  
335 method focus_next
336 {
337 my ( $how, $other ) = @_;
338  
339 # This tree search has the potential to loop infinitely, if there are no
340 # focusable widgets at all. It would only do this if it cycles via the root
341 # widget twice in a row. Technically we could detect it earlier, but that
342 # is more difficult to arrange for
343 my $done_root;
344  
345 my $next;
346  
347 my $children = $self->can( "children_for_focus" ) || "children";
348  
349 while(1) {
350 $next = $self->find_child( $how, $other,
351 where => CONTAINER_OR_FOCUSABLE,
352 children => $children,
353 );
354 last if $next and $next->CAN_FOCUS;
355  
356 # Either we found a container (recurse into it),
357 if( $next ) {
358 my $childhow = $how;
359 if( $how eq "after" ) { $childhow = "first" }
360 elsif( $how eq "before" ) { $childhow = "last" }
361  
362 # See if child has it
363 return 1 if $next->focus_next( $childhow => undef );
364  
365 $other = $next;
366 redo;
367 }
368 # or we'll have to recurse up to my parent
369 elsif( my $parent = $self->parent ) {
370 if( $how eq "after" or $how eq "before" ) {
371 $other = $self;
372 $self = $parent;
373 redo;
374 }
375 else {
376 return undef;
377 }
378 }
379 # or we'll have to cycle around the root
380 else {
381 die "Cycled through the entire widget tree and did not find a focusable widget" if $done_root;
382 $done_root++;
383  
384 if( $how eq "after" ) { $how = "first" }
385 elsif( $how eq "before" ) { $how = "last" }
386 else { die "Cannot cycle how=$how around root widget"; }
387  
388 $other = undef;
389 redo;
390 }
391 }
392  
393 $next->take_focus;
394 return 1;
395 }
396  
397 =head1 SUBCLASS METHODS
398  
399 =head2 children
400  
401 @children = $widget->children;
402  
403 Required. Should return a list of all the contained child widgets. The order
404 is not specified, but should be in some stable order that makes sense given
405 the layout of the widget's children.
406  
407 This method is used by C<window_lost> to remove the windows from all the child
408 widgets automatically, and by C<find_child> to obtain a child relative to
409 another given one.
410  
411 =head2 children_for_focus
412  
413 @children = $widget->children_for_focus;
414  
415 Optional. If implemented, this method is called to obtain a list of child
416 widgets to perform a child search on when changing focus using the
417 C<focus_next> method. If it is not implemented, the regular C<children> method
418 is called instead.
419  
420 Normally this method shouldn't be used, but it may be useful on container
421 widgets that also display "helper" widgets that should not be considered as
422 part of the main focus set. This method can then exclude them.
423  
424 =head2 children_changed
425  
426 $widget->children_changed;
427  
428 Optional. If implemented, this method will be called after any change of the
429 contained child widgets or their options. Typically this will be used to set
430 windows on them by sub-dividing the window of the parent.
431  
432 If not overridden, the base implementation will call C<reshape>.
433  
434 =head2 child_resized
435  
436 $widget->child_resized( $child );
437  
438 Optional. If implemented, this method will be called after a child widget
439 changes or may have changed its size requirements. Typically this will be used
440 to adjusts the windows allocated to children.
441  
442 If not overridden, the base implementation will call C<reshape>.
443  
444 =head1 AUTHOR
445  
446 Paul Evans <leonerd@leonerd.org.uk>
447  
448 =cut
449  
450 0x55AA;
« Widget/VSplit.pm ↑ Summary Widget/Border.pm »