| 1 | package Term::Fabulous::Widget::Menu; | ||
| 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::Popup; | ||
| 11 | use Term::Fabulous::Widget::Input; | ||
| 12 | |||
| 13 | our $VERSION = '0.05'; | ||
| 14 | |||
| 15 | class Term::Fabulous::Widget::Menu | ||
| 16 | :isa(Term::Fabulous::Widget::Input) | ||
| 17 | :does(Term::Fabulous::Role::Popup) | ||
| 18 | :strict(params) | ||
| 19 | { | ||
| 20 | use Clay::UI::Enum::Result; | ||
| 21 | use Clay::UI::Role::Layout::HasParent qw(is_widget); | ||
| 22 | use Clay::XS qw(CLAY_ATTACH_TO_ROOT); | ||
| 23 | use List::Util qw(first max min); | ||
| 24 | use Scalar::Util qw(blessed refaddr weaken); | ||
| 25 | use Term::Fabulous::Check qw(describe non_negative_integer); | ||
| 26 | use Term::Fabulous::Command; | ||
| 27 | use Term::Fabulous::Event::Choose; | ||
| 28 | use Term::Fabulous::Event::Close; | ||
| 29 | use Term::Fabulous::Placement qw(place_popup TOPMOST_Z_INDEX); | ||
| 30 | use Term::Fabulous::Roving qw(roving_target); | ||
| 31 | use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_RELEASE); | ||
| 32 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 33 | |||
| 34 | use constant SEPARATOR => '-'; | ||
| 35 | use constant SEPARATOR_GLYPH => "\x{2500}"; | ||
| 36 | use constant CHECK_MARK => "\x{2713}"; | ||
| 37 | use constant KEYS_GAP => 3; # columns between the longest label and the keys | ||
| 38 | |||
| 39 | # PageUp and PageDown go to the ends of a menu, as Home and End do. | ||
| 40 | my %MOVE_KEY = ( PageUp => 'Home', PageDown => 'End' ); | ||
| 41 | |||
| 42 | field $commands :param = undef; | ||
| 43 | |||
| 44 | field @_items; # Term::Fabulous::Command objects, undef for a separator | ||
| 45 | field $_highlight; # the index of the highlighted item, or undef | ||
| 46 | field $_armed = 0; # whether a mouse release may choose: a press or a move inside came first | ||
| 47 | field $_anchor; # where the menu floats: { x, y } or { below, column }, or undef | ||
| 48 | field $_is_open = 0; # opened as a popup with open | ||
| 49 | field $_focus_before; # the widget the popup gives the focus back to | ||
| 50 | |||
| 51 | ADJUST :params ( :$items = [] ) { | ||
| 52 | die ref($self) . ": commands must be a Term::Fabulous::Commands, got " . describe($commands) if defined $commands && !( blessed $commands && $commands->isa('Term::Fabulous::Commands') ); | ||
| 53 | @_items = $self->_parsed_items($items); | ||
| 54 | $_highlight = $self->_first_enabled; | ||
| 55 | $self->on( | ||
| 56 | MouseMove => sub ( $self, $event ) { | ||
| 57 | $self->_pointer_at($event) if refaddr( $event->target ) == refaddr($self); | ||
| 58 | return Clay::UI::Enum::Result->CONTINUE; | ||
| 59 | }, | ||
| 60 | owner => $self | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | method theme_family :common () { | ||
| 65 | return 'menu'; | ||
| 66 | } | ||
| 67 | |||
| 68 | method themed_params :common () { | ||
| 69 | return ( | ||
| 70 | $class->SUPER::themed_params, | ||
| 71 | keys_color => [ 'keys', 'normal', 'cell_color' ], | ||
| 72 | separator_color => [ 'separator', 'normal', 'cell_color' ], | ||
| 73 | highlight_text_color => [ 'highlight.text', 'normal', 'cell_color' ], | ||
| 74 | highlight_background_color => [ 'highlight.background', 'normal', 'cell_color' ], | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | |||
| 78 | method keys_color (@new) { return @new ? $self->set_look( keys_color => $new[0] ) : $self->look_value('keys_color') } | ||
| 79 | method separator_color (@new) { return @new ? $self->set_look( separator_color => $new[0] ) : $self->look_value('separator_color') } | ||
| 80 | method highlight_text_color (@new) { return @new ? $self->set_look( highlight_text_color => $new[0] ) : $self->look_value('highlight_text_color') } | ||
| 81 | method highlight_background_color (@new) { return @new ? $self->set_look( highlight_background_color => $new[0] ) : $self->look_value('highlight_background_color') } | ||
| 82 | |||
| 83 | # --------------------------------------------------------------------- | ||
| 84 | # Items | ||
| 85 | # --------------------------------------------------------------------- | ||
| 86 | |||
| 87 | method _parsed_items ($items) { | ||
| 88 | die ref($self) . ": items must be an array reference, got " . describe($items) unless ref $items eq 'ARRAY'; | ||
| 89 | return map { $self->_parsed_item($_) } @$items; | ||
| 90 | } | ||
| 91 | |||
| 92 | # An item is a separator, a command, the id of a command in the | ||
| 93 | # commands table, or the hash reference of a command's parameters | ||
| 94 | # (whose id defaults to its label). | ||
| 95 | method _parsed_item ($item) { | ||
| 96 | return undef if defined $item && !ref $item && $item eq SEPARATOR; | ||
| 97 | return $item if blessed $item && $item->isa('Term::Fabulous::Command'); | ||
| 98 | if ( ref $item eq 'HASH' ) { | ||
| 99 | die ref($self) . ": an item needs a label, got a hash reference without one" unless defined $item->{label}; | ||
| 100 | return Term::Fabulous::Command->new( id => $item->{label}, %$item ); | ||
| 101 | } | ||
| 102 | die ref($self) . ": an item must be a Term::Fabulous::Command, the id of a command, a hash reference or '-', got " . describe($item) if ref $item || !defined $item; | ||
| 103 | die ref($self) . ": the item '$item' is the id of a command, but the menu has no commands" unless defined $commands; | ||
| 104 | return $commands->command($item); | ||
| 105 | } | ||
| 106 | |||
| 107 | method items (@new) { | ||
| 108 | return map { $_ // SEPARATOR } @_items unless @new; | ||
| 109 | @_items = $self->_parsed_items( $new[0] ); | ||
| 110 | $_highlight = $self->_first_enabled; | ||
| 111 | $self->mark_changed; | ||
| 112 | return $self->items; | ||
| 113 | } | ||
| 114 | |||
| 115 | method _enabled_indexes () { | ||
| 116 | return grep { defined $_items[$_] && $_items[$_]->is_enabled } 0 .. $#_items; | ||
| 117 | } | ||
| 118 | |||
| 119 | method _first_enabled () { | ||
| 120 | my ($first) = $self->_enabled_indexes; | ||
| 121 | return $first; | ||
| 122 | } | ||
| 123 | |||
| 124 | method highlighted () { | ||
| 125 | return $_highlight; | ||
| 126 | } | ||
| 127 | |||
| 128 | # Highlights an enabled item; undef highlights none. | ||
| 129 | method highlight ($index) { | ||
| 130 | if ( defined $index ) { | ||
| 131 | $index = non_negative_integer( $self, index => $index ); | ||
| 132 | die ref($self) . ": there is no item $index" if $index > $#_items; | ||
| 133 | return $self unless defined $_items[$index] && $_items[$index]->is_enabled; | ||
| 134 | } | ||
| 135 | $_highlight = $index; | ||
| 136 | $self->mark_changed; | ||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | # Chooses an item as the user does: a popup closes first, then Choose | ||
| 141 | # fires, then the command runs. A separator or a disabled item does | ||
| 142 | # nothing. | ||
| 143 | method choose ($index) { | ||
| 144 | $index = non_negative_integer( $self, index => $index ); | ||
| 145 | die ref($self) . ": there is no item $index" if $index > $#_items; | ||
| 146 | my $command = $_items[$index]; | ||
| 147 | return $self unless defined $command && $command->is_enabled; | ||
| 148 | $self->close if $_is_open; | ||
| 149 | $self->fire_event( Term::Fabulous::Event::Choose->new( command => $command, index => $index ) ); | ||
| 150 | $self->_tell_owner; | ||
| 151 | $command->run; | ||
| 152 | return $self; | ||
| 153 | } | ||
| 154 | |||
| 155 | # --------------------------------------------------------------------- | ||
| 156 | # Opening and closing | ||
| 157 | # --------------------------------------------------------------------- | ||
| 158 | |||
| 159 | method is_open () { | ||
| 160 | return $_is_open; | ||
| 161 | } | ||
| 162 | |||
| 163 | method open ( $ui, %where ) { ## no critic (InputOutput::RequireBriefOpen) a method named open, not a file | ||
| 164 | die ref($self) . ": open needs the Term::Fabulous object, got " . describe($ui) unless blessed $ui && $ui->isa('Clay::UI'); | ||
| 165 | return $self if $_is_open; | ||
| 166 | die ref($self) . ": a menu that is a child of another widget cannot open" if defined $self->parent; | ||
| 167 | die ref($self) . ": a disabled menu cannot open" unless $self->is_enabled; | ||
| 168 | my $anchor = $self->_parsed_anchor(%where); | ||
| 169 | die ref($self) . ": below needs a widget that the last frame drew" | ||
| 170 | if exists $anchor->{below} && !defined $ui->bounding_box( $anchor->{below} ); | ||
| 171 | |||
| 172 | my $focused = $ui->interaction->get_focused_widget; | ||
| 173 | $_focus_before = $focused; | ||
| 174 | weaken $_focus_before if defined $_focus_before; | ||
| 175 | $self->_float_at($anchor); | ||
| 176 | $_is_open = 1; | ||
| 177 | $ui->root->add_child($self); | ||
| 178 | $ui->interaction->set_focused_widget($self); | ||
| 179 | return $self; | ||
| 180 | } | ||
| 181 | |||
| 182 | method _parsed_anchor (%where) { | ||
| 183 | if ( exists $where{below} ) { | ||
| 184 | my $widget = delete $where{below}; | ||
| 185 | die ref($self) . ": below must be a widget, got " . describe($widget) unless is_widget($widget); | ||
| 186 | die ref($self) . ": open takes x and y, or below, got " . join( ', ', sort keys %where ) if %where; | ||
| 187 | return { below => $widget, column => 0 }; | ||
| 188 | } | ||
| 189 | my @given = sort keys %where; | ||
| 190 | die ref($self) . ": open takes x and y, or below, got " . ( @given ? join( ', ', @given ) : 'neither' ) unless "@given" eq 'x y'; | ||
| 191 | return { x => non_negative_integer( $self, x => $where{x} ), y => non_negative_integer( $self, y => $where{y} ) }; | ||
| 192 | } | ||
| 193 | |||
| 194 | # Shows the menu below a widget, starting a number of columns from its | ||
| 195 | # left edge, as the list of a menu bar does. The menu does not take | ||
| 196 | # the focus: its owner, the widget, hands it the keys and hears of a | ||
| 197 | # choice through its menu_chosen method. | ||
| 198 | method show_below ( $widget, $column = 0 ) { | ||
| 199 | die ref($self) . ": show_below needs a widget, got " . describe($widget) unless blessed $widget && $widget->isa('Term::Fabulous::Widget'); | ||
| 200 | $self->_float_at( { below => $widget, column => non_negative_integer( $self, column => $column ) } ); | ||
| 201 | return $self; | ||
| 202 | } | ||
| 203 | |||
| 204 | # The owner closes before the command runs, even when a listener of | ||
| 205 | # the menu kept Choose from bubbling up to it. | ||
| 206 | method _tell_owner () { | ||
| 207 | return if $_is_open; | ||
| 208 | my $owner = defined $_anchor ? $_anchor->{below} : undef; | ||
| 209 | $owner->menu_chosen($self) if defined $owner && $owner->can('menu_chosen'); | ||
| 210 | return; | ||
| 211 | } | ||
| 212 | |||
| 213 | method _float_at ($anchor) { | ||
| 214 | $_anchor = $anchor; | ||
| 215 | $_armed = 0; | ||
| 216 | $_highlight = $self->_first_enabled; | ||
| 217 | weaken $_anchor->{below} if defined $_anchor->{below}; | ||
| 218 | $self->floating( { attach_to => CLAY_ATTACH_TO_ROOT, offset => { x => 0, y => 0 }, z_index => TOPMOST_Z_INDEX } ); | ||
| 219 | $self->mark_changed; | ||
| 220 | return; | ||
| 221 | } | ||
| 222 | |||
| 223 | # Takes a popup off the screen; Close fires. A popup that has the focus | ||
| 224 | # gives it back as it leaves, through its focus heir; a close because | ||
| 225 | # the focus went elsewhere leaves the focus there. | ||
| 226 | method close () { | ||
| 227 | return $self unless $_is_open; | ||
| 228 | $_is_open = 0; | ||
| 229 | my $holder = $self->parent; | ||
| 230 | $holder->remove_child($self) if defined $holder; | ||
| 231 | $_focus_before = undef; | ||
| 232 | $self->floating(undef); | ||
| 233 | $_anchor = undef; | ||
| 234 | $self->fire_event( Term::Fabulous::Event::Close->new ); | ||
| 235 | return $self; | ||
| 236 | } | ||
| 237 | |||
| 238 | # Who takes the focus when an open popup leaves with the focus: the | ||
| 239 | # widget that had it when the popup opened. | ||
| 240 | method focus_heir () { | ||
| 241 | return $_focus_before; | ||
| 242 | } | ||
| 243 | |||
| 244 | method focus_changed :override ($is_focused) { | ||
| 245 | $self->close if !$is_focused && $_is_open; | ||
| 246 | return; | ||
| 247 | } | ||
| 248 | |||
| 249 | # A popup keeps Tab and Shift+Tab: they move the highlight. | ||
| 250 | method captures_tab () { | ||
| 251 | return $_is_open; | ||
| 252 | } | ||
| 253 | |||
| 254 | # The size the menu takes on the screen: its content and its border. | ||
| 255 | method outer_size () { | ||
| 256 | my ( $columns, $rows ) = $self->natural_size; | ||
| 257 | my $sides = $self->bordered; | ||
| 258 | return ( $columns + $sides->{left} + $sides->{right}, $rows + $sides->{top} + $sides->{bottom} ); | ||
| 259 | } | ||
| 260 | |||
| 261 | method contribute_floating :override ($config) { | ||
| 262 | $self->SUPER::contribute_floating($config); | ||
| 263 | $self->float_popup($config) if defined $_anchor && defined $config->{floating}; | ||
| 264 | return; | ||
| 265 | } | ||
| 266 | |||
| 267 | # Where the menu floats in this frame (Term::Fabulous::Placement, | ||
| 268 | # Term::Fabulous::Role::Popup): below its widget, or over it when | ||
| 269 | # there is no room below, or at its point; kept on the screen. | ||
| 270 | method popup_position () { | ||
| 271 | return undef unless defined $_anchor; | ||
| 272 | my $ui = $self->ui // return undef; | ||
| 273 | my $place = place_popup( | ||
| 274 | anchor => $self->_anchor_box($ui), | ||
| 275 | size => [ $self->outer_size ], | ||
| 276 | screen => [ $ui->width, $ui->height ], | ||
| 277 | prefer => exists $_anchor->{below} ? 'below' : 'at', | ||
| 278 | ); | ||
| 279 | return { x => $place->{x}, y => $place->{y} }; | ||
| 280 | } | ||
| 281 | |||
| 282 | # The box the menu opens from: its point, or its widget's (open | ||
| 283 | # checks that it was drawn; one that went away since leaves the top | ||
| 284 | # left corner) moved right by the column. | ||
| 285 | method _anchor_box ($ui) { | ||
| 286 | return { x => $_anchor->{x}, y => $_anchor->{y}, width => 0, height => 0 } unless exists $_anchor->{below}; | ||
| 287 | my $box = defined $_anchor->{below} ? $ui->bounding_box( $_anchor->{below} ) : undef; | ||
| 288 | $box //= { x => 0, y => 0, width => 0, height => 0 }; | ||
| 289 | return { %$box, x => $box->{x} + $_anchor->{column} }; | ||
| 290 | } | ||
| 291 | |||
| 292 | # --------------------------------------------------------------------- | ||
| 293 | # Keys and the mouse | ||
| 294 | # --------------------------------------------------------------------- | ||
| 295 | |||
| 296 | method handle_key ($event) { | ||
| 297 | my $name = $event->main_key_name // ''; | ||
| 298 | my $text = $event->text; | ||
| 299 | return $self->_move( $MOVE_KEY{$name} // $name ) if $name =~ /\A(?:Up|Down|Home|End|PageUp|PageDown)\z/; | ||
| 300 | if ( $name eq 'Enter' || $name eq 'Space' ) { | ||
| 301 | $self->choose($_highlight) if defined $_highlight; | ||
| 302 | return 1; | ||
| 303 | } | ||
| 304 | return $self->_find($text) || $_is_open if defined $text && $text =~ /\S/; | ||
| 305 | |||
| 306 | # Inline or in a menu bar, the other keys belong to the owner; a | ||
| 307 | # popup keeps every key while it is open. | ||
| 308 | return 0 unless $_is_open; | ||
| 309 | return $self->_move('Down') if $name eq 'Tab'; | ||
| 310 | return $self->_move('Up') if $name eq 'BackTab'; | ||
| 311 | $self->close if $name eq 'Escape'; | ||
| 312 | return 1; | ||
| 313 | } | ||
| 314 | |||
| 315 | method _move ($key) { | ||
| 316 | my $target = roving_target( [ $self->_enabled_indexes ], $_highlight, $key, keys => 'vertical' ); | ||
| 317 | $self->highlight($target) if defined $target; | ||
| 318 | return 1; | ||
| 319 | } | ||
| 320 | |||
| 321 | # A letter highlights the next enabled item whose label starts with | ||
| 322 | # it; returns whether there was one. | ||
| 323 | method _find ($letter) { | ||
| 324 | my @enabled = $self->_enabled_indexes; | ||
| 325 | my $start = $_highlight // -1; | ||
| 326 | my @order = ( ( grep { $_ > $start } @enabled ), ( grep { $_ <= $start } @enabled ) ); | ||
| 327 | my $found = first { lc( substr( $_items[$_]->label, 0, 1 ) ) eq lc $letter } @order; | ||
| 328 | return 0 unless defined $found; | ||
| 329 | $self->highlight($found); | ||
| 330 | return 1; | ||
| 331 | } | ||
| 332 | |||
| 333 | # The mouse arms the menu by pressing or dragging inside it; a release | ||
| 334 | # over an enabled item chooses it once armed. | ||
| 335 | method handle_mouse ($event) { | ||
| 336 | return 0 unless refaddr( $event->target ) == refaddr($self); | ||
| 337 | my $row = $self->_item_at($event); | ||
| 338 | if ( $event->key == TB_KEY_MOUSE_RELEASE ) { | ||
| 339 | $self->choose($row) if $_armed && defined $row; | ||
| 340 | return 1; | ||
| 341 | } | ||
| 342 | $_armed = 1; | ||
| 343 | $self->highlight($row) if defined $row; | ||
| 344 | return 1; | ||
| 345 | } | ||
| 346 | |||
| 347 | method _pointer_at ($event) { | ||
| 348 | my $row = $self->_item_at($event) // return; | ||
| 349 | $_armed = 1; | ||
| 350 | $self->highlight($row) unless defined $_highlight && $_highlight == $row; | ||
| 351 | return; | ||
| 352 | } | ||
| 353 | |||
| 354 | # The index of the item under the pointer, or undef: the menu may be | ||
| 355 | # laid out taller than its items. | ||
| 356 | method _item_at ($event) { | ||
| 357 | my ( undef, $row ) = $self->cell_at($event); | ||
| 358 | return defined $row && $row <= $#_items ? $row : undef; | ||
| 359 | } | ||
| 360 | |||
| 361 | # A click is handled by handle_mouse, on the release. | ||
| 362 | method activate :override () { | ||
| 363 | return; | ||
| 364 | } | ||
| 365 | |||
| 366 | # --------------------------------------------------------------------- | ||
| 367 | # Painting | ||
| 368 | # --------------------------------------------------------------------- | ||
| 369 | |||
| 370 | method _gutter_columns () { | ||
| 371 | return ( grep { defined && $_->is_toggle } @_items ) ? 2 : 0; | ||
| 372 | } | ||
| 373 | |||
| 374 | method _keys_columns () { | ||
| 375 | return max( 0, map { string_columns( $_->keys_text ) } grep { defined } @_items ); | ||
| 376 | } | ||
| 377 | |||
| 378 | method natural_size () { | ||
| 379 | my $labels = max( 0, map { string_columns( $_->label ) } grep { defined } @_items ); | ||
| 380 | my $keys = $self->_keys_columns; | ||
| 381 | return ( 1 + $self->_gutter_columns + $labels + ( $keys ? KEYS_GAP + $keys : 0 ) + 1, scalar @_items ); | ||
| 382 | } | ||
| 383 | |||
| 384 | method paint_key :override () { | ||
| 385 | return ( $self->SUPER::paint_key, $_highlight // -1, map { defined $_ ? ( $_->is_enabled, $_->is_checked ) : '-' } @_items ); | ||
| 386 | } | ||
| 387 | |||
| 388 | method paint () { | ||
| 389 | my $text = $self->color_attr( $self->text_color ); | ||
| 390 | my $disabled = $self->color_attr( $self->disabled_color ); | ||
| 391 | my $keys = $self->color_attr( $self->look_value('keys_color') ); | ||
| 392 | my $separator = $self->color_attr( $self->look_value('separator_color') ); | ||
| 393 | my @highlight = ( $self->color_attr( $self->look_value('highlight_text_color') ), $self->color_attr( $self->look_value('highlight_background_color') ) ); | ||
| 394 | my $gutter = $self->_gutter_columns; | ||
| 395 | foreach my $row ( 0 .. min( $#_items, $self->rows - 1 ) ) { | ||
| 396 | my $item = $_items[$row]; | ||
| 397 | if ( !defined $item ) { | ||
| 398 | $self->fill_attrs( 0, $row, $self->columns, SEPARATOR_GLYPH, $separator, undef ); | ||
| 399 | next; | ||
| 400 | } | ||
| 401 | my $is_highlighted = defined $_highlight && $_highlight == $row; | ||
| 402 | my ( $fg, $bg ) = $is_highlighted ? @highlight : ( $item->is_enabled ? $text : $disabled, undef ); | ||
| 403 | $self->fill_attrs( 0, $row, $self->columns, ' ', $fg, $bg ) if defined $bg; | ||
| 404 | $self->put_attrs( 1, $row, CHECK_MARK, $fg, $bg ) if $item->is_checked; | ||
| 405 | $self->paint_text( 1 + $gutter, $row, $item->label, $fg, $bg ); | ||
| 406 | my $key_text = $item->keys_text; | ||
| 407 | next unless length $key_text; | ||
| 408 | $self->paint_text( $self->columns - 1 - string_columns($key_text), $row, $key_text, $is_highlighted ? $fg : $keys, $bg ); | ||
| 409 | } | ||
| 410 | return; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | 1; | ||
| 415 | |||
| 416 | __END__ |