| 1 | package Term::Fabulous::Widget::Tabs::Page; | ||
| 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 | |||
| 12 | our $VERSION = '0.05'; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::Tabs::Page | ||
| 15 | :isa(Term::Fabulous::Widget::Box) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use Clay::XS qw(sizing_grow padding_all CLAY_TOP_TO_BOTTOM); | ||
| 19 | use Scalar::Util qw(refaddr weaken); | ||
| 20 | use Term::Fabulous::Check qw(boolean optional string); | ||
| 21 | use Term::Fabulous::Enum::BorderStyle; | ||
| 22 | |||
| 23 | field $title :param = ''; | ||
| 24 | field $icon :param = undef; | ||
| 25 | field $disabled :param = 0; | ||
| 26 | |||
| 27 | # Whether the page asks to be the active one when it joins a Tabs. | ||
| 28 | field $_wants_active = 0; | ||
| 29 | |||
| 30 | # The Tabs the page belongs to, also while it is not shown and so has | ||
| 31 | # no parent. Set by the Tabs. | ||
| 32 | field $_tabs; | ||
| 33 | |||
| 34 | ADJUST :params ( :$active = 0 ) { | ||
| 35 | $title = string( $self, title => $title ); | ||
| 36 | $icon = optional( \&string, $self, icon => $icon ); | ||
| 37 | $disabled = boolean( $self, disabled => $disabled ); | ||
| 38 | $_wants_active = boolean( $self, active => $active ); | ||
| 39 | |||
| 40 | my $layout = $self->layout; | ||
| 41 | $self->layout( | ||
| 42 | { | ||
| 43 | layout_direction => CLAY_TOP_TO_BOTTOM, | ||
| 44 | padding => padding_all(1), | ||
| 45 | %$layout, | ||
| 46 | sizing => { width => sizing_grow(), height => sizing_grow(), %{ $layout->{sizing} // {} } }, | ||
| 47 | } | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | method tabs () { | ||
| 52 | return $_tabs; | ||
| 53 | } | ||
| 54 | |||
| 55 | # Called by the Tabs when the page joins it or leaves it. | ||
| 56 | method _set_tabs ($tabs) { | ||
| 57 | $_tabs = $tabs; | ||
| 58 | weaken $_tabs if defined $_tabs; | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | # Whether the page asked to start active; read by the Tabs when the | ||
| 63 | # page joins it. | ||
| 64 | method _wants_active () { | ||
| 65 | return $_wants_active; | ||
| 66 | } | ||
| 67 | |||
| 68 | method _notify_tabs () { | ||
| 69 | $_tabs->_page_changed($self) if defined $_tabs; | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | method title (@new) { | ||
| 74 | return $title unless @new; | ||
| 75 | $title = string( $self, title => $new[0] ); | ||
| 76 | $self->_notify_tabs; | ||
| 77 | return $title; | ||
| 78 | } | ||
| 79 | |||
| 80 | method icon (@new) { | ||
| 81 | return $icon unless @new; | ||
| 82 | $icon = optional( \&string, $self, icon => $new[0] ); | ||
| 83 | $self->_notify_tabs; | ||
| 84 | return $icon; | ||
| 85 | } | ||
| 86 | |||
| 87 | method disabled (@new) { | ||
| 88 | return $disabled unless @new; | ||
| 89 | $disabled = boolean( $self, disabled => $new[0] ); | ||
| 90 | $self->_notify_tabs; | ||
| 91 | return $disabled; | ||
| 92 | } | ||
| 93 | |||
| 94 | method is_enabled () { | ||
| 95 | return $disabled ? 0 : 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | method is_active () { | ||
| 99 | return 0 unless defined $_tabs; | ||
| 100 | my $active = $_tabs->active; | ||
| 101 | return defined $active && refaddr($active) == refaddr($self) ? 1 : 0; | ||
| 102 | } | ||
| 103 | |||
| 104 | # In a Tabs, the page is active when it is the shown one, and writing | ||
| 105 | # selects it (or, with a false value, no page). On its own, the page | ||
| 106 | # remembers the wish for the Tabs it joins. | ||
| 107 | method active (@new) { | ||
| 108 | unless (@new) { | ||
| 109 | return defined $_tabs ? $self->is_active : $_wants_active; | ||
| 110 | } | ||
| 111 | my $wanted = boolean( $self, active => $new[0] ); | ||
| 112 | if ( defined $_tabs ) { | ||
| 113 | $_tabs->select( $wanted ? $self : undef ) if $wanted != $self->is_active; | ||
| 114 | return $self->is_active; | ||
| 115 | } | ||
| 116 | $_wants_active = $wanted; | ||
| 117 | return $_wants_active; | ||
| 118 | } | ||
| 119 | |||
| 120 | method layout_properties :common () { | ||
| 121 | return ( $class->SUPER::layout_properties, title => 'scalar', icon => 'scalar', disabled => 'boolean', active => 'boolean' ); | ||
| 122 | } | ||
| 123 | |||
| 124 | # --------------------------------------------------------------------- | ||
| 125 | # The border: in a Tabs with page_border, the line's style on the | ||
| 126 | # three sides away from the bar and none on the bar's side, in the | ||
| 127 | # line's color, read when the frame is built; a style or color the | ||
| 128 | # page was given wins. | ||
| 129 | # --------------------------------------------------------------------- | ||
| 130 | |||
| 131 | method _bar_with_page_border () { | ||
| 132 | my $bar = defined $_tabs ? $_tabs->bar : return undef; | ||
| 133 | return $bar->page_border ? $bar : undef; | ||
| 134 | } | ||
| 135 | |||
| 136 | # The style of a side without one of its own | ||
| 137 | # (Term::Fabulous::Role::HasBorderStyle). | ||
| 138 | method derived_border_style ($side) { | ||
| 139 | my $bar = $self->_bar_with_page_border // return undef; | ||
| 140 | return $side eq $bar->side ? Term::Fabulous::Enum::BorderStyle->Hidden : $bar->line_style; | ||
| 141 | } | ||
| 142 | |||
| 143 | method contribute_look_theme :override ($config) { | ||
| 144 | $self->SUPER::contribute_look_theme($config); | ||
| 145 | my $border = $config->{border} // return; | ||
| 146 | return if $self->has_look_override('border_color'); | ||
| 147 | my $bar = $self->_bar_with_page_border // return; | ||
| 148 | $config->{border} = { %$border, color => $bar->line_color }; | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | 1; | ||
| 154 | |||
| 155 | __END__ |