« blib/lib/Term/Fabulous/Chart/Scale/Category.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/Table/Value.pm »
| 1 | package Term::Fabulous::Scrolling; | ||
| 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 Exporter 'import'; | ||
| 11 | our @EXPORT_OK = qw( | ||
| 12 | inner_rows page_rows reveal_rows scroll_by_rows scroll_by_columns scroll_by_pages scroll_by_key | ||
| 13 | scroll_to_end scroll_to_start scroll_offset max_scroll_offset scrolled_percent | ||
| 14 | ); | ||
| 15 | |||
| 16 | use List::Util qw(max min); | ||
| 17 | use Scalar::Util qw(blessed); | ||
| 18 | use Term::Fabulous::Check qw(describe one_of); | ||
| 19 | use Term::Fabulous::Viewport qw(clamp_offset max_offset reveal_range); | ||
| 20 | |||
| 21 | sub _checked ( $function, $ui, $widget ) { | ||
| 22 | die "Term::Fabulous::Scrolling: $function needs a UI, got " . describe($ui) unless blessed $ui && $ui->can('scroll_state'); | ||
| 23 | die "Term::Fabulous::Scrolling: $function needs a widget, got " . describe($widget) unless blessed $widget; | ||
| 24 | return; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub _checked_count ( $function, $name, $value ) { | ||
| 28 | die "Term::Fabulous::Scrolling: $function needs a number of $name, got " . describe($value) unless defined $value && !ref $value && $value =~ /\A-?[0-9]+\z/; | ||
| 29 | return $value; | ||
| 30 | } | ||
| 31 | |||
| 32 | # Clay reports the viewport as the whole box of the container; the | ||
| 33 | # content shows inside the padding the frame declared, which holds the | ||
| 34 | # border and the scrollbars too (see Term::Fabulous::Role::HasBorderStyle). | ||
| 35 | sub inner_rows ( $ui, $widget ) { | ||
| 36 | _checked( inner_rows => $ui, $widget ); | ||
| 37 | my $content = $ui->content_box($widget) // return (); | ||
| 38 | return ( $content->{y}, $content->{y} + $content->{height} ); | ||
| 39 | } | ||
| 40 | |||
| 41 | # A page is the rows the content shows less one, so that the last row | ||
| 42 | # stays in view as a landmark; at least one row. | ||
| 43 | sub page_rows ( $ui, $widget ) { | ||
| 44 | my ( $top, $bottom ) = inner_rows( $ui, $widget ) or return undef; | ||
| 45 | return max( 1, int( $bottom - $top + 0.5 ) - 1 ); | ||
| 46 | } | ||
| 47 | |||
| 48 | # Clay keeps the scroll position as a negative offset from the start of | ||
| 49 | # the content, x for columns, y for rows; this module turns it into a | ||
| 50 | # positive offset, so a program never has to. | ||
| 51 | my %AXIS = ( rows => [ y => 'height' ], columns => [ x => 'width' ] ); | ||
| 52 | |||
| 53 | sub _offset ( $state, $axis = 'rows' ) { | ||
| 54 | my ($position) = @{ $AXIS{$axis} }; | ||
| 55 | return -( $state->{position}{$position} // 0 ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _max_offset ( $state, $axis = 'rows' ) { | ||
| 59 | my ( undef, $size ) = @{ $AXIS{$axis} }; | ||
| 60 | return max_offset( $state->{content}{$size}, $state->{viewport}{$size} ); | ||
| 61 | } | ||
| 62 | |||
| 63 | # Scrolls to an offset, within the ends. Clay keeps the position in | ||
| 64 | # fractions of a row; a move of less than half a row is none. | ||
| 65 | sub _scroll_to ( $ui, $widget, $state, $offset, $axis = 'rows' ) { | ||
| 66 | my ( $position, $size ) = @{ $AXIS{$axis} }; | ||
| 67 | my $from = _offset( $state, $axis ); | ||
| 68 | my $to = clamp_offset( $state->{content}{$size}, $state->{viewport}{$size}, $offset ); | ||
| 69 | return 0 if int( $to + 0.5 ) == int( $from + 0.5 ); | ||
| 70 | $ui->scroll_to( $widget, { $position => -$to } ); | ||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub scroll_offset ( $ui, $widget ) { | ||
| 75 | _checked( scroll_offset => $ui, $widget ); | ||
| 76 | my $state = $ui->scroll_state($widget) // return undef; | ||
| 77 | return _offset($state); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub max_scroll_offset ( $ui, $widget ) { | ||
| 81 | _checked( max_scroll_offset => $ui, $widget ); | ||
| 82 | my $state = $ui->scroll_state($widget) // return undef; | ||
| 83 | return _max_offset($state); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub scroll_by_rows ( $ui, $widget, $rows ) { | ||
| 87 | _checked( scroll_by_rows => $ui, $widget ); | ||
| 88 | _checked_count( scroll_by_rows => rows => $rows ); | ||
| 89 | my $state = $ui->scroll_state($widget) // return 0; | ||
| 90 | return _scroll_to( $ui, $widget, $state, _offset($state) + $rows ); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub scroll_by_columns ( $ui, $widget, $columns ) { | ||
| 94 | _checked( scroll_by_columns => $ui, $widget ); | ||
| 95 | _checked_count( scroll_by_columns => columns => $columns ); | ||
| 96 | my $state = $ui->scroll_state($widget) // return 0; | ||
| 97 | return _scroll_to( $ui, $widget, $state, _offset( $state, 'columns' ) + $columns, 'columns' ); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub scroll_by_pages ( $ui, $widget, $pages ) { | ||
| 101 | _checked( scroll_by_pages => $ui, $widget ); | ||
| 102 | _checked_count( scroll_by_pages => pages => $pages ); | ||
| 103 | my $page = page_rows( $ui, $widget ) // return 0; | ||
| 104 | return scroll_by_rows( $ui, $widget, $pages * $page ); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub scroll_to_start ( $ui, $widget ) { | ||
| 108 | _checked( scroll_to_start => $ui, $widget ); | ||
| 109 | my $state = $ui->scroll_state($widget) // return 0; | ||
| 110 | return _scroll_list_to( $ui, $widget, $state, 'start' ) if _is_list($widget); | ||
| 111 | return _scroll_to( $ui, $widget, $state, 0 ); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub scroll_to_end ( $ui, $widget ) { | ||
| 115 | _checked( scroll_to_end => $ui, $widget ); | ||
| 116 | my $state = $ui->scroll_state($widget) // return 0; | ||
| 117 | return _scroll_list_to( $ui, $widget, $state, 'end' ) if _is_list($widget); | ||
| 118 | return _scroll_to( $ui, $widget, $state, _max_offset($state) ); | ||
| 119 | } | ||
| 120 | |||
| 121 | # A VirtualList builds only the items near its view and knows the height | ||
| 122 | # of the others as an estimate, so its content height is one too: it is | ||
| 123 | # sent to its first or last item, which it measures on the way. Once the | ||
| 124 | # last item is in view, the end of the content is where the frame put | ||
| 125 | # it, and the list scrolls there like any container: the start of an | ||
| 126 | # item taller than the view is not its end. | ||
| 127 | sub _is_list ($widget) { | ||
| 128 | return $widget->isa('Term::Fabulous::Widget::VirtualList') ? 1 : 0; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _scroll_list_to ( $ui, $list, $state, $end ) { | ||
| 132 | my $count = $list->count or return 0; | ||
| 133 | my ( $index, $offset ) = $end eq 'start' ? ( 0, 0 ) : ( $count - 1, _max_offset($state) ); | ||
| 134 | my $visible = $list->visible_items; | ||
| 135 | my $in_view = defined $visible && ( $end eq 'start' ? $visible->{first} : $visible->{last} ) == $index; | ||
| 136 | return _scroll_to( $ui, $list, $state, $offset ) if $in_view; | ||
| 137 | $list->scroll_to_item($index); | ||
| 138 | return 1; | ||
| 139 | } | ||
| 140 | |||
| 141 | # The keys of a pager, by set: what each one does. | ||
| 142 | my %KEY_SETS = ( | ||
| 143 | pager => { | ||
| 144 | PageUp => [ pages => -1 ], | ||
| 145 | PageDown => [ pages => 1 ], | ||
| 146 | 'Ctrl+Home' => ['start'], | ||
| 147 | 'Ctrl+End' => ['end'], | ||
| 148 | }, | ||
| 149 | less => { | ||
| 150 | Down => [ rows => 1 ], | ||
| 151 | j => [ rows => 1 ], | ||
| 152 | Up => [ rows => -1 ], | ||
| 153 | k => [ rows => -1 ], | ||
| 154 | PageDown => [ pages => 1 ], | ||
| 155 | Space => [ pages => 1 ], | ||
| 156 | PageUp => [ pages => -1 ], | ||
| 157 | b => [ pages => -1 ], | ||
| 158 | Home => ['start'], | ||
| 159 | g => ['start'], | ||
| 160 | End => ['end'], | ||
| 161 | G => ['end'], | ||
| 162 | }, | ||
| 163 | ); | ||
| 164 | |||
| 165 | my %MOVE = ( | ||
| 166 | rows => \&scroll_by_rows, | ||
| 167 | pages => \&scroll_by_pages, | ||
| 168 | start => \&scroll_to_start, | ||
| 169 | end => \&scroll_to_end, | ||
| 170 | ); | ||
| 171 | |||
| 172 | # undef for a key the set does not have; else whether it moved. | ||
| 173 | sub scroll_by_key ( $ui, $widget, $key_name, %options ) { | ||
| 174 | _checked( scroll_by_key => $ui, $widget ); | ||
| 175 | my @unknown = grep { $_ ne 'keys' } sort keys %options; | ||
| 176 | die "Term::Fabulous::Scrolling: scroll_by_key does not take @unknown (known: keys)" if @unknown; | ||
| 177 | my $set = one_of( 'Term::Fabulous::Scrolling: scroll_by_key', keys => $options{keys}, sort keys %KEY_SETS ); | ||
| 178 | my $move = defined $key_name ? $KEY_SETS{$set}{$key_name} : undef; | ||
| 179 | return undef unless defined $move; | ||
| 180 | my ( $how, @count ) = @$move; | ||
| 181 | return $MOVE{$how}->( $ui, $widget, @count ) ? 1 : 0; | ||
| 182 | } | ||
| 183 | |||
| 184 | # Screen rows from $from to before $to into view: with the smallest move | ||
| 185 | # (align nearest), or with the first of them at the top (align top). | ||
| 186 | # Viewport's functions want one coordinate system: the content's rows, | ||
| 187 | # counted from the first row inside the padding, which shows the inner | ||
| 188 | # rows from the offset on. | ||
| 189 | sub reveal_rows ( $ui, $widget, $from, $to, %options ) { | ||
| 190 | _checked( reveal_rows => $ui, $widget ); | ||
| 191 | my @unknown = grep { $_ ne 'align' } sort keys %options; | ||
| 192 | die "Term::Fabulous::Scrolling: reveal_rows does not take @unknown (known: align)" if @unknown; | ||
| 193 | my $align = one_of( 'Term::Fabulous::Scrolling: reveal_rows', align => $options{align} // 'nearest', qw(nearest top) ); | ||
| 194 | my $state = $ui->scroll_state($widget) // return 0; | ||
| 195 | my ( $top, $bottom ) = inner_rows( $ui, $widget ) or return 0; | ||
| 196 | my $offset = _offset($state); | ||
| 197 | my $inner = $bottom - $top; | ||
| 198 | my $content = $state->{content}{height} - ( $state->{viewport}{height} - $inner ); | ||
| 199 | my ( $first, $end ) = ( $from - $top + $offset, $to - $top + $offset ); | ||
| 200 | my $target = $align eq 'top' ? clamp_offset( $content, $inner, $first ) : reveal_range( $content, $inner, $offset, $first, $end ); | ||
| 201 | return _scroll_to( $ui, $widget, $state, $target ); | ||
| 202 | } | ||
| 203 | |||
| 204 | # How far down the content is scrolled, from 0 to 100; content that fits | ||
| 205 | # is seen whole, 100. undef before the container was laid out. | ||
| 206 | sub scrolled_percent ( $ui, $widget ) { | ||
| 207 | _checked( scrolled_percent => $ui, $widget ); | ||
| 208 | my $state = $ui->scroll_state($widget) // return undef; | ||
| 209 | my $last = _max_offset($state); | ||
| 210 | return 100 unless $last > 0; | ||
| 211 | return min( 100, max( 0, int( 100 * _offset($state) / $last + 0.5 ) ) ); | ||
| 212 | } | ||
| 213 | |||
| 214 | 1; | ||
| 215 | |||
| 216 | __END__ |