| 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, 2020-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.800; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | package Tickit::WidgetRole::SingleChildContainer 0.42; | ||
| 11 | role Tickit::WidgetRole::SingleChildContainer; | ||
| 12 | |||
| 13 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | C<Tickit::WidgetRole::SingleChildContainer> - role for widgets that contain a | ||
| 18 | single other widget | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | class Some::Widget::Class | ||
| 23 | :isa(Tickit::Widget) | ||
| 24 | :does(Tickit::WidgetRole::SingleChildContainer); | ||
| 25 | |||
| 26 | ... | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Applying this role to a L<Tickit::Widget> subclass adds behaviour for it to | ||
| 31 | act as a container widget holding a single child widget. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | 0 | ▶ | field $_child :reader; |
|
Subroutine
child: not called CC=1 SCAR=2.6
Pod
documented
| |||
| 36 | |||
| 37 | =head1 METHODS | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | =head2 child | ||
| 42 | |||
| 43 | $child = $widget->child; | ||
| 44 | |||
| 45 | Returns the contained child widget. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | # generated accessor | ||
| 50 | |||
| 51 | method children | ||
| 52 | { | ||
| 53 | return $_child ? ( $_child ) : () if wantarray; | ||
| 54 | return $_child ? 1 : 0; | ||
| 55 | 1 | } | |
| 56 | |||
| 57 | =head2 set_child | ||
| 58 | |||
| 59 | $widget->set_child( $child ); | ||
| 60 | |||
| 61 | Sets the child widget, or C<undef> to remove. | ||
| 62 | |||
| 63 | This method returns the container widget instance itself making it suitable to | ||
| 64 | use as a chaining mutator; e.g. | ||
| 65 | |||
| 66 | my $container = Tickit::SingleChildWidget->new( ... ) | ||
| 67 | ->set_child( Tickit::Widget::Static->new( ... ) ); | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | method set_child | ||
| 72 | { | ||
| 73 | my ( $child ) = @_; | ||
| 74 | |||
| 75 | if( my $old_child = $_child ) { | ||
| 76 | $self->remove( $old_child ); | ||
| 77 | } | ||
| 78 | |||
| 79 | if( $child ) { | ||
| 80 | $self->add( $child ); | ||
| 81 | } | ||
| 82 | |||
| 83 | return $self; | ||
| 84 | 1 | } | |
| 85 | |||
| 86 | method add | ||
| 87 | { | ||
| 88 | croak "Already have a child; cannot add another" if $_child; | ||
| 89 | ( $_child ) = @_; | ||
| 90 | $self->next::method( @_ ); | ||
| 91 | 1 | } | |
| 92 | |||
| 93 | method remove | ||
| 94 | { | ||
| 95 | my ( $child ) = @_; | ||
| 96 | croak "Cannot remove this child" if !$_child or $_child != $child; | ||
| 97 | undef $_child; | ||
| 98 | $self->next::method( $child ); | ||
| 99 | 1 | } | |
| 100 | |||
| 101 | =head1 AUTHOR | ||
| 102 | |||
| 103 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | 1 | 0x55AA; | |