blib/lib/Term/Fabulous/Placement.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Editor.pm ↑ Summary blib/lib/Term/Fabulous/Range.pm »
1 package Term::Fabulous::Placement;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.05';
9  
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 our @EXPORT_OK = qw(place_popup TOPMOST_Z_INDEX);
12  
13 1 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶use Term::Fabulous::Check qw(describe one_of);
Subroutine BEGIN: called CC=1 SCAR=2.6
16  
17 # The highest z_index Clay has (int16): a popup belongs to the focused
18 # widget, so it floats over everything, a dialog included.
19 use constant TOPMOST_Z_INDEX => 32767;
20  
21 my %IS_OPTION = map { $_ => 1 } qw(anchor size screen prefer align);
22  
23 # The side a popup opens to when it fits there, or when the other side
24 # has no more room; [ the rows on that side, the rows on the other ].
25 my %SIDES = (
26 below => sub ( $anchor, $screen_height ) { ( $screen_height - $anchor->{y} - $anchor->{height}, $anchor->{y} ) },
27 above => sub ( $anchor, $screen_height ) { ( $anchor->{y}, $screen_height - $anchor->{y} - $anchor->{height} ) },
28 );
29 my %OTHER_SIDE = ( below => 'above', above => 'below' );
30  
31 sub place_popup (%options) {
32 my @unknown = grep { !$IS_OPTION{$_} } sort keys %options;
33 die "Term::Fabulous::Placement: place_popup does not take @unknown (known: align anchor prefer screen size)" if @unknown;
34 my $anchor = _box( $options{anchor} );
35 my ( $width, $height ) = _pair( size => $options{size} );
36 my ( $screen_width, $screen_height ) = _pair( screen => $options{screen} );
37 my $prefer = one_of( 'Term::Fabulous::Placement: place_popup', prefer => $options{prefer} // 'below', qw(below above at) );
38 my $align = one_of( 'Term::Fabulous::Placement: place_popup', align => $options{align} // 'left', qw(left right) );
39  
40 my ( $side, $flipped, $rows ) = _side( $prefer, $anchor, $height, $screen_height );
41 my $x = $align eq 'left' ? $anchor->{x} : $anchor->{x} + $anchor->{width} - $width;
42 my $y = $side eq 'below' ? $anchor->{y} + $anchor->{height} : $side eq 'above' ? $anchor->{y} - $height : $anchor->{y};
43 return {
44 x => max( 0, min( $x, $screen_width - $width ) ),
45 y => max( 0, min( $y, $screen_height - $height ) ),
46 flipped => $flipped,
47 rows => $rows,
48 };
49 }
50  
51 # A popup opens to the side it prefers when it fits there or the other
52 # side has no more room; at its anchor it may take the whole height.
53 sub _side ( $prefer, $anchor, $height, $screen_height ) {
54 return ( 'at', 0, $screen_height ) if $prefer eq 'at';
55 my ( $room, $other_room ) = $SIDES{$prefer}->( $anchor, $screen_height );
56 return ( $prefer, 0, $room ) if $height <= $room || $other_room <= $room;
57 return ( $OTHER_SIDE{$prefer}, 1, $other_room );
58 }
59  
60 sub _box ($anchor) {
61 my $is_box = ref $anchor eq 'HASH' && !grep { !looks_like_number( $anchor->{$_} // '' ) } qw(x y width height);
62 die "Term::Fabulous::Placement: place_popup anchor must be a hash reference with the numbers x, y, width and height, got " . describe($anchor) unless $is_box;
63 return $anchor;
64 }
65  
66 sub _pair ( $name, $pair ) {
67 my $is_pair = ref $pair eq 'ARRAY' && @$pair == 2 && !grep { !looks_like_number( $_ // '' ) || $_ < 0 } @$pair;
68 die "Term::Fabulous::Placement: place_popup $name must be an array reference of two numbers of at least 0, got " . describe($pair) unless $is_pair;
69 return @$pair;
70 }
71  
72 1;
73  
74 __END__
« blib/lib/Term/Fabulous/Editor.pm ↑ Summary blib/lib/Term/Fabulous/Range.pm »