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

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 153 CC 1 SCAR 6.9
CC
1
Cov
0%
BUILDARGS
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::Dialog;
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 Term::Fabulous::Widget::Box;
11 use Term::Fabulous::Widget::Dialog::Backdrop;
12  
13 our $VERSION = '0.03';
14  
15 class Term::Fabulous::Widget::Dialog
16 :isa(Term::Fabulous::Widget::Box)
17 :strict(params)
18 {
19 use Clay::XS qw(CLAY_TOP_TO_BOTTOM);
20 use Scalar::Util qw(blessed weaken);
21 use Term::Fabulous::Check qw(boolean integer);
22 use Term::Fabulous::Event::Close;
23  
24 my %DEFAULT_LAYOUT = (
25 layout_direction => CLAY_TOP_TO_BOTTOM,
26 padding => { left => 1, right => 1, top => 1, bottom => 1 },
27 child_gap => 1,
28 );
29  
30 field $z_index :param = 1000;
31 field $close_on_escape :param = 1;
32  
33 # The Term::Fabulous::Widget::Dialog::Backdrop while open. The tree owns
34 # it (it holds the dialog as its child), so the reference is weak: a
35 # dialog left open in a dropped UI is freed with it.
36 field $_backdrop;
37 field $_is_open = 0;
38 field $_focus_before; # the widget focused when the dialog opened
39  
40 # A dialog looks like a dialog unless told otherwise: a panel in the
41 # theme's dialog colors and border (dialog.border.enabled), laid out
42 # top to bottom with a cell of padding.
43 sub BUILDARGS ( $class, %params ) {
44 $params{layout} = { %DEFAULT_LAYOUT, %{ $params{layout} // {} } };
45 return $class->SUPER::BUILDARGS(%params);
46 }
47  
48 ADJUST {
49 $z_index = integer( $self, z_index => $z_index );
50 $close_on_escape = boolean( $self, close_on_escape => $close_on_escape );
51 }
52  
53 method theme_family :common () {
54 return 'dialog';
55 }
56  
57 method themed_params :common () {
58 return ( $class->SUPER::themed_params, backdrop_color => [ 'backdrop', 'normal', 'color' ] );
59 }
60  
61 method layout_properties :common () {
62 return (
63 $class->SUPER::layout_properties,
64 z_index => 'scalar',
65 close_on_escape => 'boolean',
66 );
67 }
68  
69 # The backdrop reads the color when a frame is drawn.
70 method backdrop_color (@new) {
71 return $self->look_value('backdrop_color') unless @new;
72 return $self->set_look( backdrop_color => $new[0] );
73 }
74  
75 method z_index (@new) {
76 return $z_index unless @new;
77 $z_index = integer( $self, z_index => $new[0] );
78 $_backdrop->floating( { %{ $_backdrop->floating }, z_index => $z_index } ) if defined $_backdrop;
79 return $z_index;
80 }
81  
82 method close_on_escape (@new) {
83 return $close_on_escape unless @new;
84 return $close_on_escape = boolean( $self, close_on_escape => $new[0] );
85 }
86  
87 method is_open () {
88 return $_is_open;
89 }
90  
91 method backdrop () {
92 return $_backdrop;
93 }
94  
95 method open ($ui) {
96 die "Term::Fabulous::Widget::Dialog: open needs the Term::Fabulous object, got " . ( ref $ui || $ui // 'undef' )
97 unless blessed $ui && $ui->isa('Clay::UI');
98 return $self if defined $_backdrop;
99 die "Term::Fabulous::Widget::Dialog: a dialog that is a child of another widget cannot open" if defined $self->parent;
100  
101 my $interaction = $ui->interaction;
102 $_focus_before = $interaction->get_focused_widget;
103 weaken $_focus_before if defined $_focus_before;
104  
105 my $backdrop = Term::Fabulous::Widget::Dialog::Backdrop->new( dialog => $self, z_index => $z_index );
106 $backdrop->add_child($self);
107 $ui->root->add_child($backdrop);
108 weaken( $_backdrop = $backdrop );
109 $_is_open = 1;
110 $interaction->set_focused_widget( $backdrop->get_next_focus );
111 return $self;
112 }
113  
114 # The program may have taken the backdrop out of the tree already (with
115 # clear_children on the root, for example): then there is nothing to
116 # remove and no UI to give the focus back in, but Close still fires.
117 method close () {
118 return $self unless $_is_open;
119 my $backdrop = $_backdrop;
120 my $ui = $self->ui;
121 ( $_is_open, $_backdrop ) = ( 0, undef );
122  
123 if ( defined $backdrop ) {
124 my $holder = $backdrop->parent;
125 $holder->remove_child($backdrop) if defined $holder;
126 $backdrop->clear_children;
127 }
128 $self->_restore_focus($ui) if defined $ui;
129 $self->fire_event( Term::Fabulous::Event::Close->new );
130 return $self;
131 }
132  
133 # The focus goes back to the widget that had it, if it still can take it.
134 method _restore_focus ($ui) {
135 my $previous = $_focus_before;
136 $_focus_before = undef;
137 return unless defined $previous && $ui->interaction->can_take_focus($previous);
138 $ui->interaction->set_focused_widget($previous);
139 return;
140 }
141 }
142  
143 1;
144  
145 __END__