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