Skip Menu |

This queue is for tickets about the Tickit-Widgets CPAN distribution.

Report information
The Basics
Id: 88953
Status: resolved
Priority: 0/
Queue: Tickit-Widgets

People
Owner: Nobody in particular
Requestors: TEAM [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.12
Fixed in: 0.13



Subject: Support for buttons without borders
Where space is at a premium it'd be useful to have a plain text button without the borders. I've used 'border: none;' as the style key for Tickit::Widget::Layout::Relative, for example. cheers, Tom
On Wed Sep 25 02:43:27 2013, TEAM wrote: Show quoted text
> Where space is at a premium it'd be useful to have a plain text button > without the borders. I've used 'border: none;' as the style key for > Tickit::Widget::Layout::Relative, for example.
T:W:Frame uses a style key called "linetype"; perhaps we should reuse that? -- Paul Evans
Attached is a patch to implement it. -- Paul Evans
Subject: rt88953.patch
=== modified file 'examples/demo-button.pl' --- examples/demo-button.pl 2013-06-07 23:36:36 +0000 +++ examples/demo-button.pl 2013-09-28 13:26:42 +0000 @@ -4,7 +4,7 @@ use warnings; use Tickit; -use Tickit::Widgets qw( Border Button VBox ); +use Tickit::Widgets qw( Border Button VBox RadioButton ); Tickit::Style->load_style( <<'EOF' ); Button { @@ -19,22 +19,39 @@ child => my $vbox = Tickit::Widget::VBox->new( spacing => 2, bg => "black" ), ); +my @buttons; foreach my $colour (qw( red blue green yellow )) { $vbox->add( - Tickit::Widget::Button->new( + my $button = Tickit::Widget::Button->new( label => $colour, on_click => sub { $border->pen->chattr( bg => $colour ) }, ) ); + push @buttons, $button; } my $tickit = Tickit->new( root => $border ); $vbox->add( - Tickit::Widget::Button->new( + my $button = Tickit::Widget::Button->new( label => "Quit", on_click => sub { $tickit->stop }, ) ); +push @buttons, $button; + +{ + my $group = Tickit::Widget::RadioButton::Group->new; + $group->set_on_changed( sub { + my ( undef, $type ) = @_; + $_->set_style( linetype => $type ) for @buttons; + }); + + $vbox->add( Tickit::Widget::RadioButton->new( + label => $_, + value => $_, + group => $group, + ) ) for qw( none single double thick ); +} $tickit->run; === modified file 'lib/Tickit/Widget/Button.pm' --- lib/Tickit/Widget/Button.pm 2013-08-30 23:19:22 +0000 +++ lib/Tickit/Widget/Button.pm 2013-09-28 13:53:51 +0000 @@ -12,7 +12,7 @@ use base qw( Tickit::Widget ); use Tickit::Style; -use Tickit::RenderBuffer qw( LINE_SINGLE ); +use Tickit::RenderBuffer qw( LINE_SINGLE LINE_DOUBLE LINE_THICK ); our $VERSION = '0.12'; @@ -52,6 +52,12 @@ =over 4 +=item linetype => STRING + +What kind of border to draw around the button; one of + + none single double thick + =item marker_left => STRING A two-character string to place just before the button label @@ -87,6 +93,7 @@ style_definition base => fg => "black", bg => "blue", + linetype => "single", marker_left => "> ", marker_right => " <", '<Enter>' => "click"; @@ -98,6 +105,7 @@ style_definition ':active' => rv => 1; +style_reshape_keys qw( linetype ); style_redraw_keys qw( marker_left marker_right ); use constant WIDGET_PEN_FROM_STYLE => 1; @@ -145,13 +153,16 @@ sub lines { - return 3; + my $self = shift; + my $has_border = ( $self->get_style_values( "linetype" ) ) ne "none"; + return 1 + 2*$has_border; } sub cols { my $self = shift; - return 4 + textwidth $self->label; + my $has_border = ( $self->get_style_values( "linetype" ) ) ne "none"; + return 4 + textwidth( $self->label ) + 2*$has_border; } =head1 ACCESSORS @@ -257,10 +268,12 @@ my $width = textwidth $self->label; - my ( $lines_before, undef, $lines_after ) = $self->_valign_allocation( 1, $lines - 2 ); + my $has_border = ( $self->get_style_values( "linetype" ) ) ne "none"; + + my ( $lines_before, undef, $lines_after ) = $self->_valign_allocation( 1, $lines - (2 * $has_border) ); my ( $cols_before, undef, $cols_after ) = $self->_align_allocation( $width + 2, $cols - 2 ); - $self->{label_line} = $lines_before + 1; + $self->{label_line} = $lines_before + $has_border; $self->{label_col} = $cols_before + 2; $self->{label_end} = $cols_before + $width + 2; @@ -276,16 +289,30 @@ my $lines = $win->lines; my $cols = $win->cols; - $rb->hline_at( 0, 0, $cols-1, LINE_SINGLE ); - $rb->hline_at( $lines-1, 0, $cols-1, LINE_SINGLE ); - $rb->vline_at( 0, $lines-1, 0, LINE_SINGLE ); - $rb->vline_at( 0, $lines-1, $cols-1, LINE_SINGLE ); - - foreach my $line ( $rect->linerange( 1, $lines-2 ) ) { - $rb->erase_at( $line, 1, $cols-2 ); - } - - my ( $marker_left, $marker_right ) = $self->get_style_values( "marker_left", "marker_right" ); + my ( $linetype, $marker_left, $marker_right ) = + $self->get_style_values(qw( linetype marker_left marker_right )); + + my $linestyle = $linetype eq "single" ? LINE_SINGLE : + $linetype eq "double" ? LINE_DOUBLE : + $linetype eq "thick" ? LINE_THICK : + undef; + + if( defined $linestyle ) { + $rb->hline_at( 0, 0, $cols-1, $linestyle ); + $rb->hline_at( $lines-1, 0, $cols-1, $linestyle ); + $rb->vline_at( 0, $lines-1, 0, $linestyle ); + $rb->vline_at( 0, $lines-1, $cols-1, $linestyle ); + + foreach my $line ( $rect->linerange( 1, $lines-2 ) ) { + $rb->erase_at( $line, 1, $cols-2 ); + } + } + else { + foreach my $line ( $rect->linerange( 0, $lines-1 ) ) { + $rb->erase_at( $line, 0, $cols ); + } + } + $rb->text_at( $self->{label_line}, $self->{label_col} - 2, $marker_left ); $rb->text_at( $self->{label_line}, $self->{label_end}, $marker_right ); === modified file 't/10widget-button.t' --- t/10widget-button.t 2013-06-26 14:05:05 +0000 +++ t/10widget-button.t 2013-09-28 13:53:19 +0000 @@ -35,6 +35,19 @@ [TEXT("└────────────┘",fg=>0,bg=>4)] ], 'Display initially' ); +{ + $button->set_style( linetype => "double" ); + + flush_tickit; + + is_display( [ [TEXT("╔════════════╗",fg=>0,bg=>4)], + [TEXT("║> Click me <║",fg=>0,bg=>4)], + [TEXT("╚════════════╝",fg=>0,bg=>4)] ], + 'Display with linetype "double"' ); + + $button->set_style( linetype => undef ); +} + pressmouse( press => 1, 1, 10 ); flush_tickit; @@ -63,4 +76,14 @@ is( $clicked, 2, '$clicked after <Enter> key' ); +{ + my $button_without_border = Tickit::Widget::Button->new( + label => "My label", + style => { linetype => "none" }, + ); + + is( $button_without_border->lines, 1, '->lines without border' ); + is( $button_without_border->cols, 12, '->cols without border' ); +} + done_testing;
Released in 0.13. -- Paul Evans