« blib/lib/Term/Fabulous/Widget/RadioButton.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/ScatterPlot.pm »
| 1 | package Term::Fabulous::Widget::RadioGroup; | ||
| 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 Term::Fabulous::Widget::Box; | ||
| 13 | |||
| 14 | our $VERSION = '0.05'; | ||
| 15 | |||
| 16 | class Term::Fabulous::Widget::RadioGroup | ||
| 17 | :isa(Term::Fabulous::Widget::Box) | ||
| 18 | :does(Clay::UI::Role::Interaction::Focusable) | ||
| 19 | :does(Clay::UI::Role::Interaction::Disableable) | ||
| 20 | :strict(params) | ||
| 21 | { | ||
| 22 | use Clay::UI::Enum::Result; | ||
| 23 | use Clay::XS qw(CLAY_TOP_TO_BOTTOM); | ||
| 24 | use List::Util qw(first); | ||
| 25 | use Term::Fabulous::Roving qw(roving_target); | ||
| 26 | use Scalar::Util qw(blessed refaddr); | ||
| 27 | use Term::Fabulous::Event::Change; | ||
| 28 | |||
| 29 | use constant BUTTON_CLASS => 'Term::Fabulous::Widget::RadioButton'; | ||
| 30 | |||
| 31 | field $value :param = undef; | ||
| 32 | |||
| 33 | ADJUST { | ||
| 34 | my $layout = $self->layout; | ||
| 35 | $self->layout( { %$layout, layout_direction => CLAY_TOP_TO_BOTTOM } ) unless exists $layout->{layout_direction}; | ||
| 36 | |||
| 37 | my $continue = Clay::UI::Enum::Result->CONTINUE; | ||
| 38 | $self->on( | ||
| 39 | KeyPress => sub ( $self, $event ) { | ||
| 40 | return Clay::UI::Enum::Result->HANDLED if $self->is_enabled && $self->handle_key($event); | ||
| 41 | return $continue; | ||
| 42 | }, | ||
| 43 | owner => $self | ||
| 44 | ); | ||
| 45 | } | ||
| 46 | |||
| 47 | method value (@new) { | ||
| 48 | return $value unless @new; | ||
| 49 | $value = $new[0]; | ||
| 50 | $self->mark_changed; | ||
| 51 | return $value; | ||
| 52 | } | ||
| 53 | |||
| 54 | method layout_properties :common () { | ||
| 55 | return ( $class->SUPER::layout_properties, value => 'scalar', disabled => 'boolean', can_focus => 'boolean' ); | ||
| 56 | } | ||
| 57 | |||
| 58 | # The radio buttons of the group in tree order: its descendants, except | ||
| 59 | # those of nested groups. | ||
| 60 | method buttons () { | ||
| 61 | return grep { $_->isa(BUTTON_CLASS) && refaddr( $_->group ) == refaddr($self) } $self->descendants; | ||
| 62 | } | ||
| 63 | |||
| 64 | method holds_value ($candidate) { | ||
| 65 | return defined $value && defined $candidate && $candidate eq $value; | ||
| 66 | } | ||
| 67 | |||
| 68 | method selected_button () { | ||
| 69 | return first { $self->holds_value( $_->value ) } $self->buttons; | ||
| 70 | } | ||
| 71 | |||
| 72 | # The button the keyboard is on: the selected one, or else the first | ||
| 73 | # enabled one. | ||
| 74 | method cursor_button () { | ||
| 75 | my $selected = $self->selected_button; | ||
| 76 | return $selected if defined $selected && $selected->is_enabled; | ||
| 77 | return first { $_->is_enabled } $self->buttons; | ||
| 78 | } | ||
| 79 | |||
| 80 | # Selects a button as the user does: fires Change when the value | ||
| 81 | # changes. | ||
| 82 | method choose ($button) { | ||
| 83 | die "Term::Fabulous::Widget::RadioGroup: choose needs one of the group's radio buttons" | ||
| 84 | unless blessed $button && grep { refaddr($_) == refaddr($button) } $self->buttons; | ||
| 85 | return $self if $self->holds_value( $button->value ); | ||
| 86 | $self->value( $button->value ); | ||
| 87 | $self->fire_event( Term::Fabulous::Event::Change->new( value => $value ) ); | ||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | method handle_key ($event) { | ||
| 92 | my $name = $event->main_key_name // return 0; | ||
| 93 | my $cursor = $self->cursor_button // return 0; | ||
| 94 | |||
| 95 | if ( $name eq 'Space' || $name eq 'Enter' ) { | ||
| 96 | $self->choose($cursor); | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | my @buttons = $self->buttons; | ||
| 101 | my @enabled = grep { $buttons[$_]->is_enabled } 0 .. $#buttons; | ||
| 102 | my $at = first { refaddr( $buttons[$_] ) == refaddr($cursor) } 0 .. $#buttons; | ||
| 103 | my $target = roving_target( \@enabled, $at, $name ) // return 0; | ||
| 104 | $self->choose( $buttons[$target] ); | ||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ |