« blib/lib/Term/Fabulous/Widget/SegmentedControl.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/Sparkline.pm »
| 1 | package Term::Fabulous::Widget::Slider; | ||
| 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::Slider | ||
| 16 | :isa(Term::Fabulous::Widget::Input) | ||
| 17 | :does(Term::Fabulous::Role::HasRange) | ||
| 18 | :strict(params) | ||
| 19 | { | ||
| 20 | use List::Util (); # min and max are methods here | ||
| 21 | use Term::Fabulous::Check qw(boolean glyph positive_integer); | ||
| 22 | use Term::Fabulous::Range; | ||
| 23 | use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT TB_KEY_MOUSE_WHEEL_UP TB_KEY_MOUSE_WHEEL_DOWN); | ||
| 24 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 25 | |||
| 26 | field $show_value :param = 1; | ||
| 27 | field $preferred_columns :param = 20; | ||
| 28 | field $fill_glyph :param = "\x{2501}"; | ||
| 29 | field $track_glyph :param = "\x{2500}"; | ||
| 30 | field $thumb_glyph :param = "\x{25CF}"; | ||
| 31 | |||
| 32 | field $range; # a Term::Fabulous::Range: min, max, step and the value | ||
| 33 | |||
| 34 | ADJUST :params ( :$min = 0, :$max = 100, :$step = 1, :$page_step = undef, :$value_format = undef, :$value = undef ) { | ||
| 35 | $show_value = boolean( $self, show_value => $show_value ); | ||
| 36 | $preferred_columns = positive_integer( $self, preferred_columns => $preferred_columns ); | ||
| 37 | $fill_glyph = glyph( $self, fill_glyph => $fill_glyph ); | ||
| 38 | $track_glyph = glyph( $self, track_glyph => $track_glyph ); | ||
| 39 | $thumb_glyph = glyph( $self, thumb_glyph => $thumb_glyph ); | ||
| 40 | $range = Term::Fabulous::Range->new( | ||
| 41 | owner => ref $self, | ||
| 42 | min => $min, | ||
| 43 | max => $max, | ||
| 44 | step => $step, | ||
| 45 | page_step => $page_step, | ||
| 46 | value_format => $value_format, | ||
| 47 | value => $value, | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | # --------------------------------------------------------------------- | ||
| 52 | # Value | ||
| 53 | # --------------------------------------------------------------------- | ||
| 54 | |||
| 55 | method value (@new) { | ||
| 56 | return $range->value unless @new; | ||
| 57 | $range->set_value( $new[0] ); | ||
| 58 | $self->mark_changed; | ||
| 59 | return $range->value; | ||
| 60 | } | ||
| 61 | |||
| 62 | # Changes min, max and step together, so a new range can be set in one | ||
| 63 | # go whatever the old one was; the value moves into it. | ||
| 64 | method set_range (%range) { | ||
| 65 | $range->set_range(%range); | ||
| 66 | $self->mark_changed; | ||
| 67 | return $self; | ||
| 68 | } | ||
| 69 | |||
| 70 | method range_properties :common () { | ||
| 71 | return qw(min max step); | ||
| 72 | } | ||
| 73 | |||
| 74 | method min (@new) { | ||
| 75 | $self->set_range( min => $new[0] ) if @new; | ||
| 76 | return $range->min; | ||
| 77 | } | ||
| 78 | |||
| 79 | method max (@new) { | ||
| 80 | $self->set_range( max => $new[0] ) if @new; | ||
| 81 | return $range->max; | ||
| 82 | } | ||
| 83 | |||
| 84 | method step (@new) { | ||
| 85 | $self->set_range( step => $new[0] ) if @new; | ||
| 86 | return $range->step; | ||
| 87 | } | ||
| 88 | |||
| 89 | method page_step (@new) { | ||
| 90 | $range->set_page_step( $new[0] ) if @new; | ||
| 91 | return $range->page_step; | ||
| 92 | } | ||
| 93 | |||
| 94 | method show_value (@new) { | ||
| 95 | return $show_value unless @new; | ||
| 96 | $show_value = boolean( $self, show_value => $new[0] ); | ||
| 97 | $self->mark_changed; | ||
| 98 | return $show_value; | ||
| 99 | } | ||
| 100 | |||
| 101 | method value_format (@new) { | ||
| 102 | return $range->value_format unless @new; | ||
| 103 | $range->set_value_format( $new[0] ); | ||
| 104 | $self->mark_changed; | ||
| 105 | return $range->value_format; | ||
| 106 | } | ||
| 107 | |||
| 108 | method preferred_columns (@new) { | ||
| 109 | return $preferred_columns unless @new; | ||
| 110 | $preferred_columns = positive_integer( $self, preferred_columns => $new[0] ); | ||
| 111 | $self->mark_changed; | ||
| 112 | return $preferred_columns; | ||
| 113 | } | ||
| 114 | |||
| 115 | method fill_glyph (@new) { return @new ? $self->_set_glyph( fill_glyph => \$fill_glyph, @new ) : $fill_glyph } | ||
| 116 | method track_glyph (@new) { return @new ? $self->_set_glyph( track_glyph => \$track_glyph, @new ) : $track_glyph } | ||
| 117 | method thumb_glyph (@new) { return @new ? $self->_set_glyph( thumb_glyph => \$thumb_glyph, @new ) : $thumb_glyph } | ||
| 118 | |||
| 119 | method _set_glyph ( $name, $field_ref, $glyph ) { | ||
| 120 | $$field_ref = glyph( $self, $name => $glyph ); | ||
| 121 | $self->mark_changed; | ||
| 122 | return $$field_ref; | ||
| 123 | } | ||
| 124 | |||
| 125 | method themed_params :common () { | ||
| 126 | return ( $class->SUPER::themed_params, track_color => [ 'track', 'normal', 'cell_color' ] ); | ||
| 127 | } | ||
| 128 | |||
| 129 | method track_color (@new) { | ||
| 130 | return @new ? $self->set_look( track_color => $new[0] ) : $self->look_value('track_color'); | ||
| 131 | } | ||
| 132 | |||
| 133 | method layout_properties :common () { | ||
| 134 | return ( | ||
| 135 | $class->SUPER::layout_properties, | ||
| 136 | ( map { $_ => 'scalar' } qw(min max step page_step value value_format preferred_columns fill_glyph track_glyph thumb_glyph) ), | ||
| 137 | show_value => 'boolean', | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | |||
| 141 | method format_value ($number) { | ||
| 142 | return $range->format_value($number); | ||
| 143 | } | ||
| 144 | |||
| 145 | # The user moved the value: fire Change if it moved. Returns 1 when | ||
| 146 | # the value changed. | ||
| 147 | method _moved ($changed) { | ||
| 148 | return 0 unless $changed; | ||
| 149 | $self->mark_changed; | ||
| 150 | $self->fire_change( $range->value ); | ||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | |||
| 154 | # --------------------------------------------------------------------- | ||
| 155 | # Input | ||
| 156 | # --------------------------------------------------------------------- | ||
| 157 | |||
| 158 | method handle_key ($event) { | ||
| 159 | my $changed = $range->move_by_key( $event->main_key_name ) // return 0; | ||
| 160 | $self->_moved($changed); | ||
| 161 | return 1; | ||
| 162 | } | ||
| 163 | |||
| 164 | method handle_mouse ($event) { | ||
| 165 | my $key = $event->key; | ||
| 166 | if ( $key == TB_KEY_MOUSE_WHEEL_UP || $key == TB_KEY_MOUSE_WHEEL_DOWN ) { | ||
| 167 | |||
| 168 | # At min or max the notch is left to a scroll box. | ||
| 169 | return 0 unless $self->_moved( $range->move_by( $key == TB_KEY_MOUSE_WHEEL_UP ? 1 : -1 ) ); | ||
| 170 | $event->use_wheel; | ||
| 171 | return 1; | ||
| 172 | } | ||
| 173 | return 0 unless $key == TB_KEY_MOUSE_LEFT; | ||
| 174 | |||
| 175 | my ($column) = $self->cell_at($event); | ||
| 176 | my $track = $self->_track_columns; | ||
| 177 | return 1 unless defined $column && $column < $track; | ||
| 178 | $self->_moved( $range->move_to( $range->min + ( $track > 1 ? ( $range->max - $range->min ) * $column / ( $track - 1 ) : 0 ) ) ); | ||
| 179 | return 1; | ||
| 180 | } | ||
| 181 | |||
| 182 | # --------------------------------------------------------------------- | ||
| 183 | # Painting | ||
| 184 | # --------------------------------------------------------------------- | ||
| 185 | |||
| 186 | # The columns of the value label: wide enough for any value, so the | ||
| 187 | # track keeps its length while the value changes. | ||
| 188 | method _label_columns () { | ||
| 189 | return 0 unless $show_value; | ||
| 190 | return List::Util::max map { string_columns( $self->format_value($_) ) } $range->min, $range->top_value, $range->value; | ||
| 191 | } | ||
| 192 | |||
| 193 | method _track_columns () { | ||
| 194 | my $label = $self->_label_columns; | ||
| 195 | return List::Util::max( 1, $self->columns - ( $label ? $label + 1 : 0 ) ); | ||
| 196 | } | ||
| 197 | |||
| 198 | method natural_size () { | ||
| 199 | my $label = $self->_label_columns; | ||
| 200 | return ( $preferred_columns + ( $label ? $label + 1 : 0 ), 1 ); | ||
| 201 | } | ||
| 202 | |||
| 203 | method paint () { | ||
| 204 | my $bg = $self->paint_focus_background; | ||
| 205 | my $track = $self->_track_columns; | ||
| 206 | my $thumb = int( ( $track - 1 ) * $range->fraction + 0.5 ); | ||
| 207 | |||
| 208 | $self->fill_attrs( 0, 0, $thumb, $fill_glyph, $self->accent_attr, $bg ); | ||
| 209 | $self->fill_attrs( $thumb + 1, 0, $track - $thumb - 1, $track_glyph, $self->color_attr( $self->is_enabled ? $self->track_color : $self->disabled_color ), $bg ); | ||
| 210 | $self->put_attrs( $thumb, 0, $thumb_glyph, $self->is_focused ? $self->foreground_attr : $self->accent_attr, $bg ); | ||
| 211 | return unless $show_value; | ||
| 212 | |||
| 213 | my $label = $self->format_value( $range->value ); | ||
| 214 | my $x = $self->columns - string_columns($label); | ||
| 215 | $self->paint_text( List::Util::max( $x, $track + 1 ), 0, $label, $self->foreground_attr, $bg ); | ||
| 216 | return; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | 1; | ||
| 221 | |||
| 222 | __END__ |