| 1 | package Term::Fabulous::Widget::MenuBar; | ||
| 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.05'; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::MenuBar | ||
| 15 | :isa(Term::Fabulous::Widget::Input) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use Clay::UI::Enum::Result; | ||
| 19 | use Clay::XS qw(sizing_grow); | ||
| 20 | use List::Util qw(first sum0); | ||
| 21 | use Scalar::Util qw(blessed refaddr weaken); | ||
| 22 | use Term::Fabulous::Check qw(describe non_negative_integer string); | ||
| 23 | use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT TB_MOD_MOTION TB_UNDERLINE); | ||
| 24 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 25 | use Term::Fabulous::Widget::Menu; | ||
| 26 | |||
| 27 | use constant GAP => 2; # columns between two titles | ||
| 28 | |||
| 29 | field $commands :param = undef; | ||
| 30 | field $caption :param = ''; | ||
| 31 | |||
| 32 | field @_menus; # [ { title, key, menu } ], in order | ||
| 33 | field $_open; # the index of the open menu, or undef | ||
| 34 | field $_focus_before; # the widget that had the focus when the bar opened | ||
| 35 | |||
| 36 | ADJUST :params ( :$menus ) { | ||
| 37 | die ref($self) . ": commands must be a Term::Fabulous::Commands, got " . describe($commands) if defined $commands && !( blessed $commands && $commands->isa('Term::Fabulous::Commands') ); | ||
| 38 | $caption = string( $self, caption => $caption ); | ||
| 39 | @_menus = $self->_parsed_menus($menus); | ||
| 40 | $self->can_focus(0); | ||
| 41 | |||
| 42 | # A choice bubbles from the open menu through the bar, which closes | ||
| 43 | # there, and goes on to the bar's ancestors. A listener on the menu | ||
| 44 | # that stops it leaves the closing to menu_chosen. | ||
| 45 | $self->on( Choose => sub ( $self, $event ) { $self->menu_chosen( $event->target ); return Clay::UI::Enum::Result->CONTINUE }, owner => $self ); | ||
| 46 | $self->on( | ||
| 47 | MouseMove => sub ( $self, $event ) { | ||
| 48 | $self->_pointer_over($event) if refaddr( $event->target ) == refaddr($self); | ||
| 49 | return Clay::UI::Enum::Result->CONTINUE; | ||
| 50 | }, | ||
| 51 | owner => $self | ||
| 52 | ); | ||
| 53 | } | ||
| 54 | |||
| 55 | method theme_family :common () { | ||
| 56 | return 'menu_bar'; | ||
| 57 | } | ||
| 58 | |||
| 59 | method themed_params :common () { | ||
| 60 | return ( | ||
| 61 | $class->SUPER::themed_params, | ||
| 62 | caption_color => [ 'caption', 'normal', 'cell_color' ], | ||
| 63 | highlight_text_color => [ 'highlight.text', 'normal', 'cell_color' ], | ||
| 64 | highlight_background_color => [ 'highlight.background', 'normal', 'cell_color' ], | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | # The menus: a title, its mnemonic letter (the first letter of the | ||
| 69 | # title unless given; it must be in the title), and the items of its | ||
| 70 | # menu. Two menus with one letter, and a letter whose Alt key runs a | ||
| 71 | # command, die. | ||
| 72 | method _parsed_menus ($menus) { | ||
| 73 | die ref($self) . ": menus must be a non-empty array reference, got " . describe($menus) unless ref $menus eq 'ARRAY' && @$menus; | ||
| 74 | my ( @parsed, %title_of_key ); | ||
| 75 | foreach my $definition (@$menus) { | ||
| 76 | die ref($self) . ": a menu must be a hash reference with a title and items, got " . describe($definition) unless ref $definition eq 'HASH'; | ||
| 77 | my %rest = %$definition; | ||
| 78 | my $title = string( $self, 'the title of a menu' => delete $rest{title} ); | ||
| 79 | die ref($self) . ": the title of a menu must not be empty" unless length $title; | ||
| 80 | my $key = lc string( $self, "the key of the menu '$title'" => delete $rest{key} // substr( $title, 0, 1 ) ); | ||
| 81 | die ref($self) . ": the key of the menu '$title' must be one letter or digit of its title, got '$key'" unless $key =~ /\A[[:alnum:]]\z/ && index( lc $title, $key ) >= 0; | ||
| 82 | die ref($self) . ": the menus '$title_of_key{$key}' and '$title' have the same key '$key'; give one of them another key" if exists $title_of_key{$key}; | ||
| 83 | $self->_check_free_key( $title, $key ) if defined $commands; | ||
| 84 | my $items = delete $rest{items}; | ||
| 85 | die ref($self) . ": the menu '$title' has unknown keys: " . join( ', ', sort keys %rest ) if %rest; | ||
| 86 | my $menu = Term::Fabulous::Widget::Menu->new( items => $items // [], commands => $commands ); | ||
| 87 | $menu->can_focus(0); | ||
| 88 | $title_of_key{$key} = $title; | ||
| 89 | push @parsed, { title => $title, key => $key, menu => $menu }; | ||
| 90 | } | ||
| 91 | return @parsed; | ||
| 92 | } | ||
| 93 | |||
| 94 | # Alt and the letter open the menu, with Shift or without, so neither | ||
| 95 | # may run a command. | ||
| 96 | method _check_free_key ( $title, $key ) { | ||
| 97 | foreach my $name ( 'Alt+' . lc $key, 'Alt+' . uc $key ) { | ||
| 98 | my $command = $commands->command_for_key($name) // next; | ||
| 99 | die ref($self) . ": the key $name of the menu '$title' runs the command '" . $command->id . "'"; | ||
| 100 | } | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | method caption (@new) { | ||
| 105 | return $caption unless @new; | ||
| 106 | $caption = string( $self, caption => $new[0] ); | ||
| 107 | $self->mark_changed; | ||
| 108 | return $caption; | ||
| 109 | } | ||
| 110 | |||
| 111 | method caption_color (@new) { return @new ? $self->set_look( caption_color => $new[0] ) : $self->look_value('caption_color') } | ||
| 112 | method highlight_text_color (@new) { return @new ? $self->set_look( highlight_text_color => $new[0] ) : $self->look_value('highlight_text_color') } | ||
| 113 | method highlight_background_color (@new) { return @new ? $self->set_look( highlight_background_color => $new[0] ) : $self->look_value('highlight_background_color') } | ||
| 114 | |||
| 115 | method titles () { | ||
| 116 | return map { $_->{title} } @_menus; | ||
| 117 | } | ||
| 118 | |||
| 119 | method menu ($index) { | ||
| 120 | $index = $self->_checked_index($index); | ||
| 121 | return $_menus[$index]{menu}; | ||
| 122 | } | ||
| 123 | |||
| 124 | method _checked_index ($index) { | ||
| 125 | $index = non_negative_integer( $self, index => $index ); | ||
| 126 | die ref($self) . ": there is no menu $index (the bar has " . scalar(@_menus) . ")" if $index > $#_menus; | ||
| 127 | return $index; | ||
| 128 | } | ||
| 129 | |||
| 130 | # The menu whose mnemonic is a letter (Alt+F opens File), or undef. | ||
| 131 | method menu_for_key ($letter) { | ||
| 132 | return undef unless defined $letter && !ref $letter; | ||
| 133 | return first { $_menus[$_]{key} eq lc $letter } 0 .. $#_menus; | ||
| 134 | } | ||
| 135 | |||
| 136 | # A press on the bar opens a menu without taking the focus from the | ||
| 137 | # widget the user was in (see Term::Fabulous, KEYBOARD AND FOCUS). | ||
| 138 | method keeps_focus_on_click () { | ||
| 139 | return 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | # --------------------------------------------------------------------- | ||
| 143 | # Opening and closing | ||
| 144 | # --------------------------------------------------------------------- | ||
| 145 | |||
| 146 | method open_menu () { | ||
| 147 | return $_open; | ||
| 148 | } | ||
| 149 | |||
| 150 | method is_open () { | ||
| 151 | return defined $_open ? 1 : 0; | ||
| 152 | } | ||
| 153 | |||
| 154 | # An input's value: the index of the open menu. | ||
| 155 | method value () { | ||
| 156 | return $_open; | ||
| 157 | } | ||
| 158 | |||
| 159 | # The column a menu's title starts at, its padding included. | ||
| 160 | method _title_column ($index) { | ||
| 161 | return sum0( map { string_columns( $_menus[$_]{title} ) + GAP } 0 .. $index - 1 ); | ||
| 162 | } | ||
| 163 | |||
| 164 | # The menu whose title covers a column, or undef. | ||
| 165 | method _menu_at ($column) { | ||
| 166 | return first { | ||
| 167 | my $start = $self->_title_column($_); | ||
| 168 | $column >= $start && $column < $start + string_columns( $_menus[$_]{title} ) + GAP; | ||
| 169 | } 0 .. $#_menus; | ||
| 170 | } | ||
| 171 | |||
| 172 | # Opens a menu below its title, or switches to it while another one is | ||
| 173 | # open. The bar takes the focus and hands the keys to the menu. | ||
| 174 | method open ($index) { | ||
| 175 | $index = $self->_checked_index($index); | ||
| 176 | my $ui = $self->ui // die ref($self) . ": a menu bar opens its menus only while it is in a Term::Fabulous UI"; | ||
| 177 | die ref($self) . ": a disabled menu bar cannot open its menus" unless $self->is_enabled; | ||
| 178 | return $self if defined $_open && $_open == $index; | ||
| 179 | if ( !defined $_open ) { | ||
| 180 | my $focused = $self->_focus_to_give_back($ui); | ||
| 181 | $_focus_before = defined $focused && !$self->contains($focused) ? $focused : undef; | ||
| 182 | weaken $_focus_before if defined $_focus_before; | ||
| 183 | } | ||
| 184 | my $previous = defined $_open ? $_menus[$_open]{menu} : undef; | ||
| 185 | $_open = $index; | ||
| 186 | $self->remove_child($previous) if defined $previous; | ||
| 187 | my $menu = $_menus[$index]{menu}; | ||
| 188 | $menu->show_below( $self, $self->_title_column($index) ); | ||
| 189 | $self->add_child($menu); | ||
| 190 | $self->can_focus(1); | ||
| 191 | $ui->interaction->set_focused_widget($self) unless $self->is_focused; | ||
| 192 | $self->mark_changed; | ||
| 193 | return $self; | ||
| 194 | } | ||
| 195 | |||
| 196 | # The widget the bar gives the focus back to when it closes. A click | ||
| 197 | # on a title keeps the focus where it is, which may be a context menu: | ||
| 198 | # that one closes first and gives the focus back to its own widget. | ||
| 199 | method _focus_to_give_back ($ui) { | ||
| 200 | my $focused = $ui->interaction->get_focused_widget; | ||
| 201 | return $focused unless blessed $focused && $focused->isa('Term::Fabulous::Widget::Menu') && $focused->is_open; | ||
| 202 | $focused->close; | ||
| 203 | return $ui->interaction->get_focused_widget; | ||
| 204 | } | ||
| 205 | |||
| 206 | # Closes the open menu. The focus goes back to the widget that had it | ||
| 207 | # when the bar opened, unless it went elsewhere already. The bar gives | ||
| 208 | # it back itself: the bar holds the focus and stays in the tree, so | ||
| 209 | # no focus heir is asked when its menu leaves. | ||
| 210 | method close () { | ||
| 211 | return $self unless defined $_open; | ||
| 212 | my $menu = $_menus[$_open]{menu}; | ||
| 213 | my $ui = $self->ui; | ||
| 214 | my $give_back = $_focus_before; | ||
| 215 | my $had_focus = $self->is_focused; | ||
| 216 | ( $_open, $_focus_before ) = ( undef, undef ); | ||
| 217 | $self->remove_child($menu); | ||
| 218 | if ( defined $ui && $had_focus ) { | ||
| 219 | $give_back = undef if defined $give_back && !$ui->interaction->can_take_focus($give_back); | ||
| 220 | $ui->interaction->set_focused_widget($give_back); | ||
| 221 | } | ||
| 222 | $self->can_focus(0); | ||
| 223 | $self->mark_changed; | ||
| 224 | return $self; | ||
| 225 | } | ||
| 226 | |||
| 227 | method focus_changed :override ($is_focused) { | ||
| 228 | $self->close if !$is_focused && defined $_open; | ||
| 229 | return; | ||
| 230 | } | ||
| 231 | |||
| 232 | # While a menu is open, Tab and Shift+Tab switch menus. | ||
| 233 | method captures_tab () { | ||
| 234 | return $self->is_open; | ||
| 235 | } | ||
| 236 | |||
| 237 | # A menu of the bar chose an item: the bar closes before the command | ||
| 238 | # runs. | ||
| 239 | method menu_chosen ($menu) { | ||
| 240 | $self->close if defined $_open && refaddr( $_menus[$_open]{menu} ) == refaddr($menu); | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | |||
| 244 | # --------------------------------------------------------------------- | ||
| 245 | # Keys and the mouse | ||
| 246 | # --------------------------------------------------------------------- | ||
| 247 | |||
| 248 | # F10 opens the first menu, Alt and a menu's letter that menu; returns | ||
| 249 | # whether the key opened one. | ||
| 250 | method handle_shortcut ($event) { | ||
| 251 | return 0 unless $self->is_enabled; | ||
| 252 | my $name = $event->key_name // return 0; | ||
| 253 | if ( $name eq 'F10' ) { | ||
| 254 | $self->open(0); | ||
| 255 | return 1; | ||
| 256 | } | ||
| 257 | my ($letter) = $name =~ /\AAlt\+([[:alnum:]])\z/ or return 0; | ||
| 258 | my $index = $self->menu_for_key($letter) // return 0; | ||
| 259 | $self->open($index); | ||
| 260 | return 1; | ||
| 261 | } | ||
| 262 | |||
| 263 | # Opens menus for the keys that reach a widget, usually the root. | ||
| 264 | method listen ($widget) { | ||
| 265 | die ref($self) . ": listen needs a widget that fires events, got " . describe($widget) unless blessed $widget && $widget->can('on'); | ||
| 266 | $widget->on( KeyPress => sub ( $self, $event ) { return $self->handle_shortcut($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE }, owner => $self ); | ||
| 267 | return $self; | ||
| 268 | } | ||
| 269 | |||
| 270 | # While a menu is open, the bar has the focus and gets every key: the | ||
| 271 | # menu uses its own, the bar switches and closes menus, keys with Ctrl | ||
| 272 | # or Alt close the bar and go on, and the rest are kept. | ||
| 273 | method handle_key ($event) { | ||
| 274 | return 0 unless defined $_open; | ||
| 275 | return 1 if $_menus[$_open]{menu}->handle_key($event); | ||
| 276 | my $name = $event->key_name // ''; | ||
| 277 | my %step = ( Left => -1, BackTab => -1, Right => 1, Tab => 1 ); | ||
| 278 | if ( exists $step{$name} ) { | ||
| 279 | $self->open( ( $_open + $step{$name} ) % @_menus ); | ||
| 280 | return 1; | ||
| 281 | } | ||
| 282 | if ( $name eq 'Escape' || $name eq 'F10' ) { | ||
| 283 | $self->close; | ||
| 284 | return 1; | ||
| 285 | } | ||
| 286 | if ( my ($letter) = $name =~ /\AAlt\+([[:alnum:]])\z/ ) { | ||
| 287 | my $index = $self->menu_for_key($letter); | ||
| 288 | if ( defined $index ) { | ||
| 289 | $self->open($index); | ||
| 290 | return 1; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | return 1 unless $name =~ /\A(?:Ctrl|Alt)\+/; | ||
| 294 | $self->close; | ||
| 295 | return 0; | ||
| 296 | } | ||
| 297 | |||
| 298 | # A press on a title opens its menu, or closes it when it is open. | ||
| 299 | method handle_mouse ($event) { | ||
| 300 | return 0 unless refaddr( $event->target ) == refaddr($self); | ||
| 301 | return 0 unless $event->key == TB_KEY_MOUSE_LEFT && !( $event->modifiers & TB_MOD_MOTION ); | ||
| 302 | my ($column) = $self->cell_at($event); | ||
| 303 | my $index = defined $column ? $self->_menu_at($column) : undef; | ||
| 304 | return 1 unless defined $index; | ||
| 305 | defined $_open && $_open == $index ? $self->close : $self->open($index); | ||
| 306 | return 1; | ||
| 307 | } | ||
| 308 | |||
| 309 | # While a menu is open, the pointer over another title opens that one. | ||
| 310 | method _pointer_over ($event) { | ||
| 311 | return unless defined $_open; | ||
| 312 | my ($column) = $self->cell_at($event); | ||
| 313 | my $index = defined $column ? $self->_menu_at($column) : undef; | ||
| 314 | $self->open($index) if defined $index && $index != $_open; | ||
| 315 | return; | ||
| 316 | } | ||
| 317 | |||
| 318 | # A click is handled by handle_mouse, on the press. | ||
| 319 | method activate :override () { | ||
| 320 | return; | ||
| 321 | } | ||
| 322 | |||
| 323 | # --------------------------------------------------------------------- | ||
| 324 | # Painting | ||
| 325 | # --------------------------------------------------------------------- | ||
| 326 | |||
| 327 | method natural_size () { | ||
| 328 | return ( sizing_grow( $self->_title_column( scalar @_menus ) ), 1 ); | ||
| 329 | } | ||
| 330 | |||
| 331 | method paint_key :override () { | ||
| 332 | return ( $self->SUPER::paint_key, $_open // -1, $caption ); | ||
| 333 | } | ||
| 334 | |||
| 335 | # Each title with a space on each side, the open one highlighted, its | ||
| 336 | # letter underlined; the caption at the right end when it fits. | ||
| 337 | method paint () { | ||
| 338 | my $text = $self->color_attr( $self->text_color ); | ||
| 339 | my @highlight = ( $self->color_attr( $self->highlight_text_color ), $self->color_attr( $self->highlight_background_color ) ); | ||
| 340 | foreach my $index ( 0 .. $#_menus ) { | ||
| 341 | my ( $title, $key ) = @{ $_menus[$index] }{qw(title key)}; | ||
| 342 | my ( $fg, $bg ) = defined $_open && $_open == $index ? @highlight : ( $text, undef ); | ||
| 343 | my $start = $self->_title_column($index); | ||
| 344 | $self->fill_attrs( $start, 0, string_columns($title) + GAP, ' ', $fg, $bg ) if defined $bg; | ||
| 345 | my $mark = index( lc $title, $key ); | ||
| 346 | my $x = $start + 1; | ||
| 347 | foreach my $position ( 0 .. length($title) - 1 ) { | ||
| 348 | my $glyph = substr( $title, $position, 1 ); | ||
| 349 | $self->put_attrs( $x, 0, $glyph, $position == $mark ? ( $fg // 0 ) | TB_UNDERLINE : $fg, $bg ); | ||
| 350 | $x += string_columns($glyph); | ||
| 351 | } | ||
| 352 | } | ||
| 353 | my $width = string_columns($caption); | ||
| 354 | my $free = $self->columns - $self->_title_column( scalar @_menus ); | ||
| 355 | $self->paint_text( $self->columns - $width - 1, 0, $caption, $self->color_attr( $self->caption_color ), undef ) if $width && $width + 2 <= $free; | ||
| 356 | return; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | 1; | ||
| 361 | |||
| 362 | __END__ |