| 1 | package Term::Fabulous::Widget::Text; | ||
| 2 | |||
| 3 | 1 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Object::Pad 0.825; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 0 | ▶ | use Clay::UI::Role::Events::Emitter; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use Term::Fabulous::Role::MarkedText; | ||
| 12 | |||
| 13 | our $VERSION = '0.05'; | ||
| 14 | |||
| 15 | class Term::Fabulous::Widget::Text | ||
| 16 | :isa(Term::Fabulous::Widget::TextNode) | ||
| 17 | :does(Term::Fabulous::Role::CanParseLayout) | ||
| 18 | :does(Term::Fabulous::Role::Themed) | ||
| 19 | :does(Clay::UI::Role::Events::Emitter) | ||
| 20 | :does(Term::Fabulous::Role::MarkedText) | ||
| 21 | :strict(params) | ||
| 22 | { | ||
| 23 | use Clay::UI::Enum::Result; | ||
| 24 | use Clay::UI::Revision qw(bump_revision); | ||
| 25 | use Clay::XS qw(CLAY_TEXT_WRAP_WORDS CLAY_TEXT_WRAP_NEWLINES CLAY_TEXT_WRAP_NONE CLAY_TEXT_ALIGN_LEFT CLAY_TEXT_ALIGN_CENTER CLAY_TEXT_ALIGN_RIGHT); | ||
| 26 | use List::Util qw(first min sum0); | ||
| 27 | use Term::Fabulous::Check qw(boolean class_names color non_negative_integer one_of positive_integer); | ||
| 28 | use Term::Fabulous::Color; | ||
| 29 | use Term::Fabulous::Event::LinkActivate; | ||
| 30 | use Term::Fabulous::Event::TextClick; | ||
| 31 | use Term::Fabulous::Render::Attr qw(color_attr); | ||
| 32 | use Term::Fabulous::Termbox qw(TB_BOLD TB_ITALIC TB_UNDERLINE TB_KEY_MOUSE_LEFT); | ||
| 33 | use Term::Fabulous::Text::Markup qw(parse_markup); | ||
| 34 | use Term::Fabulous::Text::Marks; | ||
| 35 | use Term::Fabulous::Theme; | ||
| 36 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 37 | |||
| 38 | my %WRAP_MODE_BY_NAME = ( words => CLAY_TEXT_WRAP_WORDS, newlines => CLAY_TEXT_WRAP_NEWLINES, none => CLAY_TEXT_WRAP_NONE ); | ||
| 39 | my %TEXT_ALIGNMENT_BY_NAME = ( left => CLAY_TEXT_ALIGN_LEFT, center => CLAY_TEXT_ALIGN_CENTER, right => CLAY_TEXT_ALIGN_RIGHT ); | ||
| 40 | |||
| 41 | # The keys a focused text uses; an action returns whether it did | ||
| 42 | # anything, and a key that did nothing goes on to the ancestors. | ||
| 43 | my %ACTION_OF_KEY = ( | ||
| 44 | Left => sub ($self) { $self->select_previous_link }, | ||
| 45 | Right => sub ($self) { $self->select_next_link }, | ||
| 46 | Enter => sub ($self) { $self->activate_link }, | ||
| 47 | ); | ||
| 48 | |||
| 49 | field $id :param :reader = undef; | ||
| 50 | field $bold :param = 0; | ||
| 51 | field $italic :param = 0; | ||
| 52 | field $underline :param = 0; | ||
| 53 | field $classes :param = []; | ||
| 54 | |||
| 55 | # Whether the program gave a text_color (BUILDARGS says so); without | ||
| 56 | # one the theme's text color is drawn. The Clay::UI::Text field always | ||
| 57 | # holds a color; the role knows whether it is an explicit one. | ||
| 58 | field $_has_text_color :param(_has_text_color) = 0; | ||
| 59 | |||
| 60 | # The markup the text, spans and links came from, or undef when they | ||
| 61 | # were given directly. | ||
| 62 | field $markup :param = undef; | ||
| 63 | |||
| 64 | # The spans, the highlights (what a search marks, painted over the | ||
| 65 | # spans) and the links of the text, with the count of its changes | ||
| 66 | # (Term::Fabulous::Text::Marks). | ||
| 67 | field $marks; | ||
| 68 | |||
| 69 | # The selected link (the one Enter follows) and the one under the | ||
| 70 | # mouse pointer, as indices into the links. | ||
| 71 | field $selected_link = undef; | ||
| 72 | field $hovered_link = undef; | ||
| 73 | |||
| 74 | # Whether the key and focus listeners are registered: only once the | ||
| 75 | # text has a link, since only then it can take the focus. | ||
| 76 | field $listening = 0; | ||
| 77 | |||
| 78 | # The runs of the whole text (see line_styles), computed on demand and | ||
| 79 | # dropped whenever the text, its styles, its links or the theme change. | ||
| 80 | field $runs = undef; | ||
| 81 | field $runs_generation = -1; | ||
| 82 | |||
| 83 | # Markup is parsed before the parent sees the text, so that a markup | ||
| 84 | # string is just another way to give text, spans and links. Clay::UI | ||
| 85 | # validates text_color before any ADJUST of this class runs, so it is | ||
| 86 | # converted while the arguments are still a plain list. | ||
| 87 | sub BUILDARGS ( $class, %params ) { | ||
| 88 | if ( defined $params{markup} ) { | ||
| 89 | die "Term::Fabulous::Widget::Text: markup cannot be given together with text or spans or links" if exists $params{text} || exists $params{spans} || exists $params{links}; | ||
| 90 | @params{qw(text spans links)} = parse_markup( $params{markup} ); | ||
| 91 | } | ||
| 92 | return %params unless defined $params{text_color}; | ||
| 93 | $params{text_color} = color( $class, text_color => $params{text_color} ); | ||
| 94 | $params{_has_text_color} = 1; | ||
| 95 | return %params; | ||
| 96 | } | ||
| 97 | |||
| 98 | ADJUST :params ( :$spans //= [], :$links //= [] ) { | ||
| 99 | $bold = boolean( $self, bold => $bold ); | ||
| 100 | $italic = boolean( $self, italic => $italic ); | ||
| 101 | $underline = boolean( $self, underline => $underline ); | ||
| 102 | $classes = class_names( $self, classes => $classes ); | ||
| 103 | $self->set_look( text_color => $self->SUPER::text_color ) if $_has_text_color; | ||
| 104 | |||
| 105 | $marks = Term::Fabulous::Text::Marks->new( owner => ref($self), text => $self->SUPER::text, spans => $spans, links => $links ); | ||
| 106 | $self->_listen if $marks->link_count; | ||
| 107 | } | ||
| 108 | |||
| 109 | # --------------------------------------------------------------------- | ||
| 110 | # Style words and the text color | ||
| 111 | # --------------------------------------------------------------------- | ||
| 112 | |||
| 113 | method _set_style ( $name, $field_ref, @new ) { | ||
| 114 | return $$field_ref unless @new; | ||
| 115 | $$field_ref = boolean( $self, $name => $new[0] ); | ||
| 116 | bump_revision(); | ||
| 117 | return $$field_ref; | ||
| 118 | } | ||
| 119 | |||
| 120 | method bold (@new) { return $self->_set_style( bold => \$bold, @new ) } | ||
| 121 | method italic (@new) { return $self->_set_style( italic => \$italic, @new ) } | ||
| 122 | method underline (@new) { return $self->_set_style( underline => \$underline, @new ) } | ||
| 123 | |||
| 124 | # The termbox2 style bits the renderer adds to the text color. | ||
| 125 | method style_attrs () { | ||
| 126 | return ( $bold ? TB_BOLD : 0 ) | ( $italic ? TB_ITALIC : 0 ) | ( $underline ? TB_UNDERLINE : 0 ); | ||
| 127 | } | ||
| 128 | |||
| 129 | # The value a name stands for in %$value_by_name; an unknown name dies | ||
| 130 | # with the known ones. | ||
| 131 | sub _named ( $what, $value_by_name, $name ) { | ||
| 132 | return $value_by_name->{ one_of( 'Term::Fabulous::Widget::Text', $what, $name, keys %$value_by_name ) }; | ||
| 133 | } | ||
| 134 | |||
| 135 | # The reader returns the color the text is drawn in: the explicit one, | ||
| 136 | # or the theme's. undef is not a color; reset_look returns to the theme. | ||
| 137 | method text_color :override (@new) { | ||
| 138 | return $self->_explicit_text_color // $self->look('color') unless @new; | ||
| 139 | return $self->set_look( text_color => $self->SUPER::text_color( color( $self, text_color => $new[0] ) ) ); | ||
| 140 | } | ||
| 141 | |||
| 142 | method _explicit_text_color () { | ||
| 143 | return $self->has_look_override('text_color') ? $self->SUPER::text_color : undef; | ||
| 144 | } | ||
| 145 | |||
| 146 | # --------------------------------------------------------------------- | ||
| 147 | # The theme (Term::Fabulous::Role::Themed) | ||
| 148 | # --------------------------------------------------------------------- | ||
| 149 | |||
| 150 | method theme_family :common () { | ||
| 151 | return 'text'; | ||
| 152 | } | ||
| 153 | |||
| 154 | method themed_params :common () { | ||
| 155 | return ( text_color => [ 'color', 'normal' ] ); | ||
| 156 | } | ||
| 157 | |||
| 158 | method look_state () { | ||
| 159 | return 'normal'; | ||
| 160 | } | ||
| 161 | |||
| 162 | method look_reset ($name) { | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | method classes (@new) { | ||
| 167 | return [@$classes] unless @new; | ||
| 168 | $classes = class_names( $self, classes => $new[0] ); | ||
| 169 | $self->forget_looks; | ||
| 170 | $self->mark_changed; | ||
| 171 | return [@$classes]; | ||
| 172 | } | ||
| 173 | |||
| 174 | # A text that joins or leaves a tree may be in another UI, with | ||
| 175 | # another theme, from now on; the runs were made with the old one. | ||
| 176 | method tree_changed :override () { | ||
| 177 | $self->SUPER::tree_changed; | ||
| 178 | $self->_forget_own_looks; | ||
| 179 | $runs = undef; | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | |||
| 183 | # The color the text is drawn in. The nearest ancestor that colors the | ||
| 184 | # text inside it (a Button, say) decides, given the explicit color if | ||
| 185 | # there is one; otherwise the explicit color, or the theme's. | ||
| 186 | method text_config :override () { | ||
| 187 | my $config = $self->SUPER::text_config; | ||
| 188 | my $explicit = $self->_explicit_text_color; | ||
| 189 | my $colorer = first { $_->can('child_text_color') } $self->ancestors; | ||
| 190 | my $color = defined $colorer ? $colorer->child_text_color($explicit) : undef; | ||
| 191 | return { %$config, text_color => $color // $explicit // $self->look('color') }; | ||
| 192 | } | ||
| 193 | |||
| 194 | # --------------------------------------------------------------------- | ||
| 195 | # The text, its spans and its markup | ||
| 196 | # --------------------------------------------------------------------- | ||
| 197 | |||
| 198 | # Setting the text drops the spans, the highlights and the links: they | ||
| 199 | # pointed into the old one. | ||
| 200 | method text :override (@new) { | ||
| 201 | return $self->SUPER::text unless @new; | ||
| 202 | my $text = $self->SUPER::text(@new); | ||
| 203 | $marks->set_text($text); | ||
| 204 | $markup = undef; | ||
| 205 | $self->_forget_link_state; | ||
| 206 | $self->_links_changed; | ||
| 207 | return $text; | ||
| 208 | } | ||
| 209 | |||
| 210 | method markup (@new) { | ||
| 211 | return $markup unless @new; | ||
| 212 | my ( $text, $spans, $links ) = parse_markup( $new[0] ); | ||
| 213 | $self->SUPER::text($text); | ||
| 214 | $marks->set_text($text)->set_spans($spans); | ||
| 215 | $self->_forget_link_state; | ||
| 216 | $marks->add_link( @{$_}[ 2, 0, 1 ] ) foreach @$links; | ||
| 217 | $self->_listen if @$links && !$listening; | ||
| 218 | $markup = $new[0]; | ||
| 219 | $self->_links_changed; | ||
| 220 | return $markup; | ||
| 221 | } | ||
| 222 | |||
| 223 | # Term::Fabulous::Role::MarkedText: the marks and what follows from a | ||
| 224 | # change of them. Spans given by hand replace the markup's. | ||
| 225 | method text_marks () { | ||
| 226 | return $marks; | ||
| 227 | } | ||
| 228 | |||
| 229 | method marks_changed ($is_content) { | ||
| 230 | $markup = undef if $is_content; | ||
| 231 | $self->_forget_runs; | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | |||
| 235 | method _forget_runs () { | ||
| 236 | $runs = undef; | ||
| 237 | bump_revision(); | ||
| 238 | return; | ||
| 239 | } | ||
| 240 | |||
| 241 | # --------------------------------------------------------------------- | ||
| 242 | # Links | ||
| 243 | # --------------------------------------------------------------------- | ||
| 244 | |||
| 245 | method links () { | ||
| 246 | return $marks->links; | ||
| 247 | } | ||
| 248 | |||
| 249 | # The selected and the hovered link stay the same links. The first | ||
| 250 | # link makes the text able to take the focus, so the key and focus | ||
| 251 | # listeners start here. | ||
| 252 | method add_link ( $target, $start = 0, $end = undef ) { | ||
| 253 | my $position = $marks->add_link( $target, $start, $end ); | ||
| 254 | $selected_link++ if defined $selected_link && $selected_link >= $position; | ||
| 255 | $hovered_link++ if defined $hovered_link && $hovered_link >= $position; | ||
| 256 | $self->_listen unless $listening; | ||
| 257 | $markup = undef; | ||
| 258 | $self->_links_changed; | ||
| 259 | return $self; | ||
| 260 | } | ||
| 261 | |||
| 262 | method clear_links () { | ||
| 263 | $markup = undef; | ||
| 264 | $marks->clear_links; | ||
| 265 | $self->_forget_link_state; | ||
| 266 | $self->_links_changed; | ||
| 267 | return $self; | ||
| 268 | } | ||
| 269 | |||
| 270 | method link_at ($offset) { | ||
| 271 | return $marks->link_at($offset); | ||
| 272 | } | ||
| 273 | |||
| 274 | method _listen () { | ||
| 275 | $listening = 1; | ||
| 276 | my $continue = Clay::UI::Enum::Result->CONTINUE; | ||
| 277 | $self->on( | ||
| 278 | KeyPress => sub ( $self, $event ) { | ||
| 279 | my $action = $ACTION_OF_KEY{ $event->main_key_name // '' } // return $continue; | ||
| 280 | return $action->($self) ? undef : $continue; | ||
| 281 | }, | ||
| 282 | owner => $self | ||
| 283 | ); | ||
| 284 | $self->on( OnFocus => sub ( $self, $event ) { $self->select_next_link unless defined $self->selected_link; return $continue }, owner => $self ); | ||
| 285 | $self->on( OnBlur => sub ( $self, $event ) { $self->select_link(undef); return $continue }, owner => $self ); | ||
| 286 | return; | ||
| 287 | } | ||
| 288 | |||
| 289 | # Without telling the focus: a text that gets new links at once keeps | ||
| 290 | # the focus. | ||
| 291 | method _forget_link_state () { | ||
| 292 | ( $selected_link, $hovered_link ) = (); | ||
| 293 | return; | ||
| 294 | } | ||
| 295 | |||
| 296 | # Whether the widget can take the focus may have changed with its links. | ||
| 297 | method _links_changed () { | ||
| 298 | $self->_forget_runs; | ||
| 299 | $self->focus_eligibility_changed; | ||
| 300 | return; | ||
| 301 | } | ||
| 302 | |||
| 303 | method _checked_link_index ( $what, $index ) { | ||
| 304 | return undef unless defined $index; | ||
| 305 | $index = non_negative_integer( $self, $what, $index ); | ||
| 306 | my $count = $marks->link_count; | ||
| 307 | die "Term::Fabulous::Widget::Text: $what $index is not a link; the text has $count links" if $index >= $count; | ||
| 308 | return $index; | ||
| 309 | } | ||
| 310 | |||
| 311 | method selected_link () { | ||
| 312 | return $selected_link; | ||
| 313 | } | ||
| 314 | |||
| 315 | method select_link ($index) { | ||
| 316 | $index = $self->_checked_link_index( link => $index ); | ||
| 317 | return $self if ( $index // -1 ) == ( $selected_link // -1 ); | ||
| 318 | $selected_link = $index; | ||
| 319 | $self->_forget_runs; | ||
| 320 | return $self; | ||
| 321 | } | ||
| 322 | |||
| 323 | # The selection stops at the first and the last link. | ||
| 324 | method select_next_link () { | ||
| 325 | my $next = defined $selected_link ? $selected_link + 1 : 0; | ||
| 326 | return 0 if $next >= $marks->link_count; | ||
| 327 | $self->select_link($next); | ||
| 328 | return 1; | ||
| 329 | } | ||
| 330 | |||
| 331 | method select_previous_link () { | ||
| 332 | my $previous = defined $selected_link ? $selected_link - 1 : $marks->link_count - 1; | ||
| 333 | return 0 if $previous < 0; | ||
| 334 | $self->select_link($previous); | ||
| 335 | return 1; | ||
| 336 | } | ||
| 337 | |||
| 338 | method activate_link (@index) { | ||
| 339 | my $index = @index ? $self->_checked_link_index( link => $index[0] ) : $selected_link; | ||
| 340 | return 0 unless defined $index; | ||
| 341 | my ( $start, $end, $target ) = @{ $marks->links->[$index] }; | ||
| 342 | $self->fire_event( Term::Fabulous::Event::LinkActivate->new( link => $target, index => $index, start => $start, end => $end ) ); | ||
| 343 | return 1; | ||
| 344 | } | ||
| 345 | |||
| 346 | method hovered_link () { | ||
| 347 | return $hovered_link; | ||
| 348 | } | ||
| 349 | |||
| 350 | method hover_link ($index) { | ||
| 351 | $index = $self->_checked_link_index( link => $index ); | ||
| 352 | return $self if ( $index // -1 ) == ( $hovered_link // -1 ); | ||
| 353 | $hovered_link = $index; | ||
| 354 | $self->_forget_runs; | ||
| 355 | return $self; | ||
| 356 | } | ||
| 357 | |||
| 358 | # A text takes the focus while it has links to select. | ||
| 359 | method accepts_focus :override () { | ||
| 360 | return $marks->link_count ? 1 : 0; | ||
| 361 | } | ||
| 362 | |||
| 363 | # --------------------------------------------------------------------- | ||
| 364 | # Searching (Term::Fabulous::Role::Searchable, through | ||
| 365 | # Term::Fabulous::Role::MarkedText): the one text | ||
| 366 | # --------------------------------------------------------------------- | ||
| 367 | |||
| 368 | # The text shows itself; where a character lies is known once it was | ||
| 369 | # painted (Term::Fabulous::Render::Frame's text_rows). | ||
| 370 | method reveal_text ( $key, $offset ) { | ||
| 371 | return [ $self, undef ]; | ||
| 372 | } | ||
| 373 | |||
| 374 | # --------------------------------------------------------------------- | ||
| 375 | # Height before layout | ||
| 376 | # --------------------------------------------------------------------- | ||
| 377 | |||
| 378 | # The rows the text takes in the given number of columns, as Clay will | ||
| 379 | # wrap it: at the spaces and the line breaks for the words mode, at the | ||
| 380 | # line breaks only for the newlines mode, never for none. | ||
| 381 | method estimated_rows ($columns) { | ||
| 382 | $columns = positive_integer( $self, columns => $columns ); | ||
| 383 | return length $self->text ? 1 : 0 if $self->_stays_on_one_line; | ||
| 384 | my @lines = split /\n/, $self->text, -1; | ||
| 385 | return scalar @lines unless $self->_wraps_words; | ||
| 386 | return sum0 map { _wrapped_rows( $_, $columns ) } @lines; | ||
| 387 | } | ||
| 388 | |||
| 389 | # The row, from 0, that the character at the offset lies on when the | ||
| 390 | # text is wrapped to the columns, counted as estimated_rows counts. | ||
| 391 | method row_of_offset ( $offset, $columns ) { | ||
| 392 | $offset = min( non_negative_integer( $self, offset => $offset ), length $self->text ); | ||
| 393 | $columns = positive_integer( $self, columns => $columns ); | ||
| 394 | return 0 if $self->_stays_on_one_line; | ||
| 395 | my @lines = split /\n/, substr( $self->text, 0, $offset ), -1; | ||
| 396 | my $last = pop(@lines) // ''; | ||
| 397 | my $before = $self->_wraps_words ? sum0( map { _wrapped_rows( $_, $columns ) } @lines ) : scalar @lines; | ||
| 398 | my $rest = ( split /\n/, substr( $self->text, $offset - length $last ), 2 )[0] // ''; | ||
| 399 | return $before + ( $self->_wraps_words ? _row_in_line( $rest, length $last, $columns ) : 0 ); | ||
| 400 | } | ||
| 401 | |||
| 402 | method _wraps_words () { | ||
| 403 | return ( $self->wrap_mode // CLAY_TEXT_WRAP_WORDS ) == CLAY_TEXT_WRAP_WORDS ? 1 : 0; | ||
| 404 | } | ||
| 405 | |||
| 406 | # Clay lays out the none mode as one line, newlines included. | ||
| 407 | method _stays_on_one_line () { | ||
| 408 | return ( $self->wrap_mode // CLAY_TEXT_WRAP_WORDS ) == CLAY_TEXT_WRAP_NONE ? 1 : 0; | ||
| 409 | } | ||
| 410 | |||
| 411 | # Clay wraps a line at the spaces, and only there (a no-break space is | ||
| 412 | # part of its word). It measures every word together with the space | ||
| 413 | # after it, and a word that does not fit on the row starts the next | ||
| 414 | # one; a word wider than the row has a row of its own, and runs past | ||
| 415 | # its end. | ||
| 416 | sub _wrapped_rows ( $line, $columns ) { | ||
| 417 | return 1 + _row_in_line( $line, length $line, $columns ); | ||
| 418 | } | ||
| 419 | |||
| 420 | # The row of the word that holds the character at the offset of a line | ||
| 421 | # (the last word, past its end). | ||
| 422 | sub _row_in_line ( $line, $offset, $columns ) { | ||
| 423 | my @words = split / /, $line, -1; | ||
| 424 | my ( $row, $used, $position ) = ( 0, 0, 0 ); | ||
| 425 | foreach my $index ( 0 .. $#words ) { | ||
| 426 | my $width = _columns_of( $words[$index] ) + ( $index < $#words ? 1 : 0 ); | ||
| 427 | if ( $used > 0 && $used + $width > $columns ) { ( $row, $used ) = ( $row + 1, $width ) } | ||
| 428 | else { $used += $width } | ||
| 429 | $position += length( $words[$index] ) + 1; | ||
| 430 | last if $offset < $position; | ||
| 431 | } | ||
| 432 | return $row; | ||
| 433 | } | ||
| 434 | |||
| 435 | # A printable ASCII character takes one column. Most words are such, | ||
| 436 | # and asking Unicode for every word takes three times as long. | ||
| 437 | sub _columns_of ($word) { | ||
| 438 | return $word =~ /\A[\x20-\x7E]*\z/ ? length $word : string_columns($word); | ||
| 439 | } | ||
| 440 | |||
| 441 | # --------------------------------------------------------------------- | ||
| 442 | # Clicks | ||
| 443 | # --------------------------------------------------------------------- | ||
| 444 | |||
| 445 | # Fires TextClick with what lies at the offset; a left click on a link | ||
| 446 | # selects and follows it. | ||
| 447 | method click_at ( $offset, $button, $x, $y ) { | ||
| 448 | $offset = non_negative_integer( $self, offset => $offset ); | ||
| 449 | my $text = $self->text; | ||
| 450 | die ref($self) . ": offset $offset lies past the text (" . length($text) . " characters)" if $offset >= length $text; | ||
| 451 | my $index = $self->link_at($offset); | ||
| 452 | $self->fire_event( | ||
| 453 | Term::Fabulous::Event::TextClick->new( | ||
| 454 | button => $button, | ||
| 455 | x => $x, | ||
| 456 | y => $y, | ||
| 457 | offset => $offset, | ||
| 458 | _word_around( $text, $offset ), | ||
| 459 | spans => [ grep { $_->[0] <= $offset && $offset < $_->[1] } @{ $self->spans } ], | ||
| 460 | defined $index ? ( link => $marks->links->[$index][2], link_index => $index ) : (), | ||
| 461 | ) | ||
| 462 | ); | ||
| 463 | return $self unless $button == TB_KEY_MOUSE_LEFT && defined $index; | ||
| 464 | $self->select_link($index); | ||
| 465 | $self->activate_link($index); | ||
| 466 | return $self; | ||
| 467 | } | ||
| 468 | |||
| 469 | # The run of non-blank characters around the offset, as the word, | ||
| 470 | # word_start and word_end parameters of a TextClick; none on a blank. | ||
| 471 | sub _word_around ( $text, $offset ) { | ||
| 472 | return () if substr( $text, $offset, 1 ) =~ /\s/; | ||
| 473 | my ($head) = substr( $text, 0, $offset ) =~ /(\S*)\z/; | ||
| 474 | my ($tail) = substr( $text, $offset ) =~ /\A(\S*)/; | ||
| 475 | return ( word => $head . $tail, word_start => $offset - length $head, word_end => $offset + length $tail ); | ||
| 476 | } | ||
| 477 | |||
| 478 | # --------------------------------------------------------------------- | ||
| 479 | # Painting | ||
| 480 | # --------------------------------------------------------------------- | ||
| 481 | |||
| 482 | # The runs the renderer paints a line with: the part of the text's runs | ||
| 483 | # that covers the characters [offset, offset + length). A text without | ||
| 484 | # spans, highlights or links has none; the renderer paints it plain. | ||
| 485 | method line_styles ( $offset, $length ) { | ||
| 486 | return [] if $length <= 0 || !$marks->has_marks; | ||
| 487 | my $generation = Term::Fabulous::Theme::generation(); | ||
| 488 | if ( !defined $runs || $runs_generation != $generation ) { | ||
| 489 | $runs = $self->_runs_of_text; | ||
| 490 | $runs_generation = $generation; | ||
| 491 | } | ||
| 492 | |||
| 493 | my @slice; | ||
| 494 | my $position = 0; | ||
| 495 | foreach my $run (@$runs) { | ||
| 496 | my $run_start = $position; | ||
| 497 | $position += $run->[0]; | ||
| 498 | next if $position <= $offset; | ||
| 499 | last if $run_start >= $offset + $length; | ||
| 500 | |||
| 501 | my $from = $run_start < $offset ? $offset : $run_start; | ||
| 502 | my $to = $position > $offset + $length ? $offset + $length : $position; | ||
| 503 | push @slice, [ $to - $from, @{$run}[ 1 .. 4 ] ]; | ||
| 504 | } | ||
| 505 | return \@slice; | ||
| 506 | } | ||
| 507 | |||
| 508 | # The whole text cut at every span, highlight and link boundary, each | ||
| 509 | # piece with the style its spans give it, the highlights over them, | ||
| 510 | # and over those the look of the link it lies in, its palette tokens | ||
| 511 | # in the colors of the theme the text draws with | ||
| 512 | # (Term::Fabulous::Text::Marks). A run is the tuple | ||
| 513 | # Term::Fabulous::Render::Text paints with: | ||
| 514 | # [ $characters, $set, $clear, $fg_attr, $bg_attr ], the style's bits | ||
| 515 | # and its colors as termbox attributes (undef: the color is not | ||
| 516 | # touched), converted once here rather than in every frame. | ||
| 517 | method _runs_of_text () { | ||
| 518 | my $links = $marks->links; | ||
| 519 | my @over = map { [ @{ $links->[$_] }[ 0, 1 ], $self->_link_style($_) ] } 0 .. $#$links; | ||
| 520 | return [ | ||
| 521 | map { | ||
| 522 | my ( $from, $to, $style ) = @$_; | ||
| 523 | [ $to - $from, $style->{set}, $style->{clear}, _attr_of( $style->{color} ), _attr_of( $style->{background} ) ] | ||
| 524 | } @{ $marks->runs( theme => $self->drawing_theme, over => \@over ) } | ||
| 525 | ]; | ||
| 526 | } | ||
| 527 | |||
| 528 | # The look of a link from the theme's text.link slots: underlined in | ||
| 529 | # the normal and the hovered state, not underlined when selected. | ||
| 530 | method _link_style ($index) { | ||
| 531 | my $state | ||
| 532 | = defined $selected_link && $index == $selected_link ? 'selected' | ||
| 533 | : defined $hovered_link && $index == $hovered_link ? 'hovered' | ||
| 534 | : 'normal'; | ||
| 535 | my $underline = $state eq 'selected' ? 0 : TB_UNDERLINE; | ||
| 536 | return { | ||
| 537 | set => $underline, | ||
| 538 | clear => TB_UNDERLINE & ~$underline, | ||
| 539 | color => $self->look( 'link', $state ), | ||
| 540 | background => $self->look( 'link.background', $state ), | ||
| 541 | }; | ||
| 542 | } | ||
| 543 | |||
| 544 | sub _attr_of ($rgba) { | ||
| 545 | return undef unless defined $rgba; | ||
| 546 | return color_attr( Term::Fabulous::Color->new( color => $rgba ) ); | ||
| 547 | } | ||
| 548 | |||
| 549 | # --------------------------------------------------------------------- | ||
| 550 | # KDL | ||
| 551 | # --------------------------------------------------------------------- | ||
| 552 | |||
| 553 | method layout_properties :common () { | ||
| 554 | return ( | ||
| 555 | font_id => 'scalar', | ||
| 556 | font_size => 'scalar', | ||
| 557 | letter_spacing => 'scalar', | ||
| 558 | line_height => 'scalar', | ||
| 559 | text_color => 'color', | ||
| 560 | classes => \&_parse_classes, | ||
| 561 | bold => 'boolean', | ||
| 562 | italic => 'boolean', | ||
| 563 | underline => 'boolean', | ||
| 564 | text => \&_parse_text, | ||
| 565 | markup => \&_parse_markup, | ||
| 566 | wrap_mode => \&_parse_wrap_mode, | ||
| 567 | text_alignment => \&_parse_text_alignment, | ||
| 568 | ); | ||
| 569 | } | ||
| 570 | |||
| 571 | method _parse_classes ($kid) { | ||
| 572 | $self->classes( $self->kdl_strings($kid) ); | ||
| 573 | return; | ||
| 574 | } | ||
| 575 | |||
| 576 | method _parse_text ($kid) { | ||
| 577 | my $text = $self->kdl_argument($kid); | ||
| 578 | die "Term::Fabulous::Widget::Text: 'text' needs a string argument" unless $text->is_string; | ||
| 579 | $self->text( $text->value ); | ||
| 580 | return; | ||
| 581 | } | ||
| 582 | |||
| 583 | method _parse_markup ($kid) { | ||
| 584 | my $markup_value = $self->kdl_argument($kid); | ||
| 585 | die "Term::Fabulous::Widget::Text: 'markup' needs a string argument" unless $markup_value->is_string; | ||
| 586 | $self->markup( $markup_value->value ); | ||
| 587 | return; | ||
| 588 | } | ||
| 589 | |||
| 590 | method _parse_wrap_mode ($kid) { | ||
| 591 | $self->wrap_mode( _named( wrap_mode => \%WRAP_MODE_BY_NAME, $self->kdl_argument($kid)->as_perl ) ); | ||
| 592 | return; | ||
| 593 | } | ||
| 594 | |||
| 595 | method _parse_text_alignment ($kid) { | ||
| 596 | $self->text_alignment( _named( text_alignment => \%TEXT_ALIGNMENT_BY_NAME, $self->kdl_argument($kid)->as_perl ) ); | ||
| 597 | return; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | 1; | ||
| 602 | |||
| 603 | __END__ | ||