« blib/lib/Term/Fabulous/Widget/RadarChart.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/RadioGroup.pm »
| 1 | package Term::Fabulous::Widget::RadioButton; | ||
| 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 Term::Fabulous::Widget::Input; | ||
| 11 | |||
| 12 | our $VERSION = '0.05'; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::RadioButton | ||
| 15 | :isa(Term::Fabulous::Widget::Input) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use List::Util qw(first max); | ||
| 19 | use Scalar::Util qw(refaddr); | ||
| 20 | use Term::Fabulous::Check qw(optional string); | ||
| 21 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 22 | |||
| 23 | field $label :param = ''; | ||
| 24 | field $value :param = undef; | ||
| 25 | field $selected_mark :param = "(\x{2022})"; | ||
| 26 | field $unselected_mark :param = '( )'; | ||
| 27 | |||
| 28 | ADJUST { | ||
| 29 | $label = string( $self, label => $label ); | ||
| 30 | $selected_mark = string( $self, selected_mark => $selected_mark ); | ||
| 31 | $unselected_mark = string( $self, unselected_mark => $unselected_mark ); | ||
| 32 | $value = string( $self, value => $value ) if defined $value; | ||
| 33 | } | ||
| 34 | |||
| 35 | method _set_string ( $name, $field_ref, @new ) { | ||
| 36 | return $$field_ref unless @new; | ||
| 37 | $$field_ref = string( $self, $name => $new[0] ); | ||
| 38 | $self->mark_changed; | ||
| 39 | return $$field_ref; | ||
| 40 | } | ||
| 41 | |||
| 42 | method label (@new) { return $self->_set_string( label => \$label, @new ) } | ||
| 43 | method selected_mark (@new) { return $self->_set_string( selected_mark => \$selected_mark, @new ) } | ||
| 44 | method unselected_mark (@new) { return $self->_set_string( unselected_mark => \$unselected_mark, @new ) } | ||
| 45 | |||
| 46 | # The value defaults to the label. | ||
| 47 | method value (@new) { | ||
| 48 | return $value // $label unless @new; | ||
| 49 | $value = optional( \&string, $self, value => $new[0] ); | ||
| 50 | $self->mark_changed; | ||
| 51 | return $value // $label; | ||
| 52 | } | ||
| 53 | |||
| 54 | method layout_properties :common () { | ||
| 55 | return ( $class->SUPER::layout_properties, label => 'scalar', value => 'scalar', selected_mark => 'scalar', unselected_mark => 'scalar' ); | ||
| 56 | } | ||
| 57 | |||
| 58 | # The group takes the focus for its buttons. | ||
| 59 | method accepts_focus :override () { | ||
| 60 | return 0; | ||
| 61 | } | ||
| 62 | |||
| 63 | method group () { | ||
| 64 | return first { $_->isa('Term::Fabulous::Widget::RadioGroup') } $self->ancestors; | ||
| 65 | } | ||
| 66 | |||
| 67 | method is_selected () { | ||
| 68 | my $group = $self->group; | ||
| 69 | return defined $group && $group->holds_value( $self->value ) ? 1 : 0; | ||
| 70 | } | ||
| 71 | |||
| 72 | method is_enabled :override () { | ||
| 73 | my $group = $self->group; | ||
| 74 | return $self->SUPER::is_enabled && ( !defined $group || $group->is_enabled ); | ||
| 75 | } | ||
| 76 | |||
| 77 | # The group decides the look as well: which button is selected, which | ||
| 78 | # one shows the focus, and whether the buttons are enabled. | ||
| 79 | method paint_key :override () { | ||
| 80 | my $group = $self->group; | ||
| 81 | my $cursor = defined $group ? $group->cursor_button : undef; | ||
| 82 | return ( | ||
| 83 | $self->SUPER::paint_key, | ||
| 84 | defined $group ? ( $group->value, $group->is_focused, defined $cursor ? refaddr($cursor) : 0 ) : (), | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | |||
| 88 | method focus_background_attr :override () { | ||
| 89 | my $group = $self->group; | ||
| 90 | return undef unless defined $group && $group->is_focused; | ||
| 91 | my $cursor = $group->cursor_button; | ||
| 92 | return undef unless defined $cursor && refaddr($cursor) == refaddr($self); | ||
| 93 | return $self->color_attr( $self->focus_background_color ); | ||
| 94 | } | ||
| 95 | |||
| 96 | # The columns reserved for the mark: those of the wider one, so the | ||
| 97 | # label stays in place when the selection changes. | ||
| 98 | method _mark_columns () { | ||
| 99 | return max map { string_columns($_) } $selected_mark, $unselected_mark; | ||
| 100 | } | ||
| 101 | |||
| 102 | method natural_size () { | ||
| 103 | return ( $self->_mark_columns + ( length $label ? 1 + string_columns($label) : 0 ), 1 ); | ||
| 104 | } | ||
| 105 | |||
| 106 | method paint () { | ||
| 107 | my $bg = $self->paint_focus_background; | ||
| 108 | my $selected = $self->is_selected; | ||
| 109 | $self->paint_text( 0, 0, $selected ? $selected_mark : $unselected_mark, $selected ? $self->accent_attr : $self->foreground_attr, $bg ); | ||
| 110 | $self->paint_text( $self->_mark_columns + 1, 0, $label, $self->foreground_attr, $bg ) if length $label; | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | method activate () { | ||
| 115 | my $group = $self->group // die "Term::Fabulous::Widget::RadioButton: a radio button must be inside a Term::Fabulous::Widget::RadioGroup"; | ||
| 116 | $group->choose($self); | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; | ||
| 122 | |||
| 123 | __END__ |