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

filter: Statementstmt 0.0% 0 / 180 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 311 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::KeyReference;
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::Dialog;
11  
12 our $VERSION = '0.03';
13  
14 class Term::Fabulous::Widget::KeyReference
15 :isa(Term::Fabulous::Widget::Dialog)
16 :strict(params)
17 {
18 use Clay::UI::Enum::Result;
19 use Clay::XS qw(sizing_grow CLAY_ALIGN_Y_CENTER);
20 use List::Util qw(min sum0 uniq);
21 use Scalar::Util qw(blessed refaddr weaken);
22 use Term::Fabulous::Check qw(describe positive_integer string);
23 use Term::Fabulous::Widget::Button;
24 use Term::Fabulous::Widget::Table;
25 use Term::Fabulous::Widget::Text;
26 use Term::Fabulous::Widget::TextField;
27  
28 use constant DEFAULT_WIDTH => 78;
29 use constant DEFAULT_HEIGHT => 30;
30  
31 # The roomy layout gives the table at least this many lines (or all
32 # it needs); with fewer the layout is compact.
33 use constant ROOMY_TABLE_MINIMUM => 10;
34 use constant CLOSE_HEIGHT => 3; # a bordered button
35  
36 field $title :param = 'Keys';
37 field $group :param = 'Everywhere';
38  
39 field $search :reader;
40 field $table :reader;
41  
42 field $_title_text;
43 field $_close;
44 field $_search_row; # the search field and Close, in the compact layout
45 field %_layout_of; # roomy, compact => the dialog's layout
46 field $_table_height; # the lines the table needs for all its rows
47 field $_is_compact = 0;
48 field $_resize_root; # the root whose Resize events the reference follows (weak)
49  
50 my $generated_ids = 0;
51  
52 # A key reference is a dialog with room around its table: two columns
53 # at the sides, a row above and below. It grows up to the width and
54 # the height the program asks for, and is never larger than the
55 # screen. Without an id it gets one, for the id of its table.
56 sub BUILDARGS ( $class, %params ) {
57 my $width = positive_integer( $class, width => $params{width} // DEFAULT_WIDTH );
58 my $height = positive_integer( $class, height => $params{height} // DEFAULT_HEIGHT );
59 my $layout = $params{layout} // {};
60 $params{id} //= 'key-reference-' . ++$generated_ids;
61 $params{layout} = {
62 padding => { left => 2, right => 2, top => 1, bottom => 1 },
63 %$layout,
64 sizing => { width => sizing_grow( 0, $width ), height => sizing_grow( 0, $height ), %{ $layout->{sizing} // {} } },
65 };
66 return $class->SUPER::BUILDARGS(%params);
67 }
68  
69 ADJUST :params ( :$commands = undef, :$keys = [], :$width = undef, :$height = undef ) {
70 $title = string( $self, title => $title );
71 $group = string( $self, group => $group );
72 my @rows = ( $self->_command_rows($commands), $self->_parsed_keys($keys) );
73  
74 $search = Term::Fabulous::Widget::TextField->new( placeholder => 'Search keys and actions', layout => { sizing => { width => sizing_grow() } } );
75 $table = $self->_table( \@rows );
76 weaken( my $weak_table = $table );
77 $search->on( Change => sub ($event) { $weak_table->search( $event->value ); return Clay::UI::Enum::Result->CONTINUE } );
78  
79 $_title_text = Term::Fabulous::Widget::Text->new( text => $title, bold => 1 ) if length $title;
80 $_close = $self->_close_button;
81 $_search_row = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow() }, child_gap => 2, child_alignment => { y => CLAY_ALIGN_Y_CENTER } } );
82 $_table_height = 2 + scalar( uniq map { $_->[0] } @rows ) + @rows; # the header and its line, a row per group, the rows
83 my $roomy = $self->layout;
84 %_layout_of = ( roomy => $roomy, compact => { %$roomy, child_gap => 0, padding => { %{ $roomy->{padding} // {} }, top => 0, bottom => 0 } } );
85 $self->_arrange_roomy;
86 }
87  
88 # ---------------------------------------------------------------------
89 # Parameters
90 # ---------------------------------------------------------------------
91  
92 # One row for every command with keys, in the group of the commands;
93 # a label that ends in "..." (it opens a dialog) is shown without it.
94 method _command_rows ($commands) {
95 return () unless defined $commands;
96 my @list = $self->_listed_commands($commands);
97 return map { [ $group, $_->keys_text, $_->label =~ s/\.\.\.\z//r ] } grep { $_->keys } @list;
98 }
99  
100 method _listed_commands ($commands) {
101 return $commands->commands if blessed $commands && $commands->isa('Term::Fabulous::Commands');
102 die ref($self) . ": commands must be a Term::Fabulous::Commands, an array reference of Term::Fabulous::Command objects or undef, got " . describe($commands)
103 unless ref $commands eq 'ARRAY';
104 foreach my $command (@$commands) {
105 die ref($self) . ": commands must hold Term::Fabulous::Command objects, got " . describe($command) unless blessed $command && $command->isa('Term::Fabulous::Command');
106 }
107 return @$commands;
108 }
109  
110 method _parsed_keys ($keys) {
111 die ref($self) . ": keys must be an array reference, got " . describe($keys) unless ref $keys eq 'ARRAY';
112 my @parsed;
113 foreach my $row (@$keys) {
114 die ref($self) . ": a row of keys must be an array reference of a group, the keys and the action, got " . describe($row) unless ref $row eq 'ARRAY' && @$row == 3;
115 push @parsed, [ map { string( $self, 'a group, the keys and the action of a row of keys' => $_ ) } @$row ];
116 }
117 return @parsed;
118 }
119  
120 # ---------------------------------------------------------------------
121 # The parts
122 # ---------------------------------------------------------------------
123  
124 # The table groups its rows by their hidden group value, which sorts:
125 # it starts with the number of the group in the order the groups first
126 # appear, and the label leaves the number out.
127 method _table ($rows) {
128 return Term::Fabulous::Widget::Table->new(
129 id => $self->id . '/list',
130 row_id => 'id',
131 group_by => 'group',
132 group_label => sub ($shown_group) { return $shown_group->{value} =~ s/\A\d+ //r },
133 sort => [ [ order => 'asc' ] ],
134 border => 'none',
135 column_lines => 'none',
136 header_line => 'Solid',
137 columns => [
138 { key => 'order', title => '', type => 'number', visible => 0 },
139 { key => 'group', title => 'Where', visible => 0 },
140 { key => 'keys', title => 'Keys', width => 'fit(0, 26)', sortable => 0 },
141 { key => 'action', title => 'Action', width => 'grow', sortable => 0 },
142 ],
143 rows => [ _table_rows($rows) ],
144 layout => { sizing => { width => sizing_grow(), height => sizing_grow() } },
145 );
146 }
147  
148 # The rows of the table, numbered in the order given, each in the group
149 # value of its group.
150 sub _table_rows ($rows) {
151 my %rank;
152 $rank{ $_->[0] } //= sprintf( '%03d', scalar keys %rank ) foreach @$rows;
153 return map {
154 my ( $key_group, $keys, $action ) = $rows->[$_]->@*;
155 +{ id => $_ + 1, order => $_ + 1, group => "$rank{$key_group} $key_group", keys => $keys, action => $action };
156 } 0 .. $#$rows;
157 }
158  
159 # ---------------------------------------------------------------------
160 # Roomy and compact
161 # ---------------------------------------------------------------------
162  
163 # The roomy layout puts the search field, the table and Close below
164 # each other with a gap between them; when that leaves the table too
165 # few lines, the compact layout drops the gaps and the padding above
166 # and below, and puts Close next to the search field.
167 method open :override ($ui) {
168 return $self if $self->is_open;
169 $self->_fit_height( $ui->height );
170 $self->SUPER::open($ui);
171 $self->_follow_resizes( $ui->root );
172 return $self;
173 }
174  
175 # One listener for each root the reference opens on.
176 method _follow_resizes ($root) {
177 return if defined $_resize_root && refaddr($_resize_root) == refaddr($root);
178 weaken( $_resize_root = $root );
179 weaken( my $weak_self = $self );
180 $root->on( Resize => sub ($event) { $weak_self->_fit_height( $event->height ) if $weak_self && $weak_self->is_open && $event->is_post_event; return Clay::UI::Enum::Result->CONTINUE } );
181 return;
182 }
183  
184 method _fit_height ($screen_height) {
185 my $wants_compact = $self->_roomy_table_lines( $self->_dialog_height($screen_height) ) < min( ROOMY_TABLE_MINIMUM, $_table_height );
186 return if $wants_compact == $_is_compact;
187 my $interaction = defined $self->ui ? $self->ui->interaction : undef;
188 my $focused = defined $interaction ? $interaction->get_focused_widget : undef;
189 $wants_compact ? $self->_arrange_compact : $self->_arrange_roomy;
190 $interaction->set_focused_widget($focused) if defined $focused && $interaction->can_take_focus($focused); # moving the parts lost it
191 return;
192 }
193  
194 # The height the dialog gets on a screen this high: the most its
195 # sizing allows (the height parameter, or the layout's), at most the
196 # screen.
197 method _dialog_height ($screen_height) {
198 my $most = $_layout_of{roomy}{sizing}{height}{max} // 0;
199 return $most > 0 ? min( $most, $screen_height ) : $screen_height;
200 }
201  
202 # The lines the roomy layout leaves the table in a dialog this high:
203 # the border, the padding, the title, the search field, Close and the
204 # gaps between them take the rest.
205 method _roomy_table_lines ($dialog_height) {
206 my $layout = $_layout_of{roomy};
207 my $padding = $layout->{padding} // {};
208 my @others = ( defined $_title_text ? 1 : (), 1, CLOSE_HEIGHT );
209 my $gaps = ( $layout->{child_gap} // 0 ) * @others;
210 return $dialog_height - 2 - ( $padding->{top} // 0 ) - ( $padding->{bottom} // 0 ) - $gaps - sum0(@others);
211 }
212  
213 method _arrange_roomy () {
214 $_search_row->clear_children;
215 $self->clear_children;
216 $self->layout( $_layout_of{roomy} );
217 $self->add_child( grep { defined } $_title_text, $search, $table, $_close );
218 $_is_compact = 0;
219 return;
220 }
221  
222 method _arrange_compact () {
223 $self->clear_children;
224 $_search_row->add_child( $search, $_close );
225 $self->layout( $_layout_of{compact} );
226 $self->add_child( grep { defined } $_title_text, $_search_row, $table );
227 $_is_compact = 1;
228 return;
229 }
230  
231 method _close_button () {
232 weaken( my $weak_self = $self );
233 my $close = Term::Fabulous::Widget::Button->new( bordered => 1, classes => ['primary'], layout => { padding => { left => 1, right => 1 } } );
234 $close->add_child( Term::Fabulous::Widget::Text->new( text => 'Close' ) );
235 $close->on( Activate => sub ($event) { $weak_self->close; return } );
236 return $close;
237 }
238 }
239  
240 1;
241  
242 __END__