| 1 | package Term::Fabulous::Widget::RichText; | ||
| 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::FocusableText; | ||
| 11 | |||
| 12 | our $VERSION = '0.02'; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::RichText | ||
| 15 | :isa(Term::Fabulous::Widget::FocusableText) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use Clay::UI::Enum::Result; | ||
| 19 | use Clay::UI::Revision qw(bump_revision); | ||
| 20 | use List::Util qw(first); | ||
| 21 | use Scalar::Util qw(weaken); | ||
| 22 | use Term::Fabulous::Check qw(non_negative_integer); | ||
| 23 | use Term::Fabulous::Color; | ||
| 24 | use Term::Fabulous::Event::LinkActivate; | ||
| 25 | use Term::Fabulous::Render::Attr qw(color_attr); | ||
| 26 | use Term::Fabulous::Termbox qw(TB_KEY_MOUSE_LEFT TB_UNDERLINE); | ||
| 27 | use Term::Fabulous::Text::Markup qw(parse_markup); | ||
| 28 | use Term::Fabulous::Text::Style qw(compose_styles style); | ||
| 29 | use Term::Fabulous::Theme; | ||
| 30 | |||
| 31 | # The markup the text, spans and links came from, or undef when they | ||
| 32 | # were given directly. | ||
| 33 | field $markup :param = undef; | ||
| 34 | |||
| 35 | # The spans as given, checked in ADJUST; the array holds the checked ones. | ||
| 36 | field $given_spans :param(spans) = undef; | ||
| 37 | field @spans; # [ $start, $end, $style ] | ||
| 38 | |||
| 39 | # The links as given, checked in ADJUST; the array holds the checked | ||
| 40 | # ones, ordered by their start, none overlapping another. | ||
| 41 | field $given_links :param(links) = undef; | ||
| 42 | field @links; # [ $start, $end, $target ] | ||
| 43 | |||
| 44 | # The selected link (the one Enter follows) and the one under the | ||
| 45 | # mouse pointer, as indices into @links. | ||
| 46 | field $selected_link = undef; | ||
| 47 | field $hovered_link = undef; | ||
| 48 | |||
| 49 | # The runs of the whole text (see line_styles), computed on demand and | ||
| 50 | # dropped whenever the text, the spans, the links or the theme change. | ||
| 51 | field $runs = undef; | ||
| 52 | field $runs_generation = -1; | ||
| 53 | |||
| 54 | # The keys a focused RichText uses; an action returns whether it did | ||
| 55 | # anything, and a key that did nothing goes on to the ancestors. | ||
| 56 | my %ACTION_OF_KEY = ( | ||
| 57 | Left => sub ($self) { $self->select_previous_link }, | ||
| 58 | Right => sub ($self) { $self->select_next_link }, | ||
| 59 | Enter => sub ($self) { $self->activate_link }, | ||
| 60 | ); | ||
| 61 | |||
| 62 | # Markup is parsed before the parent sees the text, so that a markup | ||
| 63 | # string is just another way to give text, spans and links. | ||
| 64 | sub BUILDARGS ( $class, %params ) { | ||
| 65 | return $class->SUPER::BUILDARGS(%params) unless defined $params{markup}; | ||
| 66 | die "Term::Fabulous::Widget::RichText: markup cannot be given together with text or spans or links" if exists $params{text} || exists $params{spans} || exists $params{links}; | ||
| 67 | my ( $text, $spans, $links ) = parse_markup( $params{markup} ); | ||
| 68 | return $class->SUPER::BUILDARGS( %params, text => $text, spans => $spans, links => $links ); | ||
| 69 | } | ||
| 70 | |||
| 71 | ADJUST { | ||
| 72 | die "Term::Fabulous::Widget::RichText: spans must be an array reference of [start, end, style]" if defined $given_spans && ref $given_spans ne 'ARRAY'; | ||
| 73 | die "Term::Fabulous::Widget::RichText: links must be an array reference of [start, end, target]" if defined $given_links && ref $given_links ne 'ARRAY'; | ||
| 74 | @spans = map { $self->_checked_span($_) } @{ $given_spans // [] }; | ||
| 75 | $given_spans = undef; | ||
| 76 | $self->_insert_link( $self->_checked_link($_) ) foreach @{ $given_links // [] }; | ||
| 77 | $given_links = undef; | ||
| 78 | |||
| 79 | weaken( my $weak_self = $self ); | ||
| 80 | my $continue = Clay::UI::Enum::Result->CONTINUE; | ||
| 81 | $self->on( | ||
| 82 | KeyPress => sub ($event) { | ||
| 83 | my $action = $ACTION_OF_KEY{ $event->main_key_name // '' } // return $continue; | ||
| 84 | return $action->($weak_self) ? undef : $continue; | ||
| 85 | } | ||
| 86 | ); | ||
| 87 | $self->on( OnFocus => sub ($event) { $weak_self->select_next_link unless defined $weak_self->selected_link; return $continue } ); | ||
| 88 | $self->on( OnBlur => sub ($event) { $weak_self->select_link(undef); return $continue } ); | ||
| 89 | } | ||
| 90 | |||
| 91 | # A copy of a span with its offsets checked against the text and its | ||
| 92 | # style normalized. | ||
| 93 | method _checked_span ($span) { | ||
| 94 | die "Term::Fabulous::Widget::RichText: a span is [start, end, style]" unless ref $span eq 'ARRAY' && @$span == 3; | ||
| 95 | my ( $start, $end, $style ) = @$span; | ||
| 96 | return $self->_span( $style, $start, $end ); | ||
| 97 | } | ||
| 98 | |||
| 99 | method _span ( $style, $start, $end ) { | ||
| 100 | return [ $self->_checked_range( 'span', $start, $end ), style($style) ]; | ||
| 101 | } | ||
| 102 | |||
| 103 | method _checked_range ( $what, $start, $end ) { | ||
| 104 | $start = non_negative_integer( $self, "$what start", $start ); | ||
| 105 | $end = non_negative_integer( $self, "$what end", $end ); | ||
| 106 | my $length = length $self->text; | ||
| 107 | die "Term::Fabulous::Widget::RichText: $what end $end lies before its start $start" if $end < $start; | ||
| 108 | die "Term::Fabulous::Widget::RichText: $what end $end lies past the text ($length characters)" if $end > $length; | ||
| 109 | return ( $start, $end ); | ||
| 110 | } | ||
| 111 | |||
| 112 | method _checked_link ($link) { | ||
| 113 | die "Term::Fabulous::Widget::RichText: a link is [start, end, target]" unless ref $link eq 'ARRAY' && @$link == 3; | ||
| 114 | my ( $start, $end, $target ) = @$link; | ||
| 115 | return $self->_link( $target, $start, $end ); | ||
| 116 | } | ||
| 117 | |||
| 118 | method _link ( $target, $start, $end ) { | ||
| 119 | die "Term::Fabulous::Widget::RichText: a link needs a target" unless defined $target; | ||
| 120 | ( $start, $end ) = $self->_checked_range( 'link', $start, $end ); | ||
| 121 | die "Term::Fabulous::Widget::RichText: a link must not be empty, got $start to $end" if $start == $end; | ||
| 122 | return [ $start, $end, $target ]; | ||
| 123 | } | ||
| 124 | |||
| 125 | # Puts a checked link in its place by start; the selected and the | ||
| 126 | # hovered link stay the same links. | ||
| 127 | method _insert_link ($link) { | ||
| 128 | my ( $start, $end ) = @$link; | ||
| 129 | my $overlapped = first { $_->[0] < $end && $start < $_->[1] } @links; | ||
| 130 | die "Term::Fabulous::Widget::RichText: the link $start to $end overlaps the link $overlapped->[0] to $overlapped->[1]" if defined $overlapped; | ||
| 131 | my $position = first { $links[$_][0] > $start } 0 .. $#links; | ||
| 132 | $position //= scalar @links; | ||
| 133 | splice @links, $position, 0, $link; | ||
| 134 | $selected_link++ if defined $selected_link && $selected_link >= $position; | ||
| 135 | $hovered_link++ if defined $hovered_link && $hovered_link >= $position; | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | method _forget_runs () { | ||
| 140 | $runs = undef; | ||
| 141 | bump_revision(); | ||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | # Whether the widget can take the focus may have changed with its links. | ||
| 146 | method _links_changed () { | ||
| 147 | $self->_forget_runs; | ||
| 148 | $self->focus_eligibility_changed; | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | # Setting the text drops the spans and the links: they pointed into the | ||
| 153 | # old one. | ||
| 154 | method text :override (@new) { | ||
| 155 | return $self->SUPER::text unless @new; | ||
| 156 | my $text = $self->SUPER::text(@new); | ||
| 157 | @spans = (); | ||
| 158 | $markup = undef; | ||
| 159 | $self->_drop_links; | ||
| 160 | return $text; | ||
| 161 | } | ||
| 162 | |||
| 163 | method _drop_links () { | ||
| 164 | $self->_forget_links; | ||
| 165 | $self->_links_changed; | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | |||
| 169 | # Without telling the focus: a RichText that gets new links at once | ||
| 170 | # keeps the focus. | ||
| 171 | method _forget_links () { | ||
| 172 | @links = (); | ||
| 173 | ( $selected_link, $hovered_link ) = (); | ||
| 174 | return; | ||
| 175 | } | ||
| 176 | |||
| 177 | method markup (@new) { | ||
| 178 | return $markup unless @new; | ||
| 179 | my ( $text, $spans, $links ) = parse_markup( $new[0] ); | ||
| 180 | $self->SUPER::text($text); | ||
| 181 | @spans = @$spans; | ||
| 182 | $self->_forget_links; | ||
| 183 | $self->_insert_link($_) foreach @$links; | ||
| 184 | $markup = $new[0]; | ||
| 185 | $self->_links_changed; | ||
| 186 | return $markup; | ||
| 187 | } | ||
| 188 | |||
| 189 | method spans () { | ||
| 190 | return [ map { [ $_->[0], $_->[1], { %{ $_->[2] } } ] } @spans ]; | ||
| 191 | } | ||
| 192 | |||
| 193 | method stylize ( $style, $start = 0, $end = undef ) { | ||
| 194 | push @spans, $self->_span( $style, $start, $end // length $self->text ); | ||
| 195 | $markup = undef; | ||
| 196 | $self->_forget_runs; | ||
| 197 | return $self; | ||
| 198 | } | ||
| 199 | |||
| 200 | method clear_spans () { | ||
| 201 | @spans = (); | ||
| 202 | $markup = undef; | ||
| 203 | $self->_forget_runs; | ||
| 204 | return $self; | ||
| 205 | } | ||
| 206 | |||
| 207 | # --------------------------------------------------------------------- | ||
| 208 | # Links | ||
| 209 | # --------------------------------------------------------------------- | ||
| 210 | |||
| 211 | method links () { | ||
| 212 | return [ map { [@$_] } @links ]; | ||
| 213 | } | ||
| 214 | |||
| 215 | method add_link ( $target, $start = 0, $end = undef ) { | ||
| 216 | $self->_insert_link( $self->_link( $target, $start, $end // length $self->text ) ); | ||
| 217 | $markup = undef; | ||
| 218 | $self->_links_changed; | ||
| 219 | return $self; | ||
| 220 | } | ||
| 221 | |||
| 222 | method clear_links () { | ||
| 223 | $markup = undef; | ||
| 224 | $self->_drop_links; | ||
| 225 | return $self; | ||
| 226 | } | ||
| 227 | |||
| 228 | method link_at ($offset) { | ||
| 229 | $offset = non_negative_integer( $self, offset => $offset ); | ||
| 230 | return first { $links[$_][0] <= $offset && $offset < $links[$_][1] } 0 .. $#links; | ||
| 231 | } | ||
| 232 | |||
| 233 | method _checked_link_index ( $what, $index ) { | ||
| 234 | return undef unless defined $index; | ||
| 235 | $index = non_negative_integer( $self, $what, $index ); | ||
| 236 | die "Term::Fabulous::Widget::RichText: $what $index is not a link; the text has " . scalar(@links) . " links" if $index > $#links; | ||
| 237 | return $index; | ||
| 238 | } | ||
| 239 | |||
| 240 | method selected_link () { | ||
| 241 | return $selected_link; | ||
| 242 | } | ||
| 243 | |||
| 244 | method select_link ($index) { | ||
| 245 | $index = $self->_checked_link_index( link => $index ); | ||
| 246 | return $self if ( $index // -1 ) == ( $selected_link // -1 ); | ||
| 247 | $selected_link = $index; | ||
| 248 | $self->_forget_runs; | ||
| 249 | return $self; | ||
| 250 | } | ||
| 251 | |||
| 252 | # The selection stops at the first and the last link. | ||
| 253 | method select_next_link () { | ||
| 254 | my $next = defined $selected_link ? $selected_link + 1 : 0; | ||
| 255 | return 0 if $next > $#links; | ||
| 256 | $self->select_link($next); | ||
| 257 | return 1; | ||
| 258 | } | ||
| 259 | |||
| 260 | method select_previous_link () { | ||
| 261 | my $previous = defined $selected_link ? $selected_link - 1 : $#links; | ||
| 262 | return 0 if $previous < 0; | ||
| 263 | $self->select_link($previous); | ||
| 264 | return 1; | ||
| 265 | } | ||
| 266 | |||
| 267 | method activate_link (@index) { | ||
| 268 | my $index = @index ? $self->_checked_link_index( link => $index[0] ) : $selected_link; | ||
| 269 | return 0 unless defined $index; | ||
| 270 | my ( $start, $end, $target ) = @{ $links[$index] }; | ||
| 271 | $self->fire_event( Term::Fabulous::Event::LinkActivate->new( link => $target, index => $index, start => $start, end => $end ) ); | ||
| 272 | return 1; | ||
| 273 | } | ||
| 274 | |||
| 275 | method hovered_link () { | ||
| 276 | return $hovered_link; | ||
| 277 | } | ||
| 278 | |||
| 279 | method hover_link ($index) { | ||
| 280 | $index = $self->_checked_link_index( link => $index ); | ||
| 281 | return $self if ( $index // -1 ) == ( $hovered_link // -1 ); | ||
| 282 | $hovered_link = $index; | ||
| 283 | $self->_forget_runs; | ||
| 284 | return $self; | ||
| 285 | } | ||
| 286 | |||
| 287 | # A RichText takes the focus while it has links to select. | ||
| 288 | method accepts_focus :override () { | ||
| 289 | return @links ? 1 : 0; | ||
| 290 | } | ||
| 291 | |||
| 292 | # --------------------------------------------------------------------- | ||
| 293 | # Clicks | ||
| 294 | # --------------------------------------------------------------------- | ||
| 295 | |||
| 296 | method _click_details :override ($offset) { | ||
| 297 | my $index = $self->link_at($offset); | ||
| 298 | return ( | ||
| 299 | spans => [ grep { $_->[0] <= $offset && $offset < $_->[1] } @{ $self->spans } ], | ||
| 300 | defined $index ? ( link => $links[$index][2], link_index => $index ) : (), | ||
| 301 | ); | ||
| 302 | } | ||
| 303 | |||
| 304 | # A left click on a link selects and follows it. | ||
| 305 | method click_at :override ( $offset, $button, $x, $y ) { | ||
| 306 | $self->SUPER::click_at( $offset, $button, $x, $y ); | ||
| 307 | return $self unless $button == TB_KEY_MOUSE_LEFT; | ||
| 308 | my $index = $self->link_at($offset) // return $self; | ||
| 309 | $self->select_link($index); | ||
| 310 | $self->activate_link($index); | ||
| 311 | return $self; | ||
| 312 | } | ||
| 313 | |||
| 314 | # --------------------------------------------------------------------- | ||
| 315 | # Painting | ||
| 316 | # --------------------------------------------------------------------- | ||
| 317 | |||
| 318 | # The runs the renderer paints a line with: the part of the text's runs | ||
| 319 | # that covers the characters [offset, offset + length). | ||
| 320 | method line_styles ( $offset, $length ) { | ||
| 321 | return [] if $length <= 0; | ||
| 322 | my $generation = Term::Fabulous::Theme::generation(); | ||
| 323 | if ( !defined $runs || $runs_generation != $generation ) { | ||
| 324 | $runs = $self->_runs_of_text; | ||
| 325 | $runs_generation = $generation; | ||
| 326 | } | ||
| 327 | |||
| 328 | my @slice; | ||
| 329 | my $position = 0; | ||
| 330 | foreach my $run (@$runs) { | ||
| 331 | my $run_start = $position; | ||
| 332 | $position += $run->[0]; | ||
| 333 | next if $position <= $offset; | ||
| 334 | last if $run_start >= $offset + $length; | ||
| 335 | |||
| 336 | my $from = $run_start < $offset ? $offset : $run_start; | ||
| 337 | my $to = $position > $offset + $length ? $offset + $length : $position; | ||
| 338 | push @slice, [ $to - $from, @{$run}[ 1 .. 4 ] ]; | ||
| 339 | } | ||
| 340 | return \@slice; | ||
| 341 | } | ||
| 342 | |||
| 343 | # The whole text cut at every span and link boundary, each piece with | ||
| 344 | # the style its spans give it and, over them, the look of the link it | ||
| 345 | # lies in. A run is the tuple Term::Fabulous::Render::Text paints with: | ||
| 346 | # [ $characters, $set, $clear, $fg_attr, $bg_attr ], the style's bits | ||
| 347 | # and its colors as termbox attributes (undef: the color is not | ||
| 348 | # touched), converted once here rather than in every frame. | ||
| 349 | method _runs_of_text () { | ||
| 350 | my $length = length $self->text; | ||
| 351 | return [ [ $length, 0, 0, undef, undef ] ] unless ( @spans || @links ) && $length; | ||
| 352 | |||
| 353 | my %seen; | ||
| 354 | my @boundaries = sort { $a <=> $b } grep { !$seen{$_}++ } 0, $length, map { @{$_}[ 0, 1 ] } @spans, @links; | ||
| 355 | my @runs; | ||
| 356 | foreach my $index ( 0 .. $#boundaries - 1 ) { | ||
| 357 | my ( $from, $to ) = @boundaries[ $index, $index + 1 ]; | ||
| 358 | my @styles = map { $_->[2] } grep { $_->[0] <= $from && $_->[1] >= $to } @spans; | ||
| 359 | my $link = first { $links[$_][0] <= $from && $links[$_][1] >= $to } 0 .. $#links; | ||
| 360 | push @styles, $self->_link_style($link) if defined $link; | ||
| 361 | push @runs, [ $to - $from, _look_of(@styles) ]; | ||
| 362 | } | ||
| 363 | return \@runs; | ||
| 364 | } | ||
| 365 | |||
| 366 | # The look of a link from the theme's text.link slots: underlined in | ||
| 367 | # the normal and the hovered state, not underlined when selected. | ||
| 368 | method _link_style ($index) { | ||
| 369 | my $state | ||
| 370 | = defined $selected_link && $index == $selected_link ? 'selected' | ||
| 371 | : defined $hovered_link && $index == $hovered_link ? 'hovered' | ||
| 372 | : 'normal'; | ||
| 373 | my $underline = $state eq 'selected' ? 0 : TB_UNDERLINE; | ||
| 374 | return { | ||
| 375 | set => $underline, | ||
| 376 | clear => TB_UNDERLINE & ~$underline, | ||
| 377 | color => $self->look( 'link', $state ), | ||
| 378 | background => $self->look( 'link.background', $state ), | ||
| 379 | }; | ||
| 380 | } | ||
| 381 | |||
| 382 | # The set and clear bits and the color attributes that the given | ||
| 383 | # styles, stacked in order, give a piece of text. | ||
| 384 | sub _look_of (@styles) { | ||
| 385 | my $style = compose_styles(@styles); | ||
| 386 | return ( $style->{set}, $style->{clear}, _attr_of( $style->{color} ), _attr_of( $style->{background} ) ); | ||
| 387 | } | ||
| 388 | |||
| 389 | sub _attr_of ($rgba) { | ||
| 390 | return undef unless defined $rgba; | ||
| 391 | return color_attr( Term::Fabulous::Color->new( color => $rgba ) ); | ||
| 392 | } | ||
| 393 | |||
| 394 | method layout_properties :common () { | ||
| 395 | return ( | ||
| 396 | $class->SUPER::layout_properties, | ||
| 397 | markup => \&_parse_markup, | ||
| 398 | ); | ||
| 399 | } | ||
| 400 | |||
| 401 | method _parse_markup ($kid) { | ||
| 402 | my $markup_value = $self->kdl_argument($kid); | ||
| 403 | die "Term::Fabulous::Widget::RichText: 'markup' needs a string argument" unless $markup_value->is_string; | ||
| 404 | $self->markup( $markup_value->value ); | ||
| 405 | return; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | 1; | ||
| 410 | |||
| 411 | __END__ |