blib/lib/Term/Fabulous/Role/HasBorderStyle.pm untested

filter: Statementstmt 0.0% 0 / 142 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 263 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Role::HasBorderStyle;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.05';
9  
10 use Object::Pad 0.825;
11  
12 role Term::Fabulous::Role::HasBorderStyle {
13 use Scalar::Util qw(refaddr);
14 use Term::Fabulous::Check qw(border_style color describe glyph optional);
15 use Term::Fabulous::Color;
16 use Term::Fabulous::Enum::BorderStyle;
17  
18 # The theme's value of a slot of the widget's family
19 # (Term::Fabulous::Role::Themed): the border.style a side falls back to.
20 method look;
21  
22 my @SIDES = qw(left right top bottom);
23  
24 # The style the program gave each side, or none.
25 field %_style_of_side;
26 field $border_corners :param = undef;
27 field $outer_border_sides :param = [];
28 field $border_background_colors :param = undef;
29  
30 my @CORNERS = qw(top_left top_right bottom_left bottom_right);
31  
32 ADJUST {
33 $border_corners = _corner_glyphs( $self, $border_corners );
34 $outer_border_sides = _side_names( $self, $outer_border_sides );
35 $border_background_colors = _background_colors( $self, $border_background_colors );
36 }
37  
38 # A side parameter wins over border_style, which fills the others.
39 ADJUST :params ( :$border_style = undef, :$border_style_top = undef, :$border_style_right = undef, :$border_style_bottom = undef, :$border_style_left = undef ) {
40 my %given = ( top => $border_style_top, right => $border_style_right, bottom => $border_style_bottom, left => $border_style_left );
41 my $all = optional( \&border_style, $self, border_style => $border_style );
42 foreach my $side (@SIDES) {
43 my $style = optional( \&border_style, $self, "border_style_$side" => $given{$side} );
44 $_style_of_side{$side} = $style // $all;
45 }
46 }
47  
48 # A copy of a list of side names, each at most once, in @SIDES order.
49 sub _side_names ( $owner, $sides ) {
50 die ref($owner) . ": outer_border_sides must be an array reference of side names, got " . describe($sides)
51 unless ref $sides eq 'ARRAY';
52 my %is_side = map { $_ => 1 } @SIDES;
53 my @unknown = grep { !defined || ref || !$is_side{$_} } @$sides;
54 die ref($owner) . ": outer_border_sides knows only the sides @SIDES, got " . join( ', ', map { defined $_ ? "'$_'" : 'undef' } @unknown ) if @unknown;
55 my %wanted = map { $_ => 1 } @$sides;
56 return [ grep { $wanted{$_} } @SIDES ];
57 }
58  
59 method outer_border_sides (@new) {
60 return [@$outer_border_sides] unless @new;
61 $outer_border_sides = _side_names( $self, $new[0] );
62 $self->mark_changed;
63 return [@$outer_border_sides];
64 }
65  
66 # Whether a side is drawn on the background outside the widget. Read by
67 # Term::Fabulous::Render::Border.
68 method is_outer_border_side ($side) {
69 return ( grep { $_ eq $side } @$outer_border_sides ) ? 1 : 0;
70 }
71  
72 # Hand-written accessors, so that writing a style marks the widget
73 # changed and reading one answers the style the side is drawn in.
74 method border_style_top (@new) { return $self->_border_style( top => @new ) }
75 method border_style_right (@new) { return $self->_border_style( right => @new ) }
76 method border_style_bottom (@new) { return $self->_border_style( bottom => @new ) }
77 method border_style_left (@new) { return $self->_border_style( left => @new ) }
78  
79 method _border_style ( $side, @new ) {
80 return $self->border_style_of($side) unless @new;
81 $_style_of_side{$side} = optional( \&border_style, $self, "border_style_$side" => $new[0] );
82 $self->mark_changed;
83 return $_style_of_side{$side};
84 }
85  
86 # The style a side is drawn in: the program's, else the one the widget
87 # derives (derived_border_style), else the theme's border.style for the
88 # widget, else Blank. Term::Fabulous::Render::Border draws with it.
89 method border_style_of ($side) {
90 die ref($self) . ": border_style_of takes a side (@SIDES), got " . describe($side) unless defined $side && !ref $side && exists $_style_of_side{$side};
91 return $_style_of_side{$side} // $self->_derived_border_style($side) // $self->look('border.style') // Term::Fabulous::Enum::BorderStyle->Blank;
92 }
93  
94 method _derived_border_style ($side) {
95 return $self->can('derived_border_style') ? $self->derived_border_style($side) : undef;
96 }
97  
98 # A copy of a corner glyph hash: undef, or a hash of single glyphs one
99 # column wide under the names in @CORNERS.
100 sub _corner_glyphs ( $owner, $corners ) {
101 return undef unless defined $corners;
102 die ref($owner) . ": border_corners must be undef or a hash reference of corner glyphs, got " . describe($corners)
103 unless ref $corners eq 'HASH';
104 my %is_corner = map { $_ => 1 } @CORNERS;
105 my @unknown = grep { !$is_corner{$_} } sort keys %$corners;
106 die ref($owner) . ": border_corners does not know @unknown (known: @CORNERS)" if @unknown;
107 return { map { $_ => glyph( $owner, "border_corners $_", $corners->{$_} ) } grep { defined $corners->{$_} } sort keys %$corners };
108 }
109  
110 method border_corners (@new) {
111 return defined $border_corners ? {%$border_corners} : undef unless @new;
112 $border_corners = _corner_glyphs( $self, $new[0] );
113 $self->mark_changed;
114 return defined $border_corners ? {%$border_corners} : undef;
115 }
116  
117 # The glyph drawn at a corner instead of the style's corner glyph, or
118 # undef. Read by Term::Fabulous::Render::Border.
119 method border_corner_glyph ($corner) {
120 return defined $border_corners ? $border_corners->{$corner} : undef;
121 }
122  
123 # A copy of a background color hash: undef, or a hash of colors as
124 # Term::Fabulous::Color objects under the names in @SIDES and @CORNERS.
125 sub _background_colors ( $owner, $colors ) {
126 return undef unless defined $colors;
127 die ref($owner) . ": border_background_colors must be undef or a hash reference of colors, got " . describe($colors)
128 unless ref $colors eq 'HASH';
129 my %is_known = map { $_ => 1 } @SIDES, @CORNERS;
130 my @unknown = grep { !$is_known{$_} } sort keys %$colors;
131 die ref($owner) . ": border_background_colors does not know @unknown (known: @SIDES @CORNERS)" if @unknown;
132 my @named = grep { defined $colors->{$_} } sort keys %$colors;
133 return { map { $_ => Term::Fabulous::Color->new( color => color( $owner, "border_background_colors $_", $colors->{$_} ) ) } @named };
134 }
135  
136 method border_background_colors (@new) {
137 if (@new) {
138 $border_background_colors = _background_colors( $self, $new[0] );
139 $self->mark_changed;
140 }
141 return undef unless defined $border_background_colors;
142 return { map { $_ => [ $border_background_colors->{$_}->to_rgba ] } keys %$border_background_colors };
143 }
144  
145 # The background under the glyphs of a side or corner instead of the
146 # widget's own, a Term::Fabulous::Color, or undef. Read by
147 # Term::Fabulous::Render::Border.
148 method border_background_color_of ($name) {
149 return defined $border_background_colors ? $border_background_colors->{$name} : undef;
150 }
151  
152 # The sides the widget has a border on (Term::Fabulous::Widget/bordered),
153 # as a shared hash of all four sides that must not be changed.
154 method _bordered_sides;
155  
156 # The sides a border is drawn on and takes room on: the bordered sides
157 # whose style is not Hidden. A new hash of all four sides, 1 or 0.
158 method drawn_border_sides () {
159 return $self->_drawn_sides // { map { $_ => 0 } @SIDES };
160 }
161  
162 # The same, or undef when no side is drawn: the contributors run for
163 # every widget in every frame, and most widgets have no border.
164 method _drawn_sides () {
165 my $bordered = $self->_bordered_sides;
166 return undef unless grep { $bordered->{$_} } @SIDES;
167 my $hidden = Term::Fabulous::Enum::BorderStyle->Hidden;
168 my %drawn = map { $_ => $bordered->{$_} && refaddr( $self->border_style_of($_) ) != refaddr($hidden) ? 1 : 0 } @SIDES;
169 return ( grep { $drawn{$_} } @SIDES ) ? \%drawn : undef;
170 }
171  
172 # The width of every side for Clay: 1 where a border is drawn, else 0.
173 # Clay::UI runs the contribute_* methods in alphabetical order, so this
174 # runs after contribute_border, which wrote the color the program gave,
175 # and before contribute_look_theme, which colors the border from the
176 # theme. Writes a fresh hash, so the border slice is never modified.
177 method contribute_border_sides ($config) {
178 my $drawn = $self->_drawn_sides // return;
179  
180 $config->{border} = { %{ $config->{border} // {} }, width => { %$drawn, between_children => 0 } };
181 return;
182 }
183  
184 # Clay draws borders over the content box, so every drawn side adds a
185 # cell to the padding: borders occupy cells inside the widget and the
186 # user's padding starts after them. Clay::UI runs contribute_* methods
187 # in alphabetical order, so the name sorts after contribute_layout;
188 # that way the inset is applied to the final layout slice. Writes fresh
189 # hashes, so the widget's own layout is never modified.
190 method contribute_layout_inset ($config) {
191 my $drawn = $self->_drawn_sides // return;
192  
193 my $layout = $config->{layout} // {};
194 my $padding = $layout->{padding} // {};
195 die "Term::Fabulous::Role::HasBorderStyle: layout padding must be a hash reference"
196 unless ref $padding eq 'HASH';
197  
198 $config->{layout} = {
199 %$layout,
200 padding => { %$padding, map { $_ => ( $padding->{$_} // 0 ) + $drawn->{$_} } @SIDES },
201 };
202 return;
203 }
204 }
205  
206 1;
207  
208 __END__