Alt-Tickit-Widgets-ObjectPad

 view release on metacpan or  search on metacpan

lib/Tickit/Widget/Spinner.pm  view on Meta::CPAN

has $_state = 0;
has $_interval;
has $_cols;

method BUILD
{
   my %params = @_;

   @_chars = $params{chars} ? $params{chars}->@* : (qw( - \ | / ));

   $_interval = $params{interval} // 0.5;

   $_cols = max map { textwidth $_ } @_chars;
}

has $_running;
has $_x;
has $_y;
has $_rect;

=head1 METHODS

=cut

method lines
{
   return 1;
}

method cols
{
   return $_cols;
}

=head2 $spinner->start

Starts the animation effect.

=cut

method start
{
   return if $_running;
   $_running = 1;
   $self->tick;
}

=head2 $spinner->stop

Stops the animation effect.

=cut

method stop
{
   $_running = 0;
}

method window_gained
{
   $self->SUPER::window_gained( @_ );
   $self->start;
}

# precache position
method reshape
{
   my $win = $self->window or return;

   ( $_x, $_y ) = map int($_ / 2), $win->cols - $self->cols, $win->lines - $self->lines;

   $_rect = Tickit::Rect->new(
      top   => $_y,
      left  => $_x,
      lines => $self->lines,
      cols  => $self->cols,
   );
}

method tick
{
   return unless $_running;

   my $state = $_state++;
   $_state %= @_chars;

   if( my $win = $self->window ) {
      $win->tickit->timer( after => $_interval => sub { $self->tick } );
      $win->expose( $_rect );
   }
}

method render_to_rb
{
   my ( $rb, $rect ) = @_;

   $rb->eraserect( $rect );

   $rb->text_at( $_y, $_x, $_chars[$_state] );
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;



( run in 0.534 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )