| 1 | package Term::Fabulous::Widget::SegmentedControl; | ||
| 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::HasOptions; | ||
| 11 | use Term::Fabulous::Widget::Input; | ||
| 12 | |||
| 13 | our $VERSION = '0.05'; | ||
| 14 | |||
| 15 | class Term::Fabulous::Widget::SegmentedControl | ||
| 16 | :isa(Term::Fabulous::Widget::Input) | ||
| 17 | :does(Term::Fabulous::Role::HasOptions) | ||
| 18 | :strict(params) | ||
| 19 | { | ||
| 20 | use Clay::UI::Enum::Result; | ||
| 21 | use List::Util qw(first sum0); | ||
| 22 | use Scalar::Util qw(refaddr); | ||
| 23 | use Term::Fabulous::Check qw(boolean non_negative_integer); | ||
| 24 | use Term::Fabulous::OptionList; | ||
| 25 | use Term::Fabulous::Roving qw(roving_target); | ||
| 26 | use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT); | ||
| 27 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 28 | |||
| 29 | field $option_list = Term::Fabulous::OptionList->new( owner => __CLASS__ ); | ||
| 30 | |||
| 31 | field $vertical :param = 0; | ||
| 32 | field $segment_padding :param = 1; | ||
| 33 | field $separator :param = "\x{2502}"; | ||
| 34 | |||
| 35 | # The segment the pointer is over while it hovers. | ||
| 36 | field $_hover_index; | ||
| 37 | |||
| 38 | ADJUST :params ( :$options = undef, :$value = undef, :$selected_index = undef ) { | ||
| 39 | die ref($self) . ": give 'value' or 'selected_index', not both" | ||
| 40 | if defined $value && defined $selected_index; | ||
| 41 | $vertical = boolean( $self, vertical => $vertical ); | ||
| 42 | $segment_padding = non_negative_integer( $self, segment_padding => $segment_padding ); | ||
| 43 | $separator = $self->_checked_separator($separator); | ||
| 44 | |||
| 45 | $self->options($options) if defined $options; | ||
| 46 | $self->value($value) if defined $value; | ||
| 47 | $self->selected_index($selected_index) if defined $selected_index; | ||
| 48 | |||
| 49 | my $continue = Clay::UI::Enum::Result->CONTINUE; | ||
| 50 | $self->on( MouseMove => sub ( $self, $event ) { $self->_hover_at( $self->_index_at($event) ); return $continue }, owner => $self ); | ||
| 51 | $self->on( OnHoverStopped => sub ( $self, $event ) { $self->_hover_at(undef) if refaddr( $event->target ) == refaddr($self); return $continue }, owner => $self ); | ||
| 52 | } | ||
| 53 | |||
| 54 | method _checked_separator ($glyph) { | ||
| 55 | return undef unless defined $glyph; | ||
| 56 | return Term::Fabulous::Check::glyph( $self, separator => $glyph ); | ||
| 57 | } | ||
| 58 | |||
| 59 | # --------------------------------------------------------------------- | ||
| 60 | # Options and value (Term::Fabulous::OptionList) | ||
| 61 | # --------------------------------------------------------------------- | ||
| 62 | |||
| 63 | method options (@new) { | ||
| 64 | return $option_list->options unless @new; | ||
| 65 | $option_list->set_options( $new[0] ); | ||
| 66 | $self->mark_changed; | ||
| 67 | return $option_list->options; | ||
| 68 | } | ||
| 69 | |||
| 70 | method value (@new) { | ||
| 71 | return $option_list->value unless @new; | ||
| 72 | $option_list->set_value( $new[0] ); | ||
| 73 | $self->mark_changed; | ||
| 74 | return $option_list->value; | ||
| 75 | } | ||
| 76 | |||
| 77 | method selected_index (@new) { | ||
| 78 | return $option_list->selected_index unless @new; | ||
| 79 | $option_list->set_selected_index( $new[0] ); | ||
| 80 | $self->mark_changed; | ||
| 81 | return $option_list->selected_index; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Selects a segment as the user does: fires Change when the selection | ||
| 85 | # changes. A disabled segment cannot be chosen. | ||
| 86 | method choose ($index) { | ||
| 87 | return $self unless $option_list->choose($index); | ||
| 88 | $self->mark_changed; | ||
| 89 | $self->fire_change( $option_list->value ); | ||
| 90 | return $self; | ||
| 91 | } | ||
| 92 | |||
| 93 | method option_disabled ( $index, @new ) { | ||
| 94 | return $option_list->is_disabled($index) unless @new; | ||
| 95 | $option_list->set_disabled( $index, $new[0] ); | ||
| 96 | $self->mark_changed; | ||
| 97 | return $option_list->is_disabled($index); | ||
| 98 | } | ||
| 99 | |||
| 100 | # --------------------------------------------------------------------- | ||
| 101 | # Accessors | ||
| 102 | # --------------------------------------------------------------------- | ||
| 103 | |||
| 104 | method _set ( $field_ref, $value ) { | ||
| 105 | $$field_ref = $value; | ||
| 106 | $self->mark_changed; | ||
| 107 | return $$field_ref; | ||
| 108 | } | ||
| 109 | |||
| 110 | method vertical (@new) { return @new ? $self->_set( \$vertical, boolean( $self, vertical => $new[0] ) ) : $vertical } | ||
| 111 | method segment_padding (@new) { return @new ? $self->_set( \$segment_padding, non_negative_integer( $self, segment_padding => $new[0] ) ) : $segment_padding } | ||
| 112 | method separator (@new) { return @new ? $self->_set( \$separator, $self->_checked_separator( $new[0] ) ) : $separator } | ||
| 113 | method selected_text_color (@new) { return @new ? $self->set_look( selected_text_color => $new[0] ) : $self->look_value('selected_text_color') } | ||
| 114 | method separator_color (@new) { return @new ? $self->set_look( separator_color => $new[0] ) : $self->look_value('separator_color') } | ||
| 115 | |||
| 116 | method hover_background_color (@new) { | ||
| 117 | return @new ? $self->set_look( hover_background_color => $new[0] ) : $self->look_value('hover_background_color'); | ||
| 118 | } | ||
| 119 | |||
| 120 | method themed_params :common () { | ||
| 121 | return ( | ||
| 122 | $class->SUPER::themed_params, | ||
| 123 | selected_text_color => [ 'selected_text', 'normal', 'cell_color' ], | ||
| 124 | separator_color => [ 'separator', 'normal', 'cell_color' ], | ||
| 125 | hover_background_color => [ 'hover_background', 'normal', 'cell_color' ], | ||
| 126 | ); | ||
| 127 | } | ||
| 128 | |||
| 129 | method layout_properties :common () { | ||
| 130 | return ( | ||
| 131 | $class->SUPER::layout_properties, | ||
| 132 | options => \&add_layout_options, | ||
| 133 | option => \&add_layout_options, | ||
| 134 | ( map { $_ => 'scalar' } qw(value selected_index segment_padding separator) ), | ||
| 135 | vertical => 'boolean', | ||
| 136 | ); | ||
| 137 | } | ||
| 138 | |||
| 139 | # --------------------------------------------------------------------- | ||
| 140 | # Geometry: where each segment lies along the control | ||
| 141 | # --------------------------------------------------------------------- | ||
| 142 | |||
| 143 | method _separator_cells () { | ||
| 144 | return defined $separator && !$vertical ? 1 : 0; | ||
| 145 | } | ||
| 146 | |||
| 147 | # The cells each segment needs along the control: its label with the | ||
| 148 | # padding on both sides; a row each when vertical. | ||
| 149 | method _natural_lengths () { | ||
| 150 | return map { $vertical ? 1 : string_columns( $_->{label} ) + 2 * $segment_padding } $option_list->options; | ||
| 151 | } | ||
| 152 | |||
| 153 | # The [start, length] of every segment along the control: the natural | ||
| 154 | # lengths, with any extra space shared out from the first segment on. | ||
| 155 | method _spans ($length) { | ||
| 156 | my @lengths = $self->_natural_lengths or return (); | ||
| 157 | my $extra = $length - sum0(@lengths) - $self->_separator_cells * $#lengths; | ||
| 158 | if ( $extra > 0 ) { | ||
| 159 | my $share = int( $extra / @lengths ); | ||
| 160 | my $left = $extra % @lengths; | ||
| 161 | $lengths[$_] += $share + ( $_ < $left ? 1 : 0 ) foreach 0 .. $#lengths; | ||
| 162 | } | ||
| 163 | my @spans; | ||
| 164 | my $at = 0; | ||
| 165 | foreach my $segment_length (@lengths) { | ||
| 166 | push @spans, [ $at, $segment_length ]; | ||
| 167 | $at += $segment_length + $self->_separator_cells; | ||
| 168 | } | ||
| 169 | return @spans; | ||
| 170 | } | ||
| 171 | |||
| 172 | method natural_size () { | ||
| 173 | my @options = $option_list->options; | ||
| 174 | my @lengths = $self->_natural_lengths; | ||
| 175 | return ( List::Util::max( 1, map { string_columns( $_->{label} ) + 2 * $segment_padding } @options ), List::Util::max( 1, scalar @options ) ) if $vertical; | ||
| 176 | return ( List::Util::max( 1, sum0(@lengths) + $self->_separator_cells * ( @options ? $#options : 0 ) ), 1 ); | ||
| 177 | } | ||
| 178 | |||
| 179 | # The segment under a mouse event, or undef. | ||
| 180 | method _index_at ($event) { | ||
| 181 | my ( $column, $row ) = $self->cell_at($event); | ||
| 182 | return undef unless defined $column; | ||
| 183 | my $position = $vertical ? $row : $column; | ||
| 184 | my @spans = $self->_spans( $vertical ? $self->rows : $self->columns ); | ||
| 185 | return first { $position >= $spans[$_][0] && $position < $spans[$_][0] + $spans[$_][1] } 0 .. $#spans; | ||
| 186 | } | ||
| 187 | |||
| 188 | # --------------------------------------------------------------------- | ||
| 189 | # Input | ||
| 190 | # --------------------------------------------------------------------- | ||
| 191 | |||
| 192 | method _hover_at ($index) { | ||
| 193 | return unless $self->is_enabled; | ||
| 194 | return if ( $index // -1 ) == ( $_hover_index // -1 ); | ||
| 195 | $_hover_index = $index; | ||
| 196 | $self->mark_changed; | ||
| 197 | return; | ||
| 198 | } | ||
| 199 | |||
| 200 | method handle_key ($event) { | ||
| 201 | my $name = $event->main_key_name // return 0; | ||
| 202 | my @enabled = $option_list->enabled_indexes; | ||
| 203 | return 0 unless @enabled; | ||
| 204 | |||
| 205 | if ( $name =~ /\A[1-9]\z/ ) { | ||
| 206 | return 0 if $name > $option_list->count; | ||
| 207 | $self->choose( $name - 1 ); | ||
| 208 | return 1; | ||
| 209 | } | ||
| 210 | my $target = roving_target( \@enabled, $option_list->selected_index, $name ) // return 0; | ||
| 211 | $self->choose($target); | ||
| 212 | return 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | method handle_mouse ($event) { | ||
| 216 | return 0 unless $event->key == TB_KEY_MOUSE_LEFT; | ||
| 217 | my $index = $self->_index_at($event) // return 1; | ||
| 218 | $self->choose($index); | ||
| 219 | return 1; | ||
| 220 | } | ||
| 221 | |||
| 222 | # --------------------------------------------------------------------- | ||
| 223 | # Painting | ||
| 224 | # --------------------------------------------------------------------- | ||
| 225 | |||
| 226 | method paint_key :override () { | ||
| 227 | return ( $self->SUPER::paint_key, $_hover_index // -1 ); | ||
| 228 | } | ||
| 229 | |||
| 230 | method paint () { | ||
| 231 | my $bg = $self->paint_focus_background; | ||
| 232 | my $length = $vertical ? $self->rows : $self->columns; | ||
| 233 | my $across = $vertical ? $self->columns : $self->rows; | ||
| 234 | my @spans = $self->_spans($length); | ||
| 235 | my $enabled = $self->is_enabled; | ||
| 236 | my @options = $option_list->options; | ||
| 237 | my $chosen = $option_list->selected_index; | ||
| 238 | |||
| 239 | foreach my $index ( 0 .. $#spans ) { | ||
| 240 | my ( $start, $segment_length ) = $spans[$index]->@*; | ||
| 241 | my $option = $options[$index]; | ||
| 242 | my $is_selected = defined $chosen && $chosen == $index; | ||
| 243 | my $is_hovered = $enabled && !$option->{disabled} && defined $_hover_index && $_hover_index == $index; | ||
| 244 | my $segment_bg = $is_selected ? $self->accent_attr : $is_hovered ? $self->color_attr( $self->hover_background_color ) : $bg; | ||
| 245 | my $fg = $is_selected ? $self->color_attr( $self->selected_text_color ) : $option->{disabled} ? $self->color_attr( $self->disabled_color ) : $self->foreground_attr; | ||
| 246 | |||
| 247 | my $label = $option->{label}; | ||
| 248 | my $width = $vertical ? $across : $segment_length; | ||
| 249 | my $height = $vertical ? $segment_length : $across; | ||
| 250 | my $x0 = $vertical ? 0 : $start; | ||
| 251 | my $y0 = $vertical ? $start : 0; | ||
| 252 | $self->fill_attrs( $x0, $y0 + $_, $width, ' ', undef, $segment_bg ) foreach 0 .. $height - 1; | ||
| 253 | my $label_x = $x0 + int( ( $width - string_columns($label) ) / 2 ); | ||
| 254 | $self->paint_text( List::Util::max( $x0, $label_x ), $y0 + int( ( $height - 1 ) / 2 ), $label, $fg, $segment_bg, $x0 + $width ); | ||
| 255 | |||
| 256 | next if $vertical || !defined $separator || $index == $#spans; | ||
| 257 | $self->put_attrs( $start + $segment_length, $_, $separator, $self->color_attr( $enabled ? $self->separator_color : $self->disabled_color ), $bg ) foreach 0 .. $height - 1; | ||
| 258 | } | ||
| 259 | return; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | 1; | ||
| 264 | |||
| 265 | __END__ |