line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Curses::UI::Volume; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Pragmas. |
4
|
2
|
|
|
2
|
|
27018
|
use Curses::UI::Widget; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use base qw(Curses::UI::ContainerWidget); |
6
|
|
|
|
|
|
|
use strict; |
7
|
|
|
|
|
|
|
use warnings; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# Modules. |
10
|
|
|
|
|
|
|
use Curses; |
11
|
|
|
|
|
|
|
use Curses::UI::Common; |
12
|
|
|
|
|
|
|
use Curses::UI::Label; |
13
|
|
|
|
|
|
|
use Encode qw(decode_utf8); |
14
|
|
|
|
|
|
|
use Readonly; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# Constants. |
17
|
|
|
|
|
|
|
Readonly::Scalar our $FULL_BLOCK => decode_utf8('█'); |
18
|
|
|
|
|
|
|
Readonly::Scalar our $LEFT_SEVEN_EIGHTS_BLOCK => decode_utf8('▉'); |
19
|
|
|
|
|
|
|
Readonly::Scalar our $LEFT_THREE_QUARTERS_BLOCK => decode_utf8('▊'); |
20
|
|
|
|
|
|
|
Readonly::Scalar our $LEFT_FIVE_EIGHTS_BLOCK => decode_utf8('▋'); |
21
|
|
|
|
|
|
|
Readonly::Scalar our $LEFT_HALF_BLOCK => decode_utf8('▌'); |
22
|
|
|
|
|
|
|
Readonly::Scalar our $LEFT_THREE_EIGHTS_BLOCK => decode_utf8('▍'); |
23
|
|
|
|
|
|
|
Readonly::Scalar our $LEFT_ONE_QUARTER_BLOCK => decode_utf8('▎'); |
24
|
|
|
|
|
|
|
Readonly::Scalar our $LEFT_ONE_EIGHTH_BLOCK => decode_utf8('▏'); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Version. |
27
|
|
|
|
|
|
|
our $VERSION = 0.02; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# Constructor. |
30
|
|
|
|
|
|
|
sub new { |
31
|
|
|
|
|
|
|
my ($class, %userargs) = @_; |
32
|
|
|
|
|
|
|
keys_to_lowercase(\%userargs); |
33
|
|
|
|
|
|
|
my %args = ( |
34
|
|
|
|
|
|
|
'-bg' => 'black', |
35
|
|
|
|
|
|
|
'-fg' => 'white', |
36
|
|
|
|
|
|
|
'-volume' => 0, |
37
|
|
|
|
|
|
|
%userargs, |
38
|
|
|
|
|
|
|
'-volume_width' => undef, |
39
|
|
|
|
|
|
|
'-focusable' => 0, |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Height and width. |
43
|
|
|
|
|
|
|
$args{'-height'} = height_by_windowscrheight(1, %args); |
44
|
|
|
|
|
|
|
if (! exists $args{'-width'}) { |
45
|
|
|
|
|
|
|
$args{'-width'} = width_by_windowscrwidth(3, %args); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Check volume. |
49
|
|
|
|
|
|
|
$args{'-volume'} = _check_volume($args{'-volume'}); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Create the widget. |
52
|
|
|
|
|
|
|
my $self = $class->SUPER::new(%args); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# Volume effective area. |
55
|
|
|
|
|
|
|
$self->{'-volume_width'} = $self->width; |
56
|
|
|
|
|
|
|
if ($self->{'-border'} || $self->{'-sbborder'}) { |
57
|
|
|
|
|
|
|
$self->{'-volume_width'} -= 2; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Main volume. |
61
|
|
|
|
|
|
|
$self->add( |
62
|
|
|
|
|
|
|
'volume', 'Label', |
63
|
|
|
|
|
|
|
'-bg' => $self->{'-bg'}, |
64
|
|
|
|
|
|
|
'-fg' => $self->{'-fg'}, |
65
|
|
|
|
|
|
|
'-text' => $self->_volume($self->{'-volume'}), |
66
|
|
|
|
|
|
|
'-width' => $args{'-width'}, |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Layout. |
70
|
|
|
|
|
|
|
$self->layout; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# Return object. |
73
|
|
|
|
|
|
|
return $self; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Get or set volume. |
77
|
|
|
|
|
|
|
sub volume { |
78
|
|
|
|
|
|
|
my ($self, $volume) = @_; |
79
|
|
|
|
|
|
|
if (defined $volume) { |
80
|
|
|
|
|
|
|
$volume = _check_volume($volume); |
81
|
|
|
|
|
|
|
$self->{'-volume'} = $volume; |
82
|
|
|
|
|
|
|
$self->getobj('volume')->text($self->_volume($volume)); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
return $self->{'-volume'}; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Check volume. |
88
|
|
|
|
|
|
|
sub _check_volume { |
89
|
|
|
|
|
|
|
my $volume = shift; |
90
|
|
|
|
|
|
|
if (int($volume) != $volume) { |
91
|
|
|
|
|
|
|
$volume = 0; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
if ($volume > 100) { |
94
|
|
|
|
|
|
|
$volume = 100; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
if ($volume < 0) { |
97
|
|
|
|
|
|
|
$volume = 0; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
return $volume; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Set text label. |
103
|
|
|
|
|
|
|
sub _volume { |
104
|
|
|
|
|
|
|
my ($self, $volume) = @_; |
105
|
|
|
|
|
|
|
my $parts = $self->{'-volume_width'} * 8; |
106
|
|
|
|
|
|
|
my $vol_parts = $volume / 100 * $parts; |
107
|
|
|
|
|
|
|
my $vol_blocks = int($vol_parts / 8); |
108
|
|
|
|
|
|
|
my $vol_other = $vol_parts % 8; |
109
|
|
|
|
|
|
|
my $other_char = ''; |
110
|
|
|
|
|
|
|
if ($vol_other == 1) { |
111
|
|
|
|
|
|
|
$other_char = $LEFT_ONE_EIGHTH_BLOCK; |
112
|
|
|
|
|
|
|
} elsif ($vol_other == 2) { |
113
|
|
|
|
|
|
|
$other_char = $LEFT_ONE_QUARTER_BLOCK; |
114
|
|
|
|
|
|
|
} elsif ($vol_other == 3) { |
115
|
|
|
|
|
|
|
$other_char = $LEFT_THREE_EIGHTS_BLOCK; |
116
|
|
|
|
|
|
|
} elsif ($vol_other == 4) { |
117
|
|
|
|
|
|
|
$other_char = $LEFT_HALF_BLOCK; |
118
|
|
|
|
|
|
|
} elsif ($vol_other == 5) { |
119
|
|
|
|
|
|
|
$other_char = $LEFT_FIVE_EIGHTS_BLOCK; |
120
|
|
|
|
|
|
|
} elsif ($vol_other == 6) { |
121
|
|
|
|
|
|
|
$other_char = $LEFT_THREE_QUARTERS_BLOCK; |
122
|
|
|
|
|
|
|
} elsif ($vol_other == 7) { |
123
|
|
|
|
|
|
|
$other_char = $LEFT_SEVEN_EIGHTS_BLOCK; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
return ($FULL_BLOCK x $vol_blocks).$other_char; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
1; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
__END__ |