Desktop.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary Desktop/Window.pm »
1 package Tickit::Widget::Layout::Desktop;
2 # ABSTRACT: desktop-like float management implementation for Tickit
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 0 ▶class Tickit::Widget::Layout::Desktop :isa(Tickit::ContainerWidget);
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 use utf8;
9  
10 our $VERSION = '0.013';
11 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
12  
13 =head1 NAME
14  
15 Tickit::Widget::Layout::Desktop - provides a holder for "desktop-like" widget behaviour
16  
17 =head1 SYNOPSIS
18  
19 #!/usr/bin/env perl
20 use strict;
21 use warnings;
22 use Tickit;
23 use Tickit::Widget::Placegrid;
24 use Tickit::Widget::Layout::Desktop;
25 my $tickit = Tickit->new;
26 my $desktop = Tickit::Widget::Layout::Desktop->new;
27 $tickit->later(sub {
28 my $left = int($desktop->window->cols * rand);
29 my $top = int($desktop->window->lines * rand);
30 my $cols = 20 + int(10 * rand);
31 my $lines = 5 + int(20 * rand);
32 $left = $desktop->window->cols - $cols if $left + $cols >= $desktop->window->cols;
33 $top = $desktop->window->lines - $lines if $top + $lines >= $desktop->window->lines;
34 $desktop->create_panel(
35 label => 'widget',
36 left => $left,
37 top => $top,
38 cols => $cols,
39 lines => $lines,
40 )->add(Tickit::Widget::Placegrid->new);
41 });
42 $tickit->set_root_widget($desktop);
43 $tickit->run;
44  
45 =head1 DESCRIPTION
46  
47 Experimental release for a container that provides move/resize/minimize/maximize "window" behaviour.
48  
49 =begin HTML
50  
51 <p><img src="http://tickit.perlsite.co.uk/cpan-screenshot/tickit-widget-layout-desktop1.gif" alt="Desktop widget in action" width="539" height="315"></p>
52  
53 =end HTML
54  
55 Constructed of:
56  
57 =over 4
58  
59 =item * L<Tickit::Widget::Layout::Desktop::Window> - the window implementation
60  
61 =item * this class - background desktop on which the floats are displayed
62  
63 =back
64  
65 and maybe later:
66  
67 =over 4
68  
69 =item * ::Desktop::Taskbar - a subclass of statusbar which provides
70 window lists and launchers
71  
72 =back
73  
74 =cut
75  
76 use curry::weak;
77 use List::UtilsBy;
78 use Scalar::Util qw(refaddr);
79 use List::Util qw(max pairmap);
80 use Tickit::Utils qw(textwidth distribute);
81  
82 use Tickit::Widget::Menu;
83 use Tickit::Widget::Menu::Item;
84  
85 use Tickit::Widget::Layout::Desktop::Window;
86 use Variable::Disposition;
87  
88 use constant CAN_FOCUS => 1;
89 use constant WIDGET_PEN_FROM_STYLE => 1;
90  
91 =head1 METHODS
92  
93 =cut
94  
95 method lines { 1 }
96 method cols { 1 }
97  
98 =head2 render_to_rb
99  
100 Clears the exposed area. All rendering happens in the
101 floating windows on top of this widget.
102  
103 =cut
104  
105 method render_to_rb ($rb, $rect) {
106 $rb->eraserect($rect);
107 }
108  
109 method children { @{$self->{widgets}} }
110  
111 =head2 overlay
112  
113 Render all window outlines on top of the target widget.
114  
115 Takes the following parameters:
116  
117 =over 4
118  
119 =item * $rb - the L<Tickit::RenderBuffer> we will be drawing into
120  
121 =item * $exclude - the current L<Tickit::Widget> we are drawing - this will be used
122 to check for intersections so we don't waste time drawing unrelated areas
123  
124 =back
125  
126 =cut
127  
128 method overlay ($rb, $rect, $exclude) {
129 my $target = $exclude->window->rect;
130  
131 # TODO change this when proper accessors are available
132 my %win_map = map {
133 refaddr($_->window) => $_
134 } @{$self->{widgets}};
135 delete $win_map{refaddr($exclude->window)};
136  
137 # Each child widget, from back to front
138 CHILD:
139 foreach my $child (reverse grep defined, map $win_map{refaddr($_)}, $self->window->subwindows) {
140 next CHILD unless my $w = $child->window;
141 next CHILD unless $w->rect->intersects($target);
142  
143 # Clear out anything that would be under this window,
144 # so we don't draw lines that are obscured by upper
145 # layers
146 $rb->eraserect(
147 $w->rect
148 );
149  
150 # Let the child window render itself to the given
151 # context, since it knows more about styles than we do
152 $child->render_frame($rb, $target);
153 }
154 }
155  
156 =head2 window_gained
157  
158 Records our initial window geometry when the L<Tickit::Window> is first attached.
159  
160 =cut
161  
162 method window_gained ($win) {
163 $self->{geometry} = {
164 map { $_ => $win->$_ } qw(top left lines cols)
165 };
166 $self->SUPER::window_gained($win);
167  
168 }
169  
170 =head2 create_panel
171  
172 Creates a L<Tickit::Widget::Layout::Desktop::Window> on this L<Tickit::Widget::Layout::Desktop>.
173  
174 Takes the following named parameters:
175  
176 =over 4
177  
178 =item * top - offset from top of desktop
179  
180 =item * left - offset from desktop left margin
181  
182 =item * lines - how many lines the new widget will have, should be >2 to display anything useful
183  
184 =item * cols - how many columns the new widget will have, should be >2 to display anything useful
185  
186 =item * label - what label to use, default is the uninspiring text C<window>
187  
188 =back
189  
190 =cut
191  
192 method create_panel (%args) {
193 my $win = $self->window or return;
194  
195 # Normalise percentages
196 $args{$_} = $self->horizontal($win, $args{$_}) for grep exists $args{$_}, qw(left right cols);
197 $args{$_} = $self->vertical($win, $args{$_}) for grep exists $args{$_}, qw(top bottom lines);
198  
199 $args{bottom} = $win->lines - $args{bottom} if exists $args{bottom};
200 $args{right} = $win->cols - $args{right} if exists $args{right};
201  
202 if(defined(my $bottom = delete $args{bottom})) {
203 # Extrapolate coördinates to ensure we have top+lines
204 $args{top} //= delete($args{bottom}) - $args{lines} if exists $args{lines};
205 $args{lines} //= delete($args{bottom}) - $args{top} if exists $args{top};
206 }
207  
208 if(defined(my $right = delete $args{right})) {
209 # Extrapolate coördinates to ensure we have left+cols
210 $args{left} //= delete($args{right}) - $args{cols} if exists $args{cols};
211 $args{cols} //= delete($args{right}) - $args{left} if exists $args{left};
212 }
213  
214 $args{top} //= 2;
215 $args{left} //= 2;
216 $args{lines} ||= 10;
217 $args{cols} ||= 10;
218  
219 my $float = $win->make_float(
220 $args{top},
221 $args{left},
222 $args{lines},
223 $args{cols},
224 );
225 $float->cursor_at(0,0);
226  
227 my $w = ($args{subclass} || 'Tickit::Widget::Layout::Desktop::Window')->new(
228 container => $self,
229 );
230 $w->label($args{label} // 'window');
231 $w->set_window($float);
232 push @{$self->{widgets}}, $w;
233  
234 # Need to redraw our window if position or size change
235 $self->{extents}{refaddr $float} = $float->rect->translate(0,0);
236 $float->bind_event(geomchange => $self->curry::weak::float_geom_changed($w));
237 $w
238 }
239  
240 method horizontal ($win, $v) {
241 $v = $1 * $win->cols if $v =~ /(-?\d+(?:\.\d*)?)%/;
242 $v
243 }
244  
245 method vertical ($win, $v) {
246 $v = $1 * $win->lines if $v =~ /(-?\d+(?:\.\d*)?)%/;
247 $v
248 }
249  
250 method show_control ($panel, @items) {
251 my $win = $self->window or return;
252 my $panel_win = $panel->window;
253  
254 my $menu;
255 my @menu_items = pairmap {
256 { # https://rt.cpan.org/Ticket/Display.html?id=95409
257 my $code = $b;
258 Tickit::Widget::Menu::Item->new(
259 name => $a,
260 on_activate => sub {
261 $menu->dismiss;
262 $win->tickit->later(sub {
263 $code->();
264 dispose $menu;
265 });
266 }
267 )
268 }
269 } @items;
270  
271 $menu = Tickit::Widget::Menu->new(
272 items => \@menu_items,
273 );
274 $menu->popup(
275 $panel_win,
276 1,
277 1
278 );
279 }
280  
281 method float_geom_changed ($w, @) {
282 my $win = $self->window or return;
283 my $float = $w->window or return;
284  
285 my $old = $self->{extents}{refaddr $float};
286 my $new = $float->rect;
287  
288 # Any time a panel moves or changes size, we'll potentially need
289 # to trigger expose events on the desktop background and any
290 # sibling windows.
291 # Start by working out what part of our current desktop
292 # has just been uncovered, and fire expose events at our top-level
293 # window for this area (for a move, it'll typically be up to two rectangles)
294 my $rs = Tickit::RectSet->new;
295 $rs->add($old);
296 $rs->add($new);
297  
298 # We have moved. This means we may be able to scroll. However! It's not quite that
299 # simple. Our move event may cause other panels to move as well, and a move is
300 # likely to involve frame redraw as well. See Tickit::Widgget::ScrollBox for more
301 # details on the scroll_with_children method.
302 if(0 && ($old->left != $new->left || $old->top != $new->top)) {
303 my @opt = (
304 -($new->top - $old->top),
305 -($new->left - $old->left),
306 );
307 Tickit::Debug->log("Wx", "scrollrect: %s => %s", $float->scroll_with_children(
308 @opt
309 ), join(',',@opt));
310 }
311  
312 # Trigger expose events for the area we used to be in, and the new location.
313 $win->expose($_) for $rs->rects;
314  
315 # Now stash the current extents for this child window so we know what's changed next time.
316 $self->{extents}{refaddr $float} = $w->window->rect->translate(0,0);
317  
318 # Also pass on the event, so the child widget knows what's going on
319 $w->reshape($w);
320 }
321  
322 =head1 API METHODS
323  
324 These methods are provided as an API for the L<Tickit::Widget::Layout::Desktop::Window> children.
325 They allow widgets to interact with the desktop for requesting focus etc.
326  
327 =head2 make_active
328  
329 Makes the requested L<Tickit::Widget::Layout::Desktop::Window> active - brings it to the front of
330 the stack and gives it focus.
331  
332 Returns $self.
333  
334 =cut
335  
336 method make_active ($child) {
337 $_->mark_inactive for grep $_->is_active, @{$self->{widgets}};
338 $child->window->raise_to_front;
339 $child->mark_active;
340 }
341  
342 =head2 weld
343  
344 "Welds" edges of windows together.
345  
346 Takes 4 parameters:
347  
348 =over 4
349  
350 =item * Source widget edge
351  
352 =item * Source widget
353  
354 =item * Destination edge
355  
356 =item * Destination widget
357  
358 =back
359  
360 For example:
361  
362 $desktop->weld(
363 right => $left_panel,
364 left => $right_panel,
365 );
366  
367 Given a two-panel layout, this would mean any changes to the right edge of the panel on the
368 left would adjust the left edge of the panel on the right, and vice versa.
369  
370 As you increase the size of the left panel, the right panel shrinks, so instead of the normal
371 behaviour where the widget on the left would start to overlap the one on the right, this maintains
372 the relative positioning by resizing the target widget. This can be used to provide movable dividers
373 between desktop panels, allowing temporary resize without disrupting the layout.
374  
375 Returns C< $self > for chaining.
376  
377 =cut
378  
379 method weld ($src_edge, $src_widget, $dst_edge, $dst_widget) {
380 my ($src) = grep { refaddr($src_widget) == refaddr($_->child) } @{$self->{widgets}}
381 or die "src not found";
382 my ($dst) = grep { refaddr($dst_widget) == refaddr($_->child) } @{$self->{widgets}}
383 or die "dst not found";
384 push @{$src->{linked_widgets}{$src_edge}}, $dst_edge => $dst;
385 push @{$dst->{linked_widgets}{$dst_edge}}, $src_edge => $src;
386 $self;
387 }
388  
389 =head2 reshape
390  
391 Deal with reshape requests.
392  
393 Since our windows are positioned directly, we're going to lose some information if shrink
394 then expand the parent window again. This isn't ideal but hopefully we can get away with
395 it for now.
396  
397 Returns $self.
398  
399 =cut
400  
401 method reshape {
402 my $win = $self->window or return;
403  
404 my @directions = qw(top left lines cols);
405  
406 my $lines_ratio = $self->{geometry}{lines} ? $win->lines / $self->{geometry}{lines} : 1;
407 my $cols_ratio = $self->{geometry}{cols} ? $win->cols / $self->{geometry}{cols} : 1;
408  
409 # First, get all the sizes across all widgets
410 foreach my $w (@{$self->{widgets}}) {
411 my $subwin = $w->window or next;
412 $w->window->change_geometry(
413 (map int,
414 $subwin->top * $lines_ratio,
415 $subwin->left * $cols_ratio),
416 (map int($_) || 1,
417 $subwin->lines * $lines_ratio,
418 $subwin->cols * $cols_ratio)
419 );
420 }
421 $self->{geometry} = { map { $_ => $win->$_ } @directions };
422  
423 # This will probably end up using Layout::Relative.
424 # my %buckets = map { $_ => [] } @directions;
425 # foreach my $w (@{$self->{widgets}}) {
426 # push @{$buckets{$_}}, { base => $w->window->$_, expand => 1 } for @directions;
427 # }
428 #
429 # # Now recalculate the distribution
430 # distribute($win->lines, @{$buckets{top}});
431 # distribute($win->lines, @{$buckets{lines}});
432 # distribute($win->cols, @{$buckets{left}});
433 # distribute($win->cols, @{$buckets{cols}});
434 # use Data::Dumper;
435 # warn Dumper(\%buckets);
436 #
437 # # Then we apply the new sizes back to the widgets
438 # foreach my $w (@{$self->{widgets}}) {
439 # $w->window->change_geometry(
440 # map { (shift @{$buckets{$_}})->{value} } @directions,
441 # )
442 # }
443 }
444  
445 =head2 cascade
446  
447 Arrange all the windows in a cascade (first at 1,1, second at 2,2, etc.).
448  
449 =cut
450  
451 method cascade {
452 my @windows = reverse $self->window->subwindows;
453 my $x = 0;
454 my $y = 0;
455 my $lines = $self->window->lines - @windows;
456 $lines = 6 if $lines < 6;
457 my $cols = $self->window->cols - @windows;
458 $cols = 6 if $cols < 6;
459 $_->change_geometry($y++, $x++, $lines, $cols) for @windows;
460 $self
461 }
462  
463 =head2 tile
464  
465 Tiles all the windows. Tries to lay them out so things don't overlap.
466 Since we're resizing, things may end up ridiculously small.
467  
468 Pass overlap => 1 to have overlapping edges.
469  
470 Returns $self.
471  
472 =cut
473  
474 method tile (%args) {
475 my $win = $self->window or return;
476 my @windows = reverse @{$win->{child_windows}};
477  
478 # Try to end up with something vaguely square. Probably a bad
479 # choice but it seems tolerable for the moment.
480 my $side = int(sqrt 0+@windows) || 1;
481  
482 # Find the tallest window in each grid row for distribution
483 my @lines;
484 {
485 my @rows = @windows;
486 while(@rows) {
487 my @batch = splice @rows, 0, $side;
488 push @lines, +{ expand => 1, base => max map $_->lines, @batch };
489 }
490 distribute($win->lines, @lines);
491 if($args{overlap}) { # haxx
492 ++$_->{value} for @lines;
493 --$lines[-1]{value};
494 }
495 }
496  
497 # Now step through all the windows, handling one row at a time
498 while(@windows) {
499 my @batch = splice @windows, 0, $side;
500 my $l = shift @lines;
501 my @cols = map +{ base => $_->cols, expand => 1}, @batch;
502 distribute($win->cols, @cols);
503 if($args{overlap}) { # haxx
504 ++$_->{value} for @cols;
505 --$cols[-1]{value};
506 }
507 foreach my $w (@batch) {
508 my $c = shift @cols;
509 $w->change_geometry($l->{start}, $c->{start}, $l->{value}, $c->{value});
510 }
511 }
512 }
513  
514 =head2 close_all
515  
516 Close all the windows.
517  
518 =cut
519  
520 method close_all {
521 $_->close for reverse $self->window->subwindows;
522 }
523  
524 method close_panel ($panel) {
525 my $rect = $panel->window->rect;
526 my $addr = refaddr($panel);
527 List::UtilsBy::extract_by {
528 refaddr($_) == $addr
529 } $self->{widgets}->@*;
530 my $win = $self->window;
531 $win->tickit->later(sub {
532 $panel->window->close;
533 $win->expose($rect);
534 });
535 }
536  
537 # Tickit::Widget
538 method focus_next {
539 $self->SUPER::focus_next(@_)
540 }
541  
542 1;
543  
544 __END__
↑ Summary Desktop/Window.pm »