| 1 | package Term::Fabulous::Widget::Checkbox; | ||
| 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::Checkbox | ||
| 15 | :isa(Term::Fabulous::Widget::Input) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use List::Util qw(max); | ||
| 19 | use Term::Fabulous::Check qw(boolean string); | ||
| 20 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 21 | |||
| 22 | field $label :param = ''; | ||
| 23 | field $checked :param = 0; | ||
| 24 | field $indeterminate :param = 0; | ||
| 25 | field $checked_mark :param = '[x]'; | ||
| 26 | field $unchecked_mark :param = '[ ]'; | ||
| 27 | field $indeterminate_mark :param = '[-]'; | ||
| 28 | |||
| 29 | ADJUST { | ||
| 30 | $label = string( $self, label => $label ); | ||
| 31 | $checked_mark = string( $self, checked_mark => $checked_mark ); | ||
| 32 | $unchecked_mark = string( $self, unchecked_mark => $unchecked_mark ); | ||
| 33 | $indeterminate_mark = string( $self, indeterminate_mark => $indeterminate_mark ); | ||
| 34 | $checked = boolean( $self, checked => $checked ); | ||
| 35 | $indeterminate = boolean( $self, indeterminate => $indeterminate ); | ||
| 36 | } | ||
| 37 | |||
| 38 | method _set_string ( $name, $field_ref, @new ) { | ||
| 39 | return $$field_ref unless @new; | ||
| 40 | $$field_ref = string( $self, $name => $new[0] ); | ||
| 41 | $self->mark_changed; | ||
| 42 | return $$field_ref; | ||
| 43 | } | ||
| 44 | |||
| 45 | method label (@new) { return $self->_set_string( label => \$label, @new ) } | ||
| 46 | method checked_mark (@new) { return $self->_set_string( checked_mark => \$checked_mark, @new ) } | ||
| 47 | method unchecked_mark (@new) { return $self->_set_string( unchecked_mark => \$unchecked_mark, @new ) } | ||
| 48 | method indeterminate_mark (@new) { return $self->_set_string( indeterminate_mark => \$indeterminate_mark, @new ) } | ||
| 49 | |||
| 50 | method checked (@new) { | ||
| 51 | return $checked unless @new; | ||
| 52 | $checked = boolean( $self, checked => $new[0] ); | ||
| 53 | $indeterminate = 0; | ||
| 54 | $self->mark_changed; | ||
| 55 | return $checked; | ||
| 56 | } | ||
| 57 | |||
| 58 | method indeterminate (@new) { | ||
| 59 | return $indeterminate unless @new; | ||
| 60 | $indeterminate = boolean( $self, indeterminate => $new[0] ); | ||
| 61 | $self->mark_changed; | ||
| 62 | return $indeterminate; | ||
| 63 | } | ||
| 64 | |||
| 65 | method value () { | ||
| 66 | return $checked; | ||
| 67 | } | ||
| 68 | |||
| 69 | # A required checkbox must be checked. | ||
| 70 | method value_is_empty :override () { | ||
| 71 | return $checked ? 0 : 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | method layout_properties :common () { | ||
| 75 | return ( | ||
| 76 | $class->SUPER::layout_properties, | ||
| 77 | label => 'scalar', | ||
| 78 | checked => 'boolean', | ||
| 79 | indeterminate => 'boolean', | ||
| 80 | checked_mark => 'scalar', | ||
| 81 | unchecked_mark => 'scalar', | ||
| 82 | indeterminate_mark => 'scalar', | ||
| 83 | ); | ||
| 84 | } | ||
| 85 | |||
| 86 | method _mark () { | ||
| 87 | return $indeterminate ? $indeterminate_mark : $checked ? $checked_mark : $unchecked_mark; | ||
| 88 | } | ||
| 89 | |||
| 90 | # The columns reserved for the mark: those of the widest one, so the | ||
| 91 | # label stays in place when the mark changes. | ||
| 92 | method _mark_columns () { | ||
| 93 | return max map { string_columns($_) } $checked_mark, $unchecked_mark, $indeterminate_mark; | ||
| 94 | } | ||
| 95 | |||
| 96 | method natural_size () { | ||
| 97 | return ( $self->_mark_columns + ( length $label ? 1 + string_columns($label) : 0 ), 1 ); | ||
| 98 | } | ||
| 99 | |||
| 100 | method paint () { | ||
| 101 | my $bg = $self->paint_focus_background; | ||
| 102 | my $mark_fg = $checked || $indeterminate ? $self->accent_attr : $self->foreground_attr; | ||
| 103 | $self->paint_text( 0, 0, $self->_mark, $mark_fg, $bg ); | ||
| 104 | $self->paint_text( $self->_mark_columns + 1, 0, $label, $self->foreground_attr, $bg ) if length $label; | ||
| 105 | return; | ||
| 106 | } | ||
| 107 | |||
| 108 | method toggle () { | ||
| 109 | $self->checked( $indeterminate || !$checked ); | ||
| 110 | $self->fire_change($checked); | ||
| 111 | return $self; | ||
| 112 | } | ||
| 113 | |||
| 114 | method handle_key ($event) { | ||
| 115 | my $name = $event->main_key_name // return 0; | ||
| 116 | return 0 unless $name eq 'Space' || $name eq 'Enter'; | ||
| 117 | $self->toggle; | ||
| 118 | return 1; | ||
| 119 | } | ||
| 120 | |||
| 121 | method activate () { | ||
| 122 | $self->toggle; | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; | ||
| 128 | |||
| 129 | __END__ |