| 1 | package Tickit::Widget::Progressbar::Vertical; | ||
| 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::Vertical - simple progressbar implementation for Tickit | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | my $bar = Tickit::Widget::Progressbar::Vertical->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 | my $total_height = $win->lines; | ||
| 49 | my $cols = $win->cols; | ||
| 50 | my $chars = $self->chars; | ||
| 51 | my $row = 0; | ||
| 52 | |||
| 53 | my $complete = $self->completion * $total_height; | ||
| 54 | my $h = floor($complete); | ||
| 55 | |||
| 56 | my $start = $self->get_style_pen('start'); | ||
| 57 | my $src = $self->get_style_pen; | ||
| 58 | my $dst = Tickit::Pen->new( | ||
| 59 | fg => $src->getattr('bg'), | ||
| 60 | bg => $src->getattr('fg') | ||
| 61 | )->default_from($src); | ||
| 62 | my $fg = $src; | ||
| 63 | my $bg = $dst; | ||
| 64 | while($row < ($total_height - $h)) { | ||
| 65 | $rb->goto($row++, 0); | ||
| 66 | if($self->direction) { | ||
| 67 | # $bg = $self->pen_for_position($row, $total_height, bg => $src, $dst); | ||
| 68 | $bg = $self->pen_for_position( | ||
| 69 | pos => $total_height - $row, | ||
| 70 | total => $total_height, | ||
| 71 | from => 'fg', | ||
| 72 | to => 'bg', | ||
| 73 | start => $start, | ||
| 74 | end => $src | ||
| 75 | ); | ||
| 76 | $rb->erase($cols, $bg); | ||
| 77 | } else { | ||
| 78 | # $fg = $self->pen_for_position($row, $total_height, fg => $src, $dst); | ||
| 79 | $rb->erase($cols, $fg); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | if(my $partial = ($complete - $h) * @$chars) { | ||
| 84 | if($self->direction) { | ||
| 85 | $fg = $self->pen_for_position( | ||
| 86 | pos => $total_height - $row, | ||
| 87 | total => $total_height, | ||
| 88 | from => 'fg', | ||
| 89 | to => 'bg', | ||
| 90 | start => $start, | ||
| 91 | end => $src, | ||
| 92 | extra_fg => $dst->getattr('fg'), | ||
| 93 | ); | ||
| 94 | } else { | ||
| 95 | $fg = $self->pen_for_position( | ||
| 96 | pos => $row, | ||
| 97 | total => $total_height, | ||
| 98 | from => 'fg', | ||
| 99 | start => $start, | ||
| 100 | end => $src | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | # $bg = $self->pen_for_position($row, $total_height, bg => $src, $dst); | ||
| 104 | $rb->char_at($row++, $_, $chars->[$partial], $fg) for 0..$cols - 1; | ||
| 105 | } | ||
| 106 | |||
| 107 | while($row <= $total_height) { | ||
| 108 | $rb->goto($row, 0); | ||
| 109 | if($self->direction) { | ||
| 110 | # $fg = $self->pen_for_position($row, $total_height, fg => $src, $dst); | ||
| 111 | $rb->erase($cols, $src); | ||
| 112 | } else { | ||
| 113 | $fg = $self->pen_for_position( | ||
| 114 | pos => $row, | ||
| 115 | total => $total_height, | ||
| 116 | from => 'fg', | ||
| 117 | to => 'bg', | ||
| 118 | start => $start, | ||
| 119 | end => $src | ||
| 120 | ); | ||
| 121 | # $bg = $self->pen_for_position($row, $total_height, fg => $src, $dst); | ||
| 122 | # $fg = $self->pen_for_position($row, $total_height, bg => $src, $start); | ||
| 123 | $rb->erase($cols, $fg); | ||
| 124 | } | ||
| 125 | ++$row; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | sub render_normal { | ||
| 130 | my ($self, $rb, $rect) = @_; | ||
| 131 | my $win = $self->window or return; | ||
| 132 | |||
| 133 | $rb->clear; | ||
| 134 | my $total_height = $win->lines; | ||
| 135 | my $cols = $win->cols; | ||
| 136 | my $chars = $self->chars; | ||
| 137 | my $row = 0; | ||
| 138 | |||
| 139 | my $complete = $self->completion * $total_height; | ||
| 140 | my $h = floor($complete); | ||
| 141 | |||
| 142 | my $fg = $self->get_style_pen; | ||
| 143 | my $bg = Tickit::Pen->new( | ||
| 144 | fg => $fg->getattr('bg'), | ||
| 145 | bg => $fg->getattr('fg') | ||
| 146 | )->default_from($fg); | ||
| 147 | while($row < ($total_height - $h)) { | ||
| 148 | $rb->goto($row++, 0); | ||
| 149 | if($self->direction) { | ||
| 150 | $rb->text(' ' x $cols, $bg); | ||
| 151 | } else { | ||
| 152 | $rb->erase($cols, $fg); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | if(my $partial = ($complete - $h) * @$chars) { | ||
| 157 | $rb->char_at($row++, $_, $chars->[$partial], $self->direction ? $bg : $fg) for 0..$cols - 1; | ||
| 158 | } | ||
| 159 | |||
| 160 | while($row <= $total_height) { | ||
| 161 | $rb->goto($row++, 0); | ||
| 162 | if($self->direction) { | ||
| 163 | $rb->erase($cols, $fg); | ||
| 164 | } else { | ||
| 165 | $rb->text(' ' x $cols, $bg); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | =head2 chars | ||
| 171 | |||
| 172 | Returns a list of chars for the various styles we support. | ||
| 173 | |||
| 174 | Currently only handles 'ascii' and 'boxchar'. | ||
| 175 | |||
| 176 | TODO - this should probably be aligned with the naming | ||
| 177 | scheme used in other widgets? | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub chars { | ||
| 182 | my $self = shift; | ||
| 183 | return { | ||
| 184 | ascii => [map ord, qw(_ x X)], | ||
| 185 | boxchar => [ | ||
| 186 | 0x2581, | ||
| 187 | 0x2582, | ||
| 188 | 0x2583, | ||
| 189 | 0x2584, | ||
| 190 | 0x2585, | ||
| 191 | 0x2586, | ||
| 192 | 0x2587, | ||
| 193 | 0x2588 | ||
| 194 | ], | ||
| 195 | }->{$self->style}; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub position_for { | ||
| 199 | my $self = shift; | ||
| 200 | return $self->window->lines - floor(shift() * $self->window->lines); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub expose_between_values { | ||
| 204 | my $self = shift; | ||
| 205 | return $self unless $self->window; | ||
| 206 | |||
| 207 | my ($prev, $next) = map $self->position_for($_), @_; | ||
| 208 | $self->window->expose( | ||
| 209 | Tickit::Rect->new( | ||
| 210 | top => min($prev, $next) - 1, | ||
| 211 | left => 0, | ||
| 212 | cols => $self->window->cols, | ||
| 213 | lines => abs($next - $prev) + 1, | ||
| 214 | ) | ||
| 215 | ); | ||
| 216 | } | ||
| 217 | |||
| 218 | 1; | ||
| 219 | |||
| 220 | __END__ | ||