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

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 10 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.05';
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  
28 field $dialog :param :weak :reader;
29 field $z_index :param;
30  
31 # The widget that had the focus when the dialog opened; it gets the
32 # focus back when the backdrop leaves (see focus_heir).
33 field $focus_before :param :weak = undef;
34  
35 ADJUST {
36 $self->floating(
37 {
38 attach_to => CLAY_ATTACH_TO_ROOT,
39 attach_points => { element => CLAY_ATTACH_POINT_CENTER_CENTER, parent => CLAY_ATTACH_POINT_CENTER_CENTER },
40 z_index => $z_index,
41 }
42 );
43 $self->layout(
44 {
45 sizing => { width => sizing_grow(), height => sizing_grow() },
46 child_alignment => { x => CLAY_ALIGN_X_CENTER, y => CLAY_ALIGN_Y_CENTER },
47 }
48 );
49 $self->glyphs_show_through(1);
50  
51 $self->on( KeyPress => sub ( $self, $event ) { return $self->_handle_key($event) }, owner => $self );
52 $self->on(
53 OnBlur => sub ( $self, $event ) {
54 $self->_keep_focus;
55 return Clay::UI::Enum::Result->CONTINUE;
56 },
57 owner => $self
58 );
59 }
60  
61 # The backdrop is painted in the dialog's backdrop color, read when
62 # the frame is drawn, so a theme switch shows at once.
63 method background_color :override (@new) {
64 return $self->SUPER::background_color(@new) if @new;
65 return $self->SUPER::background_color // ( defined $dialog ? $dialog->backdrop_color : undef );
66 }
67  
68 method contribute_look_theme :override ($config) {
69 my $background = $self->background_color // return;
70 $config->{background_color} = $background;
71 return;
72 }
73  
74 # Every key stops here while the dialog is open: the widgets and key
75 # bindings behind it do not see it. Escape closes the dialog.
76 method _handle_key ($event) {
77 my $owner = $dialog;
78 return Clay::UI::Enum::Result->CONTINUE unless defined $owner && $owner->is_open;
79 $owner->close if ( $event->main_key_name // '' ) eq 'Escape' && $owner->close_on_escape;
80 return Clay::UI::Enum::Result->HANDLED;
81 }
82  
83 # Clearing the focus from code does not take it out of an open dialog:
84 # the backdrop takes it, so keys and Tab stay inside. A focused widget
85 # that is disabled or removed needs no help, the backdrop is its focus
86 # heir anyway.
87 method _keep_focus () {
88 return unless defined $dialog && $dialog->is_open;
89 my $ui = $self->ui // return;
90 my $interaction = $ui->interaction;
91  
92 # A backdrop that is being removed (its OnBlur fires while it
93 # leaves) cannot take the focus.
94 return if defined $interaction->get_focused_widget || !$interaction->can_take_focus($self);
95 $interaction->set_focused_widget($self);
96 return;
97 }
98  
99 # Who takes the focus when the backdrop leaves with the focus inside
100 # it, that is when the dialog closes: the widget that had it before.
101 method focus_heir () {
102 return $focus_before;
103 }
104  
105 # Tab and Shift-Tab step through the focusable widgets inside the
106 # dialog, wrapping around; the backdrop itself takes the focus when
107 # there are none, so the focus has somewhere to stay.
108 method get_next_focus () {
109 return $self unless defined $dialog;
110 return $self->default_next_focus( within => $dialog ) // $self;
111 }
112  
113 method get_previous_focus () {
114 return $self unless defined $dialog;
115 return $self->default_previous_focus( within => $dialog ) // $self;
116 }
117  
118 # A widget inside the dialog that hands the focus on past its own
119 # subtree (Clay::UI::Interaction's focus_after) stays inside the
120 # dialog too, and never lands on the backdrop.
121 method focus_scope () {
122 return $dialog // $self;
123 }
124 }
125  
126 1;
127  
128 __END__