| 1 | # You may distribute under the terms of the GNU General Public License | ||
| 2 | # | ||
| 3 | # (C) Paul Evans, 2010-2023 -- leonerd@leonerd.org.uk | ||
| 4 | |||
| 5 | package Circle::FE::Term::Widget::Entry 0.240250; | ||
| 6 | |||
| 7 | 1 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use constant type => "Entry"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 0 | ▶ | use Tickit::Widget::Entry::Plugin::Completion; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | sub build | ||
| 14 | { | ||
| 15 | my $class = shift; | ||
| 16 | my ( $obj, $tab ) = @_; | ||
| 17 | |||
| 18 | my $autoclear = $obj->prop("autoclear"); | ||
| 19 | |||
| 20 | my $want_typing = $obj->can_property( "want_typing" ) && $obj->prop( "want_typing" ); | ||
| 21 | |||
| 22 | my $prehistory; | ||
| 23 | my $history_index; | ||
| 24 | |||
| 25 | my $pending_count = 0; | ||
| 26 | |||
| 27 | my $widget = Circle::FE::Term::Widget::Entry::Widget->new( | ||
| 28 | classes => $obj->prop( "classes" ), | ||
| 29 | tab => $tab, | ||
| 30 | |||
| 31 | on_enter => sub { | ||
| 32 | my ( $self, $line ) = @_; | ||
| 33 | |||
| 34 | $pending_count++; | ||
| 35 | $self->send_pending( $pending_count ); | ||
| 36 | |||
| 37 | $tab->adopt_future( | ||
| 38 | $obj->call_method( | ||
| 39 | enter => $self->text, | ||
| 40 | )->on_done( sub { | ||
| 41 | $pending_count--; | ||
| 42 | $self->send_pending( $pending_count ); | ||
| 43 | })->on_fail( sub { | ||
| 44 | my ( $message ) = @_; | ||
| 45 | warn "Failed while sending text:\n$message"; | ||
| 46 | $pending_count--; | ||
| 47 | $self->send_pending( $pending_count ); | ||
| 48 | }) | ||
| 49 | ); | ||
| 50 | |||
| 51 | $self->set_text( "" ) if $autoclear; | ||
| 52 | undef $history_index; | ||
| 53 | }, | ||
| 54 | |||
| 55 | ( $want_typing ? ( | ||
| 56 | on_typing => sub { | ||
| 57 | my ( $typing ) = @_; | ||
| 58 | $tab->adopt_future( $obj->call_method( typing => $typing ) ); | ||
| 59 | } | ||
| 60 | ) : () ), | ||
| 61 | ); | ||
| 62 | |||
| 63 | $tab->adopt_future( | ||
| 64 | $obj->watch_property_with_initial( | ||
| 65 | "text", | ||
| 66 | on_set => sub { | ||
| 67 | my ( $text ) = @_; | ||
| 68 | $text = "" unless defined $text; | ||
| 69 | $widget->set_text( $text ); | ||
| 70 | }, | ||
| 71 | ) | ||
| 72 | ); | ||
| 73 | |||
| 74 | $tab->adopt_future( | ||
| 75 | $obj->watch_property_with_initial( | ||
| 76 | "history", | ||
| 77 | on_updated => sub {}, # We ignore this, we just want a local cache | ||
| 78 | ) | ||
| 79 | ); | ||
| 80 | |||
| 81 | $tab->adopt_future( | ||
| 82 | $obj->watch_property_with_initial( | ||
| 83 | "completions", | ||
| 84 | on_updated => sub {}, # We ignore this, we just want a local cache | ||
| 85 | ) | ||
| 86 | ); | ||
| 87 | |||
| 88 | $widget->bind_keys( | ||
| 89 | Up => sub { | ||
| 90 | my $widget = shift; | ||
| 91 | |||
| 92 | my $history = $obj->prop("history"); | ||
| 93 | if( !defined $history_index ) { | ||
| 94 | $prehistory = $widget->text; | ||
| 95 | return 1 unless @$history; | ||
| 96 | $history_index = $#$history; | ||
| 97 | } | ||
| 98 | elsif( $history_index == 0 ) { | ||
| 99 | # Don't move | ||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | $history_index--; | ||
| 104 | } | ||
| 105 | |||
| 106 | my $line = $history->[$history_index]; | ||
| 107 | $widget->set_text( $line ); | ||
| 108 | $widget->set_position( length( $line ) ); # TODO: accept negative | ||
| 109 | |||
| 110 | return 1; | ||
| 111 | }, | ||
| 112 | Down => sub { | ||
| 113 | my $widget = shift; | ||
| 114 | |||
| 115 | my $history = $obj->prop("history"); | ||
| 116 | return 1 unless defined $history_index; | ||
| 117 | if( $history_index < $#$history ) { | ||
| 118 | $history_index++; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | $widget->set_text( $prehistory ); | ||
| 122 | undef $history_index; | ||
| 123 | return 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | my $line = $history->[$history_index]; | ||
| 127 | $widget->set_text( $line ); | ||
| 128 | $widget->set_position( length( $line ) ); | ||
| 129 | |||
| 130 | return 1; | ||
| 131 | }, | ||
| 132 | ); | ||
| 133 | |||
| 134 | Tickit::Widget::Entry::Plugin::Completion->apply( $widget, | ||
| 135 | ignore_case => 1, | ||
| 136 | append_after_word => "", | ||
| 137 | gen_words => sub { | ||
| 138 | my %args = @_; | ||
| 139 | my $match = qr/^\Q$args{word}\E/i; | ||
| 140 | my $at_sol = ( $args{wordpos} == 0 ); | ||
| 141 | |||
| 142 | my @matches; | ||
| 143 | foreach my $group ( values %{ $obj->prop("completions") } ) { | ||
| 144 | next if $group->prop("only_at_sol") and not $at_sol; | ||
| 145 | my $suffix = $at_sol ? $group->prop("suffix_sol") : ""; | ||
| 146 | length $suffix or $suffix = " "; | ||
| 147 | |||
| 148 | push @matches, map { "$_$suffix" } | ||
| 149 | grep { $_ =~ $match } | ||
| 150 | @{ $group->prop("items") }; | ||
| 151 | } | ||
| 152 | |||
| 153 | return @matches; | ||
| 154 | }, | ||
| 155 | ); | ||
| 156 | |||
| 157 | return $widget; | ||
| 158 | } | ||
| 159 | |||
| 160 | Tickit::Style->load_style( <<'EOF' ); | ||
| 161 | Circle::FE::Term::Widget::Entry::Widget.topic { | ||
| 162 | bg: "blue"; | ||
| 163 | } | ||
| 164 | |||
| 165 | EOF | ||
| 166 | |||
| 167 | use Object::Pad v0.800; | ||
| 168 | class Circle::FE::Term::Widget::Entry::Widget | ||
| 169 | :isa(Tickit::Widget::Entry); | ||
| 170 | |||
| 171 | Tickit::Window->VERSION( '0.42' ); | ||
| 172 | |||
| 173 | use Tickit::Async 0.21; # ->cancel_timer | ||
| 174 | |||
| 175 | use Tickit::Style -copy; | ||
| 176 | |||
| 177 | sub BUILDARGS | ||
| 178 | { | ||
| 179 | my $class = shift; | ||
| 180 | my %args = @_; | ||
| 181 | |||
| 182 | my $on_typing = $args{on_typing}; | ||
| 183 | |||
| 184 | if( $on_typing ) { | ||
| 185 | my $on_enter = $args{on_enter}; | ||
| 186 | $args{on_enter} = sub { | ||
| 187 | my ( $self ) = @_; | ||
| 188 | $self->stopped_typing; | ||
| 189 | $on_enter->( @_ ); | ||
| 190 | }; | ||
| 191 | } | ||
| 192 | |||
| 193 | return %args; | ||
| 194 | } | ||
| 195 | |||
| 196 | field $tab :param; | ||
| 197 | field $on_typing :param = undef; | ||
| 198 | |||
| 199 | field $typing_timer_id; | ||
| 200 | |||
| 201 | method on_key | ||
| 202 | { | ||
| 203 | my ( $ev ) = @_; | ||
| 204 | |||
| 205 | $tab->activated; | ||
| 206 | |||
| 207 | my $ret = $self->SUPER::on_key( @_ ); | ||
| 208 | |||
| 209 | if( $ret && $on_typing and length $self->text ) { | ||
| 210 | my $tickit = $self->window->tickit; | ||
| 211 | |||
| 212 | if( $typing_timer_id ) { | ||
| 213 | $tickit->cancel_timer( $typing_timer_id ); | ||
| 214 | } | ||
| 215 | else { | ||
| 216 | $self->started_typing; | ||
| 217 | } | ||
| 218 | |||
| 219 | $typing_timer_id = $tickit->timer( after => 5, sub { $self->stopped_typing }); | ||
| 220 | } | ||
| 221 | |||
| 222 | return $ret; | ||
| 223 | } | ||
| 224 | |||
| 225 | method started_typing | ||
| 226 | { | ||
| 227 | $on_typing->( 1 ); | ||
| 228 | } | ||
| 229 | |||
| 230 | method stopped_typing | ||
| 231 | { | ||
| 232 | $self->window->tickit->cancel_timer( $typing_timer_id ) if defined $typing_timer_id; | ||
| 233 | undef $typing_timer_id; | ||
| 234 | $on_typing->( 0 ); | ||
| 235 | } | ||
| 236 | |||
| 237 | field $pending_count; | ||
| 238 | field $pending_window; | ||
| 239 | |||
| 240 | method send_pending | ||
| 241 | { | ||
| 242 | my ( $count ) = @_; | ||
| 243 | |||
| 244 | $pending_count = $count; | ||
| 245 | |||
| 246 | if( $count ) { | ||
| 247 | my $win = $pending_window ||= do { | ||
| 248 | my $win = $self->window->make_hidden_sub( 0, $self->window->cols - 12, 1, 12 ); | ||
| 249 | my $countr = \$pending_count; | ||
| 250 | $win->bind_event( expose => sub { | ||
| 251 | my ( $win, undef, $info ) = @_; | ||
| 252 | my $rb = $info->rb; | ||
| 253 | |||
| 254 | $rb->goto( 0, 0 ); | ||
| 255 | $rb->text( "Sending $$countr..." ); | ||
| 256 | $rb->erase_to( 12 ); | ||
| 257 | }); | ||
| 258 | $win->pen->chattrs({ fg => "black", bg => "cyan", i => 1 }); | ||
| 259 | $win; | ||
| 260 | }; | ||
| 261 | |||
| 262 | if( !$win->is_visible ) { | ||
| 263 | # TODO: Use Tickit->timer when it comes out | ||
| 264 | $win->tickit->loop->watch_time( after => 0.5, code => sub { | ||
| 265 | $win->show if $pending_count and !$win->is_visible; | ||
| 266 | }); | ||
| 267 | } | ||
| 268 | |||
| 269 | $win->expose if $win->is_visible; | ||
| 270 | } | ||
| 271 | elsif( my $win = $pending_window ) { | ||
| 272 | $win->hide; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | 0x55AA; | ||