« blib/lib/Term/Fabulous/Widget/FileDialog.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/KeyReference.pm »
| 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.05'; | ||
| 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 Clay::UI::Enum::Result; | ||
| 26 | use Clay::UI::Role::Layout::HasParent qw(is_widget); | ||
| 27 | use Scalar::Util qw(blessed refaddr weaken); | ||
| 28 | use Term::Fabulous::Check qw(boolean describe one_of positive_integer); | ||
| 29 | use Term::Fabulous::Scrolling qw(max_scroll_offset reveal_rows scroll_offset scroll_to_end); | ||
| 30 | use Term::Fabulous::Widget::Scrollbar; | ||
| 31 | |||
| 32 | # A widget that is not laid out yet is revealed after the frame that | ||
| 33 | # lays it out, or the one after that at the latest. | ||
| 34 | use constant REVEAL_FRAMES => 2; | ||
| 35 | |||
| 36 | # The gutter floats over the whole box and is padded by its border: a | ||
| 37 | # column with the horizontal scrollbar at its bottom, then the | ||
| 38 | # vertical scrollbar, which runs the full height. It lets the pointer | ||
| 39 | # through, so the box below still scrolls with the wheel and its | ||
| 40 | # content is still hovered. | ||
| 41 | my %GUTTER_LAYOUT = ( layout_direction => CLAY_LEFT_TO_RIGHT, sizing => { width => sizing_grow(), height => sizing_grow() } ); | ||
| 42 | my %GUTTER_FLOATING = ( | ||
| 43 | attach_to => CLAY_ATTACH_TO_PARENT, | ||
| 44 | attach_points => { element => CLAY_ATTACH_POINT_LEFT_TOP, parent => CLAY_ATTACH_POINT_LEFT_TOP }, | ||
| 45 | clip_to => CLAY_CLIP_TO_ATTACHED_PARENT, | ||
| 46 | pointer_capture_mode => CLAY_POINTER_CAPTURE_MODE_PASSTHROUGH, | ||
| 47 | ); | ||
| 48 | my %BAR_LAYOUT = ( | ||
| 49 | vertical => { sizing => { width => sizing_fixed(1), height => sizing_grow() } }, | ||
| 50 | horizontal => { sizing => { width => sizing_grow(), height => sizing_fixed(1) } }, | ||
| 51 | ); | ||
| 52 | my %SIDE_OF_AXIS = ( vertical => 'right', horizontal => 'bottom' ); | ||
| 53 | |||
| 54 | field $scrollbar :param = 1; | ||
| 55 | field $follow_end :param = 0; | ||
| 56 | field $_gutter; | ||
| 57 | field %_bar_by_axis; | ||
| 58 | |||
| 59 | # While following the end: whether the box follows (the user has not | ||
| 60 | # scrolled up from the end), and how far the last frame was scrolled. | ||
| 61 | field $_following = 1; | ||
| 62 | field $_last_offset = 0; | ||
| 63 | |||
| 64 | # Clay::UI stores these as given; the constructor takes any truth value. | ||
| 65 | sub BUILDARGS ( $class, %params ) { | ||
| 66 | $params{$_} = $params{$_} ? 1 : 0 foreach grep { exists $params{$_} } qw(horizontal vertical); | ||
| 67 | return $class->SUPER::BUILDARGS(%params); | ||
| 68 | } | ||
| 69 | |||
| 70 | # The colors go to the scrollbars once they exist; undef leaves them | ||
| 71 | # to the theme. | ||
| 72 | ADJUST :params ( :$track_color = undef, :$thumb_color = undef ) { | ||
| 73 | $scrollbar = boolean( $self, scrollbar => $scrollbar ); | ||
| 74 | $follow_end = boolean( $self, follow_end => $follow_end ); | ||
| 75 | $self->_follow_frames if $follow_end; | ||
| 76 | %_bar_by_axis = map { $_ => Term::Fabulous::Widget::Scrollbar->new( follows => $self, axis => $_, layout => $BAR_LAYOUT{$_} ) } keys %BAR_LAYOUT; | ||
| 77 | $self->set_look( track_color => $track_color ) if defined $track_color; | ||
| 78 | $self->set_look( thumb_color => $thumb_color ) if defined $thumb_color; | ||
| 79 | |||
| 80 | my $column = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow(), height => sizing_grow() }, child_alignment => { y => CLAY_ALIGN_Y_BOTTOM } } ); | ||
| 81 | $column->add_child( $_bar_by_axis{horizontal} ); | ||
| 82 | $_gutter = Term::Fabulous::Widget::Box->new( floating => {%GUTTER_FLOATING} ); | ||
| 83 | $_gutter->add_child( $column, $_bar_by_axis{vertical} ); | ||
| 84 | $self->_pad_gutter; | ||
| 85 | $self->add_internal_children($_gutter) if $scrollbar; | ||
| 86 | } | ||
| 87 | |||
| 88 | # A descendant that gets the focus is scrolled into view. | ||
| 89 | ADJUST { | ||
| 90 | $self->on( OnFocus => sub ( $self, $event ) { $self->_focus_entered( $event->target ); return Clay::UI::Enum::Result->CONTINUE }, owner => $self ); | ||
| 91 | } | ||
| 92 | |||
| 93 | method layout_properties :common () { | ||
| 94 | return ( $class->SUPER::layout_properties, horizontal => 'boolean', vertical => 'boolean', scrollbar => 'boolean', follow_end => 'boolean' ); | ||
| 95 | } | ||
| 96 | |||
| 97 | # Whether a scrollbar is shown for an axis: wanted, and the box | ||
| 98 | # scrolls that way. | ||
| 99 | method _shows_bar ($axis) { | ||
| 100 | return $scrollbar && ( $axis eq 'vertical' ? $self->vertical : $self->horizontal ) ? 1 : 0; | ||
| 101 | } | ||
| 102 | |||
| 103 | # The content keeps clear of the scrollbars: one column at the right | ||
| 104 | # for the vertical one, one row at the bottom for the horizontal one. | ||
| 105 | method _bar_sides () { | ||
| 106 | return map { $SIDE_OF_AXIS{$_} } grep { $self->_shows_bar($_) } sort keys %SIDE_OF_AXIS; | ||
| 107 | } | ||
| 108 | |||
| 109 | method content_padding :override () { | ||
| 110 | my $padding = $self->SUPER::content_padding; | ||
| 111 | $padding->{$_}++ foreach $self->_bar_sides; | ||
| 112 | return $padding; | ||
| 113 | } | ||
| 114 | |||
| 115 | method contribute_layout :override ($config) { | ||
| 116 | $self->SUPER::contribute_layout($config); | ||
| 117 | my @sides = $self->_bar_sides; | ||
| 118 | return unless @sides; | ||
| 119 | my %padding = %{ $config->{layout}{padding} // {} }; | ||
| 120 | $padding{$_} = ( $padding{$_} // 0 ) + 1 foreach @sides; | ||
| 121 | $config->{layout} = { %{ $config->{layout} // {} }, padding => \%padding }; | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | # The gutter is padded by the border, so the scrollbars sit inside it. | ||
| 126 | method _pad_gutter () { | ||
| 127 | $_gutter->layout( { %GUTTER_LAYOUT, padding => $self->drawn_border_sides } ); | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | |||
| 131 | # The border may change with bordered or with the theme | ||
| 132 | # (Term::Fabulous::Role::Themed). | ||
| 133 | method looks_changed (@names) { | ||
| 134 | $self->_pad_gutter if grep { $_ eq 'bordered' } @names; | ||
| 135 | return; | ||
| 136 | } | ||
| 137 | |||
| 138 | method scrollbar (@new) { | ||
| 139 | return $scrollbar unless @new; | ||
| 140 | $scrollbar = boolean( $self, scrollbar => $new[0] ); | ||
| 141 | my $attached = defined $_gutter->parent ? 1 : 0; | ||
| 142 | $self->add_internal_children($_gutter) if $scrollbar && !$attached; | ||
| 143 | $self->remove_internal_children($_gutter) if !$scrollbar && $attached; | ||
| 144 | return $scrollbar; | ||
| 145 | } | ||
| 146 | |||
| 147 | # The scrollbars keep the colors (Term::Fabulous::Role::Themed): both | ||
| 148 | # take a color given here, reset_look returns both to the theme, and a | ||
| 149 | # read asks the vertical one. | ||
| 150 | method forwarded_looks :common () { | ||
| 151 | return ( _scrollbars => [ 'Term::Fabulous::Widget::Scrollbar', qw(track_color thumb_color) ] ); | ||
| 152 | } | ||
| 153 | |||
| 154 | method _scrollbars () { | ||
| 155 | return @_bar_by_axis{qw(vertical horizontal)}; | ||
| 156 | } | ||
| 157 | |||
| 158 | method track_color (@new) { return @new ? $self->set_look( track_color => $new[0] ) : $self->look_value('track_color') } | ||
| 159 | method thumb_color (@new) { return @new ? $self->set_look( thumb_color => $new[0] ) : $self->look_value('thumb_color') } | ||
| 160 | |||
| 161 | # --------------------------------------------------------------------- | ||
| 162 | # Following the end: new content at the bottom stays in view | ||
| 163 | # --------------------------------------------------------------------- | ||
| 164 | |||
| 165 | # Turning it on follows from now on. | ||
| 166 | method follow_end (@new) { | ||
| 167 | return $follow_end unless @new; | ||
| 168 | $follow_end = boolean( $self, follow_end => $new[0] ); | ||
| 169 | ( $_following, $_last_offset ) = ( 1, 0 ); | ||
| 170 | $self->_follow_frames if $follow_end; | ||
| 171 | return $follow_end; | ||
| 172 | } | ||
| 173 | |||
| 174 | # A box that leaves its UI loses its frame work; it asks again for | ||
| 175 | # its new place. | ||
| 176 | method tree_changed :override () { | ||
| 177 | $self->SUPER::tree_changed; | ||
| 178 | $self->_follow_frames if $follow_end; | ||
| 179 | return; | ||
| 180 | } | ||
| 181 | |||
| 182 | method _follow_frames () { | ||
| 183 | $self->each_frame( sub ($box) { return $box->_follow_frame }, name => 'follow end' ); | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | # After each frame: a box at the end follows; one that follows and was | ||
| 188 | # not scrolled up goes on to the end the content grew to; one that was | ||
| 189 | # scrolled up stops following until it is at the end again. True once | ||
| 190 | # following was switched off, which ends the frame work. | ||
| 191 | method _follow_frame () { | ||
| 192 | return 1 unless $follow_end; | ||
| 193 | my $ui = $self->ui; | ||
| 194 | my $offset = scroll_offset( $ui, $self ) // return 0; | ||
| 195 | if ( $offset >= max_scroll_offset( $ui, $self ) - 0.5 ) { | ||
| 196 | $_following = 1; | ||
| 197 | } | ||
| 198 | elsif ( $_following && $offset >= $_last_offset - 0.5 ) { | ||
| 199 | scroll_to_end( $ui, $self ); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | $_following = 0; | ||
| 203 | } | ||
| 204 | $_last_offset = $offset; | ||
| 205 | return 0; | ||
| 206 | } | ||
| 207 | |||
| 208 | # --------------------------------------------------------------------- | ||
| 209 | # Revealing a widget; a descendant that gets the focus is brought into | ||
| 210 | # view | ||
| 211 | # --------------------------------------------------------------------- | ||
| 212 | |||
| 213 | # Scrolls a widget inside the box into view: at once when the last | ||
| 214 | # frame laid it out, else after each frame until one has, for a few | ||
| 215 | # frames. A new reveal replaces one still waiting. | ||
| 216 | method reveal ( $widget, %options ) { | ||
| 217 | my @unknown = grep { $_ ne 'align' && $_ ne 'frames' } sort keys %options; | ||
| 218 | die ref($self) . ": reveal does not take @unknown (known: align frames)" if @unknown; | ||
| 219 | die ref($self) . ": reveal needs a widget inside the box, got " . describe($widget) unless is_widget($widget) && $self->contains($widget); | ||
| 220 | my $align = one_of( $self, align => $options{align} // 'nearest', qw(nearest top) ); | ||
| 221 | my $frames = positive_integer( $self, frames => $options{frames} // REVEAL_FRAMES ); | ||
| 222 | return $self if $self->_reveal_widget_now( $widget, $align ); | ||
| 223 | |||
| 224 | weaken $widget; | ||
| 225 | $self->each_frame( sub ($box) { return !defined $widget || $box->_reveal_widget_now( $widget, $align ) }, name => 'reveal', frames => $frames ); | ||
| 226 | return $self; | ||
| 227 | } | ||
| 228 | |||
| 229 | # One attempt: true once the widget has a box in the last frame and | ||
| 230 | # was scrolled into view. | ||
| 231 | method _reveal_widget_now ( $widget, $align ) { | ||
| 232 | my $ui = $self->ui // return 0; | ||
| 233 | my $box = $ui->bounding_box($widget) // return 0; | ||
| 234 | reveal_rows( $ui, $self, $box->{y}, $box->{y} + $box->{height}, align => $align ); | ||
| 235 | return 1; | ||
| 236 | } | ||
| 237 | |||
| 238 | method _focus_entered ($target) { | ||
| 239 | return unless blessed $target && refaddr($target) != refaddr($self) && $self->contains($target); | ||
| 240 | $self->reveal($target); | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | 1; | ||
| 246 | |||
| 247 | __END__ |