| 1 | # You may distribute under the terms of the GNU General Public License | ||
| 2 | # | ||
| 3 | # (C) Paul Evans, 2012-2023 -- leonerd@leonerd.org.uk | ||
| 4 | |||
| 5 | package Circle::FE::Term::Tab 0.240250; | ||
| 6 | |||
| 7 | 1 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 0 | ▶ | use Tickit::Widget::Tabbed; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use base qw( Tickit::Widget::Tabbed::Tab ); | ||
| 12 | Tickit::Widget::Tabbed->VERSION( '0.019' ); | ||
| 13 | |||
| 14 | use Tickit::Term 0.27; # setctl_str | ||
| 15 | |||
| 16 | use Circle::FE::Term; | ||
| 17 | |||
| 18 | use Module::Pluggable search_path => "Circle::FE::Term::Widget", | ||
| 19 | sub_name => "widgets", | ||
| 20 | inner => 0; | ||
| 21 | |||
| 22 | # Load each widget class here to ensure compiletime failures are visible early | ||
| 23 | require "$_.pm" =~ s(::)(/)gr for widgets; | ||
| 24 | |||
| 25 | use Tickit::Widget::Static; | ||
| 26 | |||
| 27 | sub new | ||
| 28 | { | ||
| 29 | my $class = shift; | ||
| 30 | my ( $tabbed, %args ) = @_; | ||
| 31 | |||
| 32 | my $object = delete $args{object}; | ||
| 33 | my $self; | ||
| 34 | |||
| 35 | my $watch_tag; | ||
| 36 | if( $object->proxy_isa( "Circle.RootObj" ) ) { | ||
| 37 | $args{label} = "Global"; | ||
| 38 | } | ||
| 39 | else { | ||
| 40 | $args{label} = $object->prop( "tag" ); | ||
| 41 | $watch_tag++; | ||
| 42 | } | ||
| 43 | |||
| 44 | $self = $class->SUPER::new( $tabbed, %args ); | ||
| 45 | $self->{object} = $object; | ||
| 46 | $self->{term} = $tabbed->window->term; | ||
| 47 | |||
| 48 | $self->adopt_future( | ||
| 49 | $object->call_method( | ||
| 50 | get_widget => (), | ||
| 51 | )->on_done( sub { | ||
| 52 | $self->widget->add( $self->build_widget( $_[0] ), expand => 1 ); | ||
| 53 | }) | ||
| 54 | ); | ||
| 55 | |||
| 56 | $self->adopt_future( | ||
| 57 | $object->watch_property_with_initial( | ||
| 58 | "level", | ||
| 59 | on_set => sub { | ||
| 60 | my ( $level ) = @_; | ||
| 61 | $self->set_level( $level ); | ||
| 62 | }, | ||
| 63 | ) | ||
| 64 | ); | ||
| 65 | |||
| 66 | $self->adopt_future( | ||
| 67 | $object->subscribe_event( | ||
| 68 | "raise", | ||
| 69 | on_fire => sub { | ||
| 70 | $self->activate; | ||
| 71 | }, | ||
| 72 | ) | ||
| 73 | ); | ||
| 74 | |||
| 75 | if( $watch_tag ) { | ||
| 76 | $self->adopt_future( | ||
| 77 | $object->watch_property( | ||
| 78 | "tag", | ||
| 79 | on_set => sub { | ||
| 80 | my ( $newtag ) = @_; | ||
| 81 | $self->set_label( $newtag ); | ||
| 82 | }, | ||
| 83 | ) | ||
| 84 | ); | ||
| 85 | } | ||
| 86 | |||
| 87 | $self->set_on_activated( 'activated' ); | ||
| 88 | |||
| 89 | return $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub adopt_future | ||
| 93 | { | ||
| 94 | my $self = shift; | ||
| 95 | my ( $f ) = @_; | ||
| 96 | |||
| 97 | my $futures = $self->{futures} //= {}; | ||
| 98 | |||
| 99 | $futures->{"$f"} = $f; | ||
| 100 | |||
| 101 | $f->on_ready( sub { | ||
| 102 | delete $futures->{shift()}; | ||
| 103 | }); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub build_widget | ||
| 107 | { | ||
| 108 | my $self = shift; | ||
| 109 | my ( $obj ) = @_; | ||
| 110 | |||
| 111 | foreach my $type ( widgets ) { | ||
| 112 | next unless $obj->proxy_isa( "Circle.Widget." . $type->type ); | ||
| 113 | return $type->build( $obj, $self ); | ||
| 114 | } | ||
| 115 | |||
| 116 | die "Cannot build widget for $obj as I don't recognise its type - " . join( ", ", map { $_->name } $obj->proxy_isa ) . "\n"; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub level | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | return $self->{object}->prop( "level" ); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub set_level | ||
| 126 | { | ||
| 127 | my $self = shift; | ||
| 128 | my ( $level ) = @_; | ||
| 129 | |||
| 130 | $self->set_pen( Circle::FE::Term->get_theme_pen( "level$level" ) ); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub label_short | ||
| 134 | { | ||
| 135 | my $self = shift; | ||
| 136 | my $label = $self->label; | ||
| 137 | $label =~ s/([a-z0-9])([a-z0-9]+)/$1/gi; | ||
| 138 | return $label; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub activated | ||
| 142 | { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | my $object = $self->{object}; | ||
| 146 | |||
| 147 | if( $object->prop("level") > 0 ) { | ||
| 148 | $self->adopt_future( | ||
| 149 | $object->call_method( | ||
| 150 | reset_level => (), | ||
| 151 | ) | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | |||
| 155 | my $tag = $object->prop("tag") // "Global"; | ||
| 156 | my $title = sprintf "%s - %s", $tag, "Circle"; | ||
| 157 | |||
| 158 | $self->{term}->setctl_str( icontitle_text => $title ); | ||
| 159 | } | ||
| 160 | |||
| 161 | 0x55AA; | ||