WidgetRole/Alignable.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 55 CC 3 SCAR 17.5
CC
3
Cov
0%
export_subs_for
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 5.8
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 package Tickit::WidgetRole::Alignable 0.52;
7  
8 use v5.14;
9 use warnings;
10 use base qw( Tickit::WidgetRole );
11  
12 use Carp;
13  
14 use Tickit::Utils qw( align );
15  
16 =head1 NAME
17  
18 C<Tickit::WidgetRole::Alignable> - implement widgets with adjustable alignment
19  
20 =head1 DESCRIPTION
21  
22 Mixing this parametric role into a L<Tickit::Widget> subclass adds behaviour
23 to implement alignment of content within a possibly-larger space.
24  
25 =cut
26  
27 =head1 METHODS
28  
29 The following methods are provided parametrically on the caller package when
30 the module is imported by
31  
32 use Tickit::WidgetRole::Alignable
33 name => NAME,
34 style => STYLE,
35 reshape => RESHAPE;
36  
37 The parameters are
38  
39 =over 4
40  
41 =item name => STRING
42  
43 Optional. The name to use for C<NAME> in the following generated methods.
44 Defaults to C<'align'> if not provided.
45  
46 =item dir => "h" or "v"
47  
48 Optional. The direction, horizontal or vertical, that the alignment
49 represents. Used to parse symbolic names into fractions. Defaults to C<'h'> if
50 not provided.
51  
52 =item reshape => BOOL
53  
54 Optional. If true, the widget will be reshaped after the value has been set by
55 calling the C<reshape> method. If false or absent then it will just be redrawn
56 by calling C<redraw>.
57  
58 =back
59  
60 =cut
61  
62 my %symbolics = (
63 h => { left => 0.0, centre => 0.5, right => 1.0 },
64 v => { top => 0.0, middle => 0.5, bottom => 1.0 },
65 );
66  
67 sub export_subs_for
68 {
69 my $class = shift;
70 shift;
71 my %args = @_;
72  
73 my $name = $args{name} || "align";
74 my $dir = $args{dir} || "h";
75  
76 my $post_set_method = $args{reshape} ? "reshape" : "redraw";
77  
78 my $symbolics = $symbolics{$dir} or croak "Unrecognised dir - $dir";
79  
80 return {
81 "$name" => sub {
82 my $self = shift;
83 return $self->{$name};
84 },
85 "set_$name" => sub {
86 my $self = shift;
87 my ( $align ) = @_;
88  
89 # Convert symbolics
90 $align = $symbolics->{$align} if exists $symbolics->{$align};
91  
92 $self->{$name} = $align;
93  
94 $self->$post_set_method;
95 },
96  
97 "_${name}_allocation" => sub {
98 my $self = shift;
99 my ( $value, $total ) = @_;
100  
101 return align( $value, $total, $self->$name );
102 },
103 };
104 }
105  
106 =head2 I<NAME>
107  
108 $align = $widget->NAME;
109  
110 Return the current alignment value
111  
112 =cut
113  
114 =head2 set_I<NAME>
115  
116 $widget->set_NAME( $value );
117  
118 Set the new alignment value
119  
120 Gives a value in the range from C<0.0> to C<1.0> to align the content display
121 within the window.
122  
123 For vertical direction alignments, the symbolic values C<top>, C<middle> and
124 C<bottom> can be supplied instead of C<0.0>, C<0.5> and C<1.0> respectively.
125  
126 For horizontal direction alignments, the symbolic values C<left>, C<centre>
127 and C<right> can be supplied instead of C<0.0>, C<0.5> and C<1.0>
128 respectively.
129  
130 =head2 _I<NAME>_allocation
131  
132 ( $before, $alloc, $after ) = $widget->_NAME_allocation( $value, $total );
133  
134 Returns a list of three integers created by aligning the C<$value> to the
135 given alignment position within the C<$total>. See also C<align> in
136 L<Tickit::Utils>.
137  
138 =cut
139  
140 =head1 AUTHOR
141  
142 Paul Evans <leonerd@leonerd.org.uk>
143  
144 =cut
145  
146 0x55AA;