blib/lib/Tickit/Widget/Choice.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
↑ Summary
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, 2014-2022 -- leonerd@leonerd.org.uk
5  
6 1 ▶use v5.26; # signatures
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Object::Pad 0.73 ':experimental(adjust_params init_expr)';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Tickit::Widget::Choice 0.07;
10 class Tickit::Widget::Choice
11 :strict(params)
12 0 ▶ :isa(Tickit::Widget);
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14 use Carp;
15  
16 use Tickit::Style;
17  
18 use Tickit::RenderBuffer qw( LINE_SINGLE LINE_DOUBLE CAP_START CAP_END );
19 use Tickit::Utils qw( textwidth );
20  
21 use Tickit::Widget::Menu 0.09; # ->highlight_item
22 use Tickit::Widget::Menu::Item;
23  
24 use List::Util qw( max );
25  
26 use constant WIDGET_PEN_FROM_STYLE => 1;
27 use constant CAN_FOCUS => 1;
28  
29 =head1 NAME
30  
31 C<Tickit::Widget::Choice> - a widget giving a choice from a list
32  
33 =cut
34  
35 style_definition base =>
36 border_fg => "hi-white",
37 border_linestyle => LINE_SINGLE,
38 '<Home>' => "first_choice",
39 '<Up>' => "prev_choice",
40 '<Down>' => "next_choice",
41 '<End>' => "last_choice",
42 '<Space>' => "popup";
43  
44 style_definition ':focus' =>
45 border_linestyle => LINE_DOUBLE;
46  
47 style_redraw_keys qw( border_linestyle );
48  
49 use constant KEYPRESSES_FROM_STYLE => 1;
50  
51 =head1 CONSTRUCTOR
52  
53 =cut
54  
55 =head2 new
56  
57 $choice = Tickit::Widget::Choice->new( %args )
58  
59 Constructs a new C<Tickit::Widget::Choice> object.
60  
61 Takes the following named arguments
62  
63 =over 8
64  
65 =item choices => ARRAY
66  
67 Optional. If supplied, should be an ARRAY reference containing two-element
68 ARRAY references. Each will be added to the list of choices as if by a call to
69 C<push_choice> for each element in the array.
70  
71 =item on_changed => CODE
72  
73 Optional. If supplied, used to set the initial value of the C<on_changed>
74 event handler.
75  
76 =back
77  
78 =cut
79  
80 field @_choices;
81 field $_on_changed :param = undef;
82  
83 field $_chosen;
84  
85 ADJUST :params (
86 :$choices = []
87 ) {
88 $self->push_choice( @$_ ) for $choices->@*;
89 }
90  
91 method lines () { 1 }
92  
93 method cols ()
94 {
95 return 4 + max( 1, map { textwidth $_->[1] } @_choices );
96 }
97  
98 method window_gained ( $window )
99 {
100 $self->SUPER::window_gained( $window );
101  
102 $window->cursor_at( 0, 1 );
103 }
104  
105 =head1 ACCESSORS
106  
107 =cut
108  
109 =head2 on_changed
110  
111 $on_changed = $self->on_changed
112  
113 =cut
114  
115 method on_changed () { $_on_changed }
116  
117 =head2 set_on_changed
118  
119 $self->set_on_changed( $on_changed )
120  
121 Return or set the CODE reference to be called when the chosen selection is
122 changed.
123  
124 $on_changed->( $choice, $value )
125  
126 =cut
127  
128 method set_on_changed ( $new ) { $_on_changed = $new }
129  
130 =head1 METHODS
131  
132 =cut
133  
134 =head2 push_choice
135  
136 $choice->push_choice( $value, $caption )
137  
138 Appends another choice to the list of choices, with the given value and
139 display caption.
140  
141 =cut
142  
143 method push_choice ( $value, $caption )
144 {
145 push @_choices, [ $value, $caption ];
146 $_chosen = 0 if !defined $_chosen;
147  
148 $self->resized;
149 $self->redraw;
150  
151 return $self;
152 }
153  
154 =head2 chosen_value
155  
156 $value = $choice->chosen_value
157  
158 Returns the value of the currently-chosen choice.
159  
160 =cut
161  
162 method chosen_value ()
163 {
164 return $_choices[ $_chosen ]->[0];
165 }
166  
167 =head2 choose_by_idx
168  
169 $choice->choose_by_idx( $idx )
170  
171 Moves the chosen choice to the one at the given index. If this wasn't the
172 previously-chosen one, invokes the C<on_changed> event.
173  
174 =cut
175  
176 method choose_by_idx ( $idx )
177 {
178 return if $_chosen == $idx;
179  
180 $_chosen = $idx;
181 $self->redraw;
182  
183 $_on_changed->( $self, $self->chosen_value ) if $_on_changed;
184 }
185  
186 =head2 choose_by_value
187  
188 $choice->choose_by_value( $value )
189  
190 Moves the chosen choise to the one having the given value, if such a choice
191 exists. If this wasn't the previously-chosen one, invokes the C<on_changed>
192 event.
193  
194 =cut
195  
196 method choose_by_value ( $value )
197 {
198 $_choices[$_][0] eq $value and return $self->choose_by_idx( $_ )
199 for 0 .. $#_choices;
200  
201 croak "No such choice with value '$value'";
202 }
203  
204 =head2 popup_menu
205  
206 $choice->popup_menu
207  
208 Display the popup menu in a modal float until a choice is made.
209  
210 =cut
211  
212 field $_menu;
213  
214 method popup_menu ()
215 {
216 $_menu = Tickit::Widget::Menu->new(
217 items => [ map {
218 my ( $value, $caption ) = @$_;
219 Tickit::Widget::Menu::Item->new(
220 name => $caption,
221 on_activate => sub {
222 undef $_menu;
223 $self->choose_by_value( $value );
224 },
225 )
226 } @_choices ],
227 );
228  
229 my $top = -1;
230 $top = 0 if $self->window->abs_top == 0;
231  
232 $_menu->popup( $self->window, $top, 0 );
233  
234 $_menu->highlight_item( $_chosen );
235 }
236  
237 method render_to_rb ( $rb, $rect )
238 {
239 my $border_pen = $self->get_style_pen( 'border' );
240 my $linestyle = $self->get_style_values( 'border_linestyle' );
241  
242 my $chosen = $_choices[ $_chosen ];
243  
244 my $right = $self->window->cols - 3;
245  
246 $rb->vline_at( 0, 0, 0, $linestyle, $border_pen, CAP_START|CAP_END );
247  
248 $rb->goto( 0, 1 );
249 $rb->text( substr( $chosen->[1], 0, $right - 1 ) );
250 $rb->erase_to( $right );
251  
252 $rb->vline_at( 0, 0, $right, $linestyle, $border_pen, CAP_START|CAP_END );
253 $rb->text_at( 0, $right+1, "-", $border_pen );
254 $rb->vline_at( 0, 0, $right+2, $linestyle, $border_pen, CAP_START|CAP_END );
255 }
256  
257 method key_first_choice ($) { $self->choose_by_idx( 0 ); 1 }
258 method key_last_choice ($) { $self->choose_by_idx( $#_choices ); 1 }
259  
260 method key_next_choice ($) { $self->choose_by_idx( $_chosen+1 ) if $_chosen < $#_choices; 1 }
261 method key_prev_choice ($) { $self->choose_by_idx( $_chosen-1 ) if $_chosen > 0; 1 }
262  
263 method key_popup ($) { $self->popup_menu; 1 }
264  
265 method on_mouse ( $ev )
266 {
267 return unless $ev->type eq "press" and $ev->button == 1;
268  
269 my $win = $self->window;
270 my $right = $win->cols - 3;
271  
272 my $col = $ev->col;
273 if( $ev->line == 0 and ( $col > 1 && $col < $right or $col == $right + 1 ) ) {
274 $self->popup_menu;
275 }
276  
277 return 1;
278 }
279  
280 =head1 TODO
281  
282 =over 4
283  
284 =item *
285  
286 Render a full border around the widget if height is at least 3.
287  
288 =back
289  
290 =cut
291  
292 =head1 AUTHOR
293  
294 Paul Evans <leonerd@leonerd.org.uk>
295  
296 =cut
297  
298 0x55AA;
↑ Summary