| 1 | package Tickit::Widget::Progressbar::Horizontal; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use parent qw(Tickit::Widget::Progressbar); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | our $VERSION = '0.102'; # VERSION | ||
| 8 | our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Tickit::Widget::Progressbar::Horizontal - simple progressbar implementation for Tickit | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | my $bar = Tickit::Widget::Progressbar::Horizontal->new( | ||
| 17 | completion => 0.00, | ||
| 18 | ); | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | See L<Tickit::Widget::Progressbar>. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | use POSIX qw(floor); | ||
| 27 | use List::Util qw(min); | ||
| 28 | |||
| 29 | # Undocumented feature for gradient support. Needs Tickit::Colour, | ||
| 30 | # since that's not on CPAN then there's little point in enabling this. | ||
| 31 | use constant ENABLE_GRADIENT => 0; | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | sub render_to_rb { | ||
| 38 | my $self = shift; | ||
| 39 | return $self->render_gradient(@_) if ENABLE_GRADIENT && scalar $self->get_style_values('gradient'); | ||
| 40 | return $self->render_normal(@_); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub render_gradient { | ||
| 44 | my ($self, $rb, $rect) = @_; | ||
| 45 | my $win = $self->window or return; | ||
| 46 | |||
| 47 | $rb->clear; | ||
| 48 | |||
| 49 | my $total_width = $win->cols; | ||
| 50 | my $chars = $self->chars; | ||
| 51 | my $complete = $self->completion * $total_width; | ||
| 52 | |||
| 53 | my $start = $self->get_style_pen('start'); | ||
| 54 | my $src = $self->get_style_pen; | ||
| 55 | my $dst = Tickit::Pen->new( | ||
| 56 | fg => $src->getattr('bg'), | ||
| 57 | bg => $src->getattr('fg') | ||
| 58 | )->default_from($src); | ||
| 59 | my $fg = $src; | ||
| 60 | my $bg = $dst; | ||
| 61 | foreach my $line (0..$win->lines - 1) { | ||
| 62 | $rb->goto($line, 0); | ||
| 63 | my $w = floor($complete); | ||
| 64 | if($self->direction) { | ||
| 65 | $rb->erase($w, $fg); | ||
| 66 | } else { | ||
| 67 | for(0..$w-1) { | ||
| 68 | $fg = $self->pen_for_position( | ||
| 69 | pos => $_, | ||
| 70 | total => $total_width, | ||
| 71 | from => 'fg', | ||
| 72 | to => 'bg', | ||
| 73 | start => $src, | ||
| 74 | end => $start, | ||
| 75 | ); | ||
| 76 | $rb->erase_at($line, $_, 1, $fg);# for 0..$w-1; | ||
| 77 | } | ||
| 78 | # $rb->char_at($line, $_, $chars->[-1], $fg) for 0..$w-1; | ||
| 79 | } | ||
| 80 | if(my $partial = ($complete - $w) * @$chars) { | ||
| 81 | # ++$w; | ||
| 82 | $fg = $self->pen_for_position( | ||
| 83 | pos => $w, | ||
| 84 | total => $total_width, | ||
| 85 | from => 'fg', | ||
| 86 | end => $start, | ||
| 87 | start => $src | ||
| 88 | ); | ||
| 89 | $rb->char_at($line, $w, $chars->[$partial], $self->direction ? $bg : $fg); | ||
| 90 | } | ||
| 91 | unless($w >= $total_width) { | ||
| 92 | if($self->direction) { | ||
| 93 | $rb->char_at($line, $w + $_, $chars->[-1], $fg) for 1..($total_width - $w - 1); | ||
| 94 | } else { | ||
| 95 | $rb->erase_at($line, $w+1, $total_width - $w, $src); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | sub render_normal { | ||
| 102 | my ($self, $rb, $rect) = @_; | ||
| 103 | my $win = $self->window or return; | ||
| 104 | |||
| 105 | $rb->clear; | ||
| 106 | |||
| 107 | my $total_width = $win->cols; | ||
| 108 | my $chars = $self->chars; | ||
| 109 | my $complete = $self->completion * $total_width; | ||
| 110 | |||
| 111 | # Generate a reversed version of the real pen, | ||
| 112 | # since reverse video does not give accurate colours | ||
| 113 | # for some terminals (gnome-terminal for example). | ||
| 114 | my $fg = $self->get_style_pen; | ||
| 115 | my $bg = Tickit::Pen->new( | ||
| 116 | fg => $fg->getattr('bg'), | ||
| 117 | bg => $fg->getattr('fg') | ||
| 118 | )->default_from($fg); | ||
| 119 | |||
| 120 | foreach my $line (0..$win->lines - 1) { | ||
| 121 | $rb->goto($line, 0); | ||
| 122 | my $w = floor($complete); | ||
| 123 | if($self->direction) { | ||
| 124 | $rb->erase($w, $fg); | ||
| 125 | } else { | ||
| 126 | $rb->erase_at($line, 0, $w, $bg);# for 0..$w-1; | ||
| 127 | # $rb->char_at($line, $_, $chars->[-1], $fg) for 0..$w-1; | ||
| 128 | } | ||
| 129 | if(my $partial = ($complete - $w) * @$chars) { | ||
| 130 | # ++$w; | ||
| 131 | $rb->char_at($line, $w, $chars->[$partial], $self->direction ? $bg : $fg); | ||
| 132 | } | ||
| 133 | unless($w >= $total_width) { | ||
| 134 | if($self->direction) { | ||
| 135 | $rb->char_at($line, $w + $_, $chars->[-1], $fg) for 1..($total_width - $w - 1); | ||
| 136 | } else { | ||
| 137 | $rb->erase_at($line, $w+1, $total_width - $w, $fg); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | =head2 chars | ||
| 144 | |||
| 145 | Returns a list of chars for the various styles we support. | ||
| 146 | |||
| 147 | Currently only handles 'ascii' and 'boxchar'. | ||
| 148 | |||
| 149 | TODO - this should probably be aligned with the naming | ||
| 150 | scheme used in other widgets? | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub chars { | ||
| 155 | my $self = shift; | ||
| 156 | return { | ||
| 157 | ascii => [qw(| X)], | ||
| 158 | boxchar => [ | ||
| 159 | 0x258F, | ||
| 160 | 0x258E, | ||
| 161 | 0x258D, | ||
| 162 | 0x258C, | ||
| 163 | 0x258B, | ||
| 164 | 0x258A, | ||
| 165 | 0x2589, | ||
| 166 | 0x2588, | ||
| 167 | ], | ||
| 168 | }->{$self->style}; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub position_for { | ||
| 172 | my $self = shift; | ||
| 173 | # return $self->window->cols - floor(shift() * $self->window->cols); | ||
| 174 | return floor(shift() * $self->window->cols); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub expose_between_values { | ||
| 178 | my $self = shift; | ||
| 179 | return $self unless $self->window; | ||
| 180 | |||
| 181 | my ($prev, $next) = map $self->position_for($_), @_; | ||
| 182 | $self->window->expose( | ||
| 183 | Tickit::Rect->new( | ||
| 184 | left => min($prev, $next) - 1, | ||
| 185 | top => 0, | ||
| 186 | lines => $self->window->lines, | ||
| 187 | cols => abs($next - $prev) + 2, | ||
| 188 | ) | ||
| 189 | ); | ||
| 190 | } | ||
| 191 | |||
| 192 | 1; | ||
| 193 | |||
| 194 | __END__ | ||