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

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