| 1 | package Term::Fabulous::Widget::Tabs::Bar; | ||
| 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::Tabs::Button; | ||
| 12 | use Term::Fabulous::Widget::Tabs::Line; | ||
| 13 | |||
| 14 | our $VERSION = '0.05'; | ||
| 15 | |||
| 16 | class Term::Fabulous::Widget::Tabs::Bar | ||
| 17 | :isa(Term::Fabulous::Widget::Box) | ||
| 18 | :strict(params) | ||
| 19 | { | ||
| 20 | use Clay::UI::Enum::Result; | ||
| 21 | use Clay::XS qw( | ||
| 22 | sizing_grow sizing_fit CLAY_LEFT_TO_RIGHT CLAY_TOP_TO_BOTTOM | ||
| 23 | CLAY_ALIGN_X_LEFT CLAY_ALIGN_X_CENTER CLAY_ALIGN_X_RIGHT CLAY_ALIGN_Y_TOP CLAY_ALIGN_Y_CENTER CLAY_ALIGN_Y_BOTTOM | ||
| 24 | ); | ||
| 25 | use List::Util qw(first); | ||
| 26 | use Scalar::Util qw(blessed refaddr weaken); | ||
| 27 | use Term::Fabulous::Check qw(boolean describe non_negative_integer one_of); | ||
| 28 | use Term::Fabulous::Roving qw(roving_target); | ||
| 29 | use Term::Fabulous::Enum::BorderStyle; | ||
| 30 | use Term::Fabulous::Event::Select; | ||
| 31 | |||
| 32 | use constant BUTTON_CLASS => 'Term::Fabulous::Widget::Tabs::Button'; | ||
| 33 | |||
| 34 | my %DEFAULT_LOOK = Term::Fabulous::Widget::Tabs::Button->default_look; | ||
| 35 | |||
| 36 | # The geometry of each side the bar can be on: whether the tabs run | ||
| 37 | # from left to right, and the directions toward the page and away from | ||
| 38 | # it, as the arms of Term::Fabulous::Enum::BorderStyle->junction. | ||
| 39 | my %SIDE = ( | ||
| 40 | top => { horizontal => 1, toward_page => 'down', away => 'up' }, | ||
| 41 | bottom => { horizontal => 1, toward_page => 'up', away => 'down' }, | ||
| 42 | left => { horizontal => 0, toward_page => 'right', away => 'left' }, | ||
| 43 | right => { horizontal => 0, toward_page => 'left', away => 'right' }, | ||
| 44 | ); | ||
| 45 | my @ORIENTATIONS = qw(horizontal vertical); | ||
| 46 | my %ALIGN_X = ( start => CLAY_ALIGN_X_LEFT, center => CLAY_ALIGN_X_CENTER, end => CLAY_ALIGN_X_RIGHT ); | ||
| 47 | my %ALIGN_Y = ( start => CLAY_ALIGN_Y_TOP, center => CLAY_ALIGN_Y_CENTER, end => CLAY_ALIGN_Y_BOTTOM ); | ||
| 48 | |||
| 49 | field $side :param = $DEFAULT_LOOK{side}; | ||
| 50 | field $orientation :param = $DEFAULT_LOOK{orientation}; | ||
| 51 | field $tab_alignment :param = 'start'; | ||
| 52 | field $tab_gap :param = 1; | ||
| 53 | field $tab_margin :param = 1; | ||
| 54 | field $tab_padding :param = $DEFAULT_LOOK{tab_padding}; | ||
| 55 | field $active_padding :param = $DEFAULT_LOOK{active_padding}; | ||
| 56 | field $active_bold :param = $DEFAULT_LOOK{active_bold}; | ||
| 57 | field $page_border :param = 0; | ||
| 58 | |||
| 59 | # The row of tabs and the line toward the page; the active tab; the | ||
| 60 | # sizing the layout asked for, which wins over the side's. | ||
| 61 | field $_row; | ||
| 62 | field $_line; | ||
| 63 | field $_active; | ||
| 64 | field $_sizing_wish; | ||
| 65 | |||
| 66 | # The Tabs the bar belongs to, which shows the page of the active tab. | ||
| 67 | field $_owner; | ||
| 68 | |||
| 69 | ADJUST { | ||
| 70 | $side = one_of( $self, side => $side, keys %SIDE ); | ||
| 71 | $orientation = one_of( $self, orientation => $orientation, @ORIENTATIONS ); | ||
| 72 | $tab_alignment = one_of( $self, tab_alignment => $tab_alignment, keys %ALIGN_X ); | ||
| 73 | $tab_gap = non_negative_integer( $self, tab_gap => $tab_gap ); | ||
| 74 | $tab_margin = non_negative_integer( $self, tab_margin => $tab_margin ); | ||
| 75 | $tab_padding = non_negative_integer( $self, tab_padding => $tab_padding ); | ||
| 76 | $active_padding = non_negative_integer( $self, active_padding => $active_padding ); | ||
| 77 | $active_bold = boolean( $self, active_bold => $active_bold ); | ||
| 78 | $page_border = boolean( $self, page_border => $page_border ); | ||
| 79 | |||
| 80 | $_sizing_wish = $self->layout->{sizing} // {}; | ||
| 81 | $_row = Term::Fabulous::Widget::Box->new; | ||
| 82 | $_line = Term::Fabulous::Widget::Tabs::Line->new; | ||
| 83 | $self->_arrange; | ||
| 84 | |||
| 85 | $self->on( KeyPress => sub ( $self, $event ) { return $self->_handle_key($event) ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE }, owner => $self ); | ||
| 86 | } | ||
| 87 | |||
| 88 | # --------------------------------------------------------------------- | ||
| 89 | # The theme: the tabs family | ||
| 90 | # --------------------------------------------------------------------- | ||
| 91 | |||
| 92 | method theme_family :common () { | ||
| 93 | return 'tabs'; | ||
| 94 | } | ||
| 95 | |||
| 96 | # The colors and the line style come from the theme's tabs family | ||
| 97 | # unless given. | ||
| 98 | method themed_params :common () { | ||
| 99 | return ( | ||
| 100 | $class->SUPER::themed_params, | ||
| 101 | line_style => [ 'line.style', 'normal', 'grid_border_style' ], | ||
| 102 | line_color => [ 'line.color', 'normal', 'color' ], | ||
| 103 | text_color => [ 'text', 'normal', 'color' ], | ||
| 104 | active_text_color => [ 'text', 'active', 'color' ], | ||
| 105 | disabled_color => [ 'text', 'disabled', 'color' ], | ||
| 106 | hover_background_color => [ 'hover_background', 'normal', 'color' ], | ||
| 107 | focus_border_color => [ 'focus_border', 'normal', 'optional_color' ], | ||
| 108 | ); | ||
| 109 | } | ||
| 110 | |||
| 111 | # The tabs and the line read the looks; they paint again | ||
| 112 | # (Term::Fabulous::Role::Themed). | ||
| 113 | method looks_changed (@names) { | ||
| 114 | $self->_restyle; | ||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | # --------------------------------------------------------------------- | ||
| 119 | # Geometry | ||
| 120 | # --------------------------------------------------------------------- | ||
| 121 | |||
| 122 | method is_horizontal () { | ||
| 123 | return $SIDE{$side}{horizontal}; | ||
| 124 | } | ||
| 125 | |||
| 126 | method toward_page () { | ||
| 127 | return $SIDE{$side}{toward_page}; | ||
| 128 | } | ||
| 129 | |||
| 130 | method away_from_page () { | ||
| 131 | return $SIDE{$side}{away}; | ||
| 132 | } | ||
| 133 | |||
| 134 | # Lays the bar out for its side: the tabs in a row (or a column) that | ||
| 135 | # runs along the bar, aligned toward the page and along the bar as | ||
| 136 | # tab_alignment says, and the line between them and the page. | ||
| 137 | method _arrange () { | ||
| 138 | my $horizontal = $self->is_horizontal; | ||
| 139 | my ( $along, $across ) = $horizontal ? qw(width height) : qw(height width); | ||
| 140 | my %sizing = ( $along => sizing_grow(), $across => sizing_fit(), %$_sizing_wish ); | ||
| 141 | $self->SUPER::layout( { %{ $self->layout }, layout_direction => $horizontal ? CLAY_TOP_TO_BOTTOM : CLAY_LEFT_TO_RIGHT, sizing => \%sizing } ); | ||
| 142 | |||
| 143 | my %alignment | ||
| 144 | = $horizontal | ||
| 145 | ? ( x => $ALIGN_X{$tab_alignment}, y => $side eq 'top' ? CLAY_ALIGN_Y_BOTTOM : CLAY_ALIGN_Y_TOP ) | ||
| 146 | : ( y => $ALIGN_Y{$tab_alignment}, x => $side eq 'left' ? CLAY_ALIGN_X_RIGHT : CLAY_ALIGN_X_LEFT ); | ||
| 147 | my %margin = $horizontal ? ( left => $tab_margin, right => $tab_margin ) : ( top => $tab_margin, bottom => $tab_margin ); | ||
| 148 | $_row->layout( | ||
| 149 | { | ||
| 150 | layout_direction => $horizontal ? CLAY_LEFT_TO_RIGHT : CLAY_TOP_TO_BOTTOM, | ||
| 151 | child_gap => $tab_gap, | ||
| 152 | child_alignment => \%alignment, | ||
| 153 | padding => \%margin, | ||
| 154 | sizing => { $along => sizing_grow(), $across => sizing_fit() }, | ||
| 155 | } | ||
| 156 | ); | ||
| 157 | |||
| 158 | my @parts = $side eq 'top' || $side eq 'left' ? ( $_row, $_line ) : ( $_line, $_row ); | ||
| 159 | $self->_reattach(@parts) unless join( ' ', map { refaddr $_ } @parts ) eq join( ' ', map { refaddr $_ } @{ $self->children } ); | ||
| 160 | return; | ||
| 161 | } | ||
| 162 | |||
| 163 | # Puts the parts in this order; a tab that had the focus keeps it. | ||
| 164 | method _reattach (@parts) { | ||
| 165 | my $focused = first { $_->is_focused } $self->buttons; | ||
| 166 | $self->SUPER::clear_children; | ||
| 167 | $self->SUPER::add_child(@parts); | ||
| 168 | $focused->focus if defined $focused; | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | # A sizing written later wins over the side's as well. | ||
| 173 | method layout :override (@new) { | ||
| 174 | return $self->SUPER::layout unless @new; | ||
| 175 | my $layout = $self->SUPER::layout(@new); | ||
| 176 | $_sizing_wish = $layout->{sizing} // {}; | ||
| 177 | return $layout; | ||
| 178 | } | ||
| 179 | |||
| 180 | # --------------------------------------------------------------------- | ||
| 181 | # Tabs | ||
| 182 | # --------------------------------------------------------------------- | ||
| 183 | |||
| 184 | method add_child :override (@kids) { | ||
| 185 | foreach my $kid (@kids) { | ||
| 186 | die ref($self) . ": a tab bar holds only Term::Fabulous::Widget::Tabs::Button widgets, got " . ( blessed $kid ? ref $kid : describe($kid) ) | ||
| 187 | unless blessed $kid && $kid->isa(BUTTON_CLASS); | ||
| 188 | } | ||
| 189 | $_row->add_child(@kids); | ||
| 190 | my $first_enabled = first { $_->is_enabled } @kids; | ||
| 191 | $self->_activate($first_enabled) if !defined $_active && defined $first_enabled; | ||
| 192 | $self->_restyle; | ||
| 193 | return $self; | ||
| 194 | } | ||
| 195 | |||
| 196 | method remove_child :override (@kids) { | ||
| 197 | $_row->remove_child(@kids); | ||
| 198 | $self->_dropped; | ||
| 199 | return $self; | ||
| 200 | } | ||
| 201 | |||
| 202 | method remove_child_with_id :override ($target_id) { | ||
| 203 | $_row->remove_child_with_id($target_id); | ||
| 204 | $self->_dropped; | ||
| 205 | return $self; | ||
| 206 | } | ||
| 207 | |||
| 208 | method remove_children_with :override ($predicate) { | ||
| 209 | $_row->remove_children_with($predicate); | ||
| 210 | $self->_dropped; | ||
| 211 | return $self; | ||
| 212 | } | ||
| 213 | |||
| 214 | method clear_children :override () { | ||
| 215 | $_row->clear_children; | ||
| 216 | $self->_dropped; | ||
| 217 | return $self; | ||
| 218 | } | ||
| 219 | |||
| 220 | # After tabs left the bar: when the active one did, the first enabled | ||
| 221 | # tab left takes its place. | ||
| 222 | method _dropped () { | ||
| 223 | $self->_activate( first { $_->is_enabled } $self->buttons ) if defined $_active && !$_row->has_child($_active); | ||
| 224 | $self->_restyle; | ||
| 225 | return; | ||
| 226 | } | ||
| 227 | |||
| 228 | method buttons () { | ||
| 229 | return @{ $_row->children }; | ||
| 230 | } | ||
| 231 | |||
| 232 | method button ($index) { | ||
| 233 | my @buttons = $self->buttons; | ||
| 234 | die ref($self) . ": button needs an index in 0.." . $#buttons . ", got " . describe($index) unless defined $index && !ref $index && $index =~ /\A[0-9]+\z/ && $index < @buttons; | ||
| 235 | return $buttons[$index]; | ||
| 236 | } | ||
| 237 | |||
| 238 | # A tab or its index: the tab. | ||
| 239 | method _button_of ($which) { | ||
| 240 | return $self->button($which) unless blessed $which; | ||
| 241 | die ref($self) . ": not a tab of this bar" unless defined $self->index_of($which); | ||
| 242 | return $which; | ||
| 243 | } | ||
| 244 | |||
| 245 | method index_of ($button) { | ||
| 246 | my @buttons = $self->buttons; | ||
| 247 | return first { refaddr( $buttons[$_] ) == refaddr($button) } 0 .. $#buttons; | ||
| 248 | } | ||
| 249 | |||
| 250 | method active () { | ||
| 251 | return $_active; | ||
| 252 | } | ||
| 253 | |||
| 254 | method active_index () { | ||
| 255 | return defined $_active ? $self->index_of($_active) : undef; | ||
| 256 | } | ||
| 257 | |||
| 258 | # Makes a tab the active one from the program; undef makes none | ||
| 259 | # active. Fires nothing. | ||
| 260 | method select ($which) { | ||
| 261 | $self->_activate( defined $which ? $self->_button_of($which) : undef ); | ||
| 262 | return $self; | ||
| 263 | } | ||
| 264 | |||
| 265 | # Chooses a tab as the user does: fires Select when the active tab | ||
| 266 | # changes, and focuses it. A disabled tab changes nothing. | ||
| 267 | method choose ($which) { | ||
| 268 | my $button = $self->_button_of($which); | ||
| 269 | return $self unless $button->is_enabled; | ||
| 270 | $self->_chosen($button); | ||
| 271 | return $self; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub _same ( $one, $other ) { | ||
| 275 | return defined $one ? defined $other && refaddr($one) == refaddr($other) : !defined $other; | ||
| 276 | } | ||
| 277 | |||
| 278 | method _activate ($button) { | ||
| 279 | return if _same( $_active, $button ); | ||
| 280 | my $had_focus = defined $_active && $_active->is_focused; | ||
| 281 | $_active = $button; | ||
| 282 | $self->_restyle; | ||
| 283 | $self->_focus_active if $had_focus; | ||
| 284 | $_owner->_sync if defined $_owner; | ||
| 285 | return; | ||
| 286 | } | ||
| 287 | |||
| 288 | method _chosen ($button) { | ||
| 289 | my $changed = !_same( $_active, $button ); | ||
| 290 | $self->_activate($button); | ||
| 291 | $self->_focus_active; | ||
| 292 | $self->fire_event( Term::Fabulous::Event::Select->new( item => $button, index => $self->index_of($button) ) ) if $changed; | ||
| 293 | return; | ||
| 294 | } | ||
| 295 | |||
| 296 | method _focus_active () { | ||
| 297 | my $ui = $self->ui; | ||
| 298 | return unless defined $_active && defined $ui && $ui->interaction->can_take_focus($_active); | ||
| 299 | $_active->focus unless $_active->is_focused; | ||
| 300 | return; | ||
| 301 | } | ||
| 302 | |||
| 303 | # The user activated a tab (a click, Enter or Space). | ||
| 304 | method _button_activated ($button) { | ||
| 305 | $self->_chosen($button) if $button->is_enabled; | ||
| 306 | return; | ||
| 307 | } | ||
| 308 | |||
| 309 | # The Tabs that owns the bar, told when the active tab changes. | ||
| 310 | method _set_owner ($owner) { | ||
| 311 | $_owner = $owner; | ||
| 312 | weaken $_owner if defined $_owner; | ||
| 313 | return; | ||
| 314 | } | ||
| 315 | |||
| 316 | # --------------------------------------------------------------------- | ||
| 317 | # Look | ||
| 318 | # --------------------------------------------------------------------- | ||
| 319 | |||
| 320 | # Only the active tab takes the focus: Tab stops at the bar once, and | ||
| 321 | # the arrow keys move between the tabs. | ||
| 322 | method _restyle () { | ||
| 323 | foreach my $button ( $self->buttons ) { | ||
| 324 | my $is_active = _same( $_active, $button ); | ||
| 325 | $button->_set_active($is_active); | ||
| 326 | $button->can_focus($is_active); | ||
| 327 | } | ||
| 328 | $_line->mark_changed; | ||
| 329 | $self->mark_changed; | ||
| 330 | return; | ||
| 331 | } | ||
| 332 | |||
| 333 | method _set ( $field_ref, $value, $arranges = 0 ) { | ||
| 334 | $$field_ref = $value; | ||
| 335 | $self->_arrange if $arranges; | ||
| 336 | $self->_restyle; | ||
| 337 | return $$field_ref; | ||
| 338 | } | ||
| 339 | |||
| 340 | method side (@new) { return @new ? $self->_set( \$side, one_of( $self, side => $new[0], keys %SIDE ), 1 ) : $side } | ||
| 341 | method orientation (@new) { return @new ? $self->_set( \$orientation, one_of( $self, orientation => $new[0], @ORIENTATIONS ), 1 ) : $orientation } | ||
| 342 | method tab_alignment (@new) { return @new ? $self->_set( \$tab_alignment, one_of( $self, tab_alignment => $new[0], keys %ALIGN_X ), 1 ) : $tab_alignment } | ||
| 343 | method tab_gap (@new) { return @new ? $self->_set( \$tab_gap, non_negative_integer( $self, tab_gap => $new[0] ), 1 ) : $tab_gap } | ||
| 344 | method tab_margin (@new) { return @new ? $self->_set( \$tab_margin, non_negative_integer( $self, tab_margin => $new[0] ), 1 ) : $tab_margin } | ||
| 345 | method tab_padding (@new) { return @new ? $self->_set( \$tab_padding, non_negative_integer( $self, tab_padding => $new[0] ) ) : $tab_padding } | ||
| 346 | method active_padding (@new) { return @new ? $self->_set( \$active_padding, non_negative_integer( $self, active_padding => $new[0] ) ) : $active_padding } | ||
| 347 | method active_bold (@new) { return @new ? $self->_set( \$active_bold, boolean( $self, active_bold => $new[0] ) ) : $active_bold } | ||
| 348 | method page_border (@new) { return @new ? $self->_set( \$page_border, boolean( $self, page_border => $new[0] ) ) : $page_border } | ||
| 349 | |||
| 350 | method line_style (@new) { | ||
| 351 | return @new ? $self->set_look( line_style => $new[0] ) : $self->look_value('line_style'); # set_look and the theme both check for joints | ||
| 352 | } | ||
| 353 | method line_color (@new) { return @new ? $self->set_look( line_color => $new[0] ) : $self->look_value('line_color') } | ||
| 354 | method text_color (@new) { return @new ? $self->set_look( text_color => $new[0] ) : $self->look_value('text_color') } | ||
| 355 | method active_text_color (@new) { return @new ? $self->set_look( active_text_color => $new[0] ) : $self->look_value('active_text_color') } | ||
| 356 | method hover_background_color (@new) { return @new ? $self->set_look( hover_background_color => $new[0] ) : $self->look_value('hover_background_color') } | ||
| 357 | method focus_border_color (@new) { return @new ? $self->set_look( focus_border_color => $new[0] ) : $self->look_value('focus_border_color') } | ||
| 358 | method disabled_color (@new) { return @new ? $self->set_look( disabled_color => $new[0] ) : $self->look_value('disabled_color') } | ||
| 359 | |||
| 360 | method layout_properties :common () { | ||
| 361 | return ( | ||
| 362 | $class->SUPER::layout_properties, | ||
| 363 | ( map { $_ => 'scalar' } qw(side orientation tab_alignment tab_gap tab_margin tab_padding active_padding) ), | ||
| 364 | ( map { $_ => 'boolean' } qw(active_bold page_border) ), | ||
| 365 | ); | ||
| 366 | } | ||
| 367 | |||
| 368 | # --------------------------------------------------------------------- | ||
| 369 | # Keys: the arrows, Home and End choose another tab while a tab of the | ||
| 370 | # bar has the focus. | ||
| 371 | # --------------------------------------------------------------------- | ||
| 372 | |||
| 373 | method _handle_key ($event) { | ||
| 374 | my @buttons = $self->buttons; | ||
| 375 | return 0 unless grep { $_->is_focused } @buttons; | ||
| 376 | my @enabled = grep { $buttons[$_]->is_enabled } 0 .. $#buttons; | ||
| 377 | my $target = roving_target( \@enabled, $self->active_index, $event->main_key_name ) // return 0; | ||
| 378 | $self->_chosen( $buttons[$target] ); | ||
| 379 | return 1; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | 1; | ||
| 384 | |||
| 385 | __END__ |