« 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.02'; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::RadioButton | ||
| 15 | :isa(Term::Fabulous::Widget::Input) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use List::Util qw(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 | for ( my $node = $self->parent; defined $node; $node = $node->parent ) { | ||
| 65 | return $node if $node->isa('Term::Fabulous::Widget::RadioGroup'); | ||
| 66 | } | ||
| 67 | return undef; | ||
| 68 | } | ||
| 69 | |||
| 70 | method is_selected () { | ||
| 71 | my $group = $self->group; | ||
| 72 | return defined $group && $group->holds_value( $self->value ) ? 1 : 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | method is_enabled :override () { | ||
| 76 | my $group = $self->group; | ||
| 77 | return $self->SUPER::is_enabled && ( !defined $group || $group->is_enabled ); | ||
| 78 | } | ||
| 79 | |||
| 80 | # The group decides the look as well: which button is selected, which | ||
| 81 | # one shows the focus, and whether the buttons are enabled. | ||
| 82 | method paint_key :override () { | ||
| 83 | my $group = $self->group; | ||
| 84 | my $cursor = defined $group ? $group->cursor_button : undef; | ||
| 85 | return ( | ||
| 86 | $self->SUPER::paint_key, | ||
| 87 | defined $group ? ( $group->value, $group->is_focused, defined $cursor ? refaddr($cursor) : 0 ) : (), | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | |||
| 91 | method focus_background_attr :override () { | ||
| 92 | my $group = $self->group; | ||
| 93 | return undef unless defined $group && $group->is_focused; | ||
| 94 | my $cursor = $group->cursor_button; | ||
| 95 | return undef unless defined $cursor && refaddr($cursor) == refaddr($self); | ||
| 96 | return $self->color_attr( $self->focus_background_color ); | ||
| 97 | } | ||
| 98 | |||
| 99 | # The columns reserved for the mark: those of the wider one, so the | ||
| 100 | # label stays in place when the selection changes. | ||
| 101 | method _mark_columns () { | ||
| 102 | return max map { string_columns($_) } $selected_mark, $unselected_mark; | ||
| 103 | } | ||
| 104 | |||
| 105 | method natural_size () { | ||
| 106 | return ( $self->_mark_columns + ( length $label ? 1 + string_columns($label) : 0 ), 1 ); | ||
| 107 | } | ||
| 108 | |||
| 109 | method paint () { | ||
| 110 | my $bg = $self->paint_focus_background; | ||
| 111 | my $selected = $self->is_selected; | ||
| 112 | $self->paint_text( 0, 0, $selected ? $selected_mark : $unselected_mark, $selected ? $self->accent_attr : $self->foreground_attr, $bg ); | ||
| 113 | $self->paint_text( $self->_mark_columns + 1, 0, $label, $self->foreground_attr, $bg ) if length $label; | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | method activate () { | ||
| 118 | my $group = $self->group // die "Term::Fabulous::Widget::RadioButton: a radio button must be inside a Term::Fabulous::Widget::RadioGroup"; | ||
| 119 | $group->choose($self); | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; | ||
| 125 | |||
| 126 | __END__ |