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