Menu.pm

filter: Statementstmt 75.0% 6 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 81.8% 9 / 11 CC 1 SCAR 0.9
CC
1
Cov
75%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Menu/itembase.pm ↑ Summary Menu/base.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2012-2022 -- leonerd@leonerd.org.uk
5  
6 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Object::Pad 0.66;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Tickit::Widget::Menu 0.16;
10 class Tickit::Widget::Menu
11 :strict(params)
12 # Much of this code actually lives in a class called T:W:Menu::base, which is
13 # the base class used by T:W:Menu and T:W:MenuBar
14 0 ▶ :isa(Tickit::Widget::Menu::base);
Subroutine BEGIN: called CC=1 SCAR=2.6
15  
16 use Tickit::Window 0.49; # hierarchy changes are asynchronous
17  
18 use Tickit::Widget::Menu::Item;
19 use Tickit::Style;
20  
21 use Tickit::RenderBuffer qw( LINE_SINGLE );
22 use List::Util qw( max min );
23  
24 # Re-import the constant for compiletime use
25 use constant separator => __PACKAGE__->separator;
26  
27 =head1 NAME
28  
29 C<Tickit::Widget::Menu> - display a menu of choices
30  
31 =head1 SYNOPSIS
32  
33 use Tickit;
34 use Tickit::Widget::Menu;
35  
36 my $tickit = Tickit->new;
37  
38 my $menu = Tickit::Widget::Menu->new(
39 items => [
40 Tickit::Widget::Menu::Item->new(
41 name => "Exit",
42 on_activate => sub { $tickit->stop }
43 ),
44 ],
45 );
46  
47 $menu->popup( $tickit->rootwin, 5, 5 );
48  
49 $tickit->run;
50  
51 =head1 DESCRIPTION
52  
53 This widget class acts as a display container for a list of items representing
54 individual choices. It can be displayed as a floating window using the
55 C<popup> method, or attached to a L<Tickit::Widget::MenuBar> or as a child
56 menu within another C<Tickit::Widget::Menu>.
57  
58 This widget is intended to be displayed transiently, either as a pop-up menu
59 over some other widget, or as a child menu of another menu or an instance of
60 a menu bar. Specifically, such objects should not be directly added to
61 container widgets.
62  
63 =head1 STYLE
64  
65 The default style pen is used as the widget pen. The following style pen
66 prefixes are also used:
67  
68 =over 4
69  
70 =item highlight => PEN
71  
72 The pen used to highlight the active menu selection
73  
74 =back
75  
76 The following style actions are used:
77  
78 =over 4
79  
80 =item highlight_next (<Down>)
81  
82 =item highlight_prev (<Up>)
83  
84 Highlight the next or previous item
85  
86 =item activate (<Enter>)
87  
88 Activate the highlighted item
89  
90 =item dismiss (<Escape>)
91  
92 Dismiss the menu
93  
94 =back
95  
96 =cut
97  
98 style_definition base =>
99 rv => 1,
100 highlight_rv => 0,
101 highlight_bg => "green",
102 "<Down>" => "highlight_next",
103 "<Up>" => "highlight_prev",
104 "<Enter>" => "activate",
105 "<Escape>" => "dismiss";
106  
107 use constant KEYPRESSES_FROM_STYLE => 1;
108 use constant WIDGET_PEN_FROM_STYLE => 1;
109  
110 # These methods come from T:W:Menu::base but better to document them here so
111 # the reader can find them
112  
113 =head1 CONSTRUCTOR
114  
115 =head2 new
116  
117 $menu = Tickit::Widget::Menu->new( %args )
118  
119 Constructs a new C<Tickit::Widget::Menu> object.
120  
121 Takes the following named arguments:
122  
123 =over 8
124  
125 =item name => STRING
126  
127 Optional. If present, gives the name of the menu item for a submenu. Not used
128 in a top-level menu.
129  
130 =item items => ARRAY
131  
132 Optional. If present, contains a list of C<Tickit::Widget::Menu::Item> or
133 C<Tickit::Widget::Menu> objects to add to the menu. Equivalent to passing each
134 to the C<push_item> method after construction.
135  
136 =back
137  
138 =head2 separator
139  
140 $separator = Tickit::Window::Menu->separator
141  
142 Returns a special menu item which draws a separation line between its
143 neighbours.
144  
145 =cut
146  
147 =head1 METHODS
148  
149 =cut
150  
151 method lines ()
152 {
153 return 2 + $self->items;
154 }
155  
156 method cols ()
157 {
158 return 4 + max( map { $self->_itemwidth( $_ ) } 0 .. $self->items-1 );
159 }
160  
161 =head2 name
162  
163 $name = $menu->name
164  
165 Returns the string name for the menu.
166  
167 =head2 items
168  
169 @items = $menu->items
170  
171 Returns the list of items currently stored.
172  
173 =head2 push_item
174  
175 $menu->push_item( $item )
176  
177 Adds another item.
178  
179 Each item may either be created using L<Tickit::Window::Menu::Item>'s
180 constructor, another C<Tickit::Widget::Menu> item itself (to create a
181 submenu), or the special separator value.
182  
183 =cut
184  
185 =head2 highlight_item
186  
187 $menu->highlight_item( $idx )
188  
189 Gives the selection highlight to the item at the given index. This may be
190 called before the menu is actually displayed in order to pre-select the
191 highlight initially.
192  
193 =cut
194  
195 =head2 popup
196  
197 $menu->popup( $win, $line, $col )
198  
199 Makes the menu appear at the given position relative to the given window. Note
200 that as C<< $win->make_popup >> is called, the menu is always displayed in a
201 popup window, floating over the root window. Passed window is used simply as
202 the origin for the given line and column position.
203  
204 =cut
205  
206 method popup ( $parentwin, $line, $col )
207 {
208 my $win = $parentwin->make_popup( $line, $col, $self->lines, $self->cols );
209 $self->set_window( $win );
210 $win->show;
211 }
212  
213 =head2 dismiss
214  
215 $menu->dismiss
216  
217 Hides a menu previously displayed using C<popup>.
218  
219 =cut
220  
221 field $_supermenu;
222  
223 method set_supermenu ( $supermenu )
224 {
225 $_supermenu = $supermenu;
226 }
227  
228 method pos2item ( $line, $col )
229 {
230 $line > 0 or return ();
231 $line--;
232  
233 $col > 1 or return ();
234 $col < $self->cols - 1 or return ();
235 $col -= 2;
236  
237 my @items = $self->items;
238 $line < @items or return ();
239  
240 return ( $items[$line], $line, $col );
241 }
242  
243 method redraw_item ( $idx )
244 {
245 $self->window->expose( Tickit::Rect->new(
246 top => $idx + 1, lines => 1,
247 left => 0, cols => $self->window->cols,
248 ) );
249 }
250  
251 method render_to_rb ( $rb, $rect )
252 {
253 my $lines = $self->window->lines;
254 my $cols = $self->window->cols;
255  
256 $rb->hline_at( 0, 0, $cols-1, LINE_SINGLE );
257 $rb->hline_at( $lines-1, 0, $cols-1, LINE_SINGLE );
258 $rb->vline_at( 0, $lines-1, 0, LINE_SINGLE );
259 $rb->vline_at( 0, $lines-1, $cols-1, LINE_SINGLE );
260  
261 foreach my $line ( $rect->linerange( 1, $lines-2 ) ) {
262 my $idx = $line - 1;
263 my $item = $self->item( $idx );
264  
265 if( $item == separator ) {
266 $rb->hline_at( $line, 0, $cols-1, LINE_SINGLE );
267 }
268 else {
269 $rb->erase_at( $line, 1, 1 );
270 if( $item->isa( "Tickit::Widget::Menu" ) ) {
271 $rb->text_at( $line, $cols-2, ">" );
272 }
273 else {
274 $rb->erase_at( $line, $cols-2, 1 );
275 }
276  
277 my $pen = defined $self->_active_idx && $idx == $self->_active_idx
278 ? $self->get_style_pen( "highlight" ) : undef;
279  
280 $rb->savepen;
281 $rb->setpen( $pen ) if $pen;
282  
283 $rb->erase_at( $line, 2, $cols-4 );
284 $rb->goto( $line, 2 );
285 $item->render_label( $rb, $cols-4, $self );
286  
287 $rb->restore;
288 }
289 }
290 }
291  
292 method popup_item ( $idx )
293 {
294 my $item = $self->item( $idx );
295  
296 $item->popup( $self->window, $idx + 1, $self->window->cols );
297 }
298  
299 field $_on_activated;
300  
301 method set_on_activated ( $on_activated )
302 {
303 $_on_activated = $on_activated;
304 }
305  
306 method activated ()
307 {
308 $self->dismiss;
309  
310 $_supermenu->activated if $_supermenu;
311 $_on_activated->() if $_on_activated;
312 }
313  
314 method dismiss ()
315 {
316 if( $self->window ) {
317 $self->window->hide;
318 $self->set_window( undef );
319 }
320  
321 $self->SUPER::dismiss;
322 }
323  
324 method on_key ( $ )
325 {
326 # Eat keys if there's no supermenu to pass them to
327 return !$_supermenu;
328 }
329  
330 method on_mouse_item ( $args, $item, $item_idx, $item_col )
331 {
332 # Separators do not react to mouse
333 return 1 if $item == separator;
334  
335 my $event = $args->type;
336 if( $event eq "press" || $event eq "drag" and $args->button == 1 ) {
337 $self->expand_item( $item_idx );
338 }
339 elsif( $event eq "release" ) {
340 if( defined $self->_active_idx and $self->_active_idx == $item_idx ) {
341 $self->activate_item( $item_idx );
342 }
343 }
344  
345 return 1;
346 }
347  
348 =head1 AUTHOR
349  
350 Paul Evans <leonerd@leonerd.org.uk>
351  
352 =cut
353  
354 0x55AA;
« Menu/itembase.pm ↑ Summary Menu/base.pm »