File: VSplit.pm

package info (click to toggle)
libtickit-widgets-perl 0.42-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 568 kB
  • sloc: perl: 5,636; makefile: 2
file content (206 lines) | stat: -rw-r--r-- 4,311 bytes parent folder | download
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
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2013-2023 -- leonerd@leonerd.org.uk

use v5.20;
use warnings;
use Object::Pad 0.807 ':experimental(inherit_field)';

package Tickit::Widget::VSplit 0.42;
class Tickit::Widget::VSplit :strict(params);

inherit Tickit::Widget::LinearSplit qw( $_split_at $_split_len );

use Tickit::Style;
use Tickit::RenderBuffer qw( LINE_SINGLE CAP_BOTH );

use Carp;

use List::Util qw( sum max );

=head1 NAME

C<Tickit::Widget::VSplit> - an adjustable vertical split between two widgets

=head1 SYNOPSIS

   use Tickit;
   use Tickit::Widget::VSplit;
   use Tickit::Widget::Static;

   my $vsplit = Tickit::Widget::VSplit->new
      ->set_left_child ( Tickit::Widget::Static->new( text => "Text above" ) ),
      ->set_right_child( Tickit::Widget::Static->new( text => "Text below" ) );

   Tickit->new( root => $vsplit )->run;

=head1 DESCRIPTION

This container widget holds two child widgets, displayed side by side. The two
widgets are displayed with a vertical split bar between them, which reacts to
mouse click-drag events, allowing the user to adjust the proportion of space
given to the two widgets.

=head1 STYLE

The default style pen is used as the widget pen. The following style pen
prefixes are also used:

=over 4

=item split => PEN

The pen used to render the vertical split area

=back

The following style keys are used:

=over 4

=item spacing => INT

The number of columns of spacing between the left and right child widgets

=back

The following style tags are used:

=over 4

=item :active

Set when a mouse drag resize operation is occurring

=back

=cut

style_definition base =>
   split_fg => "white",
   split_bg => "blue",
   spacing => 1;

style_definition ':active' =>
   split_fg => "hi-white",
   split_b => 1;

style_reshape_keys qw( spacing );

use constant WIDGET_PEN_FROM_STYLE => 1;

use constant VALUE_METHOD => "cols";

=head1 CONSTRUCTOR

=head2 new

   $vsplit = Tickit::Widget::VSplit->new( %args );

Constructs a new C<Tickit::Widget::VSplit> object.

=cut

method lines
{
   return max(
      $self->left_child  ? $self->left_child->requested_lines  : 1,
      $self->right_child ? $self->right_child->requested_lines : 1,
   );
}

method cols
{
   my $spacing = $self->get_style_values( "spacing" );
   return sum(
      $self->left_child  ? $self->left_child->requested_cols  : 1,
      $spacing,
      $self->right_child ? $self->right_child->requested_cols : 1,
   );
}

=head1 ACCESSORS

=cut

=head2 left_child

=head2 set_left_child

   $child = $hsplit->left_child;

   $vsplit->set_left_child( $child );

Accessor for the child widget used in the left half of the display.

=cut

*left_child     = __PACKAGE__->can( "A_child" );
*set_left_child = __PACKAGE__->can( "set_A_child" );

=head2 right_child

=head2 set_right_child

   $child = $hsplit->right_child;

   $vsplit->set_right_child( $child );

Accessor for the child widget used in the right half of the display.

These mutators returning the container widget itself making them suitable to
use as chaining mutators; e.g.

   my $container = Tickit::Widget::VSplit->new( ... )
      ->set_left_child ( Tickit::Widget::Box->new ... )
      ->set_right_child( Tickit::Widget::Box->new ... );

=cut

*right_child     = __PACKAGE__->can( "B_child" );
*set_right_child = __PACKAGE__->can( "set_B_child" );

method _make_child_geom
{
   my ( $start, $len ) = @_;
   return ( 0, $start, $self->window->lines, $len );
}

method render_to_rb
{
   my ( $rb, $rect ) = @_;

   my $lines = $self->window->lines;

   $rb->setpen( $self->get_style_pen( "split" ) );

   $rb->vline_at( 0, $lines-1, $_split_at, LINE_SINGLE, undef, CAP_BOTH );

   if( $_split_len > 2 ) {
      foreach my $line ( $rect->linerange ) {
         $rb->erase_at( $line, $_split_at + 1, $_split_len - 2 );
      }
   }
   if( $_split_len > 1 ) {
      $rb->vline_at( 0, $lines-1, $_split_at + $_split_len - 1, LINE_SINGLE, undef, CAP_BOTH );
   }
}

method on_mouse
{
   my ( $args ) = @_;

   if( $args->type ne "wheel" and $args->button == 1 ) {
      return $self->_on_mouse( $args->type, $args->col );
   }
   return;
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;