« blib/lib/Term/Fabulous/Widget/ScatterPlot.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/SegmentedControl.pm »
| 1 | package Term::Fabulous::Widget::Scrollbar; | ||
| 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 | use Term::Fabulous::Widget::Display; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::Scrollbar :isa(Term::Fabulous::Widget::Display) :strict(params) { | ||
| 15 | use List::Util qw(max min); | ||
| 16 | use POSIX qw(floor); | ||
| 17 | use Scalar::Util qw(weaken); | ||
| 18 | use Clay::UI::Enum::Result; | ||
| 19 | use Clay::XS qw(sizing_fixed sizing_grow); | ||
| 20 | use Term::Fabulous::Check qw(one_of); | ||
| 21 | use Term::Fabulous::Render::Attr qw(cell_color_attr); | ||
| 22 | use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT); | ||
| 23 | use Term::Fabulous::Viewport qw(max_offset scroll_thumb); | ||
| 24 | |||
| 25 | my $CONTINUE = Clay::UI::Enum::Result->CONTINUE; | ||
| 26 | my $HANDLED = Clay::UI::Enum::Result->HANDLED; | ||
| 27 | |||
| 28 | # Per axis: the glyphs of the track and the thumb, and the keys of the | ||
| 29 | # scroll position and of the viewport and content size. | ||
| 30 | my %AXIS = ( | ||
| 31 | vertical => { track => "\x{2502}", thumb => "\x{2503}", position => 'y', size => 'height', thickness => 'width' }, | ||
| 32 | horizontal => { track => "\x{2500}", thumb => "\x{2501}", position => 'x', size => 'width', thickness => 'height' }, | ||
| 33 | ); | ||
| 34 | |||
| 35 | field $follows :param; | ||
| 36 | field $axis :param = 'vertical'; | ||
| 37 | |||
| 38 | method theme_family :common () { | ||
| 39 | return 'scrollbar'; | ||
| 40 | } | ||
| 41 | |||
| 42 | method themed_params :common () { | ||
| 43 | return ( $class->SUPER::themed_params, track_color => [ 'track', 'normal', 'cell_color' ], thumb_color => [ 'thumb', 'normal', 'cell_color' ] ); | ||
| 44 | } | ||
| 45 | |||
| 46 | ADJUST { | ||
| 47 | die "Term::Fabulous::Widget::Scrollbar: follows must be a scroll container" | ||
| 48 | unless defined $follows && $follows->DOES('Clay::UI::Role::Layout::HasScroll'); | ||
| 49 | weaken $follows; | ||
| 50 | one_of( $self, axis => $axis, keys %AXIS ); | ||
| 51 | $self->on( Mouse => sub ( $self, $event ) { return $self->_on_mouse($event) }, owner => $self ); | ||
| 52 | } | ||
| 53 | |||
| 54 | method axis () { return $axis } | ||
| 55 | |||
| 56 | method track_color (@new) { return @new ? $self->set_look( track_color => $new[0] ) : $self->look_value('track_color') } | ||
| 57 | method thumb_color (@new) { return @new ? $self->set_look( thumb_color => $new[0] ) : $self->look_value('thumb_color') } | ||
| 58 | |||
| 59 | # The cells of the track along the scrollbar's axis. | ||
| 60 | method _length () { | ||
| 61 | return $axis eq 'vertical' ? $self->rows : $self->columns; | ||
| 62 | } | ||
| 63 | |||
| 64 | # Whether the container scrolls along the scrollbar's axis at all. | ||
| 65 | method _axis_scrolls () { | ||
| 66 | return 0 unless defined $follows; | ||
| 67 | return ( $axis eq 'vertical' ? $follows->vertical : $follows->horizontal ) ? 1 : 0; | ||
| 68 | } | ||
| 69 | |||
| 70 | # A scrollbar for an axis the container does not scroll along takes | ||
| 71 | # no cells, so a box that stops scrolling sideways loses the row. | ||
| 72 | method contribute_layout :override ($config) { | ||
| 73 | $self->SUPER::contribute_layout($config); | ||
| 74 | return if $self->_axis_scrolls; | ||
| 75 | my %sizing = %{ $config->{layout}{sizing} // {} }; | ||
| 76 | $sizing{ $AXIS{$axis}{thickness} } = sizing_fixed(0); | ||
| 77 | $config->{layout} = { %{ $config->{layout} // {} }, sizing => \%sizing }; | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | |||
| 81 | # The scroll state of the container in the frame being drawn, or undef | ||
| 82 | # when it was not laid out or there is nothing to scroll along the axis. | ||
| 83 | method _scrolling () { | ||
| 84 | my $ui = $self->ui // return undef; | ||
| 85 | return undef unless $self->_axis_scrolls; | ||
| 86 | my $state = $ui->scroll_state($follows) // return undef; | ||
| 87 | my $size = $AXIS{$axis}{size}; | ||
| 88 | return $state->{content}{$size} > $state->{viewport}{$size} ? $state : undef; | ||
| 89 | } | ||
| 90 | |||
| 91 | # [ first cell, cells ] of the thumb in a track of $length cells. Clay | ||
| 92 | # keeps a scroll position as a negative offset. | ||
| 93 | method _thumb ( $state, $length ) { | ||
| 94 | my $spec = $AXIS{$axis}; | ||
| 95 | return scroll_thumb( $state->{content}{ $spec->{size} }, $state->{viewport}{ $spec->{size} }, -$state->{position}{ $spec->{position} }, $length ); | ||
| 96 | } | ||
| 97 | |||
| 98 | # The thumb in the frame being drawn, or undef for an empty scrollbar. | ||
| 99 | method _shown_thumb () { | ||
| 100 | my $length = $self->_length; | ||
| 101 | my $state = $self->_scrolling; | ||
| 102 | return defined $state && $length > 0 ? $self->_thumb( $state, $length ) : undef; | ||
| 103 | } | ||
| 104 | |||
| 105 | # One cell thick, as long as its container lets it be | ||
| 106 | # (Term::Fabulous::Widget::Display). | ||
| 107 | method natural_size () { | ||
| 108 | return $axis eq 'vertical' ? ( 1, sizing_grow() ) : ( sizing_grow(), 1 ); | ||
| 109 | } | ||
| 110 | |||
| 111 | # The thumb comes from the container's scroll state, which does not mark | ||
| 112 | # the scrollbar changed. | ||
| 113 | method paint_key :override () { | ||
| 114 | my $thumb = $self->_shown_thumb; | ||
| 115 | return ( $self->SUPER::paint_key, ( defined $thumb ? @$thumb : ( undef, undef ) ), @{ $self->track_color }, @{ $self->thumb_color } ); | ||
| 116 | } | ||
| 117 | |||
| 118 | method paint () { | ||
| 119 | my $thumb = $self->_shown_thumb // return; | ||
| 120 | __CLASS__->paint_track( | ||
| 121 | $self, | ||
| 122 | axis => $axis, | ||
| 123 | at => 0, | ||
| 124 | thumb => $thumb, | ||
| 125 | track_attr => cell_color_attr( fg => $self->track_color ), | ||
| 126 | thumb_attr => cell_color_attr( fg => $self->thumb_color ), | ||
| 127 | ); | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | |||
| 131 | # Paints a track with its thumb into one column (vertical) or row | ||
| 132 | # (horizontal) of a canvas, as long as the canvas; the one painter of | ||
| 133 | # every scrollbar of Term::Fabulous, so the glyphs live only here. | ||
| 134 | method paint_track :common ( $canvas, %bar ) { | ||
| 135 | my @unknown = grep { !/\A(?:axis|at|thumb|track_attr|thumb_attr|background_attr)\z/ } sort keys %bar; | ||
| 136 | die "$class: paint_track does not take @unknown (known: at, axis, background_attr, thumb, thumb_attr, track_attr)" if @unknown; | ||
| 137 | my $spec = $AXIS{ $bar{axis} // '' } // die "$class: paint_track needs an axis, vertical or horizontal"; | ||
| 138 | my $length = $bar{axis} eq 'vertical' ? $canvas->rows : $canvas->columns; | ||
| 139 | my ( $first, $size ) = @{ $bar{thumb} }; | ||
| 140 | foreach my $cell ( 0 .. $length - 1 ) { | ||
| 141 | my $on_thumb = $cell >= $first && $cell < $first + $size; | ||
| 142 | my ( $x, $y ) = $bar{axis} eq 'vertical' ? ( $bar{at}, $cell ) : ( $cell, $bar{at} ); | ||
| 143 | $canvas->put_attrs( $x, $y, $on_thumb ? $spec->{thumb} : $spec->{track}, $on_thumb ? $bar{thumb_attr} : $bar{track_attr}, $bar{background_attr} ); | ||
| 144 | } | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | # The scroll position that puts the thumb's middle at a cell of the | ||
| 149 | # track, for a click or a drag; undef when there is nothing to scroll. | ||
| 150 | method position_at ($cell) { | ||
| 151 | my $state = $self->_scrolling // return undef; | ||
| 152 | my $length = $self->_length; | ||
| 153 | return undef unless $length > 0; | ||
| 154 | my $size = $self->_thumb( $state, $length )->[1]; | ||
| 155 | my $free = max( 1, $length - $size ); | ||
| 156 | my $fraction = max( 0, min( 1, ( $cell - floor( $size / 2 ) ) / $free ) ); | ||
| 157 | my $spec = $AXIS{$axis}; | ||
| 158 | return -$fraction * max_offset( $state->{content}{ $spec->{size} }, $state->{viewport}{ $spec->{size} } ); | ||
| 159 | } | ||
| 160 | |||
| 161 | # A left click, or a drag with the left button, scrolls the container | ||
| 162 | # so that the thumb is centered under the pointer. | ||
| 163 | method _on_mouse ($event) { | ||
| 164 | return $CONTINUE unless $event->key == TB_KEY_MOUSE_LEFT; | ||
| 165 | my $ui = $self->ui; | ||
| 166 | my ( $column, $row ) = $self->cell_at($event); | ||
| 167 | return $HANDLED unless defined $ui && defined $follows && defined $row; | ||
| 168 | my $position = $self->position_at( $axis eq 'vertical' ? $row : $column ); | ||
| 169 | $ui->scroll_to( $follows, { $AXIS{$axis}{position} => $position } ) if defined $position; | ||
| 170 | return $HANDLED; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; | ||
| 175 | |||
| 176 | __END__ |