« blib/lib/Term/Fabulous/Widget/DonutChart.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/Dropdown/List.pm »
| 1 | package Term::Fabulous::Widget::Dropdown; | ||
| 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::Dropdown::List; | ||
| 12 | use Term::Fabulous::Widget::Input; | ||
| 13 | |||
| 14 | our $VERSION = '0.03'; | ||
| 15 | |||
| 16 | class Term::Fabulous::Widget::Dropdown | ||
| 17 | :isa(Term::Fabulous::Widget::Input) | ||
| 18 | :does(Term::Fabulous::Role::HasOptions) | ||
| 19 | :strict(params) | ||
| 20 | { | ||
| 21 | use Clay::XS qw(sizing_fixed CLAY_ATTACH_TO_PARENT CLAY_ATTACH_POINT_LEFT_TOP CLAY_ATTACH_POINT_LEFT_BOTTOM); | ||
| 22 | use List::Util qw(max min); | ||
| 23 | use Scalar::Util qw(refaddr); | ||
| 24 | use Term::Fabulous::Check qw(positive_integer string); | ||
| 25 | use Term::Fabulous::OptionList; | ||
| 26 | use Term::Fabulous::Roving qw(roving_target); | ||
| 27 | use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT TB_MOD_MOTION); | ||
| 28 | use Time::HiRes qw(time); | ||
| 29 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 30 | |||
| 31 | use constant ARROW_DOWN => "\x{25BE}"; | ||
| 32 | use constant ARROW_UP => "\x{25B4}"; | ||
| 33 | use constant TYPEAHEAD_SECONDS => 1; | ||
| 34 | |||
| 35 | # The highest z_index Clay has (int16): an open list belongs to the | ||
| 36 | # focused widget, so it floats over everything, a dialog it is in | ||
| 37 | # included. | ||
| 38 | use constant LIST_Z_INDEX => 32767; | ||
| 39 | |||
| 40 | field $option_list = Term::Fabulous::OptionList->new( owner => __CLASS__ ); | ||
| 41 | |||
| 42 | field $placeholder :param = ''; | ||
| 43 | field $max_visible_options :param = 8; | ||
| 44 | |||
| 45 | # The open list, the option it highlights and whether it opened upwards. | ||
| 46 | field $list; | ||
| 47 | field $highlighted; | ||
| 48 | field $opens_upwards = 0; | ||
| 49 | |||
| 50 | # Typed characters that search the labels, and when the last one came. | ||
| 51 | field $typed = ''; | ||
| 52 | field $typed_at = 0; | ||
| 53 | |||
| 54 | ADJUST :params ( :$options = undef, :$value = undef, :$selected_index = undef ) { | ||
| 55 | die "Term::Fabulous::Widget::Dropdown: give 'value' or 'selected_index', not both" | ||
| 56 | if defined $value && defined $selected_index; | ||
| 57 | $placeholder = string( $self, placeholder => $placeholder ); | ||
| 58 | $max_visible_options = positive_integer( $self, max_visible_options => $max_visible_options ); | ||
| 59 | |||
| 60 | $self->options($options) if defined $options; | ||
| 61 | $self->value($value) if defined $value; | ||
| 62 | $self->selected_index($selected_index) if defined $selected_index; | ||
| 63 | } | ||
| 64 | |||
| 65 | # --------------------------------------------------------------------- | ||
| 66 | # Options and value (Term::Fabulous::OptionList) | ||
| 67 | # --------------------------------------------------------------------- | ||
| 68 | |||
| 69 | method options (@new) { | ||
| 70 | return $option_list->options unless @new; | ||
| 71 | $option_list->set_options( $new[0] ); | ||
| 72 | $self->close; | ||
| 73 | $self->mark_changed; | ||
| 74 | return $option_list->options; | ||
| 75 | } | ||
| 76 | |||
| 77 | method value (@new) { | ||
| 78 | return $option_list->value unless @new; | ||
| 79 | $option_list->set_value( $new[0] ); | ||
| 80 | $self->mark_changed; | ||
| 81 | return $option_list->value; | ||
| 82 | } | ||
| 83 | |||
| 84 | method selected_index (@new) { | ||
| 85 | return $option_list->selected_index unless @new; | ||
| 86 | $option_list->set_selected_index( $new[0] ); | ||
| 87 | $self->mark_changed; | ||
| 88 | return $option_list->selected_index; | ||
| 89 | } | ||
| 90 | |||
| 91 | method selected_label () { | ||
| 92 | return $option_list->selected_label; | ||
| 93 | } | ||
| 94 | |||
| 95 | method placeholder (@new) { | ||
| 96 | return $placeholder unless @new; | ||
| 97 | $placeholder = string( $self, placeholder => $new[0] ); | ||
| 98 | $self->mark_changed; | ||
| 99 | return $placeholder; | ||
| 100 | } | ||
| 101 | |||
| 102 | method max_visible_options (@new) { | ||
| 103 | return $max_visible_options unless @new; | ||
| 104 | return $max_visible_options = positive_integer( $self, max_visible_options => $new[0] ); | ||
| 105 | } | ||
| 106 | |||
| 107 | method theme_family :common () { | ||
| 108 | return 'dropdown'; | ||
| 109 | } | ||
| 110 | |||
| 111 | method themed_params :common () { | ||
| 112 | return ( | ||
| 113 | $class->SUPER::themed_params, | ||
| 114 | placeholder_color => [ 'placeholder', 'normal', 'cell_color' ], | ||
| 115 | list_background_color => [ 'list.background', 'normal', 'cell_color' ], | ||
| 116 | highlight_text_color => [ 'highlight.text', 'normal', 'cell_color' ], | ||
| 117 | ); | ||
| 118 | } | ||
| 119 | |||
| 120 | method placeholder_color (@new) { | ||
| 121 | return @new ? $self->set_look( placeholder_color => $new[0] ) : $self->look_value('placeholder_color'); | ||
| 122 | } | ||
| 123 | |||
| 124 | method list_background_color (@new) { | ||
| 125 | return @new ? $self->set_look( list_background_color => $new[0] ) : $self->look_value('list_background_color'); | ||
| 126 | } | ||
| 127 | |||
| 128 | method highlight_text_color (@new) { | ||
| 129 | return @new ? $self->set_look( highlight_text_color => $new[0] ) : $self->look_value('highlight_text_color'); | ||
| 130 | } | ||
| 131 | |||
| 132 | method disabled :override (@new) { | ||
| 133 | $self->close if @new && $new[0]; | ||
| 134 | return $self->SUPER::disabled(@new); | ||
| 135 | } | ||
| 136 | |||
| 137 | method layout_properties :common () { | ||
| 138 | return ( | ||
| 139 | $class->SUPER::layout_properties, | ||
| 140 | value => 'scalar', | ||
| 141 | selected_index => 'scalar', | ||
| 142 | placeholder => 'scalar', | ||
| 143 | max_visible_options => 'scalar', | ||
| 144 | options => \&add_layout_options, | ||
| 145 | option => \&add_layout_options, | ||
| 146 | ); | ||
| 147 | } | ||
| 148 | |||
| 149 | # --------------------------------------------------------------------- | ||
| 150 | # The list | ||
| 151 | # --------------------------------------------------------------------- | ||
| 152 | |||
| 153 | method is_open () { | ||
| 154 | return defined $list ? 1 : 0; | ||
| 155 | } | ||
| 156 | |||
| 157 | method highlighted_index () { | ||
| 158 | return $highlighted; | ||
| 159 | } | ||
| 160 | |||
| 161 | # The list opens with the selected option highlighted, or the first | ||
| 162 | # one that is enabled. | ||
| 163 | method open () { | ||
| 164 | return $self if defined $list || !$option_list->count || !$self->is_enabled; | ||
| 165 | my $selected = $option_list->selected_index; | ||
| 166 | $highlighted = defined $selected && !$option_list->is_disabled($selected) ? $selected : ( $option_list->enabled_indexes )[0]; | ||
| 167 | |||
| 168 | my ( $visible, $upwards ) = $self->_list_rows; | ||
| 169 | my $scrolls = $visible < $option_list->count; | ||
| 170 | my $width = max( $self->_box_width, 2 + ( max map { string_columns( $_->{label} ) } $option_list->options ) + 2 + ( $scrolls ? 1 : 0 ) ); | ||
| 171 | $opens_upwards = $upwards; | ||
| 172 | |||
| 173 | my ( $element, $parent ) = $upwards ? ( CLAY_ATTACH_POINT_LEFT_BOTTOM, CLAY_ATTACH_POINT_LEFT_TOP ) : ( CLAY_ATTACH_POINT_LEFT_TOP, CLAY_ATTACH_POINT_LEFT_BOTTOM ); | ||
| 174 | $list = Term::Fabulous::Widget::Dropdown::List->new( | ||
| 175 | dropdown => $self, | ||
| 176 | visible_rows => $visible, | ||
| 177 | bordered => 1, | ||
| 178 | layout => { sizing => { width => sizing_fixed($width), height => sizing_fixed( $visible + 2 ) } }, | ||
| 179 | floating => { | ||
| 180 | attach_to => CLAY_ATTACH_TO_PARENT, | ||
| 181 | attach_points => { element => $element, parent => $parent }, | ||
| 182 | z_index => LIST_Z_INDEX, | ||
| 183 | }, | ||
| 184 | ); | ||
| 185 | $self->add_child($list); | ||
| 186 | $list->show_highlight; | ||
| 187 | return $self->mark_changed; | ||
| 188 | } | ||
| 189 | |||
| 190 | method close () { | ||
| 191 | return $self unless defined $list; | ||
| 192 | my $closing = $list; | ||
| 193 | undef $list; | ||
| 194 | undef $highlighted; | ||
| 195 | $self->remove_child($closing); | ||
| 196 | return $self->mark_changed; | ||
| 197 | } | ||
| 198 | |||
| 199 | # The width of the whole widget in the last frame (its natural width | ||
| 200 | # before the first one). | ||
| 201 | method _box_width () { | ||
| 202 | my ( $left, undef, $right ) = $self->content_insets; | ||
| 203 | return $left + ( $self->columns || ( $self->natural_size )[0] ) + $right; | ||
| 204 | } | ||
| 205 | |||
| 206 | # How many options the list shows, and whether it opens upwards: below | ||
| 207 | # the widget when they fit there or there is at least as much room | ||
| 208 | # below as above. | ||
| 209 | method _list_rows () { | ||
| 210 | my $wanted = min( $option_list->count, $max_visible_options ); | ||
| 211 | my $ui = $self->ui; | ||
| 212 | my @origin = $self->content_origin; | ||
| 213 | return ( $wanted, 0 ) unless defined $ui && @origin; | ||
| 214 | |||
| 215 | my ( undef, $top, undef, $bottom ) = $self->content_insets; | ||
| 216 | my $room_above = $origin[1] - $top; | ||
| 217 | my $room_below = $ui->height - ( $origin[1] + $self->rows + $bottom ); | ||
| 218 | my $upwards = $room_below < $wanted + 2 && $room_above > $room_below; | ||
| 219 | my $room = $upwards ? $room_above : $room_below; | ||
| 220 | return ( max( 1, min( $wanted, $room - 2 ) ), $upwards ? 1 : 0 ); | ||
| 221 | } | ||
| 222 | |||
| 223 | # Chooses an option as the user does; a disabled option cannot be | ||
| 224 | # chosen, and the list stays open then. | ||
| 225 | method choose ($index) { | ||
| 226 | my $changed = $option_list->choose($index); | ||
| 227 | return $self if $option_list->is_disabled($index); | ||
| 228 | $self->close; | ||
| 229 | return $self unless $changed; | ||
| 230 | $self->mark_changed; | ||
| 231 | $self->fire_change( $option_list->value ); | ||
| 232 | return $self; | ||
| 233 | } | ||
| 234 | |||
| 235 | # A disabled option is never highlighted. | ||
| 236 | method highlight ($index) { | ||
| 237 | return $self unless defined $list; | ||
| 238 | $index = min( max( $index, 0 ), $option_list->count - 1 ); | ||
| 239 | return $self if $option_list->is_disabled($index); | ||
| 240 | $highlighted = $index; | ||
| 241 | $list->show_highlight; | ||
| 242 | return $self; | ||
| 243 | } | ||
| 244 | |||
| 245 | # --------------------------------------------------------------------- | ||
| 246 | # Input | ||
| 247 | # --------------------------------------------------------------------- | ||
| 248 | |||
| 249 | method focus_changed :override ($is_focused) { | ||
| 250 | $self->close unless $is_focused; | ||
| 251 | return; | ||
| 252 | } | ||
| 253 | |||
| 254 | method handle_mouse ($event) { | ||
| 255 | return 0 unless refaddr( $event->target ) == refaddr($self); | ||
| 256 | return 0 unless $event->key == TB_KEY_MOUSE_LEFT && !( $event->modifiers & TB_MOD_MOTION ); | ||
| 257 | $self->is_open ? $self->close : $self->open; | ||
| 258 | return 1; | ||
| 259 | } | ||
| 260 | |||
| 261 | method handle_key ($event) { | ||
| 262 | my $text = $event->text; | ||
| 263 | return $self->_type_ahead($text) if defined $text && $text ne ' '; | ||
| 264 | |||
| 265 | my $name = $event->main_key_name // return 0; | ||
| 266 | return $self->_handle_key_while_open($name) if $self->is_open; | ||
| 267 | |||
| 268 | if ( $name eq 'Enter' || $name eq 'Space' || $name eq 'Alt+Down' || $name eq 'F4' ) { | ||
| 269 | $self->open; | ||
| 270 | return 1; | ||
| 271 | } | ||
| 272 | my $target = roving_target( [ $option_list->enabled_indexes ], $option_list->selected_index, $name, keys => 'vertical', policy => 'clamp' ) // return 0; | ||
| 273 | $self->_choose_closed($target); | ||
| 274 | return 1; | ||
| 275 | } | ||
| 276 | |||
| 277 | method _choose_closed ($index) { | ||
| 278 | return unless $option_list->choose($index); | ||
| 279 | $self->mark_changed; | ||
| 280 | $self->fire_change( $option_list->value ); | ||
| 281 | return; | ||
| 282 | } | ||
| 283 | |||
| 284 | method _handle_key_while_open ($name) { | ||
| 285 | if ( $name eq 'Escape' ) { | ||
| 286 | $self->close; | ||
| 287 | return 1; | ||
| 288 | } | ||
| 289 | if ( $name eq 'Enter' || $name eq 'Space' ) { | ||
| 290 | defined $highlighted ? $self->choose($highlighted) : $self->close; | ||
| 291 | return 1; | ||
| 292 | } | ||
| 293 | my $target = roving_target( [ $option_list->enabled_indexes ], $highlighted, $name, keys => 'list', policy => 'clamp', page => $list->visible_rows ) // return 0; | ||
| 294 | $self->highlight($target); | ||
| 295 | return 1; | ||
| 296 | } | ||
| 297 | |||
| 298 | # Typing finds the next option whose label starts with the typed text; | ||
| 299 | # characters typed within a second of each other form one search. | ||
| 300 | # Disabled options are skipped. | ||
| 301 | method _type_ahead ($character) { | ||
| 302 | my @options = $option_list->options; | ||
| 303 | return 0 unless @options; | ||
| 304 | my $now = time; | ||
| 305 | $typed = $now - $typed_at <= TYPEAHEAD_SECONDS ? $typed . $character : $character; | ||
| 306 | $typed_at = $now; | ||
| 307 | |||
| 308 | my $current = $self->is_open ? $highlighted // -1 : $option_list->selected_index // -1; | ||
| 309 | my $start = length $typed > 1 ? $current : $current + 1; | ||
| 310 | my $wanted = fc $typed; | ||
| 311 | my ($match) = grep { !$options[$_]{disabled} && fc( substr( $options[$_]{label}, 0, length $typed ) ) eq $wanted } map { ( $start + $_ ) % @options } 0 .. $#options; | ||
| 312 | return 1 unless defined $match; | ||
| 313 | |||
| 314 | $self->is_open ? $self->highlight($match) : $self->_choose_closed($match); | ||
| 315 | return 1; | ||
| 316 | } | ||
| 317 | |||
| 318 | # --------------------------------------------------------------------- | ||
| 319 | # Painting | ||
| 320 | # --------------------------------------------------------------------- | ||
| 321 | |||
| 322 | method natural_size () { | ||
| 323 | my $widest = max 0, map { string_columns( $_->{label} ) } $option_list->options; | ||
| 324 | return ( max( $widest, string_columns($placeholder) ) + 2, 1 ); | ||
| 325 | } | ||
| 326 | |||
| 327 | method paint () { | ||
| 328 | my $bg = $self->paint_focus_background; | ||
| 329 | my $label = $self->selected_label; | ||
| 330 | my $fg = defined $label ? $self->foreground_attr : $self->color_attr( $self->placeholder_color ); | ||
| 331 | $self->paint_text( 0, 0, $label // $placeholder, $fg, $bg, max( 0, $self->columns - 2 ) ); | ||
| 332 | $self->put_attrs( $self->columns - 1, 0, $opens_upwards && $self->is_open ? ARROW_UP : ARROW_DOWN, $self->accent_attr, $bg ); | ||
| 333 | return; | ||
| 334 | } | ||
| 335 | |||
| 336 | # The attributes of an option's row in the list: | ||
| 337 | # ( $fg, $bg ) for the highlighted, the selected and the other options. | ||
| 338 | # The highlighted option sits on the theme's dropdown.highlight.background, | ||
| 339 | # or on the accent the program gave. | ||
| 340 | method _highlight_background_attr () { | ||
| 341 | return $self->accent_attr if $self->has_look_override('accent_color'); | ||
| 342 | return $self->color_attr( $self->look('highlight.background') ); | ||
| 343 | } | ||
| 344 | |||
| 345 | method option_attrs ($index) { | ||
| 346 | my $selected = $option_list->selected_index; | ||
| 347 | return ( $self->color_attr( $self->highlight_text_color ), $self->_highlight_background_attr ) if defined $highlighted && $index == $highlighted; | ||
| 348 | return ( $self->color_attr( $self->disabled_color ), undef ) if $option_list->is_disabled($index); | ||
| 349 | return ( $self->accent_attr, undef ) if defined $selected && $index == $selected; | ||
| 350 | return ( $self->foreground_attr, undef ); | ||
| 351 | } | ||
| 352 | |||
| 353 | method option_label ($index) { | ||
| 354 | return $option_list->label($index); | ||
| 355 | } | ||
| 356 | |||
| 357 | method option_count () { | ||
| 358 | return $option_list->count; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | 1; | ||
| 363 | |||
| 364 | __END__ |