| 1 | package Term::Fabulous::Widget::Prompt; | ||
| 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::Prompt | ||
| 15 | :isa(Term::Fabulous::Widget::Dialog) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use Clay::XS qw(sizing_fixed sizing_grow CLAY_TOP_TO_BOTTOM); | ||
| 19 | use List::Util qw(first max); | ||
| 20 | use Scalar::Util qw(blessed); | ||
| 21 | use Term::Fabulous::Check qw(describe optional positive_integer string); | ||
| 22 | use Term::Fabulous::Text::Markup qw(escape_markup); | ||
| 23 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 24 | use Term::Fabulous::Widget::Box; | ||
| 25 | use Term::Fabulous::Widget::Text; | ||
| 26 | use Term::Fabulous::Widget::TextField; | ||
| 27 | |||
| 28 | use constant DEFAULT_WIDTH => 60; | ||
| 29 | use constant CANCEL => 'cancel'; | ||
| 30 | |||
| 31 | field $message :param = ''; | ||
| 32 | field $focus :param = undef; | ||
| 33 | field $check :param = undef; | ||
| 34 | |||
| 35 | field @_fields; # [ { name, label, input } ], in order | ||
| 36 | field $_message_text; | ||
| 37 | |||
| 38 | # A prompt is a dialog with room around its text: two columns at the | ||
| 39 | # sides, a row above and below, and the width the program asks for, | ||
| 40 | # or the width of the screen when that is narrower. It always has a | ||
| 41 | # button, OK unless the program names others. | ||
| 42 | sub BUILDARGS ( $class, %params ) { | ||
| 43 | my $width = positive_integer( $class, width => delete $params{width} // DEFAULT_WIDTH ); | ||
| 44 | my $layout = $params{layout} // {}; | ||
| 45 | $params{layout} = { | ||
| 46 | padding => { left => 2, right => 2, top => 1, bottom => 1 }, | ||
| 47 | %$layout, | ||
| 48 | sizing => { width => sizing_grow( 0, $width ), %{ $layout->{sizing} // {} } }, | ||
| 49 | }; | ||
| 50 | $params{buttons} //= [ { id => 'ok', label => 'OK', primary => 1 } ]; | ||
| 51 | die "$class: buttons must be a non-empty array reference, got " . describe( $params{buttons} ) unless ref $params{buttons} eq 'ARRAY' && @{ $params{buttons} }; | ||
| 52 | return $class->SUPER::BUILDARGS(%params); | ||
| 53 | } | ||
| 54 | |||
| 55 | ADJUST :params ( :$fields = [] ) { | ||
| 56 | $message = string( $self, message => $message ); | ||
| 57 | die ref($self) . ": check must be a code reference or undef, got " . describe($check) if defined $check && ref $check ne 'CODE'; | ||
| 58 | @_fields = $self->_parsed_fields($fields); | ||
| 59 | $focus = $self->_checked_focus($focus); | ||
| 60 | |||
| 61 | $_message_text = Term::Fabulous::Widget::Text->new( markup => $message ); | ||
| 62 | $self->add_child($_message_text) if length $message; | ||
| 63 | $self->add_child( $self->_field_rows ) if @_fields; | ||
| 64 | |||
| 65 | $self->on( Submit => sub ( $self, $event ) { $self->answer( $self->default_button ); return }, owner => $self ); | ||
| 66 | } | ||
| 67 | |||
| 68 | # --------------------------------------------------------------------- | ||
| 69 | # Parameters | ||
| 70 | # --------------------------------------------------------------------- | ||
| 71 | |||
| 72 | method _parsed_fields ($fields) { | ||
| 73 | die ref($self) . ": fields must be an array reference, got " . describe($fields) unless ref $fields eq 'ARRAY'; | ||
| 74 | my ( @parsed, %seen ); | ||
| 75 | foreach my $field (@$fields) { | ||
| 76 | die ref($self) . ": a field must be a hash reference with a name and an input, got " . describe($field) unless ref $field eq 'HASH'; | ||
| 77 | my %rest = %$field; | ||
| 78 | my $name = string( $self, 'the name of a field' => delete $rest{name} ); | ||
| 79 | my $label = optional( \&string, $self, "the label of the field '$name'" => delete $rest{label} ); | ||
| 80 | my $input = delete $rest{input}; | ||
| 81 | die ref($self) . ": the field '$name' needs an input widget that has a value, got " . describe($input) | ||
| 82 | unless blessed $input && $input->can('value') && $input->isa('Term::Fabulous::Widget'); | ||
| 83 | die ref($self) . ": the field '$name' has unknown keys: " . join( ', ', sort keys %rest ) if %rest; | ||
| 84 | die ref($self) . ": two fields are named '$name'" if $seen{$name}++; | ||
| 85 | push @parsed, { name => $name, label => $label, input => $input }; | ||
| 86 | } | ||
| 87 | return @parsed; | ||
| 88 | } | ||
| 89 | |||
| 90 | # The field or button to focus when the prompt opens: a name that is | ||
| 91 | # both is ambiguous. | ||
| 92 | method _checked_focus ($name) { | ||
| 93 | return undef unless defined $name; | ||
| 94 | $name = string( $self, focus => $name ); | ||
| 95 | my $is_field = grep { $_->{name} eq $name } @_fields; | ||
| 96 | my $is_button = grep { $_ eq $name } $self->button_ids; | ||
| 97 | die ref($self) . ": focus '$name' names both a field and a button" if $is_field && $is_button; | ||
| 98 | die ref($self) . ": focus '$name' names no field and no button" unless $is_field || $is_button; | ||
| 99 | return $name; | ||
| 100 | } | ||
| 101 | |||
| 102 | # --------------------------------------------------------------------- | ||
| 103 | # The parts | ||
| 104 | # --------------------------------------------------------------------- | ||
| 105 | |||
| 106 | # One row per field, the labels in a column as wide as the longest. | ||
| 107 | method _field_rows () { | ||
| 108 | my $label_columns = max( 0, map { string_columns( $_->{label} // '' ) } @_fields ); | ||
| 109 | my $rows = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow() }, child_gap => 1 } ); | ||
| 110 | foreach my $field (@_fields) { | ||
| 111 | my $row = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow() }, child_gap => 1 } ); | ||
| 112 | if ($label_columns) { | ||
| 113 | my $label = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fixed($label_columns) } } ); | ||
| 114 | $label->add_child( Term::Fabulous::Widget::Text->new( text => $field->{label} // '' ) ); | ||
| 115 | $row->add_child($label); | ||
| 116 | } | ||
| 117 | $row->add_child( $field->{input} ); | ||
| 118 | $rows->add_child($row); | ||
| 119 | } | ||
| 120 | return $rows; | ||
| 121 | } | ||
| 122 | |||
| 123 | # --------------------------------------------------------------------- | ||
| 124 | # Accessors | ||
| 125 | # --------------------------------------------------------------------- | ||
| 126 | |||
| 127 | # The message comes right after the title. | ||
| 128 | method message (@new) { | ||
| 129 | return $message unless @new; | ||
| 130 | my $markup = string( $self, message => $new[0] ); | ||
| 131 | $_message_text->markup($markup); | ||
| 132 | $message = $markup; | ||
| 133 | $self->remove_child($_message_text) unless length $message; | ||
| 134 | $self->insert_children( length $self->title ? 1 : 0, $_message_text ) if length $message && !$self->has_child($_message_text); | ||
| 135 | return $message; | ||
| 136 | } | ||
| 137 | |||
| 138 | method field_names () { | ||
| 139 | return map { $_->{name} } @_fields; | ||
| 140 | } | ||
| 141 | |||
| 142 | method input ($name) { | ||
| 143 | my $field = first { $_->{name} eq $name } @_fields; | ||
| 144 | die ref($self) . ": the prompt has no field named " . describe($name) unless defined $field; | ||
| 145 | return $field->{input}; | ||
| 146 | } | ||
| 147 | |||
| 148 | method values :override () { | ||
| 149 | return { map { $_->{name} => $_->{input}->value } @_fields }; | ||
| 150 | } | ||
| 151 | |||
| 152 | method error () { | ||
| 153 | return $self->notice_text; | ||
| 154 | } | ||
| 155 | |||
| 156 | # --------------------------------------------------------------------- | ||
| 157 | # Opening and answering | ||
| 158 | # --------------------------------------------------------------------- | ||
| 159 | |||
| 160 | method open :override ($ui) { | ||
| 161 | return $self if $self->is_open; | ||
| 162 | $self->notice(''); | ||
| 163 | $self->SUPER::open($ui); | ||
| 164 | $self->_focus_named($focus) if defined $focus; | ||
| 165 | return $self; | ||
| 166 | } | ||
| 167 | |||
| 168 | method _focus_named ($name) { | ||
| 169 | my $field = first { $_->{name} eq $name } @_fields; | ||
| 170 | $self->_focus_into( defined $field ? $field->{input} : $self->button($name) ); | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | # Focuses the widget, or the first focusable widget inside it when it | ||
| 175 | # cannot take the focus itself (an input made of other inputs, such | ||
| 176 | # as a color picker). | ||
| 177 | method _focus_into ($widget) { | ||
| 178 | my $interaction = $self->ui->interaction; | ||
| 179 | my ($target) = $interaction->focusables( within => $widget ); | ||
| 180 | $interaction->set_focused_widget($target) if defined $target; | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | |||
| 184 | # A primary button checks the fields first and keeps the prompt open | ||
| 185 | # while something is wrong; then the dialog answers. | ||
| 186 | method answer :override ( $id, %values ) { | ||
| 187 | return $self if $self->is_primary($id) && $self->is_open && !$self->_values_are_fine; | ||
| 188 | return $self->SUPER::answer( $id, %values ); | ||
| 189 | } | ||
| 190 | |||
| 191 | # Checks every field, then the whole prompt; shows the first problem | ||
| 192 | # and focuses the field it is about. | ||
| 193 | method _values_are_fine () { | ||
| 194 | foreach my $field (@_fields) { | ||
| 195 | my $input = $field->{input}; | ||
| 196 | my $error = $input->can('validate') ? $input->validate : undef; | ||
| 197 | next unless defined $error; | ||
| 198 | $self->notice( escape_markup( ( $field->{label} // $field->{name} ) . ": $error" ), 'error' ); | ||
| 199 | $self->_focus_into( $self->_invalid_part_of($input) // $input ); | ||
| 200 | return 0; | ||
| 201 | } | ||
| 202 | my $problem = defined $check ? $check->( $self->values ) : undef; | ||
| 203 | if ($problem) { | ||
| 204 | $self->notice( escape_markup($problem), 'error' ); | ||
| 205 | return 0; | ||
| 206 | } | ||
| 207 | $self->notice(''); | ||
| 208 | return 1; | ||
| 209 | } | ||
| 210 | |||
| 211 | # The first invalid input inside a field that can take the focus (a | ||
| 212 | # color picker's text field rather than its swatch list), or undef. | ||
| 213 | method _invalid_part_of ($input) { | ||
| 214 | my $interaction = $self->ui->interaction; | ||
| 215 | return first { $interaction->can_take_focus($_) } $input->invalid_inputs; | ||
| 216 | } | ||
| 217 | |||
| 218 | # --------------------------------------------------------------------- | ||
| 219 | # Prompts in one call | ||
| 220 | # --------------------------------------------------------------------- | ||
| 221 | |||
| 222 | method alert :common ( $ui, %params ) { | ||
| 223 | my $ok = delete $params{ok} // 'OK'; | ||
| 224 | return $class->new( %params, buttons => [ { id => 'ok', label => $ok, primary => 1 } ] )->open($ui); | ||
| 225 | } | ||
| 226 | |||
| 227 | method confirm :common ( $ui, %params ) { | ||
| 228 | my $ok = delete $params{ok} // 'OK'; | ||
| 229 | my $cancel = delete $params{cancel} // 'Cancel'; | ||
| 230 | return $class->new( %params, buttons => [ { id => 'ok', label => $ok, primary => 1 }, { id => CANCEL, label => $cancel } ] )->open($ui); | ||
| 231 | } | ||
| 232 | |||
| 233 | method ask :common ( $ui, %params ) { | ||
| 234 | my %field = map { exists $params{$_} ? ( $_ => delete $params{$_} ) : () } qw(value placeholder required validator); | ||
| 235 | my $label = delete $params{label}; | ||
| 236 | my $ok = delete $params{ok} // 'OK'; | ||
| 237 | my $cancel = delete $params{cancel} // 'Cancel'; | ||
| 238 | my $input = Term::Fabulous::Widget::TextField->new( %field, layout => { sizing => { width => sizing_grow() } } ); | ||
| 239 | $input->editor->select_all; # typing replaces what the field starts with | ||
| 240 | return $class->new( | ||
| 241 | %params, | ||
| 242 | fields => [ { name => 'value', label => $label, input => $input } ], | ||
| 243 | buttons => [ { id => 'ok', label => $ok, primary => 1 }, { id => CANCEL, label => $cancel } ], | ||
| 244 | )->open($ui); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | 1; | ||
| 249 | |||
| 250 | __END__ |