| 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.03'; | ||
| 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 | 4 | ▶ | { |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | use List::Util qw(first); | ||
| 19 | use Term::Fabulous::Check qw(boolean class_names color); | ||
| 20 | use Term::Fabulous::Theme; | ||
| 21 | |||
| 22 | my @COLOR_PARAMS = qw(background_color border_color); | ||
| 23 | |||
| 24 | # The sides of the theme's border.enabled, shared and never changed. | ||
| 25 | my %EVERY_SIDE = map { $_ => 1 } qw(top right bottom left); | ||
| 26 | my %NO_SIDE = map { $_ => 0 } qw(top right bottom left); | ||
| 27 | |||
| 28 | field $classes :param = []; | ||
| 29 | field $glyphs_show_through :param = 0; | ||
| 30 | |||
| 31 | # Clay::UI validates the colors before any ADJUST of this class runs, | ||
| 32 | # so they are converted while the arguments are still a plain list. | ||
| 33 | # Clay::UI's border_width is replaced by bordered. | ||
| 34 | sub BUILDARGS ( $class, %params ) { | ||
| 35 | _no_border_width($class) if exists $params{border_width}; | ||
| 36 | $params{$_} = color( $class, $_ => $params{$_} ) foreach grep { defined $params{$_} } @COLOR_PARAMS; | ||
| 37 | return %params; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _no_border_width ($owner) { | ||
| 41 | die( ( ref $owner || $owner ) | ||
| 42 | . ": 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" | ||
| 43 | ); | ||
| 44 | } | ||
| 45 | |||
| 46 | ADJUST { | ||
| 47 | $glyphs_show_through = boolean( $self, glyphs_show_through => $glyphs_show_through ); | ||
| 48 | $classes = class_names( $self, classes => $classes ); | ||
| 49 | } | ||
| 50 | |||
| 51 | # The readers return the color in use: the given one, or the theme's | ||
| 52 | # for the widget's family (none for a plain box). | ||
| 53 | method background_color :override (@new) { | ||
| 54 | return $self->SUPER::background_color( defined $new[0] ? color( $self, background_color => $new[0] ) : @new ) if @new; | ||
| 55 | return $self->SUPER::background_color // $self->look('background'); | ||
| 56 | } | ||
| 57 | |||
| 58 | method border_color :override (@new) { | ||
| 59 | return $self->SUPER::border_color( defined $new[0] ? color( $self, border_color => $new[0] ) : @new ) if @new; | ||
| 60 | return $self->SUPER::border_color // $self->look('border.color'); | ||
| 61 | } | ||
| 62 | |||
| 63 | method border_width :override (@) { | ||
| 64 | _no_border_width($self); | ||
| 65 | } | ||
| 66 | |||
| 67 | # The sides the widget has a border on: the given ones, or every side | ||
| 68 | # or none as the theme's border.enabled says. Returns a new hash. | ||
| 69 | method bordered (@new) { | ||
| 70 | return { %{ $self->_bordered_sides } } unless @new; | ||
| 71 | $self->set_look( bordered => $new[0] ); | ||
| 72 | return { %{ $self->_bordered_sides } }; | ||
| 73 | } | ||
| 74 | |||
| 75 | # The same, shared, for Term::Fabulous::Role::HasBorderStyle, which | ||
| 76 | # reads it for every frame; it must not be changed. | ||
| 77 | method _bordered_sides () { | ||
| 78 | my $sides = $self->look_value('bordered'); | ||
| 79 | return ref $sides ? $sides : $sides ? \%EVERY_SIDE : \%NO_SIDE; | ||
| 80 | } | ||
| 81 | |||
| 82 | # The color this widget lies on: the background of the widget itself | ||
| 83 | # or of its nearest ancestor that has one, else the screen background | ||
| 84 | # of its UI, painted opaque, else undef. A background counts when it is | ||
| 85 | # opaque; with translucent => 1 any background with an alpha above 0 | ||
| 86 | # counts, the one a painter blends the widget's own cells over. | ||
| 87 | method background_below (%options) { | ||
| 88 | my @unknown = grep { $_ ne 'translucent' } sort keys %options; | ||
| 89 | die ref($self) . ": background_below does not take @unknown (known: translucent)" if @unknown; | ||
| 90 | my $lowest_alpha = $options{translucent} ? 1 : 255; | ||
| 91 | for ( my $node = $self; defined $node; $node = $node->parent ) { | ||
| 92 | next unless $node->can('background_color'); | ||
| 93 | my $color = $node->background_color // next; | ||
| 94 | return [@$color] if $color->[3] >= $lowest_alpha; | ||
| 95 | } | ||
| 96 | my $ui = $self->ui // return undef; | ||
| 97 | return undef unless $ui->can('screen_background'); | ||
| 98 | my $screen = $ui->screen_background // return undef; | ||
| 99 | return [ ( $screen->to_rgba )[ 0 .. 2 ], 255 ]; | ||
| 100 | } | ||
| 101 | |||
| 102 | # --------------------------------------------------------------------- | ||
| 103 | # The theme (Term::Fabulous::Role::Themed): a container is a box, whose | ||
| 104 | # background and border the theme colors only when the program gave | ||
| 105 | # none. Subclasses name their family and add their parameters. | ||
| 106 | # --------------------------------------------------------------------- | ||
| 107 | |||
| 108 | method theme_family :common () { | ||
| 109 | return 'box'; | ||
| 110 | } | ||
| 111 | |||
| 112 | # A family without border.enabled (box itself) leaves bordered to the | ||
| 113 | # program. | ||
| 114 | method themed_params :common () { | ||
| 115 | my $border_slot = Term::Fabulous::Theme::has_slot( $class->theme_family, 'border.enabled' ) ? 'border.enabled' : undef; | ||
| 116 | return ( | ||
| 117 | background_color => [ 'background', 'normal' ], | ||
| 118 | border_color => [ 'border.color', 'normal' ], | ||
| 119 | bordered => [ $border_slot, 'normal', 'border_sides' ], | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | |||
| 123 | method look_state () { | ||
| 124 | return 'normal'; | ||
| 125 | } | ||
| 126 | |||
| 127 | # background_color and border_color live in the Clay::UI roles. | ||
| 128 | method look_reset ($name) { | ||
| 129 | $self->SUPER::background_color(undef) if $name eq 'background_color'; | ||
| 130 | $self->SUPER::border_color(undef) if $name eq 'border_color'; | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | method classes (@new) { | ||
| 135 | return [@$classes] unless @new; | ||
| 136 | $classes = class_names( $self, classes => $new[0] ); | ||
| 137 | $self->forget_looks; | ||
| 138 | $self->mark_changed; | ||
| 139 | return [@$classes]; | ||
| 140 | } | ||
| 141 | |||
| 142 | # A widget that joins or leaves a tree may be in another UI, with | ||
| 143 | # another theme, from now on. | ||
| 144 | method tree_changed :override () { | ||
| 145 | $self->SUPER::tree_changed; | ||
| 146 | $self->_forget_own_looks; | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | |||
| 150 | # Runs after contribute_background and contribute_border (Clay::UI | ||
| 151 | # calls the contributors in alphabetical order) and before a subclass's | ||
| 152 | # contribute_state_look: fills the background and the border color the | ||
| 153 | # program left to the theme, for the state the widget is in. A border | ||
| 154 | # is colored only where there is one. | ||
| 155 | method contribute_look_theme ($config) { | ||
| 156 | my $state = $self->look_state; | ||
| 157 | my $background = $self->themed_value( 'background', $state, $config->{background_color} // $self->look('background') ); | ||
| 158 | $config->{background_color} = $background if defined $background && ref $background; | ||
| 159 | |||
| 160 | my $border = $config->{border} // return; | ||
| 161 | my $color = $self->themed_value( 'border.color', $state, $border->{color} // $self->look('border.color') ); | ||
| 162 | $config->{border} = { %$border, color => $color } if defined $color; | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | # The first widget at or below this one, in layout pre-order, whose id | ||
| 167 | # is $id. | ||
| 168 | method find_by_id ($id) { | ||
| 169 | die "Term::Fabulous::Widget: find_by_id needs an id, got undef" unless defined $id; | ||
| 170 | return first { _has_id( $_, $id ) } $self, $self->descendants; | ||
| 171 | } | ||
| 172 | |||
| 173 | # The inputs at or below this one, in layout pre-order, whose value is | ||
| 174 | # not valid. | ||
| 175 | method invalid_inputs () { | ||
| 176 | return grep { $_->DOES('Term::Fabulous::Role::Validatable') && !$_->is_valid } $self, $self->descendants; | ||
| 177 | } | ||
| 178 | |||
| 179 | # Text leaves have an id too. | ||
| 180 | sub _has_id ( $widget, $id ) { | ||
| 181 | my $widget_id = $widget->can('id') ? $widget->id : undef; | ||
| 182 | return defined $widget_id && $widget_id eq $id; | ||
| 183 | } | ||
| 184 | |||
| 185 | method get_classes () { | ||
| 186 | return ( @$classes, map { 'state_' . lc($_) } $self->states ); | ||
| 187 | } | ||
| 188 | |||
| 189 | method glyphs_show_through (@new) { | ||
| 190 | return $glyphs_show_through unless @new; | ||
| 191 | $glyphs_show_through = boolean( $self, glyphs_show_through => $new[0] ); | ||
| 192 | $self->mark_changed; | ||
| 193 | return $glyphs_show_through; | ||
| 194 | } | ||
| 195 | |||
| 196 | # Whether the renderer swaps the foreground and background colors of | ||
| 197 | # every cell the widget and its children paint. Button overrides it | ||
| 198 | # while it is pressed. | ||
| 199 | method reverse_video () { | ||
| 200 | return 0; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | 1; | ||
| 205 | |||
| 206 | __END__ | ||