| 1 | package Term::Fabulous::Widget; | ||
| 2 | |||
| 3 | 4 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 4 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 4 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | 4 | ▶ | use Object::Pad 0.825; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | class Term::Fabulous::Widget | ||
| 13 | :isa(Term::Fabulous::Widget::Element) | ||
| 14 | :does(Term::Fabulous::Role::HasBorderStyle) | ||
| 15 | :does(Term::Fabulous::Role::Themed) | ||
| 16 | :abstract | ||
| 17 | 0 | ▶ | { |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | use Clay::XS qw(CLAY__SIZING_TYPE_FIXED); | ||
| 19 | use List::Util qw(first max); | ||
| 20 | use Term::Fabulous::Check qw(boolean class_names color describe positive_integer string); | ||
| 21 | use Term::Fabulous::Theme; | ||
| 22 | |||
| 23 | my @COLOR_PARAMS = qw(background_color border_color); | ||
| 24 | |||
| 25 | # The sides of the theme's border.enabled, shared and never changed. | ||
| 26 | my %EVERY_SIDE = map { $_ => 1 } qw(top right bottom left); | ||
| 27 | my %NO_SIDE = map { $_ => 0 } qw(top right bottom left); | ||
| 28 | |||
| 29 | field $classes :param = []; | ||
| 30 | field $glyphs_show_through :param = 0; | ||
| 31 | |||
| 32 | # Work asked for with each_frame while the widget was in no UI, as | ||
| 33 | # [ code, { options } ]: handed to the UI the widget joins. | ||
| 34 | field @_waiting_frame_work; | ||
| 35 | |||
| 36 | # Clay::UI validates the colors before any ADJUST of this class runs, | ||
| 37 | # so they are converted while the arguments are still a plain list. | ||
| 38 | # Clay::UI's border_width is replaced by bordered. | ||
| 39 | sub BUILDARGS ( $class, %params ) { | ||
| 40 | _no_border_width($class) if exists $params{border_width}; | ||
| 41 | $params{$_} = color( $class, $_ => $params{$_} ) foreach grep { defined $params{$_} } @COLOR_PARAMS; | ||
| 42 | return %params; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _no_border_width ($owner) { | ||
| 46 | die( ( ref $owner || $owner ) | ||
| 47 | . ": border_width is not a parameter of Term::Fabulous widgets; use bordered (true, or a hash of the sides top, right, bottom and left) and give the room inside the border as layout padding" | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | ADJUST { | ||
| 52 | $glyphs_show_through = boolean( $self, glyphs_show_through => $glyphs_show_through ); | ||
| 53 | $classes = class_names( $self, classes => $classes ); | ||
| 54 | } | ||
| 55 | |||
| 56 | # The readers return the color in use: the given one, or the theme's | ||
| 57 | # for the widget's family (none for a plain box). | ||
| 58 | method background_color :override (@new) { | ||
| 59 | return $self->SUPER::background_color( defined $new[0] ? color( $self, background_color => $new[0] ) : @new ) if @new; | ||
| 60 | return $self->SUPER::background_color // $self->look('background'); | ||
| 61 | } | ||
| 62 | |||
| 63 | method border_color :override (@new) { | ||
| 64 | return $self->SUPER::border_color( defined $new[0] ? color( $self, border_color => $new[0] ) : @new ) if @new; | ||
| 65 | return $self->SUPER::border_color // $self->look('border.color'); | ||
| 66 | } | ||
| 67 | |||
| 68 | method border_width :override (@) { | ||
| 69 | _no_border_width($self); | ||
| 70 | } | ||
| 71 | |||
| 72 | # The sides the widget has a border on: the given ones, or every side | ||
| 73 | # or none as the theme's border.enabled says. Returns a new hash. | ||
| 74 | method bordered (@new) { | ||
| 75 | return { %{ $self->_bordered_sides } } unless @new; | ||
| 76 | $self->set_look( bordered => $new[0] ); | ||
| 77 | return { %{ $self->_bordered_sides } }; | ||
| 78 | } | ||
| 79 | |||
| 80 | # The same, shared, for Term::Fabulous::Role::HasBorderStyle, which | ||
| 81 | # reads it for every frame; it must not be changed. | ||
| 82 | method _bordered_sides () { | ||
| 83 | my $sides = $self->look_value('bordered'); | ||
| 84 | return ref $sides ? $sides : $sides ? \%EVERY_SIDE : \%NO_SIDE; | ||
| 85 | } | ||
| 86 | |||
| 87 | # --------------------------------------------------------------------- | ||
| 88 | # Work after a frame | ||
| 89 | # --------------------------------------------------------------------- | ||
| 90 | |||
| 91 | # The widget's own each_draw registration (Term::Fabulous::Render): | ||
| 92 | # called with the widget after every frame until the code returns | ||
| 93 | # true, the frames run out or the widget leaves the UI. Outside a UI | ||
| 94 | # the work waits until the widget joins one; false in a UI that draws | ||
| 95 | # no frames. | ||
| 96 | method each_frame ( $code, %options ) { | ||
| 97 | my @unknown = grep { $_ ne 'name' && $_ ne 'frames' } sort keys %options; | ||
| 98 | die ref($self) . ": each_frame does not take @unknown (known: name frames)" if @unknown; | ||
| 99 | die ref($self) . ": each_frame needs a code reference, got " . describe($code) unless ref $code eq 'CODE'; | ||
| 100 | string( $self, name => $options{name} ) if exists $options{name}; | ||
| 101 | positive_integer( $self, frames => $options{frames} ) if exists $options{frames}; | ||
| 102 | |||
| 103 | my $ui = $self->ui; | ||
| 104 | if ( !defined $ui ) { | ||
| 105 | @_waiting_frame_work = grep { !defined $options{name} || ( $_->[1]{name} // '' ) ne $options{name} } @_waiting_frame_work if exists $options{name}; | ||
| 106 | push @_waiting_frame_work, [ $code, {%options} ]; | ||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | return 0 unless $ui->can('each_draw'); | ||
| 110 | $ui->each_draw( $code, %options, owner => $self ); | ||
| 111 | return 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | # The work that waited for a UI goes to the one the widget joined; a | ||
| 115 | # UI that draws no frames drops it. | ||
| 116 | method _hand_over_frame_work () { | ||
| 117 | my $ui = $self->ui // return; | ||
| 118 | my @waiting = splice @_waiting_frame_work; | ||
| 119 | return unless $ui->can('each_draw'); | ||
| 120 | $ui->each_draw( $_->[0], %{ $_->[1] }, owner => $self ) foreach @waiting; | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | |||
| 124 | # --------------------------------------------------------------------- | ||
| 125 | # Height before layout | ||
| 126 | # --------------------------------------------------------------------- | ||
| 127 | |||
| 128 | # The rows the widget will take in the given number of columns, before | ||
| 129 | # Clay has laid it out: a fixed height as given; else the padding and | ||
| 130 | # the border around what the content takes, which a subclass knows | ||
| 131 | # (estimated_content_rows). A guess for the scrollbar of a list that | ||
| 132 | # builds only the items near its viewport; Clay's layout corrects it. | ||
| 133 | method estimated_rows ($columns) { | ||
| 134 | $columns = positive_integer( $self, columns => $columns ); | ||
| 135 | my $layout = $self->layout; | ||
| 136 | my $height = $layout->{sizing}{height}; | ||
| 137 | return $height->{min} if defined $height && $height->{type} == CLAY__SIZING_TYPE_FIXED; | ||
| 138 | |||
| 139 | my $padding = $self->content_padding; | ||
| 140 | my $sides = $self->drawn_border_sides; | ||
| 141 | my $rows = $padding->{top} + $padding->{bottom} + $sides->{top} + $sides->{bottom}; | ||
| 142 | my $frame = $padding->{left} + $padding->{right} + $sides->{left} + $sides->{right}; | ||
| 143 | return $rows + $self->estimated_content_rows( max( 1, $columns - $frame ) ); | ||
| 144 | } | ||
| 145 | |||
| 146 | # The padding around the content, all four sides: the layout's. A | ||
| 147 | # widget that keeps more room of its own (a scroll box for its | ||
| 148 | # scrollbars) adds it. | ||
| 149 | method content_padding () { | ||
| 150 | my $padding = $self->layout->{padding} // {}; | ||
| 151 | return { map { $_ => $padding->{$_} // 0 } qw(top right bottom left) }; | ||
| 152 | } | ||
| 153 | |||
| 154 | # The rows the content inside the padding and the border takes, in | ||
| 155 | # the columns left for it: one, unless a subclass knows better. | ||
| 156 | method estimated_content_rows ($columns) { | ||
| 157 | return 1; | ||
| 158 | } | ||
| 159 | |||
| 160 | # The color this widget lies on: the background of the widget itself | ||
| 161 | # or of its nearest ancestor that has one, else the screen background | ||
| 162 | # of its UI, painted opaque, else undef. A background counts when it is | ||
| 163 | # opaque; with translucent => 1 any background with an alpha above 0 | ||
| 164 | # counts, the one a painter blends the widget's own cells over. | ||
| 165 | method background_below (%options) { | ||
| 166 | my @unknown = grep { $_ ne 'translucent' } sort keys %options; | ||
| 167 | die ref($self) . ": background_below does not take @unknown (known: translucent)" if @unknown; | ||
| 168 | my $lowest_alpha = $options{translucent} ? 1 : 255; | ||
| 169 | foreach my $node ( $self, $self->ancestors ) { | ||
| 170 | next unless $node->can('background_color'); | ||
| 171 | my $color = $node->background_color // next; | ||
| 172 | return [@$color] if $color->[3] >= $lowest_alpha; | ||
| 173 | } | ||
| 174 | my $ui = $self->ui // return undef; | ||
| 175 | return undef unless $ui->can('screen_background'); | ||
| 176 | my $screen = $ui->screen_background // return undef; | ||
| 177 | return [ ( $screen->to_rgba )[ 0 .. 2 ], 255 ]; | ||
| 178 | } | ||
| 179 | |||
| 180 | # --------------------------------------------------------------------- | ||
| 181 | # The theme (Term::Fabulous::Role::Themed): a container is a box, whose | ||
| 182 | # background and border the theme colors only when the program gave | ||
| 183 | # none. Subclasses name their family and add their parameters. | ||
| 184 | # --------------------------------------------------------------------- | ||
| 185 | |||
| 186 | method theme_family :common () { | ||
| 187 | return 'box'; | ||
| 188 | } | ||
| 189 | |||
| 190 | # A family without border.enabled (box itself) leaves bordered to the | ||
| 191 | # program. | ||
| 192 | method themed_params :common () { | ||
| 193 | my $border_slot = Term::Fabulous::Theme::has_slot( $class->theme_family, 'border.enabled' ) ? 'border.enabled' : undef; | ||
| 194 | return ( | ||
| 195 | background_color => [ 'background', 'normal' ], | ||
| 196 | border_color => [ 'border.color', 'normal' ], | ||
| 197 | bordered => [ $border_slot, 'normal', 'border_sides' ], | ||
| 198 | ); | ||
| 199 | } | ||
| 200 | |||
| 201 | method look_state () { | ||
| 202 | return 'normal'; | ||
| 203 | } | ||
| 204 | |||
| 205 | # background_color and border_color live in the Clay::UI roles. | ||
| 206 | method look_reset ($name) { | ||
| 207 | $self->SUPER::background_color(undef) if $name eq 'background_color'; | ||
| 208 | $self->SUPER::border_color(undef) if $name eq 'border_color'; | ||
| 209 | return; | ||
| 210 | } | ||
| 211 | |||
| 212 | method classes (@new) { | ||
| 213 | return [@$classes] unless @new; | ||
| 214 | $classes = class_names( $self, classes => $new[0] ); | ||
| 215 | $self->forget_looks; | ||
| 216 | $self->mark_changed; | ||
| 217 | return [@$classes]; | ||
| 218 | } | ||
| 219 | |||
| 220 | # A widget that joins or leaves a tree may be in another UI, with | ||
| 221 | # another theme, from now on. | ||
| 222 | method tree_changed :override () { | ||
| 223 | $self->SUPER::tree_changed; | ||
| 224 | $self->_forget_own_looks; | ||
| 225 | $self->_hand_over_frame_work; | ||
| 226 | return; | ||
| 227 | } | ||
| 228 | |||
| 229 | # Runs after contribute_background and contribute_border (Clay::UI | ||
| 230 | # calls the contributors in alphabetical order) and before a subclass's | ||
| 231 | # contribute_state_look: fills the background and the border color the | ||
| 232 | # program left to the theme, for the state the widget is in. A border | ||
| 233 | # is colored only where there is one. | ||
| 234 | method contribute_look_theme ($config) { | ||
| 235 | my $state = $self->look_state; | ||
| 236 | my $background = $self->themed_value( 'background', $state, $config->{background_color} // $self->look('background') ); | ||
| 237 | $config->{background_color} = $background if defined $background && ref $background; | ||
| 238 | |||
| 239 | my $border = $config->{border} // return; | ||
| 240 | my $color = $self->themed_value( 'border.color', $state, $border->{color} // $self->look('border.color') ); | ||
| 241 | $config->{border} = { %$border, color => $color } if defined $color; | ||
| 242 | return; | ||
| 243 | } | ||
| 244 | |||
| 245 | # The first widget at or below this one, in layout pre-order, whose id | ||
| 246 | # is $id. | ||
| 247 | method find_by_id ($id) { | ||
| 248 | die "Term::Fabulous::Widget: find_by_id needs an id, got undef" unless defined $id; | ||
| 249 | return first { _has_id( $_, $id ) } $self->subtree; | ||
| 250 | } | ||
| 251 | |||
| 252 | # The inputs at or below this one, in layout pre-order, whose value is | ||
| 253 | # not valid. | ||
| 254 | method invalid_inputs () { | ||
| 255 | return grep { $_->DOES('Term::Fabulous::Role::Validatable') && !$_->is_valid } $self->subtree; | ||
| 256 | } | ||
| 257 | |||
| 258 | # Text leaves have an id too. | ||
| 259 | sub _has_id ( $widget, $id ) { | ||
| 260 | my $widget_id = $widget->can('id') ? $widget->id : undef; | ||
| 261 | return defined $widget_id && $widget_id eq $id; | ||
| 262 | } | ||
| 263 | |||
| 264 | method get_classes () { | ||
| 265 | return ( @$classes, map { 'state_' . lc($_) } $self->states ); | ||
| 266 | } | ||
| 267 | |||
| 268 | method glyphs_show_through (@new) { | ||
| 269 | return $glyphs_show_through unless @new; | ||
| 270 | $glyphs_show_through = boolean( $self, glyphs_show_through => $new[0] ); | ||
| 271 | $self->mark_changed; | ||
| 272 | return $glyphs_show_through; | ||
| 273 | } | ||
| 274 | |||
| 275 | # Whether the renderer swaps the foreground and background colors of | ||
| 276 | # every cell the widget and its children paint. Button overrides it | ||
| 277 | # while it is pressed. | ||
| 278 | method reverse_video () { | ||
| 279 | return 0; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | 1; | ||
| 284 | |||
| 285 | __END__ | ||