blib/lib/Tickit/WidgetRole/Movable.pm

filter: Statementstmt 16.8% 32 / 190 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 22.2% 4 / 18 Podpod 92.8% 13 / 14 totaltotal 13.9% 49 / 351 CC 57 SCAR 43.4
CC
57
Cov
10%
resize_from_corner
210.0
resize_from_edge
110.0
position_is_corner
72.0
CRAP 76.6
↑ Summary
1 package Tickit::WidgetRole::Movable;
2 # ABSTRACT: resizable/movable panel mixin for Tickit widgets
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 role Tickit::WidgetRole::Movable;
7  
8 1 our $VERSION = '0.004';
9  
10 =head1 NAME
11  
12 Tickit::WidgetRole::Movable - support for resizable/movable "panels"
13  
14 =head1 SYNOPSIS
15  
16 package Tickit::Widget::MovingThing;
17 use parent qw(Tickit::WidgetRole::Movable Tickit::Widget);
18  
19 sub lines { 2 }
20 sub cols { 2 }
21 sub render_to_rb { ... }
22  
23 =head1 DESCRIPTION
24  
25 Apply this as a parent class to a widget to provide for resize/move semantics, similar to
26 behaviour provided by common window managers.
27  
28 Expects the widget to be contained by a parent object which provides a suitable area in
29 which to resize/move the widget.
30  
31 State information is stored in the C< _movable_role > hashref in C< $self >, so this requires
32 instances to be blessed hashrefs.
33  
34 =cut
35  
36 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
37 # Resizing horizontally and/or vertically
38 1 NORTH => 1,
39 EAST => 2,
40 SOUTH => 3,
41 WEST => 4,
42  
43 # Resizing by corner
44 NORTHWEST => 5,
45 NORTHEAST => 6,
46 SOUTHWEST => 7,
47 SOUTHEAST => 8,
48 1 };
49  
50 =head2 MIN_HEIGHT
51  
52 Minimum height to apply to this widget. Default is 2.
53  
54 =cut
55  
56 1 method MIN_HEIGHT { 2 }
57  
58 =head2 MIN_WIDTH
59  
60 Minimum width to apply to this widget. Default is 2.
61  
62 =cut
63  
64 1 method MIN_WIDTH { 2 }
65  
66 =head1 METHODS
67  
68 =cut
69  
70 =head2 export_subs_for
71  
72 Empty implementation for L<Tickit::WidgetRole> C<export_subs_for>.
73  
74 =cut
75  
76 # TODO The model used by L<Tickit::WidgetRole> doesn't seem to be a good fit here. We
77 # want something closer to classical inheritance; the target widget needs to have the
78 # ability to override/wrap our methods, so we don't want to inject those as subs directly.
79 1 method export_subs_for { +{ } }
80  
81 =head2 on_mouse
82  
83 Handle mouse events.
84  
85 We can be in one of three states here: a mouse press, a drag event, or a release.
86  
87 We delegate each of these to separate methods - see:
88  
89 =over 4
90  
91 =item * L</mouse_press> - first click, this is either a one-off or the start of a drag event
92  
93 =item * L</mouse_release> - mouse has been released, either after a click or after dragging
94  
95 =item * L</mouse_drag> - one or more mouse buttons are pressed and the mouse has moved
96  
97 =back
98  
99 =cut
100  
101 0 ▶method on_mouse ($ev) {
Subroutine on_mouse: not called CC=5 SCAR=34.0
Pod documented
102 0 ▶ return $self->mouse_release($ev->line, $ev->col) if $ev->type eq 'release';
Branch: 1 branch
BranchTrueFalseTotal
if $ev->type eq "release" 0 0 0
103 0 ▶ return unless $ev->button & 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $ev->button & 1 0 0 0
104  
105 0 ▶ return $self->mouse_press($ev->line, $ev->col) if $ev->type eq 'press';
Branch: 1 branch
BranchTrueFalseTotal
if $ev->type eq "press" 0 0 0
106 0 ▶ return $self->mouse_drag($ev->line, $ev->col) if $ev->type eq 'drag';
Branch: 1 branch
BranchTrueFalseTotal
if $ev->type eq "drag" 0 0 0
107 1 }
108  
109 =head2 mouse_press
110  
111 Handle a mouse press event.
112  
113 We're either in:
114  
115 =over 4
116  
117 =item * a corner - start resizing in both directions
118  
119 =item * an edge - start resizing in one dimension
120  
121 =item * the title bar - start moving the window
122  
123 =back
124  
125 =cut
126  
127 0 ▶method mouse_press ($line, $col) {
Subroutine mouse_press: not called CC=7 SCAR=40.3
Pod documented
128 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
129  
130 # Allow extra actions before we start dealing with our defaults
131 0 ▶ if(my $code = $self->can('before_mouse_press')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->can("before_mouse_press")) 0 0 0
132 0 my $skip = $self->$code($line, $col);
133 0 ▶ return $skip if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 0 0
134 }
135  
136 0 ▶ if(my $corner = $self->position_is_corner($line, $col)) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $corner = $self->position_is_corner($line, $col)) { } 0 0 0
elsif ($self->position_is_title($line, $col)) { } 0 0 0
elsif (my $edge = $self->position_is_edge($line, $col)) { } 0 0 0
137 0 $self->start_resize_from_corner($corner);
138 } elsif($self->position_is_title($line, $col)) {
139 0 $self->start_moving($line, $col);
140 } elsif(my $edge = $self->position_is_edge($line, $col)) {
141 0 $self->start_resize_from_edge($edge);
142 }
143 0 return 1;
144 1 }
145  
146 =head2 position_is_corner
147  
148 If this location is a corner of the window, return the
149 appropriate constant (NORTHEAST, NORTHWEST, SOUTHEAST,
150 SOUTHWEST), otherwise returns false.
151  
152 =cut
153  
154 0 ▶method position_is_corner ($line, $col) {
Subroutine position_is_corner: not called CC=8 SCAR=42.8
Pod documented
155 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
156 0 ▶ if($line == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line == 0) 0 0 0
157 0 ▶ return NORTHWEST if $col == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $col == 0 0 0 0
158 0 ▶ return NORTHEAST if $col == $win->cols - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $col == $win->cols - 1 0 0 0
159 0 return 0;
160 }
161 0 ▶ return 0 unless $line == $win->lines - 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $line == $win->lines - 1 0 0 0
162 0 ▶ return SOUTHWEST if $col == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $col == 0 0 0 0
163 0 ▶ return SOUTHEAST if $col == $win->cols - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $col == $win->cols - 1 0 0 0
164 0 return 0;
165 1 }
166  
167 =head2 position_is_corner
168  
169 If this location is an edge for this window, return the
170 appropriate constant (NORTH, EAST, SOUTH, WEST), otherwise
171 returns false.
172  
173 =cut
174  
175 0 ▶method position_is_edge ($line, $col) {
Subroutine position_is_edge: not called CC=6 SCAR=37.4
Pod undocumented
176 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
177 0 ▶ return NORTH if $line == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $line == 0 0 0 0
178 0 ▶ return WEST if $col == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $col == 0 0 0 0
179 0 ▶ return SOUTH if $line == $win->lines - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $line == $win->lines - 1 0 0 0
180 0 ▶ return EAST if $col == $win->cols - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $col == $win->cols - 1 0 0 0
181 0 return 0;
182 1 }
183  
184 =head2 position_is_title
185  
186 If this location is somewhere in the title (currently defined
187 as "top row, apart from corners and close button), returns true.
188  
189 =cut
190  
191 0 ▶method position_is_title ($line, $col) {
Subroutine position_is_title: not called CC=5 SCAR=34.0
Pod documented
192 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
193 0 ▶ return 1 if $line == 0 && $col > 0 && $col < ($win->cols - 2);
Branch: 1 branch
BranchTrueFalseTotal
if $line == 0 and $col > 0 and $col < $win->cols - 2 0 0 0
Condition0%0/6
$line == 0 and $col > 0 and $col < $win->cols - 2
!ll&&!rl&&r
000
$line == 0 and $col > 0
!ll&&!rl&&r
000
MC/DC0%0/3
$line == 0 and $col > 0 and $col < $win->cols - 2
$line == 0 $col > 0 $col < $win->cols - 2
Truth table0%0/4
A and B and C
A= $line == 0B= $col > 0C= $col < $win->cols - 2
ABCresult
0XX0
10X0
1100
1111
194 0 return 0;
195 1 }
196  
197 =head2 start_resize_from_corner
198  
199 Start resizing from a corner.
200  
201 =cut
202  
203 0 ▶method start_resize_from_corner ($corner) {
Subroutine start_resize_from_corner: not called CC=2 SCAR=17.9
Pod documented
204 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
205 0 $self->{_movable_role}{mouse_action} = 'resize_from_corner';
206 0 $self->{_movable_role}{corner} = $corner;
207 $self->{_movable_role}{origin} = {
208 0 map { $_ => $win->$_ } qw(top left bottom right)
209 };
210 0 $win->set_steal_input(1);
211 1 }
212  
213 =head2 start_resize_from_edge
214  
215 Start resizing from an edge.
216  
217 =cut
218  
219 0 ▶method start_resize_from_edge ($edge) {
Subroutine start_resize_from_edge: not called CC=2 SCAR=17.9
Pod documented
220 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
221 0 $self->{_movable_role}{mouse_action} = 'resize_from_edge';
222 0 $self->{_movable_role}{edge} = $edge;
223 $self->{_movable_role}{origin} = {
224 0 map { $_ => $win->$_ } qw(top left bottom right)
225 };
226 0 $win->set_steal_input(1);
227 1 }
228  
229 =head2 start_moving
230  
231 Start moving the window.
232  
233 =cut
234  
235 0 ▶method start_moving ($line, $col) {
Subroutine start_moving: not called CC=2 SCAR=17.9
Pod documented
236 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
237 0 $self->{_movable_role}{mouse_action} = 'move';
238 $self->{_movable_role}{origin} = {
239 0 line => $line,
240 col => $col,
241 };
242 0 $win->set_steal_input(1);
243 1 }
244  
245 =head2 mouse_drag
246  
247 Deal with our drag events by changing window geometry
248 accordingly.
249  
250 =cut
251  
252 0 ▶method mouse_drag ($line, $col) {
Subroutine mouse_drag: not called CC=2 SCAR=17.9
Pod documented
253 0 ▶ if(my $action = $self->{_movable_role}{mouse_action}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $self->{'_movable_role'}{'mouse_action'}) { } 0 0 0
254 0 $self->$action($line, $col);
255 } else {
256 # Dragging one window over another is probably
257 # going to raise this warning...
258 # die "Unknown action";
259 }
260 1 }
261  
262 =head2 move
263  
264 Handle ongoing move events.
265  
266 =cut
267  
268 0 ▶method move ($line, $col) {
Subroutine move: not called CC=2 SCAR=17.9
Pod documented
269 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
270 0 my $top = $win->top + ($line - $self->{_movable_role}{origin}{line});
271 0 my $left = $win->left + ($col - $self->{_movable_role}{origin}{col});
272 0 $self->change_geometry(
273 $top,
274 $left,
275 $win->lines,
276 $win->cols,
277 );
278 1 }
279  
280 =head2 resize_from_corner
281  
282 Resize action, from a corner.
283  
284 =cut
285  
286 0 ▶method resize_from_corner ($line, $col) {
Subroutine resize_from_corner: not called CC=14 SCAR=53.5
Pod documented
287 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
288 0 ▶ if($self->{_movable_role}{corner} == SOUTHEAST) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'_movable_role'}{'corner'} == 8) { } 0 0 0
elsif ($self->{'_movable_role'}{'corner'} == 6) { } 0 0 0
elsif ($self->{'_movable_role'}{'corner'} == 5) { } 0 0 0
elsif ($self->{'_movable_role'}{'corner'} == 7) { } 0 0 0
289 0 my $lines = $line + 1;
290 0 my $cols = $col + 1;
291 0 ▶ return unless $lines >= $self->MIN_HEIGHT && $cols >= $self->MIN_WIDTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH 0 0 0
Condition0%0/3
$lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH
!ll&&!rl&&r
000
MC/DC0%0/2
$lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH
$lines >= $self->MIN_HEIGHT $cols >= $self->MIN_WIDTH
Truth table0%0/3
A and B
A= $lines >= $self->MIN_HEIGHTB= $cols >= $self->MIN_WIDTH
ABresult
0X0
100
111
292 0 $self->change_geometry(
293 $win->top,
294 $win->left,
295 $lines,
296 $cols,
297 );
298 } elsif($self->{_movable_role}{corner} == NORTHEAST) {
299 0 my $lines = $win->bottom - ($win->top + $line);
300 0 my $cols = $col + 1;
301 0 ▶ return unless $lines >= $self->MIN_HEIGHT && $cols >= $self->MIN_WIDTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH 0 0 0
Condition0%0/3
$lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH
!ll&&!rl&&r
000
MC/DC0%0/2
$lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH
$lines >= $self->MIN_HEIGHT $cols >= $self->MIN_WIDTH
Truth table0%0/3
A and B
A= $lines >= $self->MIN_HEIGHTB= $cols >= $self->MIN_WIDTH
ABresult
0X0
100
111
302 0 $self->change_geometry(
303 $win->top + $line,
304 $win->left,
305 $lines,
306 $cols,
307 );
308 } elsif($self->{_movable_role}{corner} == NORTHWEST) {
309 0 my $lines = $win->bottom - ($win->top + $line);
310 0 my $cols = $win->right - ($win->left + $col);
311 0 ▶ return unless $lines >= $self->MIN_HEIGHT && $cols >= $self->MIN_WIDTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH 0 0 0
Condition0%0/3
$lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH
!ll&&!rl&&r
000
MC/DC0%0/2
$lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH
$lines >= $self->MIN_HEIGHT $cols >= $self->MIN_WIDTH
Truth table0%0/3
A and B
A= $lines >= $self->MIN_HEIGHTB= $cols >= $self->MIN_WIDTH
ABresult
0X0
100
111
312 0 $self->change_geometry(
313 $win->top + $line,
314 $win->left + $col,
315 $lines,
316 $cols,
317 );
318 } elsif($self->{_movable_role}{corner} == SOUTHWEST) {
319 0 my $lines = $line + 1;
320 0 my $cols = $win->right - ($win->left + $col);
321 0 ▶ return unless $lines >= $self->MIN_HEIGHT && $cols >= $self->MIN_WIDTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH 0 0 0
Condition0%0/3
$lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH
!ll&&!rl&&r
000
MC/DC0%0/2
$lines >= $self->MIN_HEIGHT and $cols >= $self->MIN_WIDTH
$lines >= $self->MIN_HEIGHT $cols >= $self->MIN_WIDTH
Truth table0%0/3
A and B
A= $lines >= $self->MIN_HEIGHTB= $cols >= $self->MIN_WIDTH
ABresult
0X0
100
111
322 0 $self->change_geometry(
323 $win->top,
324 $win->left + $col,
325 $lines,
326 $cols,
327 );
328 }
329 1 }
330  
331 =head2 resize_from_edge
332  
333 Resize action - starting from an edge.
334  
335 =cut
336  
337 0 ▶method resize_from_edge ($line, $col) {
Subroutine resize_from_edge: not called CC=10 SCAR=47.0
Pod documented
338 0 ▶ my $win = $self->window or return;
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or return 0 0 0
339 0 ▶ if($self->{_movable_role}{edge} == NORTH) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'_movable_role'}{'edge'} == 1) { } 0 0 0
elsif ($self->{'_movable_role'}{'edge'} == 2) { } 0 0 0
elsif ($self->{'_movable_role'}{'edge'} == 3) { } 0 0 0
elsif ($self->{'_movable_role'}{'edge'} == 4) { } 0 0 0
340 0 my $lines = $win->bottom - ($win->top + $line);
341 0 ▶ return unless $lines >= $self->MIN_HEIGHT;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines >= $self->MIN_HEIGHT 0 0 0
342 0 $self->change_geometry(
343 $win->top + $line,
344 $win->left,
345 $lines,
346 $win->cols,
347 );
348 } elsif($self->{_movable_role}{edge} == EAST) {
349 0 my $cols = $col + 1;
350 0 ▶ return unless $cols >= $self->MIN_WIDTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols >= $self->MIN_WIDTH 0 0 0
351 0 $self->change_geometry(
352 $win->top,
353 $win->left,
354 $win->lines,
355 $cols,
356 );
357 } elsif($self->{_movable_role}{edge} == SOUTH) {
358 0 my $lines = $line + 1;
359 0 ▶ return unless $lines >= $self->MIN_HEIGHT;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines >= $self->MIN_HEIGHT 0 0 0
360 0 $self->change_geometry(
361 $win->top,
362 $win->left,
363 $line + 1,
364 $win->cols,
365 );
366 } elsif($self->{_movable_role}{edge} == WEST) {
367 0 my $cols = $win->right - ($win->left + $col),;
368 0 ▶ return unless $cols >= $self->MIN_WIDTH;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols >= $self->MIN_WIDTH 0 0 0
369 0 $self->change_geometry(
370 $win->top,
371 $win->left + $col,
372 $win->lines,
373 $cols,
374 );
375 }
376 1 }
377  
378 =head2 mouse_release
379  
380 On release make sure we hand back input to the previous handler.
381  
382 =cut
383  
384 0 ▶method mouse_release ($row, $col) {
Subroutine mouse_release: not called CC=2 SCAR=17.9
Pod documented
385 0 ▶ my $win = $self->window or die "no window?";
Branch: 1 branch
BranchTrueFalseTotal
my $win = $self->window or die "no window?" 0 0 0
386 0 $win->set_steal_input(0);
387 0 $self->{_movable_role}{mouse_action} = '';
388 1 }
389  
390 =head2 change_geometry
391  
392 Default action when attempting to change geometry is to proxy this
393 to the L<Tickit::Window> directly. Override this in subclasses to
394 implement constraints (e.g. clamp co-ordinates and pass to C< ->SUPER::change_geometry >,
395 or return early without applying the action) or linked window actions
396 (move/resize another window after applying the geometry change to
397 this one).
398  
399 =cut
400  
401 0 ▶method change_geometry (@args) {
Subroutine change_geometry: not called CC=3 SCAR=24.8
Pod documented
402 # Allow extra actions before we start dealing with our defaults
403 0 ▶ if(my $code = $self->can('before_change_geometry')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->can("before_change_geometry")) 0 0 0
404 0 my $skip = $self->$code(@args);
405 0 ▶ return $skip if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 0 0
406 }
407  
408 0 $self->window->change_geometry(@args);
409 1 }
410  
411 1 1;
412  
413 __END__
↑ Summary