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