File: Tabbed.pm

package info (click to toggle)
libcircle-be-perl 0.173320-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 388 kB
  • sloc: perl: 6,042; makefile: 2; sh: 1
file content (395 lines) | stat: -rw-r--r-- 9,094 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#  You may distribute under the terms of the GNU General Public License
#
#  (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk

package Circle::Session::Tabbed;

use strict;
use base qw( Tangence::Object Circle::Commandable Circle::Configurable );
use Carp;

our $VERSION = '0.173320';

sub _session_type
{
   my ( $opts ) = @_;

   keys %$opts or return __PACKAGE__;

   print STDERR "Need Tabbed FE session for extra options:\n";
   print STDERR "  ".join( "|", sort keys %$opts )."\n";

   return undef;
}

sub new
{
   my $class = shift;
   my %args = @_;

   my $self = $class->SUPER::new( %args );

   $self->{root} = $args{root};
   $self->{identity} = $args{identity};

   # Start with just the root object in first tab

   $self->set_prop_tabs( [ $args{root} ] );
   $self->{items} = {};

   return $self;
}

sub items
{
   my $self = shift;
   return @{ $self->get_prop_tabs };
}

sub describe
{
   my $self = shift;
   return __PACKAGE__."[$self->{identity}]";
}

sub _item_to_index
{
   my $self = shift;
   my ( $item ) = @_;

   my @items = $self->items;
   $items[$_] == $item and return $_ for 0 .. $#items;

   return undef;
}

sub show_item
{
   my $self = shift;
   my ( $item ) = @_;

   return if grep { $_ == $item } $self->items;

   $self->push_prop_tabs( $item );
}

sub unshow_item
{
   my $self = shift;
   my ( $item ) = @_;

   my $index;
   if( ref $item ) {
      $index = $self->_item_to_index( $item );
      return unless defined $index;
   }
   else {
      $index = $item;
   }

   $self->splice_prop_tabs( $index, 1, () );
}

sub new_item
{
   my $self = shift;
   my ( $item ) = @_;

   # Did we know about it?
   return if exists $self->{items}->{$item};

   $self->{items}->{$item} = 1;
   $self->show_item( $item );
}

sub delete_item
{
   my $self = shift;
   my ( $item ) = @_;

   delete $self->{items}->{$item};
   $self->unshow_item( $item );
}

sub clonefrom
{
   my $self = shift;
   my ( $src ) = @_;

   my @srcitems = $src->items;

   foreach my $index ( 0 .. $#srcitems ) {
      my $item = $srcitems[$index];

      my $curindex = $self->_item_to_index( $item );
      if( !defined $curindex ) {
         $self->splice_prop_tabs( $index, 0, $item );
      }
      elsif( $curindex != $index ) {
         $self->move_prop_tabs( $curindex, $index - $curindex );
      }
   }

   $self->splice_prop_tabs( scalar @srcitems, scalar $self->items - scalar @srcitems, () );
}

sub _get_item
{
   my $self = shift;
   my ( $path, $curitem, $create ) = @_;

   $curitem or $path =~ m{^/} or croak "Cannot walk a relative path without a start item";

   $curitem = $self->{root} if $path =~ s{^/}{};

   foreach ( split( m{/}, $path ) ) {
      next unless length $_; # skip empty path elements

      my $nextitem;
      if( $curitem->can( "get_item" ) ) {
         $nextitem = $curitem->get_item( $_, $create );
      }
      elsif( $curitem->can( "enumerate_items" ) ) {
         $nextitem = $curitem->enumerate_items->{$_};
      }
      else {
         die "@{[ $curitem->describe ]} has no child items\n";
      }

      defined $nextitem or die "@{[ $curitem->describe ]} has no child item called $_\n";

      $curitem = $nextitem;
   }

   return $curitem;
}

sub _cat_path
{
   my ( $p, $q ) = @_;

   return $q if $p eq "";
   return "/$q" if $p eq "/";
   return "$p/$q";
}

sub load_configuration
{
   my $self = shift;
   my ( $ynode ) = @_;

   $self->set_prop_tabs( [] );

   foreach my $tab ( @{ $ynode->{tabs} } ) {
      my $item = $self->_get_item( $tab, $self->{root}, 1 );
      $self->push_prop_tabs( $item );
   }
}

sub store_configuration
{
   my $self = shift;
   my ( $ynode ) = @_;

   $ynode->{tabs} = [ map {
      my $item = $_;
      my @components;
      while( $item ) {
         unshift @components, $item->enumerable_name;
         $item = $item->parent;
      }
      join "/", @components;
   } $self->items ];
}

sub command_list
   : Command_description("List showable window items")
   : Command_arg('path?')
   : Command_opt('all=+', desc => "list all the items")
{
   my $self = shift;
   my ( $itempath, $opts, $cinv ) = @_;

   my @items;
   
   if( $opts->{all} ) {
      @items = ( [ "/" => $self->{root} ] );
   }
   else {
      @items = ( [ "" => $cinv->invocant ] );
   }

   if( defined $itempath ) {
      if( $itempath =~ m{^/} ) {
         $items[0]->[0] = $itempath;
      }
      else {
         $items[0]->[0] .= $itempath;
      }
      $items[0]->[1] = $self->_get_item( $itempath, $items[0]->[1] );
   }

   $cinv->respond( "The following items exist" . ( defined $itempath ? " from path $itempath" : "" ) );

   # Walk a tree without using a recursive function
   my @table;
   while( my $i = pop @items ) {
      my ( $name, $item ) = @$i;

      push @table, [ "  $name", ref($item) ] if length $name;

      if( my $subitems = $item->can( "enumerate_items" ) && $item->enumerate_items ) {
         push @items, [ _cat_path( $name, $_ ) => $subitems->{$_} ] for reverse sort keys %$subitems;
      }
   }

   $cinv->respond_table( \@table, colsep => " - " );

   return;
}

sub command_show
   : Command_description("Show a window item")
   : Command_arg("path?")
   : Command_opt("all=+", desc => "show all the non-visible items")
{
   my $self = shift;
   my ( $itempath, $opts, $cinv ) = @_;

   my @items;
   if( $opts->{all} ) {
      my %visible = map { $_ => 1 } $self->items;

      my @more = ( $self->{root} );
      while( my $item = pop @more ) {
         push @items, $item if !$visible{$item};

         if( my $subitems = $item->can( "enumerate_items" ) && $item->enumerate_items ) {
            push @more, @{$subitems}{sort keys %$subitems};
         }
      }
   }
   elsif( defined $itempath ) {
      @items = $self->_get_item( $itempath, $cinv->invocant );
   }
   else {
      $cinv->responderr( "show: require PATH or -all" );
      return;
   }

   $self->show_item( $_ ) for @items;

   return;
}

sub command_hide
   : Command_description("Hide a window item")
{
   my $self = shift;
   my ( $cinv ) = @_;

   my $item = $cinv->invocant;

   if( $item->isa( "Circle::RootObj" ) ) {
      $cinv->responderr( "Cannot hide the global tab" );
      return;
   }

   $self->unshow_item( $item );

   return;
}

sub command_tab
   : Command_description("Manipulate window item tabs")
{
}

sub command_tab_move
   : Command_description("Move the tab to elsewhere in the window ordering\n".
                         "POSITION may be an absolute number starting from 1,\n".
                         "                a relative number with a leading + or -,\n".
                         "                one of  first | left | right | last")
   : Command_subof('tab')
   : Command_arg('position')
{
   my $self = shift;
   my ( $position, $cinv ) = @_;

   my $tabs = $self->get_prop_tabs;

   my $item = $cinv->invocant;

   my $index;
   $tabs->[$_] eq $item and ( $index = $_, last ) for 0 .. $#$tabs;

   defined $index or return $cinv->responderr( "Cannot find current index of item" );

   $position = "+1"   if $position eq "right";
   $position = "-1"   if $position eq "left";
   $position = "1"    if $position eq "first"; # 1-based
   $position = @$tabs if $position eq "last";  # 1-based

   my $delta;
   if( $position =~ m/^[+-]/ ) {
      # relative
      $delta = $position+0;
   }
   elsif( $position =~ m/^\d+$/ ) {
      # absolute; but input from user was 1-based.
      $position -= 1;
      $delta = $position - $index;
   }
   else {
      return $cinv->responderr( "Unrecognised position/movement specification: $position" );
   }

   return $cinv->responderr( "Cannot move that far left"  ) if $index + $delta < 0;
   return $cinv->responderr( "Cannot move that far right" ) if $index + $delta > $#$tabs;

   $self->move_prop_tabs( $index, $delta );
   return;
}

sub command_tab_goto
   : Command_description("Activate a numbered tab\n".
                         "POSITION may be an absolute number starting from 1,\n".
                         "                a relative number with a leading + or -,\n".
                         "                one of  first | left | right | last")
   : Command_subof('tab')
   : Command_arg('position')
{
   my $self = shift;
   my ( $position, $cinv ) = @_;

   my $tabs = $self->get_prop_tabs;

   my $item = $cinv->invocant;

   my $index;
   $tabs->[$_] eq $item and ( $index = $_, last ) for 0 .. $#$tabs;

   defined $index or return $cinv->responderr( "Cannot find current index of item" );

   $position = "+1"   if $position eq "right";
   $position = "-1"   if $position eq "left";
   $position = "1"    if $position eq "first"; # 1-based
   $position = @$tabs if $position eq "last";  # 1-based

   if( $position =~ m/^[+-]/ ) {
      # relative
      $index += $position;
   }
   elsif( $position =~ m/^\d+$/ ) {
      # absolute; but input from user was 1-based.
      $index = $position - 1;
   }
   else {
      return $cinv->responderr( "Unrecognised position/movement specification: $position" );
   }

   $self->get_prop_tabs->[$index]->fire_event( raise => () );
   return;
}

0x55AA;