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

filter: Statementstmt 0.0% 0 / 179 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 15 Podpod n/a totaltotal 0.0% 0 / 337 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.03';
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 weaken);
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 weaken( my $weak_self = $self );
70 my $continue = Clay::UI::Enum::Result->CONTINUE;
71 $self->on( MouseMove => sub ($event) { $weak_self->_hover_at( $weak_self->_star_at($event) ) if $weak_self; return $continue } );
72 $self->on( OnHoverStopped => sub ($event) { $weak_self->_hover_at(undef) if $weak_self && refaddr( $event->target ) == refaddr($weak_self); return $continue } );
73 }
74  
75 method _checked_half_glyph ($value) {
76 return optional( \&Term::Fabulous::Check::glyph, $self, half_glyph => $value );
77 }
78  
79 # ---------------------------------------------------------------------
80 # Value
81 # ---------------------------------------------------------------------
82  
83 method value (@new) {
84 return $range->value unless @new;
85 $range->set_value( $new[0] );
86 $self->mark_changed;
87 return $range->value;
88 }
89  
90 # The user moved the value: fire Change if it moved. Returns 1 when the
91 # value changed.
92 method _moved ($changed) {
93 return 0 unless $changed;
94 $self->mark_changed;
95 $self->fire_change( $range->value );
96 return 1;
97 }
98  
99 method step () {
100 return $range->step;
101 }
102  
103 # The default text of a value: 3/5, or 3.5/5 with half stars.
104 sub _stars_of ( $number, $range ) {
105 return sprintf '%s/%d', ( $range->step < 1 ? sprintf( '%.1f', $number ) : $number ), $range->max;
106 }
107  
108 method format_value ($number) {
109 return $range->format_value($number);
110 }
111  
112 # max and half decide the grid the value snaps to; a layout gives them
113 # in one go (Term::Fabulous::Role::HasRange).
114 method set_range (%parts) {
115 my @unknown = grep { $_ ne 'max' && $_ ne 'half' } sort keys %parts;
116 die ref($self) . ": set_range takes max and half, got @unknown" if @unknown;
117 my $stars = exists $parts{max} ? positive_integer( $self, max => $parts{max} ) : $range->max;
118 $half = boolean( $self, half => $parts{half} ) if exists $parts{half};
119 $range->set_range( max => $stars, step => $half ? 0.5 : 1 );
120 $self->mark_changed;
121 return $self;
122 }
123  
124 method range_properties :common () {
125 return qw(max half);
126 }
127  
128 # ---------------------------------------------------------------------
129 # Accessors
130 # ---------------------------------------------------------------------
131  
132 method _set ( $field_ref, $value ) {
133 $$field_ref = $value;
134 $self->mark_changed;
135 return $$field_ref;
136 }
137  
138 method _set_value_format ($format) {
139 $range->set_value_format($format);
140 $self->mark_changed;
141 return $range->value_format;
142 }
143  
144 method max (@new) {
145 $self->set_range( max => $new[0] ) if @new;
146 return $range->max;
147 }
148  
149 method half (@new) {
150 $self->set_range( half => $new[0] ) if @new;
151 return $half;
152 }
153  
154 method read_only (@new) {
155 return $read_only unless @new;
156 $self->_set( \$read_only, boolean( $self, read_only => $new[0] ) );
157 $self->focus_eligibility_changed; # a read-only rating gives the focus up
158 return $read_only;
159 }
160  
161 method show_value (@new) { return @new ? $self->_set( \$show_value, boolean( $self, show_value => $new[0] ) ) : $show_value }
162 method value_format (@new) { return @new ? $self->_set_value_format( $new[0] ) : $range->value_format }
163 method gap (@new) { return @new ? $self->_set( \$gap, non_negative_integer( $self, gap => $new[0] ) ) : $gap }
164 method full_glyph (@new) { return @new ? $self->_set( \$full_glyph, Term::Fabulous::Check::glyph( $self, full_glyph => $new[0] ) ) : $full_glyph }
165 method empty_glyph (@new) { return @new ? $self->_set( \$empty_glyph, Term::Fabulous::Check::glyph( $self, empty_glyph => $new[0] ) ) : $empty_glyph }
166 method half_glyph (@new) { return @new ? $self->_set( \$half_glyph, $self->_checked_half_glyph( $new[0] ) ) : $half_glyph }
167 method inactive_color (@new) { return @new ? $self->set_look( inactive_color => $new[0] ) : $self->look_value('inactive_color') }
168 method half_color (@new) { return @new ? $self->set_look( half_color => $new[0] ) : $self->look_value('half_color') }
169  
170 # A read-only rating never takes the focus.
171 method accepts_focus :override () {
172 return $read_only ? 0 : $self->SUPER::accepts_focus;
173 }
174  
175 method layout_properties :common () {
176 return (
177 $class->SUPER::layout_properties,
178 ( map { $_ => 'scalar' } qw(max value value_format gap full_glyph empty_glyph half_glyph) ),
179 ( map { $_ => 'boolean' } qw(half read_only show_value) ),
180 );
181 }
182  
183 # ---------------------------------------------------------------------
184 # Input
185 # ---------------------------------------------------------------------
186  
187 # The star, counted from 1, under a mouse event; undef on a gap, the
188 # label or outside.
189 method _star_at ($event) {
190 my ($column) = $self->cell_at($event);
191 return undef unless defined $column;
192 my $pitch = 1 + $gap;
193 return undef if $column % $pitch != 0;
194 my $star = $column / $pitch + 1;
195 return $star <= $range->max ? $star : undef;
196 }
197  
198 method _hover_at ($star) {
199 return if $read_only || !$self->is_enabled;
200 return if ( $star // 0 ) == ( $_hover_star // 0 );
201 $_hover_star = $star;
202 $self->mark_changed;
203 return;
204 }
205  
206 method handle_key ($event) {
207 return 0 if $read_only;
208 my $name = $event->main_key_name // return 0;
209 if ( $name =~ /\A[0-9]\z/ ) {
210 return 0 if $name > $range->max;
211 $self->_moved( $range->move_to($name) );
212 return 1;
213 }
214 my $changed = $range->move_by_key($name) // return 0;
215 $self->_moved($changed);
216 return 1;
217 }
218  
219 method handle_mouse ($event) {
220 return 0 if $read_only;
221 my $key = $event->key;
222 if ( $key == TB_KEY_MOUSE_WHEEL_UP || $key == TB_KEY_MOUSE_WHEEL_DOWN ) {
223  
224 # At 0 or max the notch is left to a scroll box.
225 return 0 unless $self->_moved( $range->move_by( $key == TB_KEY_MOUSE_WHEEL_UP ? 1 : -1 ) );
226 $event->use_wheel;
227 return 1;
228 }
229 return 0 unless $key == TB_KEY_MOUSE_LEFT;
230 my $star = $self->_star_at($event) // return 1;
231 $self->_moved( $range->move_to( $half && $event->modifiers & TB_MOD_SHIFT ? $star - 0.5 : $star ) );
232 return 1;
233 }
234  
235 # ---------------------------------------------------------------------
236 # Painting
237 # ---------------------------------------------------------------------
238  
239 # The columns of the value label: wide enough for any value, so the
240 # stars stay in place while the value changes.
241 method _label_columns () {
242 return 0 unless $show_value;
243 return List::Util::max map { string_columns( $self->format_value($_) ) } 0, $range->max, $range->value, ( $half ? 0.5 : () );
244 }
245  
246 method _stars_columns () {
247 return $range->max + ( $range->max - 1 ) * $gap;
248 }
249  
250 method natural_size () {
251 my $label = $self->_label_columns;
252 return ( $self->_stars_columns + ( $label ? $label + 1 : 0 ), 1 );
253 }
254  
255 method paint_key :override () {
256 return ( $self->SUPER::paint_key, $_hover_star // 0 );
257 }
258  
259 method _half_attr () {
260 return $self->color_attr( $self->disabled_color ) unless $self->is_enabled;
261 my $half_color = $self->half_color;
262 return $self->color_attr($half_color) if defined $half_color;
263 my $accent = Term::Fabulous::Color->new( color => $self->accent_color );
264 return $self->color_attr( $accent->blend( Term::Fabulous::Color->new( color => $self->inactive_color ), 0.5 ) );
265 }
266  
267 method paint () {
268 my $bg = $self->paint_focus_background;
269 my $shown = $_hover_star // $range->value;
270 my $full_fg = $self->accent_attr;
271 my $empty_fg = $self->color_attr( $self->is_enabled ? $self->inactive_color : $self->disabled_color );
272 my $half_fg = $self->_half_attr;
273  
274 foreach my $star ( 1 .. $range->max ) {
275 my $x = ( $star - 1 ) * ( 1 + $gap );
276 my ( $glyph, $fg )
277 = $star <= $shown ? ( $full_glyph, $full_fg )
278 : $star - 0.5 == $shown ? ( $half_glyph // $full_glyph, $half_fg )
279 : ( $empty_glyph, $empty_fg );
280 $self->put_attrs( $x, 0, $glyph, $fg, $bg );
281 }
282 return unless $show_value;
283 $self->paint_text( $self->_stars_columns + 1, 0, $self->format_value($shown), $self->foreground_attr, $bg );
284 return;
285 }
286 }
287  
288 1;
289  
290 __END__