| 1 | package Term::Fabulous::Widget::Input; | ||
| 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 Clay::UI::Role::Interaction::Disableable; | ||
| 11 | use Clay::UI::Role::Interaction::Focusable; | ||
| 12 | use Clay::UI::Role::Interaction::Hoverable; | ||
| 13 | use Clay::UI::Role::Interaction::Pressable; | ||
| 14 | use Term::Fabulous::Role::Validatable; | ||
| 15 | use Term::Fabulous::Widget::Display; | ||
| 16 | |||
| 17 | our $VERSION = '0.05'; | ||
| 18 | |||
| 19 | class Term::Fabulous::Widget::Input | ||
| 20 | :isa(Term::Fabulous::Widget::Display) | ||
| 21 | :does(Clay::UI::Role::Interaction::Focusable) | ||
| 22 | :does(Clay::UI::Role::Interaction::Hoverable) | ||
| 23 | :does(Clay::UI::Role::Interaction::Pressable) | ||
| 24 | :does(Clay::UI::Role::Interaction::Disableable) | ||
| 25 | :does(Term::Fabulous::Role::Validatable) | ||
| 26 | :abstract | ||
| 27 | { | ||
| 28 | use Clay::UI::Enum::Result; | ||
| 29 | use Scalar::Util qw(refaddr); | ||
| 30 | use Term::Fabulous::Termbox qw(TB_REVERSE); | ||
| 31 | use Term::Fabulous::Color; | ||
| 32 | use Term::Fabulous::Event::Change; | ||
| 33 | |||
| 34 | ADJUST { | ||
| 35 | my $continue = Clay::UI::Enum::Result->CONTINUE; | ||
| 36 | my $own = sub ( $self, $event ) { refaddr( $event->target ) == refaddr($self) }; | ||
| 37 | |||
| 38 | $self->on( OnFocus => sub ( $self, $event ) { $self->focus_changed(1) if $own->( $self, $event ); return $continue }, owner => $self ); | ||
| 39 | $self->on( OnBlur => sub ( $self, $event ) { $self->focus_changed(0) if $own->( $self, $event ); return $continue }, owner => $self ); | ||
| 40 | $self->on( KeyPress => sub ( $self, $event ) { return $self->_dispatch( handle_key => $event ) }, owner => $self ); | ||
| 41 | $self->on( Mouse => sub ( $self, $event ) { return $self->_dispatch( handle_mouse => $event ) }, owner => $self ); | ||
| 42 | $self->on( | ||
| 43 | OnRelease => sub ( $self, $event ) { | ||
| 44 | $self->activate if $own->( $self, $event ) && $self->is_enabled; | ||
| 45 | return $continue; | ||
| 46 | }, | ||
| 47 | owner => $self | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | # --------------------------------------------------------------------- | ||
| 52 | # Colors: anything Term::Fabulous::Widget::Canvas accepts for a cell, | ||
| 53 | # kept as [r, g, b, a]; the theme's input family supplies the rest | ||
| 54 | # --------------------------------------------------------------------- | ||
| 55 | |||
| 56 | method theme_family :common () { | ||
| 57 | return 'input'; | ||
| 58 | } | ||
| 59 | |||
| 60 | method themed_params :common () { | ||
| 61 | return ( | ||
| 62 | $class->SUPER::themed_params, | ||
| 63 | text_color => [ 'text', 'normal', 'cell_color' ], | ||
| 64 | disabled_color => [ 'text', 'disabled', 'cell_color' ], | ||
| 65 | invalid_color => [ 'text', 'invalid', 'cell_color' ], | ||
| 66 | accent_color => [ 'accent', 'normal', 'cell_color' ], | ||
| 67 | focus_background_color => [ 'background', 'focused', 'cell_color' ], | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | |||
| 71 | # A disabled input is neither focused nor hovered; an invalid value | ||
| 72 | # shows over the focus. | ||
| 73 | method look_state :override () { | ||
| 74 | return 'disabled' unless $self->is_enabled; | ||
| 75 | return 'invalid' unless $self->is_valid; | ||
| 76 | return $self->is_focused ? 'focused' : $self->is_hovered ? 'hovered' : 'normal'; | ||
| 77 | } | ||
| 78 | |||
| 79 | # The focus background is painted inside the content, not on the | ||
| 80 | # widget's box, so only the border follows the state here. | ||
| 81 | method contribute_look_theme :override ($config) { | ||
| 82 | my $background = $config->{background_color} // $self->look('background'); | ||
| 83 | $config->{background_color} = $background if defined $background; | ||
| 84 | |||
| 85 | my $border = $config->{border} // return; | ||
| 86 | my $color = $self->themed_value( 'border.color', $self->look_state, $border->{color} // $self->look('border.color') ); | ||
| 87 | $config->{border} = { %$border, color => $color } if defined $color; | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | |||
| 91 | method text_color (@new) { | ||
| 92 | return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color'); | ||
| 93 | } | ||
| 94 | |||
| 95 | method disabled_color (@new) { | ||
| 96 | return @new ? $self->set_look( disabled_color => $new[0] ) : $self->look_value('disabled_color'); | ||
| 97 | } | ||
| 98 | |||
| 99 | method invalid_color (@new) { | ||
| 100 | return @new ? $self->set_look( invalid_color => $new[0] ) : $self->look_value('invalid_color'); | ||
| 101 | } | ||
| 102 | |||
| 103 | method accent_color (@new) { | ||
| 104 | return @new ? $self->set_look( accent_color => $new[0] ) : $self->look_value('accent_color'); | ||
| 105 | } | ||
| 106 | |||
| 107 | method focus_background_color (@new) { | ||
| 108 | return @new ? $self->set_look( focus_background_color => $new[0] ) : $self->look_value('focus_background_color'); | ||
| 109 | } | ||
| 110 | |||
| 111 | # The attribute for normal text: the text color, or the disabled or | ||
| 112 | # the invalid color. | ||
| 113 | method foreground_attr () { | ||
| 114 | my $name = !$self->is_enabled ? 'disabled_color' : !$self->is_valid ? 'invalid_color' : 'text_color'; | ||
| 115 | return $self->color_attr( $self->look_value($name) ); | ||
| 116 | } | ||
| 117 | |||
| 118 | method accent_attr () { | ||
| 119 | return $self->color_attr( $self->look_value( $self->is_enabled ? 'accent_color' : 'disabled_color' ) ); | ||
| 120 | } | ||
| 121 | |||
| 122 | method reverse_attr ($attr) { | ||
| 123 | return ( $attr // 0 ) | TB_REVERSE; | ||
| 124 | } | ||
| 125 | |||
| 126 | # A color as the [r, g, b, a] that Clay takes for backgrounds and | ||
| 127 | # borders. A packed 0xRRGGBB integer is opaque. | ||
| 128 | method rgba_of ($color) { | ||
| 129 | return [ ( $color >> 16 ) & 0xFF, ( $color >> 8 ) & 0xFF, $color & 0xFF, 255 ] if !ref $color && $color =~ /\A[0-9]+\z/; | ||
| 130 | return [ Term::Fabulous::Color->new( color => $color )->to_rgba ]; | ||
| 131 | } | ||
| 132 | |||
| 133 | # --------------------------------------------------------------------- | ||
| 134 | # Painting | ||
| 135 | # --------------------------------------------------------------------- | ||
| 136 | |||
| 137 | # What paint reads, besides the size and the widget's own state: what | ||
| 138 | # other objects decide, the focus, whether the widget is enabled and | ||
| 139 | # whether its value is valid. | ||
| 140 | method paint_key :override () { | ||
| 141 | return ( $self->SUPER::paint_key, $self->is_focused, $self->is_enabled, $self->is_valid ); | ||
| 142 | } | ||
| 143 | |||
| 144 | method focus_changed ($is_focused) { | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | # The background of the content while the input shows the focus, | ||
| 149 | # otherwise undef: the widget's own background shows. | ||
| 150 | method focus_background_attr () { | ||
| 151 | return $self->is_focused ? $self->color_attr( $self->look_value('focus_background_color') ) : undef; | ||
| 152 | } | ||
| 153 | |||
| 154 | # Paints the focus background under the whole buffer, when there is one, | ||
| 155 | # and returns it. | ||
| 156 | method paint_focus_background () { | ||
| 157 | my $bg = $self->focus_background_attr // return undef; | ||
| 158 | $self->fill_attrs( 0, $_, $self->columns, ' ', undef, $bg ) foreach 0 .. $self->rows - 1; | ||
| 159 | return $bg; | ||
| 160 | } | ||
| 161 | |||
| 162 | # --------------------------------------------------------------------- | ||
| 163 | # Input | ||
| 164 | # --------------------------------------------------------------------- | ||
| 165 | |||
| 166 | # Disabled widgets ignore input; a handler returns true when it used the | ||
| 167 | # event, which then stops bubbling. | ||
| 168 | method _dispatch ( $handler, $event ) { | ||
| 169 | my $result = Clay::UI::Enum::Result->CONTINUE; | ||
| 170 | return $result unless $self->is_enabled && $self->$handler($event); | ||
| 171 | return Clay::UI::Enum::Result->HANDLED; | ||
| 172 | } | ||
| 173 | |||
| 174 | method handle_key ($event) { | ||
| 175 | return 0; | ||
| 176 | } | ||
| 177 | |||
| 178 | method handle_mouse ($event) { | ||
| 179 | return 0; | ||
| 180 | } | ||
| 181 | |||
| 182 | # A click: the left button pressed and released over the widget. | ||
| 183 | method activate () { | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | # The value the input holds, which validation reads. An input without | ||
| 188 | # a value of its own (a list that only navigates) has none: it counts | ||
| 189 | # as empty and is valid unless it is required. | ||
| 190 | method value () { | ||
| 191 | return undef; | ||
| 192 | } | ||
| 193 | |||
| 194 | # Fires Change, then ValidityChange when the message changed with it. | ||
| 195 | method fire_change ($value) { | ||
| 196 | $self->fire_event( Term::Fabulous::Event::Change->new( value => $value ) ); | ||
| 197 | $self->validate; | ||
| 198 | return; | ||
| 199 | } | ||
| 200 | |||
| 201 | method layout_properties :common () { | ||
| 202 | return ( | ||
| 203 | $class->SUPER::layout_properties, | ||
| 204 | can_focus => 'boolean', | ||
| 205 | disabled => 'boolean', | ||
| 206 | required => 'boolean', | ||
| 207 | required_message => 'scalar', | ||
| 208 | validator => 'scalar', | ||
| 209 | ); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | 1; | ||
| 214 | |||
| 215 | __END__ |