blib/lib/Term/Fabulous/Widget/Dialog/Backdrop.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 142 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::Dialog::Backdrop;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 use Object::Pad 0.825;
9  
10 use Clay::UI::Role::Interaction::Focusable;
11 use Clay::UI::Role::Interaction::HasFocusOrder;
12 use Term::Fabulous::Widget::Box;
13  
14 our $VERSION = '0.03';
15  
16 class Term::Fabulous::Widget::Dialog::Backdrop
17 :isa(Term::Fabulous::Widget::Box)
18 :does(Clay::UI::Role::Interaction::Focusable)
19 :does(Clay::UI::Role::Interaction::HasFocusOrder)
20 :strict(params)
21 {
22 use Clay::UI::Enum::Result;
23 use Clay::XS qw(
24 CLAY_ATTACH_TO_ROOT CLAY_ATTACH_POINT_CENTER_CENTER
25 CLAY_ALIGN_X_CENTER CLAY_ALIGN_Y_CENTER sizing_grow
26 );
27 use Scalar::Util qw(weaken);
28  
29 field $dialog :param :weak :reader;
30 field $z_index :param;
31  
32 ADJUST {
33 $self->floating(
34 {
35 attach_to => CLAY_ATTACH_TO_ROOT,
36 attach_points => { element => CLAY_ATTACH_POINT_CENTER_CENTER, parent => CLAY_ATTACH_POINT_CENTER_CENTER },
37 z_index => $z_index,
38 }
39 );
40 $self->layout(
41 {
42 sizing => { width => sizing_grow(), height => sizing_grow() },
43 child_alignment => { x => CLAY_ALIGN_X_CENTER, y => CLAY_ALIGN_Y_CENTER },
44 }
45 );
46 $self->glyphs_show_through(1);
47  
48 weaken( my $weak_self = $self );
49 $self->on( KeyPress => sub ($event) { return $weak_self->_handle_key($event) } );
50 $self->on(
51 OnBlur => sub ($event) {
52 $weak_self->_keep_focus if defined $weak_self;
53 return Clay::UI::Enum::Result->CONTINUE;
54 }
55 );
56 }
57  
58 # The backdrop is painted in the dialog's backdrop color, read when
59 # the frame is drawn, so a theme switch shows at once.
60 method background_color :override (@new) {
61 return $self->SUPER::background_color(@new) if @new;
62 return $self->SUPER::background_color // ( defined $dialog ? $dialog->backdrop_color : undef );
63 }
64  
65 method contribute_look_theme :override ($config) {
66 my $background = $self->background_color // return;
67 $config->{background_color} = $background;
68 return;
69 }
70  
71 # Every key stops here while the dialog is open: the widgets and key
72 # bindings behind it do not see it. Escape closes the dialog.
73 method _handle_key ($event) {
74 my $owner = $dialog;
75 return Clay::UI::Enum::Result->CONTINUE unless defined $owner && $owner->is_open;
76 $owner->close if ( $event->main_key_name // '' ) eq 'Escape' && $owner->close_on_escape;
77 return Clay::UI::Enum::Result->HANDLED;
78 }
79  
80 # The focus does not leave an open dialog for nothing: when the focused
81 # widget inside it is disabled or removed, the backdrop takes it, so
82 # keys and Tab stay inside the dialog.
83 method _keep_focus () {
84 return unless defined $dialog && $dialog->is_open;
85 my $ui = $self->ui // return;
86 my $interaction = $ui->interaction;
87  
88 # A backdrop that is being removed (its OnBlur fires while it
89 # leaves) cannot take the focus.
90 return if defined $interaction->get_focused_widget || !$interaction->can_take_focus($self);
91 $interaction->set_focused_widget($self);
92 return;
93 }
94  
95 # Tab and Shift-Tab step through the focusable widgets inside the
96 # dialog, wrapping around; the backdrop itself takes the focus when
97 # there are none, so the focus has somewhere to stay.
98 method get_next_focus () {
99 return $self unless defined $dialog;
100 return $self->default_next_focus( within => $dialog ) // $self;
101 }
102  
103 method get_previous_focus () {
104 return $self unless defined $dialog;
105 return $self->default_previous_focus( within => $dialog ) // $self;
106 }
107 }
108  
109 1;
110  
111 __END__