blib/lib/Term/Fabulous/Widget/StarRating.pm untested

filter: Statementstmt 0.0% 0 / 177 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 15 Podpod n/a totaltotal 0.0% 0 / 328 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::StarRating;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 use Object::Pad 0.825;
9  
10 use Term::Fabulous::Role::HasRange;
11 use Term::Fabulous::Widget::Input;
12  
13 our $VERSION = '0.05';
14  
15 class Term::Fabulous::Widget::StarRating
16 :isa(Term::Fabulous::Widget::Input)
17 :does(Term::Fabulous::Role::HasRange)
18 :strict(params)
19 {
20 use Clay::UI::Enum::Result;
21 use List::Util (); # max is a method here
22 use Scalar::Util qw(refaddr);
23 use Term::Fabulous::Check qw(boolean non_negative_integer optional positive_integer);
24 use Term::Fabulous::Color;
25 use Term::Fabulous::Range;
26 use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT TB_KEY_MOUSE_WHEEL_UP TB_KEY_MOUSE_WHEEL_DOWN TB_MOD_SHIFT);
27 use Term::Fabulous::Unicode qw(string_columns);
28  
29 field $half :param = 0;
30 field $read_only :param = 0;
31 field $show_value :param = 0;
32 field $gap :param = 1;
33 field $full_glyph :param = "\x{2605}";
34 field $empty_glyph :param = "\x{2606}";
35 field $half_glyph :param = undef;
36  
37 field $range; # a Term::Fabulous::Range from 0 to max, in steps of a (half) star
38  
39 # The star the pointer is over, counted from 1, while it hovers.
40 field $_hover_star;
41  
42 method themed_params :common () {
43 return (
44 $class->SUPER::themed_params,
45 accent_color => [ 'star', 'normal', 'cell_color' ],
46 inactive_color => [ 'inactive', 'normal', 'cell_color' ],
47 half_color => [ 'half', 'normal', 'optional_cell_color' ],
48 );
49 }
50  
51 ADJUST :params ( :$max = 5, :$value_format = undef, :$value = 0 ) {
52 $half = boolean( $self, half => $half );
53 $read_only = boolean( $self, read_only => $read_only );
54 $show_value = boolean( $self, show_value => $show_value );
55 $gap = non_negative_integer( $self, gap => $gap );
56 $full_glyph = Term::Fabulous::Check::glyph( $self, full_glyph => $full_glyph );
57 $empty_glyph = Term::Fabulous::Check::glyph( $self, empty_glyph => $empty_glyph );
58 $half_glyph = $self->_checked_half_glyph($half_glyph);
59 $range = Term::Fabulous::Range->new(
60 owner => ref $self,
61 max => positive_integer( $self, max => $max ),
62 step => $half ? 0.5 : 1,
63 paging => 0,
64 value_format => $value_format,
65 default_format => \&_stars_of,
66 value => $value,
67 );
68  
69 my $continue = Clay::UI::Enum::Result->CONTINUE;
70 $self->on( MouseMove => sub ( $self, $event ) { $self->_hover_at( $self->_star_at($event) ); return $continue }, owner => $self );
71 $self->on( OnHoverStopped => sub ( $self, $event ) { $self->_hover_at(undef) if refaddr( $event->target ) == refaddr($self); return $continue }, owner => $self );
72 }
73  
74 method _checked_half_glyph ($value) {
75 return optional( \&Term::Fabulous::Check::glyph, $self, half_glyph => $value );
76 }
77  
78 # ---------------------------------------------------------------------
79 # Value
80 # ---------------------------------------------------------------------
81  
82 method value (@new) {
83 return $range->value unless @new;
84 $range->set_value( $new[0] );
85 $self->mark_changed;
86 return $range->value;
87 }
88  
89 # The user moved the value: fire Change if it moved. Returns 1 when the
90 # value changed.
91 method _moved ($changed) {
92 return 0 unless $changed;
93 $self->mark_changed;
94 $self->fire_change( $range->value );
95 return 1;
96 }
97  
98 method step () {
99 return $range->step;
100 }
101  
102 # The default text of a value: 3/5, or 3.5/5 with half stars.
103 sub _stars_of ( $number, $range ) {
104 return sprintf '%s/%d', ( $range->step < 1 ? sprintf( '%.1f', $number ) : $number ), $range->max;
105 }
106  
107 method format_value ($number) {
108 return $range->format_value($number);
109 }
110  
111 # max and half decide the grid the value snaps to; a layout gives them
112 # in one go (Term::Fabulous::Role::HasRange).
113 method set_range (%parts) {
114 my @unknown = grep { $_ ne 'max' && $_ ne 'half' } sort keys %parts;
115 die ref($self) . ": set_range takes max and half, got @unknown" if @unknown;
116 my $stars = exists $parts{max} ? positive_integer( $self, max => $parts{max} ) : $range->max;
117 $half = boolean( $self, half => $parts{half} ) if exists $parts{half};
118 $range->set_range( max => $stars, step => $half ? 0.5 : 1 );
119 $self->mark_changed;
120 return $self;
121 }
122  
123 method range_properties :common () {
124 return qw(max half);
125 }
126  
127 # ---------------------------------------------------------------------
128 # Accessors
129 # ---------------------------------------------------------------------
130  
131 method _set ( $field_ref, $value ) {
132 $$field_ref = $value;
133 $self->mark_changed;
134 return $$field_ref;
135 }
136  
137 method _set_value_format ($format) {
138 $range->set_value_format($format);
139 $self->mark_changed;
140 return $range->value_format;
141 }
142  
143 method max (@new) {
144 $self->set_range( max => $new[0] ) if @new;
145 return $range->max;
146 }
147  
148 method half (@new) {
149 $self->set_range( half => $new[0] ) if @new;
150 return $half;
151 }
152  
153 method read_only (@new) {
154 return $read_only unless @new;
155 $self->_set( \$read_only, boolean( $self, read_only => $new[0] ) );
156 $self->focus_eligibility_changed; # a read-only rating gives the focus up
157 return $read_only;
158 }
159  
160 method show_value (@new) { return @new ? $self->_set( \$show_value, boolean( $self, show_value => $new[0] ) ) : $show_value }
161 method value_format (@new) { return @new ? $self->_set_value_format( $new[0] ) : $range->value_format }
162 method gap (@new) { return @new ? $self->_set( \$gap, non_negative_integer( $self, gap => $new[0] ) ) : $gap }
163 method full_glyph (@new) { return @new ? $self->_set( \$full_glyph, Term::Fabulous::Check::glyph( $self, full_glyph => $new[0] ) ) : $full_glyph }
164 method empty_glyph (@new) { return @new ? $self->_set( \$empty_glyph, Term::Fabulous::Check::glyph( $self, empty_glyph => $new[0] ) ) : $empty_glyph }
165 method half_glyph (@new) { return @new ? $self->_set( \$half_glyph, $self->_checked_half_glyph( $new[0] ) ) : $half_glyph }
166 method inactive_color (@new) { return @new ? $self->set_look( inactive_color => $new[0] ) : $self->look_value('inactive_color') }
167 method half_color (@new) { return @new ? $self->set_look( half_color => $new[0] ) : $self->look_value('half_color') }
168  
169 # A read-only rating never takes the focus.
170 method accepts_focus :override () {
171 return $read_only ? 0 : $self->SUPER::accepts_focus;
172 }
173  
174 method layout_properties :common () {
175 return (
176 $class->SUPER::layout_properties,
177 ( map { $_ => 'scalar' } qw(max value value_format gap full_glyph empty_glyph half_glyph) ),
178 ( map { $_ => 'boolean' } qw(half read_only show_value) ),
179 );
180 }
181  
182 # ---------------------------------------------------------------------
183 # Input
184 # ---------------------------------------------------------------------
185  
186 # The star, counted from 1, under a mouse event; undef on a gap, the
187 # label or outside.
188 method _star_at ($event) {
189 my ($column) = $self->cell_at($event);
190 return undef unless defined $column;
191 my $pitch = 1 + $gap;
192 return undef if $column % $pitch != 0;
193 my $star = $column / $pitch + 1;
194 return $star <= $range->max ? $star : undef;
195 }
196  
197 method _hover_at ($star) {
198 return if $read_only || !$self->is_enabled;
199 return if ( $star // 0 ) == ( $_hover_star // 0 );
200 $_hover_star = $star;
201 $self->mark_changed;
202 return;
203 }
204  
205 method handle_key ($event) {
206 return 0 if $read_only;
207 my $name = $event->main_key_name // return 0;
208 if ( $name =~ /\A[0-9]\z/ ) {
209 return 0 if $name > $range->max;
210 $self->_moved( $range->move_to($name) );
211 return 1;
212 }
213 my $changed = $range->move_by_key($name) // return 0;
214 $self->_moved($changed);
215 return 1;
216 }
217  
218 method handle_mouse ($event) {
219 return 0 if $read_only;
220 my $key = $event->key;
221 if ( $key == TB_KEY_MOUSE_WHEEL_UP || $key == TB_KEY_MOUSE_WHEEL_DOWN ) {
222  
223 # At 0 or max the notch is left to a scroll box.
224 return 0 unless $self->_moved( $range->move_by( $key == TB_KEY_MOUSE_WHEEL_UP ? 1 : -1 ) );
225 $event->use_wheel;
226 return 1;
227 }
228 return 0 unless $key == TB_KEY_MOUSE_LEFT;
229 my $star = $self->_star_at($event) // return 1;
230 $self->_moved( $range->move_to( $half && $event->modifiers & TB_MOD_SHIFT ? $star - 0.5 : $star ) );
231 return 1;
232 }
233  
234 # ---------------------------------------------------------------------
235 # Painting
236 # ---------------------------------------------------------------------
237  
238 # The columns of the value label: wide enough for any value, so the
239 # stars stay in place while the value changes.
240 method _label_columns () {
241 return 0 unless $show_value;
242 return List::Util::max map { string_columns( $self->format_value($_) ) } 0, $range->max, $range->value, ( $half ? 0.5 : () );
243 }
244  
245 method _stars_columns () {
246 return $range->max + ( $range->max - 1 ) * $gap;
247 }
248  
249 method natural_size () {
250 my $label = $self->_label_columns;
251 return ( $self->_stars_columns + ( $label ? $label + 1 : 0 ), 1 );
252 }
253  
254 method paint_key :override () {
255 return ( $self->SUPER::paint_key, $_hover_star // 0 );
256 }
257  
258 method _half_attr () {
259 return $self->color_attr( $self->disabled_color ) unless $self->is_enabled;
260 my $half_color = $self->half_color;
261 return $self->color_attr($half_color) if defined $half_color;
262 my $accent = Term::Fabulous::Color->new( color => $self->accent_color );
263 return $self->color_attr( $accent->blend( Term::Fabulous::Color->new( color => $self->inactive_color ), 0.5 ) );
264 }
265  
266 method paint () {
267 my $bg = $self->paint_focus_background;
268 my $shown = $_hover_star // $range->value;
269 my $full_fg = $self->accent_attr;
270 my $empty_fg = $self->color_attr( $self->is_enabled ? $self->inactive_color : $self->disabled_color );
271 my $half_fg = $self->_half_attr;
272  
273 foreach my $star ( 1 .. $range->max ) {
274 my $x = ( $star - 1 ) * ( 1 + $gap );
275 my ( $glyph, $fg )
276 = $star <= $shown ? ( $full_glyph, $full_fg )
277 : $star - 0.5 == $shown ? ( $half_glyph // $full_glyph, $half_fg )
278 : ( $empty_glyph, $empty_fg );
279 $self->put_attrs( $x, 0, $glyph, $fg, $bg );
280 }
281 return unless $show_value;
282 $self->paint_text( $self->_stars_columns + 1, 0, $self->format_value($shown), $self->foreground_attr, $bg );
283 return;
284 }
285 }
286  
287 1;
288  
289 __END__