| 1 | package Term::Fabulous::Widget::TextInput; | ||
| 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::Editor; | ||
| 11 | use Term::Fabulous::TextView; | ||
| 12 | use Term::Fabulous::Widget::Input; | ||
| 13 | |||
| 14 | our $VERSION = '0.02'; | ||
| 15 | |||
| 16 | class Term::Fabulous::Widget::TextInput | ||
| 17 | :isa(Term::Fabulous::Widget::Input) | ||
| 18 | :abstract | ||
| 19 | { | ||
| 20 | use Feature::Compat::Try; | ||
| 21 | use Scalar::Util qw(weaken); | ||
| 22 | use Term::Fabulous::Check qw(boolean string); | ||
| 23 | use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT TB_KEY_MOUSE_RELEASE TB_MOD_MOTION TB_MOD_SHIFT); | ||
| 24 | use Time::HiRes qw(time); | ||
| 25 | use Term::Fabulous::Unicode qw(sanitize_text grapheme_clusters); | ||
| 26 | |||
| 27 | use constant DOUBLE_CLICK_SECONDS => 0.4; | ||
| 28 | |||
| 29 | # Cursor movement: key name => [ editor method, extend the selection ]. | ||
| 30 | my %MOVEMENT_BY_KEY = ( | ||
| 31 | 'Left' => [ move_left => 0 ], | ||
| 32 | 'Shift+Left' => [ move_left => 1 ], | ||
| 33 | 'Right' => [ move_right => 0 ], | ||
| 34 | 'Shift+Right' => [ move_right => 1 ], | ||
| 35 | 'Ctrl+Left' => [ move_word_left => 0 ], | ||
| 36 | 'Ctrl+Shift+Left' => [ move_word_left => 1 ], | ||
| 37 | 'Ctrl+Right' => [ move_word_right => 0 ], | ||
| 38 | 'Ctrl+Shift+Right' => [ move_word_right => 1 ], | ||
| 39 | 'Home' => [ move_line_start => 0 ], | ||
| 40 | 'Shift+Home' => [ move_line_start => 1 ], | ||
| 41 | 'End' => [ move_line_end => 0 ], | ||
| 42 | 'Shift+End' => [ move_line_end => 1 ], | ||
| 43 | 'Ctrl+Home' => [ move_document_start => 0 ], | ||
| 44 | 'Ctrl+Shift+Home' => [ move_document_start => 1 ], | ||
| 45 | 'Ctrl+End' => [ move_document_end => 0 ], | ||
| 46 | 'Ctrl+Shift+End' => [ move_document_end => 1 ], | ||
| 47 | ); | ||
| 48 | |||
| 49 | # Edits: key name => editor method. | ||
| 50 | my %EDIT_BY_KEY = ( | ||
| 51 | 'Backspace' => 'delete_backward', | ||
| 52 | 'Delete' => 'delete_forward', | ||
| 53 | 'Ctrl+W' => 'delete_word_backward', | ||
| 54 | 'Ctrl+Delete' => 'delete_word_forward', | ||
| 55 | 'Ctrl+U' => 'delete_to_line_start', | ||
| 56 | 'Ctrl+K' => 'delete_to_line_end', | ||
| 57 | 'Ctrl+X' => 'cut', | ||
| 58 | 'Shift+Delete' => 'cut', | ||
| 59 | 'Ctrl+V' => 'paste', | ||
| 60 | 'Shift+Insert' => 'paste', | ||
| 61 | 'Ctrl+Z' => 'undo', | ||
| 62 | 'Ctrl+Y' => 'redo', | ||
| 63 | ); | ||
| 64 | |||
| 65 | # A text that must not show (a password) has no words to move or delete | ||
| 66 | # by, which would tell where its spaces are: these keys act on the | ||
| 67 | # whole text instead. It cannot be copied or cut either. | ||
| 68 | my %WHOLE_TEXT_KEY_FOR = ( | ||
| 69 | 'Ctrl+Left' => 'Home', | ||
| 70 | 'Ctrl+Shift+Left' => 'Shift+Home', | ||
| 71 | 'Ctrl+Right' => 'End', | ||
| 72 | 'Ctrl+Shift+Right' => 'Shift+End', | ||
| 73 | 'Ctrl+W' => 'Ctrl+U', | ||
| 74 | 'Ctrl+Delete' => 'Ctrl+K', | ||
| 75 | ); | ||
| 76 | my %COPIES = map { $_ => 1 } 'Ctrl+X', 'Shift+Delete', 'Ctrl+Insert'; | ||
| 77 | |||
| 78 | field $editor :reader = Term::Fabulous::Editor->new( multi_line => __CLASS__->is_multi_line ); | ||
| 79 | |||
| 80 | field $placeholder :param = ''; | ||
| 81 | field $read_only :param = 0; | ||
| 82 | |||
| 83 | # The explicit accept spec; undef takes the validator's suggestion. | ||
| 84 | field $accept :param = undef; | ||
| 85 | |||
| 86 | # How the text is laid out in the buffer: one row without wrapping | ||
| 87 | # unless a subclass says otherwise. | ||
| 88 | field $view :reader; | ||
| 89 | |||
| 90 | # Mouse: whether a press in the widget started a drag, and the time and | ||
| 91 | # position of the last press, for double clicks. | ||
| 92 | field $dragging = 0; | ||
| 93 | field @last_press; | ||
| 94 | |||
| 95 | ADJUST :params ( :$value = undef, :$max_length = undef ) { | ||
| 96 | weaken( my $weak_self = $self ); | ||
| 97 | $view = Term::Fabulous::TextView->new( editor => $editor, display => sub ($cluster) { $weak_self->display_cluster($cluster) } ); | ||
| 98 | |||
| 99 | $read_only = boolean( $self, read_only => $read_only ); | ||
| 100 | $placeholder = string( $self, placeholder => $placeholder ); | ||
| 101 | $self->max_length($max_length) if defined $max_length; | ||
| 102 | $self->_apply_accept; | ||
| 103 | $self->value($value) if defined $value; | ||
| 104 | } | ||
| 105 | |||
| 106 | # Runs an editor call for a public method; the editor's errors are | ||
| 107 | # reworded to name this widget, which is the class the caller used. | ||
| 108 | method _in_editor ($code) { | ||
| 109 | try { | ||
| 110 | return $code->(); | ||
| 111 | } | ||
| 112 | catch ($error) { | ||
| 113 | $error =~ s/\ATerm::Fabulous::Editor:/ref($self) . ':'/e; | ||
| 114 | die $error; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | method is_multi_line :common () { | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | # True when the text is not shown as it is (a masked password field). | ||
| 123 | method hides_text () { | ||
| 124 | return 0; | ||
| 125 | } | ||
| 126 | |||
| 127 | # --------------------------------------------------------------------- | ||
| 128 | # Properties | ||
| 129 | # --------------------------------------------------------------------- | ||
| 130 | |||
| 131 | method value (@new) { | ||
| 132 | return $editor->text unless @new; | ||
| 133 | $self->_in_editor( sub { $editor->set_text( $new[0] ) } ); | ||
| 134 | $self->mark_changed; | ||
| 135 | return $editor->text; | ||
| 136 | } | ||
| 137 | |||
| 138 | method max_length (@new) { | ||
| 139 | return $editor->max_length unless @new; | ||
| 140 | $self->_in_editor( sub { $editor->set_max_length( $new[0] ) } ); | ||
| 141 | return $editor->max_length; | ||
| 142 | } | ||
| 143 | |||
| 144 | method accept (@new) { | ||
| 145 | return $accept unless @new; | ||
| 146 | my $previous = $accept; | ||
| 147 | $accept = $new[0]; | ||
| 148 | try { | ||
| 149 | $self->_apply_accept; | ||
| 150 | } | ||
| 151 | catch ($error) { | ||
| 152 | $accept = $previous; | ||
| 153 | die $error; | ||
| 154 | } | ||
| 155 | return $accept; | ||
| 156 | } | ||
| 157 | |||
| 158 | # The editor takes the explicit accept spec, or the validator's | ||
| 159 | # suggestion. | ||
| 160 | method _apply_accept () { | ||
| 161 | my $validator = $self->validator; | ||
| 162 | my $spec = $accept // ( defined $validator ? $validator->accept : undef ); | ||
| 163 | $self->_in_editor( sub { $editor->set_accept($spec) } ); | ||
| 164 | return; | ||
| 165 | } | ||
| 166 | |||
| 167 | method validator_changed :override () { | ||
| 168 | $self->_apply_accept; | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | method placeholder (@new) { | ||
| 173 | return $placeholder unless @new; | ||
| 174 | $placeholder = string( $self, placeholder => $new[0] ); | ||
| 175 | $self->mark_changed; | ||
| 176 | return $placeholder; | ||
| 177 | } | ||
| 178 | |||
| 179 | method read_only (@new) { | ||
| 180 | return $read_only unless @new; | ||
| 181 | $read_only = boolean( $self, read_only => $new[0] ); | ||
| 182 | return $read_only; | ||
| 183 | } | ||
| 184 | |||
| 185 | method theme_family :common () { | ||
| 186 | return 'text_input'; | ||
| 187 | } | ||
| 188 | |||
| 189 | method themed_params :common () { | ||
| 190 | return ( $class->SUPER::themed_params, placeholder_color => [ 'placeholder', 'normal', 'cell_color' ], selection_color => [ 'selection', 'normal', 'cell_color' ] ); | ||
| 191 | } | ||
| 192 | |||
| 193 | method placeholder_color (@new) { | ||
| 194 | return @new ? $self->set_look( placeholder_color => $new[0] ) : $self->look_value('placeholder_color'); | ||
| 195 | } | ||
| 196 | |||
| 197 | method selection_color (@new) { | ||
| 198 | return @new ? $self->set_look( selection_color => $new[0] ) : $self->look_value('selection_color'); | ||
| 199 | } | ||
| 200 | |||
| 201 | method layout_properties :common () { | ||
| 202 | return ( | ||
| 203 | $class->SUPER::layout_properties, | ||
| 204 | value => 'scalar', | ||
| 205 | placeholder => 'scalar', | ||
| 206 | max_length => 'scalar', | ||
| 207 | accept => 'scalar', | ||
| 208 | read_only => 'boolean', | ||
| 209 | ); | ||
| 210 | } | ||
| 211 | |||
| 212 | # The max_length, accept and validator of a layout come first, so they | ||
| 213 | # limit the value wherever it stands. | ||
| 214 | my %LIMITS_VALUE = map { $_ => 1 } qw(max_length accept validator); | ||
| 215 | |||
| 216 | method apply_layout_settings :override (@settings) { | ||
| 217 | return $self->SUPER::apply_layout_settings( ( grep { $LIMITS_VALUE{ $_->[0] } } @settings ), ( grep { !$LIMITS_VALUE{ $_->[0] } } @settings ) ); | ||
| 218 | } | ||
| 219 | |||
| 220 | # --------------------------------------------------------------------- | ||
| 221 | # Keys | ||
| 222 | # --------------------------------------------------------------------- | ||
| 223 | |||
| 224 | method handle_key ($event) { | ||
| 225 | my $text = $event->text; | ||
| 226 | return $self->apply_edit( $editor->type($text) ) if defined $text && !$read_only; | ||
| 227 | |||
| 228 | my $name = $event->main_key_name // return 0; | ||
| 229 | if ( $self->hides_text ) { | ||
| 230 | return 0 if $COPIES{$name}; | ||
| 231 | $name = $WHOLE_TEXT_KEY_FOR{$name} // $name; | ||
| 232 | } | ||
| 233 | if ( my $movement = $MOVEMENT_BY_KEY{$name} ) { | ||
| 234 | my ( $method, $extend ) = @$movement; | ||
| 235 | $editor->$method($extend); | ||
| 236 | return $self->_view_changed; | ||
| 237 | } | ||
| 238 | if ( my $edit = $EDIT_BY_KEY{$name} ) { | ||
| 239 | return 0 if $read_only; | ||
| 240 | return $self->apply_edit( $editor->$edit ); | ||
| 241 | } | ||
| 242 | if ( $name eq 'Ctrl+A' ) { | ||
| 243 | $editor->select_all; | ||
| 244 | return $self->_view_changed; | ||
| 245 | } | ||
| 246 | if ( $name eq 'Ctrl+Insert' ) { | ||
| 247 | $editor->copy; | ||
| 248 | return 1; | ||
| 249 | } | ||
| 250 | return 0; | ||
| 251 | } | ||
| 252 | |||
| 253 | # The editor changed the cursor, the selection or the text: the next | ||
| 254 | # frame shows it. Returns 1, for handlers that used their event. | ||
| 255 | method _view_changed () { | ||
| 256 | $self->mark_changed; | ||
| 257 | return 1; | ||
| 258 | } | ||
| 259 | |||
| 260 | # After an editor edit: fires Change when the text changed. Returns 1, | ||
| 261 | # the key was used either way. | ||
| 262 | method apply_edit ($changed) { | ||
| 263 | $self->_view_changed; | ||
| 264 | $self->fire_change( $editor->text ) if $changed; | ||
| 265 | return 1; | ||
| 266 | } | ||
| 267 | |||
| 268 | # --------------------------------------------------------------------- | ||
| 269 | # Mouse: a press places the cursor, a Shift+click extends the | ||
| 270 | # selection to it, a double click selects a word (the whole text when | ||
| 271 | # it is hidden), dragging selects. | ||
| 272 | # --------------------------------------------------------------------- | ||
| 273 | |||
| 274 | method handle_mouse ($event) { | ||
| 275 | my $key = $event->key; | ||
| 276 | if ( $key == TB_KEY_MOUSE_RELEASE ) { | ||
| 277 | $dragging = 0; | ||
| 278 | return 1; | ||
| 279 | } | ||
| 280 | return 0 unless $key == TB_KEY_MOUSE_LEFT; | ||
| 281 | |||
| 282 | my ( $column, $row ) = $self->cell_at($event); | ||
| 283 | return 1 unless defined $column; | ||
| 284 | my @position = $view->position_at( $column, $row ); | ||
| 285 | |||
| 286 | if ( $event->modifiers & TB_MOD_MOTION ) { | ||
| 287 | $editor->move_to( @position, 1 ) if $dragging; | ||
| 288 | return $self->_view_changed; | ||
| 289 | } | ||
| 290 | |||
| 291 | my $now = time; | ||
| 292 | if ( $event->modifiers & TB_MOD_SHIFT ) { | ||
| 293 | $editor->move_to( @position, 1 ); | ||
| 294 | @last_press = (); | ||
| 295 | } | ||
| 296 | elsif ( @last_press && $now - $last_press[0] <= DOUBLE_CLICK_SECONDS && "@last_press[1, 2]" eq "@position" ) { | ||
| 297 | $self->hides_text ? $editor->select_all : $editor->select_word_at(@position); | ||
| 298 | @last_press = (); | ||
| 299 | } | ||
| 300 | else { | ||
| 301 | $editor->move_to( @position, 0 ); | ||
| 302 | @last_press = ( $now, @position ); | ||
| 303 | } | ||
| 304 | $dragging = 1; | ||
| 305 | return $self->_view_changed; | ||
| 306 | } | ||
| 307 | |||
| 308 | # --------------------------------------------------------------------- | ||
| 309 | # Painting | ||
| 310 | # --------------------------------------------------------------------- | ||
| 311 | |||
| 312 | # Edits made through the editor, also by the program, show when the | ||
| 313 | # frame is drawn: the view follows the cursor whenever the editor or | ||
| 314 | # the size changed. | ||
| 315 | method refresh :override () { | ||
| 316 | $view->set_size( $self->columns, $self->rows )->follow_cursor; | ||
| 317 | return $self->SUPER::refresh; | ||
| 318 | } | ||
| 319 | |||
| 320 | method paint_key :override () { | ||
| 321 | return ( $self->SUPER::paint_key, $editor->revision, $view->top_row, $view->left_column ); | ||
| 322 | } | ||
| 323 | |||
| 324 | method focus_changed :override ($is_focused) { | ||
| 325 | $dragging = 0; | ||
| 326 | return; | ||
| 327 | } | ||
| 328 | |||
| 329 | # How a cluster of the text is shown; a password field masks it. | ||
| 330 | method display_cluster ($cluster) { | ||
| 331 | return sanitize_text($cluster); | ||
| 332 | } | ||
| 333 | |||
| 334 | method paint () { | ||
| 335 | $self->paint_focus_background; | ||
| 336 | return $self->_paint_placeholder if $editor->is_empty && length $placeholder; | ||
| 337 | |||
| 338 | my $width = $view->text_columns; | ||
| 339 | my $y = 0; | ||
| 340 | foreach my $row ( $view->visible_rows ) { | ||
| 341 | my ( $line, $from, $to, $is_last, $shown ) = @$row; | ||
| 342 | my $end_x = $self->_paint_part( $y, $line, $shown, $to, $is_last, $from, $width ); | ||
| 343 | $self->_paint_selected_line_break( $y, $line, $end_x, $width ) if $is_last; | ||
| 344 | $y++; | ||
| 345 | } | ||
| 346 | return; | ||
| 347 | } | ||
| 348 | |||
| 349 | sub _is_selected ( $selection, $row, $offset ) { | ||
| 350 | my ( $row_0, $offset_0, $row_1, $offset_1 ) = @$selection; | ||
| 351 | return 0 unless defined $row_0; | ||
| 352 | return 0 if $row < $row_0 || ( $row == $row_0 && $offset < $offset_0 ); | ||
| 353 | return 0 if $row > $row_1 || ( $row == $row_1 && $offset >= $offset_1 ); | ||
| 354 | return 1; | ||
| 355 | } | ||
| 356 | |||
| 357 | # Paints the part [from, to) of a line on buffer row $y, scrolled as | ||
| 358 | # the view is, with the selection and (when $cursor_at_end allows a | ||
| 359 | # cursor at $to) the cursor. Text in [hidden_from, from) belongs to the | ||
| 360 | # row unseen; a cursor on it is shown at $from. Returns the column after | ||
| 361 | # the text. | ||
| 362 | method _paint_part ( $y, $row, $from, $to, $cursor_at_end, $hidden_from, $width ) { | ||
| 363 | my @selection = $editor->selection; | ||
| 364 | my ( $cursor_row, $cursor_offset ) = $editor->cursor; | ||
| 365 | my $has_cursor = $self->is_focused && $cursor_row == $row; | ||
| 366 | $cursor_offset = $from if $cursor_offset >= $hidden_from && $cursor_offset < $from; | ||
| 367 | my $fg = $self->foreground_attr; | ||
| 368 | my $bg = $self->focus_background_attr; | ||
| 369 | my $selected = $self->color_attr( $self->look_value('selection_color') ); | ||
| 370 | |||
| 371 | my $x = -$view->left_column; | ||
| 372 | foreach my $cluster ( $view->clusters( $row, $from, $to ) ) { | ||
| 373 | my ( $offset, $display, $columns ) = @$cluster; | ||
| 374 | my $cell_bg = _is_selected( \@selection, $row, $offset ) ? $selected : $bg; | ||
| 375 | my $cell_fg = $has_cursor && $offset == $cursor_offset ? $self->reverse_attr($fg) : $fg; | ||
| 376 | $self->put_attrs( $x, $y, $display, $cell_fg, $cell_bg ) if $x >= 0 && $x + $columns <= $width; | ||
| 377 | $x += $columns; | ||
| 378 | } | ||
| 379 | $self->put_attrs( $x, $y, ' ', $self->reverse_attr($fg), $bg ) | ||
| 380 | if $has_cursor && $cursor_at_end && $cursor_offset == $to && $x >= 0 && $x < $width; | ||
| 381 | return $x; | ||
| 382 | } | ||
| 383 | |||
| 384 | # A selection reaching over the end of a line shows a selected cell | ||
| 385 | # after it. | ||
| 386 | method _paint_selected_line_break ( $y, $line, $x, $width ) { | ||
| 387 | return if $line == $editor->line_count - 1 || $x < 0 || $x >= $width; | ||
| 388 | my ( $row_0, $offset_0, $row_1 ) = $editor->selection; | ||
| 389 | return unless defined $row_0 && $line < $row_1; | ||
| 390 | return if $line < $row_0 || ( $line == $row_0 && $offset_0 > length $editor->line($line) ); | ||
| 391 | |||
| 392 | my ( $cursor_row, $cursor_offset ) = $editor->cursor; | ||
| 393 | return if $self->is_focused && $cursor_row == $line && $cursor_offset == length $editor->line($line); | ||
| 394 | $self->put_attrs( $x, $y, ' ', undef, $self->color_attr( $self->look_value('selection_color') ) ); | ||
| 395 | return; | ||
| 396 | } | ||
| 397 | |||
| 398 | method _paint_placeholder () { | ||
| 399 | my $bg = $self->focus_background_attr; | ||
| 400 | $self->paint_text( 0, 0, $placeholder, $self->color_attr( $self->look_value('placeholder_color') ), $bg ); | ||
| 401 | return unless $self->is_focused; | ||
| 402 | |||
| 403 | my ($first) = grapheme_clusters($placeholder); | ||
| 404 | $self->put_attrs( 0, 0, $first // ' ', $self->reverse_attr( $self->foreground_attr ), $bg ); | ||
| 405 | return; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | 1; | ||
| 410 | |||
| 411 | __END__ |