| 1 | package Term::Fabulous::Widget::Accordion::Item; | ||
| 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::Button; | ||
| 12 | use Term::Fabulous::Widget::Text; | ||
| 13 | |||
| 14 | our $VERSION = '0.03'; | ||
| 15 | |||
| 16 | class Term::Fabulous::Widget::Accordion::Item | ||
| 17 | :isa(Term::Fabulous::Widget::Box) | ||
| 18 | :strict(params) | ||
| 19 | { | ||
| 20 | use Clay::UI::Enum::Result; | ||
| 21 | use Clay::XS qw(sizing_grow CLAY_TOP_TO_BOTTOM); | ||
| 22 | use Scalar::Util qw(blessed weaken); | ||
| 23 | use Term::Fabulous::Check qw(boolean optional string); | ||
| 24 | use Term::Fabulous::Theme; | ||
| 25 | |||
| 26 | # The settings an item shows while it has no accordion to take them | ||
| 27 | # from; the colors come from the default theme's accordion family. | ||
| 28 | my %DEFAULT_LOOK = ( | ||
| 29 | toggle_position => 'start', | ||
| 30 | open_glyph => "\x{25BE}", | ||
| 31 | closed_glyph => "\x{25B8}", | ||
| 32 | title_bold => 0, | ||
| 33 | body_indent => 2, | ||
| 34 | ); | ||
| 35 | |||
| 36 | field $title :param = ''; | ||
| 37 | field $icon :param = undef; | ||
| 38 | |||
| 39 | # The header the user acts on, the body that holds the children, and | ||
| 40 | # the texts of the header, which the look updates. | ||
| 41 | field $_header; | ||
| 42 | field $_body; | ||
| 43 | field $_toggle_text; | ||
| 44 | field $_icon_text; | ||
| 45 | field $_title_text; | ||
| 46 | field $_is_open = 0; | ||
| 47 | field $_built = 0; | ||
| 48 | |||
| 49 | ADJUST :params ( :$open = 0, :$disabled = 0 ) { | ||
| 50 | $title = string( $self, title => $title ); | ||
| 51 | $icon = optional( \&string, $self, icon => $icon ); | ||
| 52 | $self->layout( { %{ $self->layout }, layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow(), %{ $self->layout->{sizing} // {} } } } ); | ||
| 53 | |||
| 54 | weaken( my $weak_self = $self ); | ||
| 55 | my $continue = Clay::UI::Enum::Result->CONTINUE; | ||
| 56 | $_header = Term::Fabulous::Widget::Button->new( layout => { sizing => { width => sizing_grow() }, child_gap => 1 }, pressed_background_color => undef ); | ||
| 57 | $_header->on( Activate => sub ($event) { $weak_self->_activated if $weak_self; return } ); | ||
| 58 | $_header->on( $_ => sub ($event) { $weak_self->refresh_look if $weak_self; return $continue } ) foreach qw(OnFocus OnBlur OnHoverStart OnHoverStopped); | ||
| 59 | $_body = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow() } } ); | ||
| 60 | $_toggle_text = Term::Fabulous::Widget::Text->new( text => '' ); | ||
| 61 | $_icon_text = Term::Fabulous::Widget::Text->new( text => $icon // '' ); | ||
| 62 | $_title_text = Term::Fabulous::Widget::Text->new( text => $title ); | ||
| 63 | |||
| 64 | $self->SUPER::add_child($_header); | ||
| 65 | $_built = 1; | ||
| 66 | $_header->disabled( boolean( $self, disabled => $disabled ) ); | ||
| 67 | $self->open( boolean( $self, open => $open ) ); | ||
| 68 | $self->refresh_look; | ||
| 69 | } | ||
| 70 | |||
| 71 | # The accordion the item belongs to, if any. | ||
| 72 | method accordion () { | ||
| 73 | my $parent = $self->parent; | ||
| 74 | return blessed $parent && $parent->isa('Term::Fabulous::Widget::Accordion') ? $parent : undef; | ||
| 75 | } | ||
| 76 | |||
| 77 | # --------------------------------------------------------------------- | ||
| 78 | # The parts | ||
| 79 | # --------------------------------------------------------------------- | ||
| 80 | |||
| 81 | method header () { return $_header } | ||
| 82 | method body () { return $_body } | ||
| 83 | |||
| 84 | # The item's children live in its body; the header is its own. | ||
| 85 | method add_child :override (@kids) { | ||
| 86 | return $self->SUPER::add_child(@kids) unless $_built; | ||
| 87 | $_body->add_child(@kids); | ||
| 88 | return $self; | ||
| 89 | } | ||
| 90 | |||
| 91 | method remove_child :override (@kids) { | ||
| 92 | $_body->remove_child(@kids); | ||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | method remove_child_with_id :override ($target_id) { | ||
| 97 | $_body->remove_child_with_id($target_id); | ||
| 98 | return $self; | ||
| 99 | } | ||
| 100 | |||
| 101 | method remove_children_with :override ($predicate) { | ||
| 102 | $_body->remove_children_with($predicate); | ||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | method clear_children :override () { | ||
| 107 | $_body->clear_children; | ||
| 108 | return $self; | ||
| 109 | } | ||
| 110 | |||
| 111 | # --------------------------------------------------------------------- | ||
| 112 | # State | ||
| 113 | # --------------------------------------------------------------------- | ||
| 114 | |||
| 115 | method is_open () { | ||
| 116 | return $_is_open; | ||
| 117 | } | ||
| 118 | |||
| 119 | # Opens or closes the item: the body is part of the tree only while | ||
| 120 | # the item is open. Fires nothing. | ||
| 121 | method open (@new) { | ||
| 122 | return $_is_open unless @new; | ||
| 123 | my $wanted = boolean( $self, open => $new[0] ); | ||
| 124 | return $_is_open if $wanted == $_is_open; | ||
| 125 | $_is_open = $wanted; | ||
| 126 | if ($wanted) { | ||
| 127 | $self->SUPER::add_child($_body); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | $self->SUPER::remove_child($_body); | ||
| 131 | } | ||
| 132 | $self->refresh_look; | ||
| 133 | return $_is_open; | ||
| 134 | } | ||
| 135 | |||
| 136 | method disabled (@new) { | ||
| 137 | return $_header->disabled unless @new; | ||
| 138 | $_header->disabled( $new[0] ); | ||
| 139 | $self->refresh_look; | ||
| 140 | return $_header->disabled; | ||
| 141 | } | ||
| 142 | |||
| 143 | method is_enabled () { | ||
| 144 | return $_header->is_enabled; | ||
| 145 | } | ||
| 146 | |||
| 147 | method title (@new) { | ||
| 148 | return $title unless @new; | ||
| 149 | $title = string( $self, title => $new[0] ); | ||
| 150 | $_title_text->text($title); | ||
| 151 | return $title; | ||
| 152 | } | ||
| 153 | |||
| 154 | method icon (@new) { | ||
| 155 | return $icon unless @new; | ||
| 156 | $icon = optional( \&string, $self, icon => $new[0] ); | ||
| 157 | $_icon_text->text( $icon // '' ); | ||
| 158 | $self->refresh_look; | ||
| 159 | return $icon; | ||
| 160 | } | ||
| 161 | |||
| 162 | method focus () { | ||
| 163 | my $ui = $self->ui // die ref($self) . ": the item is not part of a Term::Fabulous, so nothing can focus it"; | ||
| 164 | $ui->interaction->set_focused_widget($_header); | ||
| 165 | return $self; | ||
| 166 | } | ||
| 167 | |||
| 168 | method is_focused () { | ||
| 169 | return $_header->is_focused; | ||
| 170 | } | ||
| 171 | |||
| 172 | method layout_properties :common () { | ||
| 173 | return ( $class->SUPER::layout_properties, title => 'scalar', icon => 'scalar', open => 'boolean', disabled => 'boolean' ); | ||
| 174 | } | ||
| 175 | |||
| 176 | # The user toggled the item: the accordion decides what happens to the | ||
| 177 | # others and fires Select; an item on its own just toggles. A header | ||
| 178 | # without a background paints no cells of its own, so a click on its | ||
| 179 | # text would not focus it by itself. | ||
| 180 | method _activated () { | ||
| 181 | my $ui = $self->ui; | ||
| 182 | $ui->interaction->set_focused_widget($_header) if defined $ui && $ui->interaction->can_take_focus($_header) && !$_header->is_focused; | ||
| 183 | my $accordion = $self->accordion; | ||
| 184 | return $accordion->_item_activated($self) if defined $accordion; | ||
| 185 | $self->open( !$_is_open ); | ||
| 186 | return; | ||
| 187 | } | ||
| 188 | |||
| 189 | # --------------------------------------------------------------------- | ||
| 190 | # Look | ||
| 191 | # --------------------------------------------------------------------- | ||
| 192 | |||
| 193 | # One setting of the look: the accordion's; without one, the default | ||
| 194 | # setting, or the default theme's accordion look. | ||
| 195 | method _look ($name) { | ||
| 196 | my $accordion = $self->accordion; | ||
| 197 | return $accordion->$name if defined $accordion; | ||
| 198 | return $DEFAULT_LOOK{$name} if exists $DEFAULT_LOOK{$name}; | ||
| 199 | require Term::Fabulous::Widget::Accordion; | ||
| 200 | my %themed = Term::Fabulous::Widget::Accordion->themed_params; | ||
| 201 | return Term::Fabulous::Theme->default->look( 'accordion', @{ $themed{$name} }[ 0, 1 ] ); # slot and state | ||
| 202 | } | ||
| 203 | |||
| 204 | # Updates the header's texts, order and colors from the state and the | ||
| 205 | # accordion's settings. Called by the accordion when they change. | ||
| 206 | method refresh_look () { | ||
| 207 | my $enabled = $_header->is_enabled; | ||
| 208 | my $at_end = $self->_look('toggle_position') eq 'end'; | ||
| 209 | |||
| 210 | $_toggle_text->text( $_is_open ? $self->_look('open_glyph') : $self->_look('closed_glyph') ); | ||
| 211 | $_toggle_text->text_color( $enabled ? ( $_is_open ? $self->_look('accent_color') : $self->_look('title_color') ) : $self->_look('disabled_color') ); | ||
| 212 | $_title_text->text_color( $self->_look('title_color') ); | ||
| 213 | $_title_text->bold( $self->_look('title_bold') ); | ||
| 214 | $_icon_text->text_color( $self->_look('title_color') ); | ||
| 215 | $_header->disabled_color( $self->_look('disabled_color') ); | ||
| 216 | |||
| 217 | my @parts = ( ( defined $icon ? $_icon_text : () ), $_title_text ); | ||
| 218 | my @order = $at_end ? ( @parts, Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow() } } ), $_toggle_text ) : ( $_toggle_text, @parts ); | ||
| 219 | $_header->clear_children; | ||
| 220 | $_header->add_child(@order); | ||
| 221 | |||
| 222 | my $background | ||
| 223 | = !$enabled ? $self->_look('header_background_color') | ||
| 224 | : $_header->is_focused ? $self->_look('focus_background_color') | ||
| 225 | : $_header->is_hovered ? $self->_look('hover_background_color') | ||
| 226 | : $self->_look('header_background_color'); | ||
| 227 | $_header->background_color($background); | ||
| 228 | |||
| 229 | my $indent = $self->_look('body_indent'); | ||
| 230 | $_body->layout( { %{ $_body->layout }, padding => { %{ $_body->layout->{padding} // {} }, left => $indent } } ); | ||
| 231 | return $self; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | 1; | ||
| 236 | |||
| 237 | __END__ |