blib/lib/Term/Fabulous/Widget/ScrollBox.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 152 CC 2 SCAR 9.8
CC
2
Cov
0%
BUILDARGS
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.7
1 package Term::Fabulous::Widget::ScrollBox;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 use Object::Pad 0.825;
9  
10 use Clay::UI::Role::Layout::HasScroll;
11 use Term::Fabulous::Widget::Box;
12  
13 our $VERSION = '0.03';
14  
15 class Term::Fabulous::Widget::ScrollBox
16 :isa(Term::Fabulous::Widget::Box)
17 :does(Clay::UI::Role::Layout::HasScroll)
18 :strict(params)
19 {
20 use Clay::XS qw(
21 sizing_grow sizing_fixed CLAY_LEFT_TO_RIGHT CLAY_ALIGN_Y_BOTTOM
22 CLAY_ATTACH_TO_PARENT CLAY_ATTACH_POINT_LEFT_TOP CLAY_CLIP_TO_ATTACHED_PARENT
23 CLAY_POINTER_CAPTURE_MODE_PASSTHROUGH
24 );
25 use Term::Fabulous::Check qw(boolean);
26 use Term::Fabulous::Widget::Scrollbar;
27  
28 # The gutter floats over the whole box and is padded by its border: a
29 # column with the horizontal scrollbar at its bottom, then the
30 # vertical scrollbar, which runs the full height. It lets the pointer
31 # through, so the box below still scrolls with the wheel and its
32 # content is still hovered.
33 my %GUTTER_LAYOUT = ( layout_direction => CLAY_LEFT_TO_RIGHT, sizing => { width => sizing_grow(), height => sizing_grow() } );
34 my %GUTTER_FLOATING = (
35 attach_to => CLAY_ATTACH_TO_PARENT,
36 attach_points => { element => CLAY_ATTACH_POINT_LEFT_TOP, parent => CLAY_ATTACH_POINT_LEFT_TOP },
37 clip_to => CLAY_CLIP_TO_ATTACHED_PARENT,
38 pointer_capture_mode => CLAY_POINTER_CAPTURE_MODE_PASSTHROUGH,
39 );
40 my %BAR_LAYOUT = (
41 vertical => { sizing => { width => sizing_fixed(1), height => sizing_grow() } },
42 horizontal => { sizing => { width => sizing_grow(), height => sizing_fixed(1) } },
43 );
44 my %SIDE_OF_AXIS = ( vertical => 'right', horizontal => 'bottom' );
45  
46 field $scrollbar :param = 1;
47 field $_gutter;
48 field %_bar_by_axis;
49  
50 # Clay::UI stores these as given; the constructor takes any truth value.
51 sub BUILDARGS ( $class, %params ) {
52 $params{$_} = $params{$_} ? 1 : 0 foreach grep { exists $params{$_} } qw(horizontal vertical);
53 return $class->SUPER::BUILDARGS(%params);
54 }
55  
56 # The colors go to the scrollbars once they exist; undef leaves them
57 # to the theme.
58 ADJUST :params ( :$track_color = undef, :$thumb_color = undef ) {
59 $scrollbar = boolean( $self, scrollbar => $scrollbar );
60 %_bar_by_axis = map { $_ => Term::Fabulous::Widget::Scrollbar->new( follows => $self, axis => $_, layout => $BAR_LAYOUT{$_} ) } keys %BAR_LAYOUT;
61 $self->set_look( track_color => $track_color ) if defined $track_color;
62 $self->set_look( thumb_color => $thumb_color ) if defined $thumb_color;
63  
64 my $column = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow(), height => sizing_grow() }, child_alignment => { y => CLAY_ALIGN_Y_BOTTOM } } );
65 $column->add_child( $_bar_by_axis{horizontal} );
66 $_gutter = Term::Fabulous::Widget::Box->new( floating => {%GUTTER_FLOATING} );
67 $_gutter->add_child( $column, $_bar_by_axis{vertical} );
68 $self->_pad_gutter;
69 $self->add_internal_children($_gutter) if $scrollbar;
70 }
71  
72 method layout_properties :common () {
73 return ( $class->SUPER::layout_properties, horizontal => 'boolean', vertical => 'boolean', scrollbar => 'boolean' );
74 }
75  
76 # Whether a scrollbar is shown for an axis: wanted, and the box
77 # scrolls that way.
78 method _shows_bar ($axis) {
79 return $scrollbar && ( $axis eq 'vertical' ? $self->vertical : $self->horizontal ) ? 1 : 0;
80 }
81  
82 # The content keeps clear of the scrollbars: one column at the right
83 # for the vertical one, one row at the bottom for the horizontal one.
84 method contribute_layout :override ($config) {
85 $self->SUPER::contribute_layout($config);
86 my @sides = map { $SIDE_OF_AXIS{$_} } grep { $self->_shows_bar($_) } sort keys %SIDE_OF_AXIS;
87 return unless @sides;
88 my %padding = %{ $config->{layout}{padding} // {} };
89 $padding{$_} = ( $padding{$_} // 0 ) + 1 foreach @sides;
90 $config->{layout} = { %{ $config->{layout} // {} }, padding => \%padding };
91 return;
92 }
93  
94 # The gutter is padded by the border, so the scrollbars sit inside it.
95 method _pad_gutter () {
96 $_gutter->layout( { %GUTTER_LAYOUT, padding => $self->drawn_border_sides } );
97 return;
98 }
99  
100 # The border may change with bordered or with the theme
101 # (Term::Fabulous::Role::Themed).
102 method looks_changed (@names) {
103 $self->_pad_gutter if grep { $_ eq 'bordered' } @names;
104 return;
105 }
106  
107 method scrollbar (@new) {
108 return $scrollbar unless @new;
109 $scrollbar = boolean( $self, scrollbar => $new[0] );
110 my $attached = defined $_gutter->parent ? 1 : 0;
111 $self->add_internal_children($_gutter) if $scrollbar && !$attached;
112 $self->remove_internal_children($_gutter) if !$scrollbar && $attached;
113 return $scrollbar;
114 }
115  
116 # The scrollbars keep the colors (Term::Fabulous::Role::Themed): both
117 # take a color given here, reset_look returns both to the theme, and a
118 # read asks the vertical one.
119 method forwarded_looks :common () {
120 return ( _scrollbars => [ 'Term::Fabulous::Widget::Scrollbar', qw(track_color thumb_color) ] );
121 }
122  
123 method _scrollbars () {
124 return @_bar_by_axis{qw(vertical horizontal)};
125 }
126  
127 method track_color (@new) { return @new ? $self->set_look( track_color => $new[0] ) : $self->look_value('track_color') }
128 method thumb_color (@new) { return @new ? $self->set_look( thumb_color => $new[0] ) : $self->look_value('thumb_color') }
129 }
130  
131 1;
132  
133 __END__