Desktop/Window.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
« Desktop.pm ↑ Summary
1 package Tickit::Widget::Layout::Desktop::Window;
2  
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::Window :isa(Tickit::SingleChildWidget);
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 apply Tickit::WidgetRole::Movable;
9  
10 our $VERSION = '0.013'; # VERSION
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 =cut
18  
19 use curry::weak;
20 use Tickit::RenderBuffer qw(LINE_THICK LINE_SINGLE LINE_DOUBLE);
21 use Tickit::Utils qw(textwidth);
22 use Tickit::Style;
23  
24 use constant WIDGET_PEN_FROM_STYLE => 1;
25  
26 BEGIN {
27 style_definition base =>
28 fg => 'grey', # Generic frame lines
29 linetype => 'round', # How to draw frames, 'round' means single with rounded corners
30 maximise_fg => 'green', # Maximise button
31 close_fg => 'red', # Close button
32 control_fg => 'white', # Control
33 title_fg => 'white';
34  
35 style_definition ':active' =>
36 fg => 'white',
37 maximise_fg => 'hi-green',
38 close_fg => 'hi-red',
39 title_fg => 'hi-green';
40  
41 style_definition ':focus-child' =>
42 fg => 'hi-green';
43 style_definition ':focus' =>
44 fg => 'hi-red';
45 }
46  
47 =head1 METHODS
48  
49 =cut
50  
51 BUILD (%args) {
52 Scalar::Util::weaken(
53 $self->{container} = $args{container} or die "No container provided?"
54 );
55 $self;
56 }
57  
58 =head2 position_is_maximise
59  
60 Returns true if this location is the maximise button.
61  
62 =cut
63  
64 method position_is_maximise ($line, $col) {
65 my $win = $self->window or return;
66 # what hardcoded madness is this
67 return 1 if $line == 0 && $col == $win->cols - 4;
68 return 0;
69 }
70  
71 =head2 position_is_close
72  
73 Returns true if this location is the close button.
74  
75 =cut
76  
77 method position_is_close ($line, $col) {
78 my $win = $self->window or return;
79 # again I say with the numbers
80 return 1 if $line == 0 && $col == $win->cols - 2;
81 return 0;
82 }
83  
84 method position_is_control ($line, $col) {
85 my $win = $self->window or return;
86 # more numbers!
87 return 1 if $line == 0 && $col >= 1 && $col <= 3;
88 return 0;
89 }
90  
91 method action_close {
92 # Close button... probably need some way to indicate when
93 # this happens, Tickit::Window doesn't appear to have set_on_closed ?
94 $self->close;
95 }
96  
97 method close {
98 $self->{container}->close_panel($self);
99 $self
100 }
101  
102 method action_restore {
103 my $win = $self->window or return 1;
104 return 1 unless $self->{maximised};
105 $win->change_geometry(
106 $self->{maximised}->top,
107 $self->{maximised}->left,
108 $self->{maximised}->lines,
109 $self->{maximised}->cols,
110 );
111 delete $self->{maximised};
112 }
113  
114 method parent { $self->{container} }
115  
116 method action_maximise {
117 my $win = $self->window or return 1;
118 return 1 if $self->{maximised};
119 $self->{maximised} = $win->rect;
120 $win->change_geometry(
121 0,
122 0,
123 $win->parent->lines,
124 $win->parent->cols,
125 );
126 }
127  
128 method action_control {
129 $self->{container}->show_control(
130 $self,
131 $self->{maximised}
132 ? ('Restore' => $self->curry::weak::action_restore)
133 : ('Maximise' => $self->curry::weak::action_maximise),
134 'Minimise' => $self->curry::weak::action_minimise,
135 'Weld' => $self->curry::weak::action_weld,
136 'Unweld' => $self->curry::weak::action_unweld,
137 'On top' => sub { },
138 'Close' => $self->curry::weak::action_close,
139 );
140 }
141  
142 method action_weld { }
143 method action_unweld { }
144  
145 =head2 before_mouse_press
146  
147 Override mouse click events to mark this window as active
148 before continuing with the usual move/resize detection logic.
149  
150 Provides click-to-raise and click-to-focus behaviour.
151  
152 =cut
153  
154 method before_mouse_press ($line, $col) {
155 $self->{container}->make_active($self);
156 if($self->position_is_close($line, $col)) {
157 $self->action_close;
158 return 1;
159 } elsif($self->position_is_maximise($line, $col)) {
160 $self->action_maximise;
161 return 1;
162 } elsif($self->position_is_control($line, $col)) {
163 $self->action_control;
164 return 1;
165 } else {
166 return 0;
167 }
168 }
169  
170 =head2 with_rb
171  
172 Runs the given coderef with a L<Tickit::RenderBuffer>, saving
173 and restoring the context around the call.
174  
175 Returns $self.
176  
177 =cut
178  
179 method with_rb ($rb, $code) {
180 $rb->save;
181 $code->($rb);
182 $rb->restore;
183 $self;
184 }
185  
186 =head2 content_rect
187  
188 Represents the inner area of this window, i.e. the
189 content without the frame.
190  
191 =cut
192  
193 method content_rect {
194 my $win = $self->window;
195 $self->child->window->rect->translate(
196 $win->top,
197 $win->left
198 )
199 }
200  
201 method container { $self->{container} }
202  
203 my %override = (
204 southeast => 0x256D,
205 northeast => 0x2570,
206 southwest => 0x256E,
207 northwest => 0x256f,
208 );
209  
210 =head2 render_to_rb
211  
212 Returns $self.
213  
214 =cut
215  
216 method render_to_rb ($rb, $rect) {
217 my $win = $self->window or return;
218 return unless $self->child->window;
219  
220 # If the exposed area does not overlap the frame, bail out now
221 return if $self->content_rect->contains($rect);
222  
223 # Use a default pen for drawing all the line-related pieces
224 $rb->setpen($self->get_style_pen);
225  
226 # First, work out any line intersections for our border.
227 $self->with_rb($rb => sub {
228 my $rb = shift;
229 my ($top, $left) = ($win->top, $win->left);
230  
231 # We'll be rendering relative to the container
232 $rb->translate(-$top, -$left);
233  
234 # Ask our container to ask all other floating
235 # windows to render their frames on our context,
236 # so we join line segments where expected
237 $self->{container}->overlay($rb, $rect, $self);
238  
239 # Restore our origin
240 # TODO would've thought ->restore should handle this?
241 $rb->translate($top, $left);
242 });
243  
244 my ($w, $h) = map $win->$_ - 1, qw(cols lines);
245 my $text_pen = $self->get_style_pen('title');
246  
247 # This is a nasty hack - we want to know whether it's safe to draw
248 # rounded corners, so we start by checking whether we have any line
249 # cells already in place in the corners...
250  
251 # ... then we render our actual border, possibly using a different style for
252 # active window...
253 my $line = {
254 round => LINE_SINGLE,
255 single => LINE_SINGLE,
256 thick => LINE_THICK,
257 double => LINE_DOUBLE,
258 }->{$self->get_style_values('linetype')};
259  
260 # So we first render the frame. This will pick up any adjoining lines from
261 # our overlay, all being well.
262 $rb->linebox_at(0, $h, 0, $w, $line);
263  
264 if($self->get_style_values('linetype') eq 'round') {
265 my $limit = [
266 $win->root->bottom,
267 $win->root->right
268 ];
269  
270 my @corner_char;
271 CORNER:
272 foreach my $corner ([0,0], [0,$w], [$h,0], [$h,$w]) {
273 my ($y, $x) = @$corner;
274 next CORNER if $y >= $limit->[0] or $x >= $limit->[1] or $x < 0 or $y < 0;
275  
276 # Apply our window offset... note that ->get_cell will segfault if
277 # we're outside the render area, so the widget width had better be
278 # correct here.
279 my $cell = eval { $rb->get_cell($y, $x); };
280  
281 # If we have a line segment here, ->linemask should be an object...
282 next CORNER unless $cell and my $linemask = $cell->linemask;
283  
284 # ... which we map to a "corner" type
285 my $corners = join "", grep { $linemask->$_ == LINE_SINGLE } qw( north south east west );
286  
287 push @corner_char, [
288 $y, $x, $override{$corners}, $cell->pen
289 ] if exists $override{$corners};
290 }
291 # ... and finally we overdraw the corners.
292 $rb->char_at(@$_) for @corner_char;
293 }
294  
295 # Then the title
296 my $txt = $self->format_label;
297 $rb->text_at(0, (1 + $w - textwidth($txt)) >> 1, $txt, $text_pen);
298  
299 # and the icons for min/max/close.
300 $rb->text_at(0, $w - 3, " ", $self->get_style_pen('maximise'));
301 $rb->text_at(0, $w - 1, " ", $self->get_style_pen('maximise'));
302 $rb->text_at(0, $w - 4, "\N{U+25CE}", $self->get_style_pen('maximise'));
303 $rb->text_at(0, $w - 2, "\N{U+2612}", $self->get_style_pen('close'));
304  
305 $rb->text_at(0, 1, "[\N{U+25AA}]", $self->get_style_pen('control'));
306  
307 # Minimise isn't particularly useful, so let's not bother with that one.
308 # $rb->text_at(0, $w - 5, "\N{U+238A}", Tickit::Pen->new(fg => 'hi-yellow'));
309 }
310  
311 method format_label {
312 ' ' . $self->label . ' ';
313 }
314  
315 method render_frame ($rb, $target) {
316 my $win = $self->window or return;
317  
318 my $line_type = LINE_SINGLE; #LINE_DOUBLE; #$self->is_active ? LINE_DOUBLE : LINE_SINGLE;
319  
320 $self->with_rb($rb, sub {
321 my $rb = shift;
322 # so we restrict our frame rendering to the area covered by the target...
323 $rb->clip($target);
324  
325 # then render all 4 edges, taking into account potential split where the
326 # target area goes. We want to render up to the target but not actually
327 # overlapping it, otherwise we'll end up with T junctions rather than
328 # corners. This all seems mildly inefficient and overcomplicated but
329 # calculating as Tickit::Rect overlap/subtract combinations was beyond
330 # me at the time of writing. Patches welcome.
331 if($win->left < $target->left) {
332 $rb->hline_at($win->top, $win->left, $target->left, $line_type);
333 $rb->hline_at($win->bottom - 1, $win->left, $target->left, $line_type);
334 }
335 if($win->right > $target->right) {
336 $rb->hline_at($win->top, $target->right - 1, $win->right - 1, $line_type);
337 $rb->hline_at($win->bottom - 1, $target->right - 1, $win->right - 1, $line_type);
338 }
339 if($win->top < $target->top) {
340 $rb->vline_at($win->top, $target->top, $win->left, $line_type);
341 $rb->vline_at($win->top, $target->top, $win->right - 1, $line_type);
342 }
343 if($win->bottom > $target->bottom) {
344 $rb->vline_at($target->bottom - 1, $win->bottom - 1, $win->left, $line_type);
345 $rb->vline_at($target->bottom - 1, $win->bottom - 1, $win->right - 1, $line_type);
346 }
347 });
348 }
349  
350 method is_active { $self->{active} ? 1 : 0 }
351  
352 method label ($v = undef) {
353 return $self->{label} // '' unless defined $v;
354 $self->{label} = $v;
355 return $self;
356 }
357  
358 method lines {
359 my $child = $self->child;
360 return 2 + ($child ? $child->lines : 0);
361 }
362  
363 method cols {
364 my $child = $self->child;
365 return 2 + ($child ? $child->cols : 0);
366 }
367  
368 method children_changed { $self->set_child_window }
369  
370 method window_gained ($win) {
371 delete $self->{frame_rects};
372 $self->{window_lines} = $win->lines;
373 $self->{window_cols} = $win->cols;
374 return $self->SUPER::window_gained($win);
375 }
376  
377 method reshape {
378 my $win = $self->window;
379  
380 # Keep our frame info if we're just moving the window around?
381 delete $self->{frame_rects};# unless $self->{window_lines} == $win->lines && $self->{window_cols} == $win->cols;
382 $self->{window_lines} = $win->lines;
383 $self->{window_cols} = $win->cols;
384 $self->set_child_window
385 }
386  
387 method set_child_window {
388  
389 my $window = $self->window or return;
390 my $child = $self->child or return;
391  
392 my $lines = $window->lines;
393 my $cols = $window->cols;
394  
395 if( $lines > 2 and $cols > 2 ) {
396 if( my $childwin = $child->window ) {
397 $childwin->change_geometry( 1, 1, $lines - 2, $cols - 2 );
398 } else {
399 my $childwin = $window->make_sub( 1, 1, $lines - 2, $cols - 2 );
400 $child->set_window( $childwin );
401 }
402 } else {
403 if( $child->window ) {
404 $child->set_window( undef );
405 }
406 }
407 }
408  
409 method mark_active {
410 $self->{active} = 1;
411 $self->set_style_tag(active => 1);
412 $self->expose_frame;
413 $self
414 }
415  
416 method mark_inactive {
417 $self->{active} = 0;
418 $self->set_style_tag(active => 0);
419 $self->expose_frame;
420 $self
421 }
422  
423 # 'hmmm.'
424 method expose_frame {
425 my $win = $self->window or return $self;
426  
427 my @rect = $self->frame_rects;
428 $win->expose($_) for @rect;
429 $self;
430 }
431  
432 method frame_rects {
433 @{ $self->{frame_rects} ||= [
434 # Tickit::Rect really is quite neat
435 $self->window->rect->subtract($self->content_rect)
436 ] };
437 }
438  
439 method adjust_left ($delta) {
440 my $rect = $self->window->rect;
441 my $cols = $rect->cols - $delta;
442 return if $cols < $self->MIN_WIDTH;
443 $self->window->change_geometry(
444 $rect->top,
445 $rect->left + $delta,
446 $rect->lines,
447 $cols,
448 )
449 }
450  
451 method adjust_right ($delta) {
452 my $rect = $self->window->rect;
453 my $cols = $rect->cols + $delta;
454 return if $cols < $self->MIN_WIDTH;
455 $self->window->change_geometry(
456 $rect->top,
457 $rect->left,
458 $rect->lines,
459 $cols,
460 )
461 }
462  
463 method adjust_top ($delta) {
464 my $rect = $self->window->rect;
465 my $lines = $rect->lines - $delta;
466 return if $lines < $self->MIN_HEIGHT;
467 $self->window->change_geometry(
468 $rect->top + $delta,
469 $rect->left,
470 $lines,
471 $rect->cols,
472 )
473 }
474  
475 method adjust_bottom ($delta) {
476 my $rect = $self->window->rect;
477 my $lines = $rect->lines + $delta;
478 return if $lines < $self->MIN_HEIGHT;
479 $self->window->change_geometry(
480 $rect->top,
481 $rect->left,
482 $lines,
483 $rect->cols,
484 )
485 }
486  
487 method linked_widgets {
488 $self->{linked_widgets} ||= {}
489 }
490  
491 =head2 change_geometry
492  
493 Override geometry changes to allow welding and constraints.
494  
495 We have a set of rules for each widget, of the following form:
496  
497 {
498 left => [
499 left => $w1,
500 right => $w2,
501 ],
502 top => [
503 top => $w2
504 ]
505 }
506  
507 If the left edge changes, the left edge of $w1 and the right edge of $w2 would move by the same amount.
508  
509 If the top changes, the top of $w2 would move by the same amount
510  
511 That's about it. The idea is that edges can be "joined", meaning that resizing applies to multiple widgets at once.
512  
513 =cut
514  
515 method before_change_geometry ($top, $left, $lines, $cols) {
516 delete $self->{maximised};
517  
518 my $deskwin = $self->container->window;
519  
520 $left = 0 if $left < 0;
521 $top = 0 if $top < 0;
522  
523 $lines = $deskwin->lines if $top < $self->window->top && $self->window->bottom == $deskwin->bottom;
524 $cols = $deskwin->cols if $left < $self->window->left && $self->window->right == $deskwin->right;
525  
526 $lines = $deskwin->lines - $top if $top + $lines > $deskwin->lines;
527 $cols = $deskwin->cols - $left if $left + $cols > $deskwin->cols;
528  
529 my $rect = Tickit::Rect->new(
530 top => $top,
531 left => $left,
532 lines => $lines,
533 cols => $cols,
534 );
535  
536 my $linked = $self->linked_widgets;
537 EDGE:
538 for my $edge (keys %$linked) {
539 my $delta = $rect->$edge - $self->window->$edge or next EDGE;
540 my @target = @{$linked->{$edge} ||= []};
541 while(my ($k, $v) = splice @target, 0, 2) {
542 my $method = 'adjust_' . $k;
543 $v->$method(
544 $delta
545 );
546 }
547 }
548 return 0;
549 }
550  
551 1;
552  
553 __END__
« Desktop.pm ↑ Summary