blib/lib/Term/Fabulous/Widget/MenuBar.pm untested

filter: Statementstmt 0.0% 0 / 227 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 87 MC/DCmcdc 0.0% 0 / 54 Subroutinesub 0.0% 0 / 15 Podpod n/a totaltotal 0.0% 0 / 489 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
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.02';
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 weaken( my $weak_self = $self );
46 $self->on( Choose => sub ($event) { $weak_self->menu_chosen( $event->target ); return Clay::UI::Enum::Result->CONTINUE } );
47 $self->on(
48 MouseMove => sub ($event) {
49 $weak_self->_pointer_over($event) if refaddr( $event->target ) == refaddr($weak_self);
50 return Clay::UI::Enum::Result->CONTINUE;
51 }
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.
208 method close () {
209 return $self unless defined $_open;
210 my $menu = $_menus[$_open]{menu};
211 my $ui = $self->ui;
212 my $give_back = $_focus_before;
213 my $had_focus = $self->is_focused;
214 ( $_open, $_focus_before ) = ( undef, undef );
215 $self->remove_child($menu);
216 if ( defined $ui && $had_focus ) {
217 $give_back = undef if defined $give_back && !$ui->interaction->can_take_focus($give_back);
218 $ui->interaction->set_focused_widget($give_back);
219 }
220 $self->can_focus(0);
221 $self->mark_changed;
222 return $self;
223 }
224  
225 method focus_changed :override ($is_focused) {
226 $self->close if !$is_focused && defined $_open;
227 return;
228 }
229  
230 # While a menu is open, Tab and Shift+Tab switch menus.
231 method captures_tab () {
232 return $self->is_open;
233 }
234  
235 # A menu of the bar chose an item: the bar closes before the command
236 # runs.
237 method menu_chosen ($menu) {
238 $self->close if defined $_open && refaddr( $_menus[$_open]{menu} ) == refaddr($menu);
239 return;
240 }
241  
242 # ---------------------------------------------------------------------
243 # Keys and the mouse
244 # ---------------------------------------------------------------------
245  
246 # F10 opens the first menu, Alt and a menu's letter that menu; returns
247 # whether the key opened one.
248 method handle_shortcut ($event) {
249 return 0 unless $self->is_enabled;
250 my $name = $event->key_name // return 0;
251 if ( $name eq 'F10' ) {
252 $self->open(0);
253 return 1;
254 }
255 my ($letter) = $name =~ /\AAlt\+([[:alnum:]])\z/ or return 0;
256 my $index = $self->menu_for_key($letter) // return 0;
257 $self->open($index);
258 return 1;
259 }
260  
261 # Opens menus for the keys that reach a widget, usually the root.
262 method listen ($widget) {
263 die ref($self) . ": listen needs a widget that fires events, got " . describe($widget) unless blessed $widget && $widget->can('on');
264 weaken( my $weak_self = $self );
265 $widget->on( KeyPress => sub ($event) { return $weak_self && $weak_self->handle_shortcut($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE } );
266 return $self;
267 }
268  
269 # While a menu is open, the bar has the focus and gets every key: the
270 # menu uses its own, the bar switches and closes menus, keys with Ctrl
271 # or Alt close the bar and go on, and the rest are kept.
272 method handle_key ($event) {
273 return 0 unless defined $_open;
274 return 1 if $_menus[$_open]{menu}->handle_key($event);
275 my $name = $event->key_name // '';
276 my %step = ( Left => -1, BackTab => -1, Right => 1, Tab => 1 );
277 if ( exists $step{$name} ) {
278 $self->open( ( $_open + $step{$name} ) % @_menus );
279 return 1;
280 }
281 if ( $name eq 'Escape' || $name eq 'F10' ) {
282 $self->close;
283 return 1;
284 }
285 if ( my ($letter) = $name =~ /\AAlt\+([[:alnum:]])\z/ ) {
286 my $index = $self->menu_for_key($letter);
287 if ( defined $index ) {
288 $self->open($index);
289 return 1;
290 }
291 }
292 return 1 unless $name =~ /\A(?:Ctrl|Alt)\+/;
293 $self->close;
294 return 0;
295 }
296  
297 # A press on a title opens its menu, or closes it when it is open.
298 method handle_mouse ($event) {
299 return 0 unless refaddr( $event->target ) == refaddr($self);
300 return 0 unless $event->key == TB_KEY_MOUSE_LEFT && !( $event->modifiers & TB_MOD_MOTION );
301 my ($column) = $self->cell_at($event);
302 my $index = defined $column ? $self->_menu_at($column) : undef;
303 return 1 unless defined $index;
304 defined $_open && $_open == $index ? $self->close : $self->open($index);
305 return 1;
306 }
307  
308 # While a menu is open, the pointer over another title opens that one.
309 method _pointer_over ($event) {
310 return unless defined $_open;
311 my ($column) = $self->cell_at($event);
312 my $index = defined $column ? $self->_menu_at($column) : undef;
313 $self->open($index) if defined $index && $index != $_open;
314 return;
315 }
316  
317 # A click is handled by handle_mouse, on the press.
318 method activate :override () {
319 return;
320 }
321  
322 # ---------------------------------------------------------------------
323 # Painting
324 # ---------------------------------------------------------------------
325  
326 method natural_size () {
327 return ( sizing_grow( $self->_title_column( scalar @_menus ) ), 1 );
328 }
329  
330 method paint_key :override () {
331 return ( $self->SUPER::paint_key, $_open // -1, $caption );
332 }
333  
334 # Each title with a space on each side, the open one highlighted, its
335 # letter underlined; the caption at the right end when it fits.
336 method paint () {
337 my $text = $self->color_attr( $self->text_color );
338 my @highlight = ( $self->color_attr( $self->highlight_text_color ), $self->color_attr( $self->highlight_background_color ) );
339 foreach my $index ( 0 .. $#_menus ) {
340 my ( $title, $key ) = @{ $_menus[$index] }{qw(title key)};
341 my ( $fg, $bg ) = defined $_open && $_open == $index ? @highlight : ( $text, undef );
342 my $start = $self->_title_column($index);
343 $self->fill_attrs( $start, 0, string_columns($title) + GAP, ' ', $fg, $bg ) if defined $bg;
344 my $mark = index( lc $title, $key );
345 my $x = $start + 1;
346 foreach my $position ( 0 .. length($title) - 1 ) {
347 my $glyph = substr( $title, $position, 1 );
348 $self->put_attrs( $x, 0, $glyph, $position == $mark ? ( $fg // 0 ) | TB_UNDERLINE : $fg, $bg );
349 $x += string_columns($glyph);
350 }
351 }
352 my $width = string_columns($caption);
353 my $free = $self->columns - $self->_title_column( scalar @_menus );
354 $self->paint_text( $self->columns - $width - 1, 0, $caption, $self->color_attr( $self->caption_color ), undef ) if $width && $width + 2 <= $free;
355 return;
356 }
357 }
358  
359 1;
360  
361 __END__