| 1 | package Tickit::Widget::Progressbar; | ||
| 2 | # ABSTRACT: horizontal/vertical progress bars for Tickit | ||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use Object::Pad; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | class Tickit::Widget::Progressbar :isa(Tickit::Widget); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | our $VERSION = '0.102'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Tickit::Widget::Progressbar - simple progressbar implementation for Tickit | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use Tickit::Widget::Progressbar::Horizontal; | ||
| 17 | my $bar = Tickit::Widget::Progressbar::Horizontal->new( | ||
| 18 | completion => 0.00, | ||
| 19 | ); | ||
| 20 | $bar->completion($_ / 100.0) for 0..100; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | Provides support for a 'progress bar' widget. Use the L<Tickit::Widget::Progressbar::Horizontal> | ||
| 25 | or L<Tickit::Widget::Progressbar::Vertical> subclasses depending on whether you want the progress | ||
| 26 | bar to go from left to right or bottom to top. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use Tickit::Style; | ||
| 31 | |||
| 32 | use constant CLEAR_BEFORE_RENDER => 0; | ||
| 33 | use constant WIDGET_PEN_FROM_STYLE => 0; | ||
| 34 | use constant CAN_FOCUS => 0; | ||
| 35 | |||
| 36 | BEGIN { | ||
| 37 | style_definition base => | ||
| 38 | fg => 255, | ||
| 39 | bg => 'black', | ||
| 40 | gradient => 0, | ||
| 41 | start_fg => 232; | ||
| 42 | } | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | method lines { 1 } | ||
| 49 | method cols { 1 } | ||
| 50 | |||
| 51 | =head2 new | ||
| 52 | |||
| 53 | Instantiate a new L<Tickit::Widget::Progressbar> object. Takes the following named parameters: | ||
| 54 | |||
| 55 | =over 4 | ||
| 56 | |||
| 57 | =item * completion - a value from 0.0 to 1.0 indicating progress | ||
| 58 | |||
| 59 | =item * orientation - 'vertical' or 'horizontal' | ||
| 60 | |||
| 61 | =item * direction - whether progress goes forwards (left to right, bottom to top) or backwards | ||
| 62 | (right to left, top to bottom). | ||
| 63 | |||
| 64 | =back | ||
| 65 | |||
| 66 | Note that this is a base class, and the appropriate L<Tickit::Widget::Progressbar::Horizontal> | ||
| 67 | or L<Tickit::Widget::Progressbar::Vertical> subclass should be used when instantiating a real | ||
| 68 | widget. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | BUILD (%args) { | ||
| 73 | my $completion = delete $args{completion}; | ||
| 74 | my $orientation = delete $args{orientation}; | ||
| 75 | my $direction = delete $args{direction}; | ||
| 76 | $self->{completion} = $completion || 0.0; | ||
| 77 | $self->{orientation} = $orientation || 'horizontal'; | ||
| 78 | $self->{direction} = $direction || 0; | ||
| 79 | return $self; | ||
| 80 | } | ||
| 81 | |||
| 82 | method orientation { 'horizontal' } | ||
| 83 | method style { 'boxchar' } | ||
| 84 | method direction { $self->{direction} } | ||
| 85 | |||
| 86 | =head2 completion | ||
| 87 | |||
| 88 | Accessor for the current progress bar completion state - call this with a float value from 0.00..1.00 | ||
| 89 | to set completion and re-render. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | method completion { | ||
| 94 | if(@_) { | ||
| 95 | my $previous = $self->{completion}; | ||
| 96 | $self->{completion} = shift; | ||
| 97 | if(defined $previous) { | ||
| 98 | # Not entirely sure this part is working reliably enough yet | ||
| 99 | # $self->expose_between_values($previous, $self->{completion}); | ||
| 100 | $self->redraw; | ||
| 101 | } else { | ||
| 102 | $self->redraw; | ||
| 103 | } | ||
| 104 | return $self; | ||
| 105 | } | ||
| 106 | return $self->{completion}; | ||
| 107 | } | ||
| 108 | |||
| 109 | method pen_for_position (%args) { | ||
| 110 | $self->{gradient_pen} ||= {}; | ||
| 111 | $self->{gradient_pen}{join ',', map { $_ => $args{$_} } sort keys %args} ||= do { | ||
| 112 | my @start = Tickit::Colour->colour_to_rgb($args{start}->getattr($args{from})); | ||
| 113 | my @end = Tickit::Colour->colour_to_rgb($args{end}->getattr($args{from})); | ||
| 114 | my $col = Tickit::Colour->rgb_to_colour(map { | ||
| 115 | ($start[$_] * $args{pos} + $end[$_] * ($args{total} - $args{pos})) / $args{total} | ||
| 116 | } 0..2); | ||
| 117 | my %extra = map {; /^extra_(.*)$/ ? ($1 => $args{$_}) : () } keys %args; | ||
| 118 | |||
| 119 | Tickit::Pen::Immutable->new( | ||
| 120 | ($args{to} || $args{from}) => $col, | ||
| 121 | %extra, | ||
| 122 | ); | ||
| 123 | }; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | __END__ | ||