blib/lib/Term/Fabulous/Widget/Button.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 15 Podpod n/a totaltotal 0.0% 0 / 157 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
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.02';
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 weaken);
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 weaken( my $weak_self = $self );
37 my $continue = Clay::UI::Enum::Result->CONTINUE;
38 $self->on(
39 OnRelease => sub ($event) {
40 $weak_self->activate if refaddr( $event->target ) == refaddr($weak_self) && $weak_self->is_enabled;
41 return $continue;
42 }
43 );
44 $self->on(
45 KeyPress => sub ($event) {
46 return $continue unless $ACTIVATES{ $event->main_key_name // '' } && $weak_self->is_enabled;
47 $weak_self->activate;
48 return;
49 }
50 );
51 }
52  
53 # The pressed look: 'reverse', a color, or undef for no change.
54 sub _pressed_look ( $owner, $name, $value ) {
55 return REVERSE_VIDEO if defined $value && !ref $value && $value eq REVERSE_VIDEO;
56 return optional( \&color, $owner, $name, $value );
57 }
58  
59 method layout_properties :common () {
60 return (
61 $class->SUPER::layout_properties,
62 can_focus => 'boolean',
63 disabled => 'boolean',
64 );
65 }
66  
67 # ---------------------------------------------------------------------
68 # The theme: the button family, whose border, background and text
69 # follow the state. A disabled Button is neither pressed nor focused.
70 # ---------------------------------------------------------------------
71  
72 method theme_family :common () {
73 return 'button';
74 }
75  
76 # The looks of a focused, a pressed and a disabled Button come from the
77 # theme unless given: focus_border_color (a color, or undef for no
78 # focus look), pressed_background_color ('reverse', a color, or undef
79 # for no change) and disabled_color.
80 method themed_params :common () {
81 return (
82 $class->SUPER::themed_params,
83 focus_border_color => [ 'border.color', 'focused', 'optional_color' ],
84 pressed_background_color => [ 'background', 'pressed', \&_pressed_look ],
85 disabled_color => [ 'border.color', 'disabled', 'color' ],
86 );
87 }
88  
89 method look_state :override () {
90 return
91 !$self->is_enabled ? 'disabled'
92 : $self->is_pressed ? 'pressed'
93 : $self->is_focused ? 'focused'
94 : $self->is_hovered ? 'hovered'
95 : 'normal';
96 }
97  
98 method disabled_color (@new) {
99 return $self->look_value('disabled_color') unless @new;
100 return $self->set_look( disabled_color => $new[0] );
101 }
102  
103 # The color the Text widgets inside the Button are drawn in: the
104 # disabled color while it is disabled (the given one grays the text as
105 # well as the border), otherwise the text's own color or the theme's
106 # for the button in its state (see Term::Fabulous::Widget::Text).
107 method child_text_color ($explicit) {
108 return $self->has_look_override('disabled_color') ? $self->look_value('disabled_color') : $self->look( 'text', 'disabled' ) unless $self->is_enabled;
109 return $explicit // $self->look( 'text', $self->look_state );
110 }
111  
112 method focus_border_color (@new) {
113 return $self->look_value('focus_border_color') unless @new;
114 return $self->set_look( focus_border_color => $new[0] );
115 }
116  
117 method pressed_background_color (@new) {
118 return $self->look_value('pressed_background_color') unless @new;
119 return $self->set_look( pressed_background_color => $new[0] );
120 }
121  
122 method reverse_video :override () {
123 my $pressed = $self->look_value('pressed_background_color');
124 return defined $pressed && !ref $pressed && $self->is_pressed ? 1 : 0;
125 }
126  
127 method activate () {
128 return $self->fire_event( Term::Fabulous::Event::Activate->new );
129 }
130  
131 }
132  
133 1;
134  
135 __END__