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 396 397 398 399 400 401 402 403 404 405 406 407 408
|
# 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, 2014-2022 -- leonerd@leonerd.org.uk
use v5.26; # signatures
use Object::Pad 0.73 ':experimental(adjust_params init_expr)';
package Tickit::Widget::FloatBox 0.11;
class Tickit::Widget::FloatBox
:strict(params)
:isa(Tickit::ContainerWidget);
use Carp;
# We don't actually have a pen, but then we don't actually have any style
# either. This keeps deprecation warnings happy
use constant WIDGET_PEN_FROM_STYLE => 1;
=head1 NAME
C<Tickit::Widget::FloatBox> - manage a collection of floating widgets
=head1 SYNOPSIS
TODO
=head1 DESCRIPTION
This container widget maintains a collection of floating widgets that can be
displayed over the top of a single base widget. The box itself is entirely
occupied by the base widget, and by default when no floats are created or
displayed it will behave essentially invisibly, as though the box were not
there and the base widget was an immediate child of the container the floatbox
is inside.
=cut
=head1 CONSTRUCTOR
=cut
=head2 new
$floatbox = Tickit::Widget::FloatBox->new( %args )
Constructs a new C<Tickit::Widget::FloatBox> object.
Takes the following named arguments in addition to those taken by the base
L<Tickit::ContainerWidget> constructor.
=over 8
=item base_child => Tickit::Widget
The main L<Tickit::Widget> instance to use as the base.
This argument is now discouraged as it complicates the construction of
subclasses; see instead the L</set_base_child> method used as a chaining
mutator.
=back
=cut
field $_base_child;
field @_floats;
=head1 ACCESSORS
=cut
method children ()
{
my @children;
push @children, $self->base_child if $self->base_child;
push @children, $_->child for @_floats;
return @children;
}
method lines ()
{
return $self->base_child ? $self->base_child->requested_lines : 1;
}
method cols ()
{
return $self->base_child ? $self->base_child->requested_cols : 1;
}
=head2 base_child
=head2 set_base_child
$base_child = $floatbox->base_child
$floatbox->set_base_child( $base_child )
Returns or sets the base widget to use.
The mutator method returns the container widget instance itself making it
suitable to use as a chaining mutator; e.g.
my $container = Tickit::Widget::FloatBox->new( ... )
->set_base_child( Tickit::Widget::Box->new ... );
=cut
method base_child () { $_base_child }
method set_base_child ( $new )
{
if( $_base_child ) {
$self->remove( $_base_child );
}
$_base_child = $new;
$self->add( $new );
if( my $win = $self->window ) {
$new->set_window( $win->make_sub( 0, 0, $win->lines, $win->cols ) );
}
return $self;
}
method reshape ()
{
return unless my $win = $self->window;
if( $_base_child ) {
if( $_base_child->window ) {
$_base_child->window->resize( $win->lines, $win->cols );
}
else {
$_base_child->set_window( $win->make_sub( 0, 0, $win->lines, $win->cols ) );
}
}
$self->_reshape_float( $_, $win ) for @_floats;
$self->redraw;
}
method _reshape_float ( $float, $win )
{
my $child = $float->child;
my @geom = $float->_get_geom( $win->lines, $win->cols );
if( my $childwin = $child->window ) {
$childwin->expose;
$childwin->change_geometry( @geom );
$childwin->expose;
}
else {
# TODO: Ordering?
# TODO: I want a ->make_hidden_float
$child->set_window( $win->make_float( @geom ) );
$child->window->hide if !$float->is_visible;
}
}
method render_to_rb ( $rb, $rect )
{
return if $self->base_child;
$rb->eraserect( $rect );
}
=head2 add_float
$float = $floatbox->add_float( %args )
Adds a widget as a floating child and returns a new C<Float> object. Takes the
following arguments:
=over 8
=item child => Tickit::Widget
The child widget
=item top, bottom, left, right => INT
The initial geometry of the floating area. These follow the same behaviour as
the C<move> method on the Float object.
=item hidden => BOOL
Optional. If true, the float starts off hidden initally, and must be shown by
the C<show> method before it becomes visible.
=back
=cut
method add_float ( %args )
{
my $float = Tickit::Widget::FloatBox::Float->new(
floatbox => $self, %args
);
push @_floats, $float;
$self->add( $float->child );
if( my $win = $self->window ) {
$self->_reshape_float( $float, $win );
}
return $float;
}
method _remove_float ( $float )
{
my $idx;
$_floats[$_] == $float and $idx = $_, last for 0 .. $#_floats;
defined $idx or croak "Cannot remove float - not a member of the FloatBox";
splice @_floats, $idx, 1, ();
$self->remove( $float->child );
}
=head1 FLOATS
The following objects represent a floating region as returned by the
C<add_float> method.
=cut
class # hide
Tickit::Widget::FloatBox::Float
{
use Carp;
field $_fb :param(floatbox);
field $_child :param;
field $_hidden :param = 0;
ADJUST :params ( %params )
{
$self->move( %params{qw( top bottom left right )} );
delete @params{qw( top bottom left right )};
}
=head2 child
$child = $float->child
Returns the child widget in the region.
=cut
method child () { $_child }
=head2 move
$float->move( %args )
Redefines the area geometry of the region. Takes arguments named C<top>,
C<bottom>, C<left> and C<right>, each of which should either be a numeric
value, or C<undef>.
The region must have at least one of C<top> or C<bottom> and at least one of
C<left> or C<right> defined, which will then fix the position of one corner of
the region. If the size is not otherwise determined by the geometry, it will
use the preferred size of the child widget. Any geometry argument may be
negative to count backwards from the limits of the parent.
For example,
# top-left corner
$float->move( top => 0, left => 0 )
# top-right corner
$float->move( top => 0, right => -1 )
# bottom 3 lines, flush left
$float->move( left => 0, top => -3, bottom => -1 )
Any arguments not passed will be left unchanged; to specifically clear the
current value pass a value of C<undef>.
=cut
field $_top;
field $_bottom;
field $_left;
field $_right;
method move ( %args )
{
exists $args{top} and $_top = $args{top};
exists $args{bottom} and $_bottom = $args{bottom};
exists $args{left} and $_left = $args{left};
exists $args{right} and $_right = $args{right};
defined $_top or defined $_bottom or
croak "A Float needs at least one of 'top' or 'bottom'";
defined $_left or defined $_right or
croak "A Float needs at least one of 'left' or 'right'";
if( my $win = $_fb->window ) {
$_fb->_reshape_float( $self, $win );
}
}
method _get_geom ( $lines, $cols )
{
my $clines = $self->child->requested_lines;
my $ccols = $self->child->requested_cols;
my ( $top, $bottom ) = _alloc_dimension( $_top, $_bottom, $lines, $clines );
my ( $left, $right ) = _alloc_dimension( $_left, $_right, $cols, $ccols );
return ( $top, $left, $bottom-$top, $right-$left );
}
sub _alloc_dimension ( $start, $end, $parentsz, $childsz )
{
# Need to off-by-one to allow -1 == right, etc..
defined and $_ < 0 and $_ += $parentsz+1 for $start, $end;
$end = $start + $childsz if !defined $end;
$start = $end - $childsz if !defined $start;
return ( $start, $end );
}
=head2 remove
$float->remove
Removes the float from the FloatBox.
=cut
method remove ()
{
$_fb->_remove_float( $self );
}
=head2 hide
$float->hide
Hide the float by hiding the window of its child widget.
=cut
method hide ()
{
$_hidden = 1;
$_child->window->hide if $_child->window;
}
=head2 show
$float->show
Show the float by showing the window of its child widget. Undoes the effect
of C<hide>.
=cut
method show ()
{
$_hidden = 0;
$_child->window->show if $_child->window;
}
=head2 is_visible
$visible = $float->is_visible
Return true if the float is currently visible.
=cut
method is_visible ()
{
return !$_hidden;
}
}
=head1 TODO
=over 4
=item *
Support adjusting stacking order of floats.
=back
=cut
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA
|