| 1 | package Term::Fabulous::Widget::Accordion; | ||
| 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::Box; | ||
| 11 | use Term::Fabulous::Widget::Accordion::Item; | ||
| 12 | |||
| 13 | our $VERSION = '0.05'; | ||
| 14 | |||
| 15 | class Term::Fabulous::Widget::Accordion | ||
| 16 | :isa(Term::Fabulous::Widget::Box) | ||
| 17 | :strict(params) | ||
| 18 | { | ||
| 19 | use Clay::UI::Enum::Result; | ||
| 20 | use Clay::XS qw(sizing_grow CLAY_TOP_TO_BOTTOM); | ||
| 21 | use List::Util qw(first); | ||
| 22 | use Scalar::Util qw(blessed refaddr); | ||
| 23 | use Term::Fabulous::Check qw(boolean describe non_negative_integer one_of string); | ||
| 24 | use Term::Fabulous::Roving qw(roving_target); | ||
| 25 | use Term::Fabulous::Event::Select; | ||
| 26 | |||
| 27 | use constant ITEM_CLASS => 'Term::Fabulous::Widget::Accordion::Item'; | ||
| 28 | |||
| 29 | my @TOGGLE_POSITIONS = qw(start end); | ||
| 30 | |||
| 31 | field $multiple :param = 0; | ||
| 32 | field $item_borders :param = 0; | ||
| 33 | field $toggle_position :param = 'start'; | ||
| 34 | field $open_glyph :param = "\x{25BE}"; | ||
| 35 | field $closed_glyph :param = "\x{25B8}"; | ||
| 36 | field $title_bold :param = 0; | ||
| 37 | field $body_indent :param = 2; | ||
| 38 | |||
| 39 | ADJUST { | ||
| 40 | $multiple = boolean( $self, multiple => $multiple ); | ||
| 41 | $item_borders = boolean( $self, item_borders => $item_borders ); | ||
| 42 | $toggle_position = one_of( $self, toggle_position => $toggle_position, @TOGGLE_POSITIONS ); | ||
| 43 | $open_glyph = string( $self, open_glyph => $open_glyph ); | ||
| 44 | $closed_glyph = string( $self, closed_glyph => $closed_glyph ); | ||
| 45 | $title_bold = boolean( $self, title_bold => $title_bold ); | ||
| 46 | $body_indent = non_negative_integer( $self, body_indent => $body_indent ); | ||
| 47 | |||
| 48 | my $layout = $self->layout; | ||
| 49 | $self->layout( { %$layout, layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow(), %{ $layout->{sizing} // {} } } } ); | ||
| 50 | |||
| 51 | $self->on( KeyPress => sub ( $self, $event ) { return $self->_handle_key($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE }, owner => $self ); | ||
| 52 | } | ||
| 53 | |||
| 54 | # --------------------------------------------------------------------- | ||
| 55 | # Items | ||
| 56 | # --------------------------------------------------------------------- | ||
| 57 | |||
| 58 | method add_child :override (@kids) { | ||
| 59 | foreach my $kid (@kids) { | ||
| 60 | die ref($self) . ": an accordion holds only Term::Fabulous::Widget::Accordion::Item widgets, got " . ( blessed $kid ? ref $kid : describe($kid) ) | ||
| 61 | unless blessed $kid && $kid->isa(ITEM_CLASS); | ||
| 62 | } | ||
| 63 | $self->SUPER::add_child(@kids); | ||
| 64 | foreach my $kid (@kids) { | ||
| 65 | $self->_style_item($kid); | ||
| 66 | $self->_close_others($kid) if $kid->is_open && !$multiple; | ||
| 67 | } | ||
| 68 | return $self; | ||
| 69 | } | ||
| 70 | |||
| 71 | method items () { | ||
| 72 | return @{ $self->children }; | ||
| 73 | } | ||
| 74 | |||
| 75 | method item ($index) { | ||
| 76 | my @items = $self->items; | ||
| 77 | die ref($self) . ": item needs an index in 0.." . $#items . ", got " . describe($index) unless defined $index && !ref $index && $index =~ /\A[0-9]+\z/ && $index < @items; | ||
| 78 | return $items[$index]; | ||
| 79 | } | ||
| 80 | |||
| 81 | # An item or its index: the item. | ||
| 82 | method _item_of ($which) { | ||
| 83 | return $self->item($which) unless blessed $which; | ||
| 84 | die ref($self) . ": not an item of this accordion" unless first { refaddr($_) == refaddr($which) } $self->items; | ||
| 85 | return $which; | ||
| 86 | } | ||
| 87 | |||
| 88 | method index_of ($item) { | ||
| 89 | my @items = $self->items; | ||
| 90 | return first { refaddr( $items[$_] ) == refaddr($item) } 0 .. $#items; | ||
| 91 | } | ||
| 92 | |||
| 93 | method open_items () { | ||
| 94 | return grep { $_->is_open } $self->items; | ||
| 95 | } | ||
| 96 | |||
| 97 | # The open item of an accordion that allows one; the first open one | ||
| 98 | # otherwise. | ||
| 99 | method selected () { | ||
| 100 | my @open = $self->open_items; | ||
| 101 | return $open[0]; | ||
| 102 | } | ||
| 103 | |||
| 104 | method _close_others ($kept) { | ||
| 105 | $_->open(0) foreach grep { refaddr($_) != refaddr($kept) } $self->open_items; | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | |||
| 109 | # Opens an item from the program: closes the others unless multiple | ||
| 110 | # are allowed. Fires nothing. | ||
| 111 | method open ($which) { | ||
| 112 | my $item = $self->_item_of($which); | ||
| 113 | $self->_close_others($item) unless $multiple; | ||
| 114 | $item->open(1); | ||
| 115 | return $self; | ||
| 116 | } | ||
| 117 | |||
| 118 | method close ($which) { | ||
| 119 | $self->_item_of($which)->open(0); | ||
| 120 | return $self; | ||
| 121 | } | ||
| 122 | |||
| 123 | method open_all () { | ||
| 124 | die ref($self) . ": open_all needs multiple => 1" unless $multiple; | ||
| 125 | $_->open(1) foreach $self->items; | ||
| 126 | return $self; | ||
| 127 | } | ||
| 128 | |||
| 129 | method close_all () { | ||
| 130 | $_->open(0) foreach $self->items; | ||
| 131 | return $self; | ||
| 132 | } | ||
| 133 | |||
| 134 | # Toggles an item as the user does: fires Select. A disabled item | ||
| 135 | # changes nothing. | ||
| 136 | method choose ($which) { | ||
| 137 | my $item = $self->_item_of($which); | ||
| 138 | return $self unless $item->is_enabled; | ||
| 139 | $self->_item_activated($item); | ||
| 140 | return $self; | ||
| 141 | } | ||
| 142 | |||
| 143 | method _item_activated ($item) { | ||
| 144 | my $opens = !$item->is_open; | ||
| 145 | $self->_close_others($item) if $opens && !$multiple; | ||
| 146 | $item->open($opens); | ||
| 147 | $self->fire_event( Term::Fabulous::Event::Select->new( item => $item, index => $self->index_of($item), open => $opens ) ); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | # --------------------------------------------------------------------- | ||
| 152 | # Look | ||
| 153 | # --------------------------------------------------------------------- | ||
| 154 | |||
| 155 | # With item_borders, an item takes the theme's accordion border; | ||
| 156 | # without, it has none. | ||
| 157 | method _style_item ($item) { | ||
| 158 | if ($item_borders) { | ||
| 159 | my $style = $self->look('border.style'); | ||
| 160 | $item->bordered(1); | ||
| 161 | $item->$_($style) foreach qw(border_style_top border_style_right border_style_bottom border_style_left); | ||
| 162 | $item->border_color( $self->look('border.color') ); | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | $item->bordered(0); | ||
| 166 | } | ||
| 167 | $item->refresh_look; | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | |||
| 171 | method theme_family :common () { | ||
| 172 | return 'accordion'; | ||
| 173 | } | ||
| 174 | |||
| 175 | method themed_params :common () { | ||
| 176 | return ( | ||
| 177 | $class->SUPER::themed_params, | ||
| 178 | title_color => [ 'title', 'normal', 'color' ], | ||
| 179 | accent_color => [ 'accent', 'normal', 'color' ], | ||
| 180 | header_background_color => [ 'header.background', 'normal', 'optional_color' ], | ||
| 181 | focus_background_color => [ 'header.background', 'focused', 'color' ], | ||
| 182 | hover_background_color => [ 'header.background', 'hovered', 'color' ], | ||
| 183 | disabled_color => [ 'disabled', 'normal', 'color' ], | ||
| 184 | ); | ||
| 185 | } | ||
| 186 | |||
| 187 | # The items copy the looks (Term::Fabulous::Role::Themed). | ||
| 188 | method looks_changed (@names) { | ||
| 189 | $self->_restyle; | ||
| 190 | return; | ||
| 191 | } | ||
| 192 | |||
| 193 | method _restyle () { | ||
| 194 | $self->_style_item($_) foreach $self->items; | ||
| 195 | $self->mark_changed; | ||
| 196 | return; | ||
| 197 | } | ||
| 198 | |||
| 199 | method _set ( $field_ref, $value ) { | ||
| 200 | $$field_ref = $value; | ||
| 201 | $self->_restyle; | ||
| 202 | return $$field_ref; | ||
| 203 | } | ||
| 204 | |||
| 205 | method multiple (@new) { | ||
| 206 | return $multiple unless @new; | ||
| 207 | $multiple = boolean( $self, multiple => $new[0] ); | ||
| 208 | $self->_close_others( $self->selected ) if !$multiple && defined $self->selected; | ||
| 209 | return $multiple; | ||
| 210 | } | ||
| 211 | |||
| 212 | method item_borders (@new) { return @new ? $self->_set( \$item_borders, boolean( $self, item_borders => $new[0] ) ) : $item_borders } | ||
| 213 | method toggle_position (@new) { return @new ? $self->_set( \$toggle_position, one_of( $self, toggle_position => $new[0], @TOGGLE_POSITIONS ) ) : $toggle_position } | ||
| 214 | method open_glyph (@new) { return @new ? $self->_set( \$open_glyph, string( $self, open_glyph => $new[0] ) ) : $open_glyph } | ||
| 215 | method closed_glyph (@new) { return @new ? $self->_set( \$closed_glyph, string( $self, closed_glyph => $new[0] ) ) : $closed_glyph } | ||
| 216 | method title_bold (@new) { return @new ? $self->_set( \$title_bold, boolean( $self, title_bold => $new[0] ) ) : $title_bold } | ||
| 217 | method body_indent (@new) { return @new ? $self->_set( \$body_indent, non_negative_integer( $self, body_indent => $new[0] ) ) : $body_indent } | ||
| 218 | method title_color (@new) { return @new ? $self->set_look( title_color => $new[0] ) : $self->look_value('title_color') } | ||
| 219 | method accent_color (@new) { return @new ? $self->set_look( accent_color => $new[0] ) : $self->look_value('accent_color') } | ||
| 220 | |||
| 221 | method header_background_color (@new) { | ||
| 222 | return @new ? $self->set_look( header_background_color => $new[0] ) : $self->look_value('header_background_color'); | ||
| 223 | } | ||
| 224 | method focus_background_color (@new) { return @new ? $self->set_look( focus_background_color => $new[0] ) : $self->look_value('focus_background_color') } | ||
| 225 | method hover_background_color (@new) { return @new ? $self->set_look( hover_background_color => $new[0] ) : $self->look_value('hover_background_color') } | ||
| 226 | method disabled_color (@new) { return @new ? $self->set_look( disabled_color => $new[0] ) : $self->look_value('disabled_color') } | ||
| 227 | |||
| 228 | method layout_properties :common () { | ||
| 229 | return ( | ||
| 230 | $class->SUPER::layout_properties, | ||
| 231 | ( map { $_ => 'boolean' } qw(multiple item_borders title_bold) ), | ||
| 232 | ( map { $_ => 'scalar' } qw(toggle_position open_glyph closed_glyph body_indent) ), | ||
| 233 | ); | ||
| 234 | } | ||
| 235 | |||
| 236 | # --------------------------------------------------------------------- | ||
| 237 | # Keys: Up, Down, Home and End move the focus between the headers. | ||
| 238 | # --------------------------------------------------------------------- | ||
| 239 | |||
| 240 | method _handle_key ($event) { | ||
| 241 | my @items = $self->items; | ||
| 242 | my @enabled = grep { $items[$_]->is_enabled } 0 .. $#items; | ||
| 243 | my $focused = first { $items[$_]->is_focused } @enabled; | ||
| 244 | return 0 unless defined $focused; | ||
| 245 | my $target = roving_target( \@enabled, $focused, $event->main_key_name, keys => 'vertical' ) // return 0; | ||
| 246 | $items[$target]->focus; | ||
| 247 | return 1; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | 1; | ||
| 252 | |||
| 253 | __END__ |