| 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.70 ':experimental(adjust_params)'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | package Tickit::Widget::Menu::base 0.16; | ||
| 10 | class Tickit::Widget::Menu::base | ||
| 11 | :strict(params) | ||
| 12 | :isa(Tickit::Widget) | ||
| 13 | 0 | ▶ | :does(Tickit::Widget::Menu::itembase); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | use Carp; | ||
| 16 | |||
| 17 | use Tickit::Utils qw( textwidth ); | ||
| 18 | |||
| 19 | use constant separator => []; | ||
| 20 | |||
| 21 | # foreach my $method (qw( pos2item on_mouse_item redraw_item popup_item activated )) { | ||
| 22 | # $class->can( $method ) or | ||
| 23 | # croak "$class cannot ->$method - do you subclass and implement it?"; | ||
| 24 | # } | ||
| 25 | |||
| 26 | field @_items; | ||
| 27 | field @_itemwidths; | ||
| 28 | |||
| 29 | field $_active_idx; # index of keyboard-selected highlight | ||
| 30 | |||
| 31 | ADJUST :params ( | ||
| 32 | :$items = undef | ||
| 33 | ) { | ||
| 34 | if( $items ) { | ||
| 35 | $self->push_item( $_ ) for $items->@*; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | method _active_idx () | ||
| 40 | { | ||
| 41 | return $_active_idx; | ||
| 42 | } | ||
| 43 | |||
| 44 | method items () | ||
| 45 | { | ||
| 46 | return @_items; | ||
| 47 | } | ||
| 48 | |||
| 49 | method item ( $idx ) | ||
| 50 | { | ||
| 51 | return $_items[$idx]; | ||
| 52 | } | ||
| 53 | |||
| 54 | method _itemwidth ( $idx ) | ||
| 55 | { | ||
| 56 | return $_itemwidths[$idx]; | ||
| 57 | } | ||
| 58 | |||
| 59 | method push_item ( $item ) | ||
| 60 | { | ||
| 61 | push @_items, $item; | ||
| 62 | push @_itemwidths, $item == separator ? 0 : textwidth $item->name; | ||
| 63 | } | ||
| 64 | |||
| 65 | method highlight_item ( $idx ) | ||
| 66 | { | ||
| 67 | return if defined $_active_idx and $idx == $_active_idx; | ||
| 68 | |||
| 69 | my $have_window = defined $self->window; | ||
| 70 | |||
| 71 | if( defined( my $old_idx = $_active_idx ) ) { | ||
| 72 | undef $_active_idx; | ||
| 73 | my $old_item = $_items[$old_idx]; | ||
| 74 | if( $old_item->isa( "Tickit::Widget::Menu" ) ) { | ||
| 75 | $old_item->dismiss; | ||
| 76 | } | ||
| 77 | $self->redraw_item( $old_idx ) if $have_window; | ||
| 78 | } | ||
| 79 | |||
| 80 | $_active_idx = $idx; | ||
| 81 | $self->redraw_item( $idx ) if $have_window; | ||
| 82 | } | ||
| 83 | |||
| 84 | method expand_item ( $idx ) | ||
| 85 | { | ||
| 86 | $self->highlight_item( $idx ); | ||
| 87 | |||
| 88 | my $item = $_items[$idx]; | ||
| 89 | if( $item->isa( "Tickit::Widget::Menu" ) ) { | ||
| 90 | $self->popup_item( $idx ); | ||
| 91 | $item->set_supermenu( $self ); | ||
| 92 | } | ||
| 93 | # else don't bother expanding non-menus | ||
| 94 | } | ||
| 95 | |||
| 96 | method activate_item ( $idx ) | ||
| 97 | { | ||
| 98 | my $item = $_items[$idx]; | ||
| 99 | if( $item->isa( "Tickit::Widget::Menu" ) ) { | ||
| 100 | $self->expand_item( $idx ); | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | $self->activated; | ||
| 104 | $item->activate; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | method dismiss () | ||
| 109 | { | ||
| 110 | if( defined $_active_idx ) { | ||
| 111 | my $item = $_items[$_active_idx]; | ||
| 112 | $item->dismiss if $item->isa( "Tickit::Widget::Menu" ); | ||
| 113 | } | ||
| 114 | |||
| 115 | undef $_active_idx; | ||
| 116 | } | ||
| 117 | |||
| 118 | method key_highlight_next ( $ ) | ||
| 119 | { | ||
| 120 | my $idx = $_active_idx; | ||
| 121 | |||
| 122 | if( defined $idx ) { | ||
| 123 | $idx++, $idx %= @_items; | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | $idx = 0; | ||
| 127 | } | ||
| 128 | |||
| 129 | $idx++, $idx %= @_items while $_items[$idx] == separator; | ||
| 130 | |||
| 131 | $self->highlight_item( $idx ); | ||
| 132 | |||
| 133 | return 1; | ||
| 134 | } | ||
| 135 | |||
| 136 | method key_highlight_prev ( $ ) | ||
| 137 | { | ||
| 138 | my $idx = $_active_idx; | ||
| 139 | |||
| 140 | if( defined $idx ) { | ||
| 141 | $idx--, $idx %= @_items; | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | $idx = $#_items; | ||
| 145 | } | ||
| 146 | |||
| 147 | $idx--, $idx %= @_items while $_items[$idx] == separator; | ||
| 148 | |||
| 149 | $self->highlight_item( $idx ); | ||
| 150 | |||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | |||
| 154 | method key_dismiss ( $ ) | ||
| 155 | { | ||
| 156 | $self->dismiss; | ||
| 157 | |||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | |||
| 161 | method key_activate ( $ ) | ||
| 162 | { | ||
| 163 | if( defined( my $idx = $_active_idx ) ) { | ||
| 164 | $self->activate_item( $idx ); | ||
| 165 | } | ||
| 166 | |||
| 167 | return 1; | ||
| 168 | } | ||
| 169 | |||
| 170 | method on_mouse ( $args ) | ||
| 171 | { | ||
| 172 | my $line = $args->line; | ||
| 173 | my $col = $args->col; | ||
| 174 | |||
| 175 | if( $line < 0 or $line >= $self->window->lines or | ||
| 176 | $col < 0 or $col >= $self->window->cols ) { | ||
| 177 | $self->dismiss, return 0 if $args->type eq "press"; | ||
| 178 | return 0; | ||
| 179 | } | ||
| 180 | |||
| 181 | my ( $item, $item_idx, $item_col ) = $self->pos2item( $line, $col ); | ||
| 182 | $item or return 1; | ||
| 183 | |||
| 184 | $self->on_mouse_item( $args, $item, $item_idx, $item_col ); | ||
| 185 | } | ||
| 186 | |||
| 187 | 0x55AA; | ||