| 1 | package Term::Fabulous::Widget::Button; | ||
| 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::Widget::Box; | ||
| 15 | |||
| 16 | our $VERSION = '0.05'; | ||
| 17 | |||
| 18 | class Term::Fabulous::Widget::Button | ||
| 19 | :isa(Term::Fabulous::Widget::Box) | ||
| 20 | :does(Clay::UI::Role::Interaction::Focusable) | ||
| 21 | :does(Clay::UI::Role::Interaction::Pressable) | ||
| 22 | :does(Clay::UI::Role::Interaction::Hoverable) | ||
| 23 | :does(Clay::UI::Role::Interaction::Disableable) | ||
| 24 | :strict(params) | ||
| 25 | { | ||
| 26 | use Clay::UI::Enum::Result; | ||
| 27 | use Scalar::Util qw(refaddr); | ||
| 28 | use Term::Fabulous::Check qw(color optional); | ||
| 29 | use Term::Fabulous::Event::Activate; | ||
| 30 | |||
| 31 | use constant REVERSE_VIDEO => 'reverse'; | ||
| 32 | |||
| 33 | my %ACTIVATES = map { $_ => 1 } qw(Enter Space); | ||
| 34 | |||
| 35 | ADJUST { | ||
| 36 | my $continue = Clay::UI::Enum::Result->CONTINUE; | ||
| 37 | $self->on( | ||
| 38 | OnRelease => sub ( $self, $event ) { | ||
| 39 | $self->activate if refaddr( $event->target ) == refaddr($self) && $self->is_enabled; | ||
| 40 | return $continue; | ||
| 41 | }, | ||
| 42 | owner => $self | ||
| 43 | ); | ||
| 44 | $self->on( | ||
| 45 | KeyPress => sub ( $self, $event ) { | ||
| 46 | return $continue unless $ACTIVATES{ $event->main_key_name // '' } && $self->is_enabled; | ||
| 47 | $self->activate; | ||
| 48 | return; | ||
| 49 | }, | ||
| 50 | owner => $self | ||
| 51 | ); | ||
| 52 | } | ||
| 53 | |||
| 54 | # The pressed look: 'reverse', a color, or undef for no change. | ||
| 55 | sub _pressed_look ( $owner, $name, $value ) { | ||
| 56 | return REVERSE_VIDEO if defined $value && !ref $value && $value eq REVERSE_VIDEO; | ||
| 57 | return optional( \&color, $owner, $name, $value ); | ||
| 58 | } | ||
| 59 | |||
| 60 | method layout_properties :common () { | ||
| 61 | return ( | ||
| 62 | $class->SUPER::layout_properties, | ||
| 63 | can_focus => 'boolean', | ||
| 64 | disabled => 'boolean', | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | # --------------------------------------------------------------------- | ||
| 69 | # The theme: the button family, whose border, background and text | ||
| 70 | # follow the state. A disabled Button is neither pressed nor focused. | ||
| 71 | # --------------------------------------------------------------------- | ||
| 72 | |||
| 73 | method theme_family :common () { | ||
| 74 | return 'button'; | ||
| 75 | } | ||
| 76 | |||
| 77 | # The looks of a focused, a pressed and a disabled Button come from the | ||
| 78 | # theme unless given: focus_border_color (a color, or undef for no | ||
| 79 | # focus look), pressed_background_color ('reverse', a color, or undef | ||
| 80 | # for no change) and disabled_color. | ||
| 81 | method themed_params :common () { | ||
| 82 | return ( | ||
| 83 | $class->SUPER::themed_params, | ||
| 84 | focus_border_color => [ 'border.color', 'focused', 'optional_color' ], | ||
| 85 | pressed_background_color => [ 'background', 'pressed', \&_pressed_look ], | ||
| 86 | disabled_color => [ 'border.color', 'disabled', 'color' ], | ||
| 87 | ); | ||
| 88 | } | ||
| 89 | |||
| 90 | method look_state :override () { | ||
| 91 | return | ||
| 92 | !$self->is_enabled ? 'disabled' | ||
| 93 | : $self->is_pressed ? 'pressed' | ||
| 94 | : $self->is_focused ? 'focused' | ||
| 95 | : $self->is_hovered ? 'hovered' | ||
| 96 | : 'normal'; | ||
| 97 | } | ||
| 98 | |||
| 99 | method disabled_color (@new) { | ||
| 100 | return $self->look_value('disabled_color') unless @new; | ||
| 101 | return $self->set_look( disabled_color => $new[0] ); | ||
| 102 | } | ||
| 103 | |||
| 104 | # The color the Text widgets inside the Button are drawn in: the | ||
| 105 | # disabled color while it is disabled (the given one grays the text as | ||
| 106 | # well as the border), otherwise the text's own color or the theme's | ||
| 107 | # for the button in its state (see Term::Fabulous::Widget::Text). | ||
| 108 | method child_text_color ($explicit) { | ||
| 109 | return $self->has_look_override('disabled_color') ? $self->look_value('disabled_color') : $self->look( 'text', 'disabled' ) unless $self->is_enabled; | ||
| 110 | return $explicit // $self->look( 'text', $self->look_state ); | ||
| 111 | } | ||
| 112 | |||
| 113 | method focus_border_color (@new) { | ||
| 114 | return $self->look_value('focus_border_color') unless @new; | ||
| 115 | return $self->set_look( focus_border_color => $new[0] ); | ||
| 116 | } | ||
| 117 | |||
| 118 | method pressed_background_color (@new) { | ||
| 119 | return $self->look_value('pressed_background_color') unless @new; | ||
| 120 | return $self->set_look( pressed_background_color => $new[0] ); | ||
| 121 | } | ||
| 122 | |||
| 123 | method reverse_video :override () { | ||
| 124 | my $pressed = $self->look_value('pressed_background_color'); | ||
| 125 | return defined $pressed && !ref $pressed && $self->is_pressed ? 1 : 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | method activate () { | ||
| 129 | return $self->fire_event( Term::Fabulous::Event::Activate->new ); | ||
| 130 | } | ||
| 131 | |||
| 132 | } | ||
| 133 | |||
| 134 | 1; | ||
| 135 | |||
| 136 | __END__ |