view release on metacpan or search on metacpan
lib/Tickit/DSL.pm view on Meta::CPAN
245246247248249250251252253254255256257258259260261262263264265Defers a block of code.
later {
"this happened later\n"
;
};
Will run the code
after
the
next
round of I/O events.
=cut
sub later(&) {
my $code = shift;
tickit->later($code)
}
=head2 timer
Sets up a timer to run a block of code later.
timer {
print "about a second has passed\n";
lib/Tickit/DSL.pm view on Meta::CPAN
267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
timer {
"about a minute has passed\n"
;
}
at
=>
time
+ 60;
Takes a codeblock and either C<at> or C<
after
> definitions. Passing
anything other than a single definition will cause an exception.
=cut
sub timer(&@) {
my $code = shift;
my %args = @_;
die 'when did you want to run the code?' unless 1 == grep exists $args{$_}, qw(at after);
tickit->timer(%args, $code);
}
=head2 add_widgets
Adds some widgets under an existing widget.
my $some_widget = vbox { };
add_widgets {
vbox { ... };
hbox { ... };
} under => $some_widget;
Returns the widget we added the new widgets under (i.e. the C< under > parameter).
=cut
sub
add_widgets(&@) {
my
$code
=
shift
;
my
%args
=
@_
;
local
$PARENT
=
delete
$args
{under} or
die
'expected add_widgets { ... } under => $some_widget;'
;
local
@WIDGET_ARGS
= (
@WIDGET_ARGS
,
%args
);
$code
->(
$PARENT
);
$PARENT
;
}
=head1 FUNCTIONS - Layout
lib/Tickit/DSL.pm view on Meta::CPAN
319320321322323324325326327328329330331332333334335336337338339
vbox {
...
}
class
=>
'some_vbox'
;
vbox {
...
}
classes
=> [
qw(other vbox)
],
style
=> {
fg
=>
'green'
};
=cut
sub vbox(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::VBox->new(%args);
{
local $PARENT = $w;
$code->($w);
}
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
}
lib/Tickit/DSL.pm view on Meta::CPAN
349350351352353354355356357358359360361362363364365366367368369
vsplit {
...
}
class
=>
'some_vsplit'
;
vsplit {
...
}
classes
=> [
qw(other vsplit)
],
style
=> {
fg
=>
'green'
};
=cut
sub vsplit(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = do {
local $PARENT = 'Tickit::Widget::VSplit';
local @PENDING_CHILD;
$code->();
Tickit::Widget::VSplit->new(
left_child => $PENDING_CHILD[0],
right_child => $PENDING_CHILD[1],
%args,
lib/Tickit/DSL.pm view on Meta::CPAN
380381382383384385386387388389390391392393394395396397398399400Any additional parameters will be passed to the new L<Tickit::Widget::Frame>
instance:
frame {
...
}
title
=>
'some frame'
,
title_align
=> 0.5;
=cut
sub frame(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::Frame->new(%args);
{
local $PARENT = $w;
$code->($w);
}
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
}
lib/Tickit/DSL.pm view on Meta::CPAN
411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452Any additional parameters will be passed to the new L<Tickit::Widget::GridBox>
instance:
gridbox {
gridrow { static
'left'
; static
'right'
};
gridrow { static
'BL'
; static
'BR'
};
}
style
=> {
col_spacing
=> 1,
row_spacing
=> 1 };
=cut
sub gridbox(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::GridBox->new(%args);
{
local $PARENT = $w;
local $GRID_COL = 0;
local $GRID_ROW = 0;
$code->($w);
}
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
}
=head2 gridrow
Marks a separate row in an existing L<Tickit::Widget::GridBox>. This behaves
something like a container, see L</gridbox> for details.
=cut
sub
gridrow(&@) {
my
(
$code
) =
@_
;
die
"Grid rows must be in a gridbox"
unless
$PARENT
->isa(
'Tickit::Widget::GridBox'
);
$code
->(
$PARENT
);
$GRID_COL
= 0;
++
$GRID_ROW
;
}
=head2 hbox
Creates a L<Tickit::Widget::HBox>. This is a container, so the first
lib/Tickit/DSL.pm view on Meta::CPAN
458459460461462463464465466467468469470471472473474475476477478
hbox {
...
}
class
=>
'some_hbox'
;
hbox {
...
}
classes
=> [
qw(other hbox)
],
style
=> {
fg
=>
'green'
};
=cut
sub hbox(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::HBox->new(%args);
{
local $PARENT = $w;
$code->($w);
}
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
}
lib/Tickit/DSL.pm view on Meta::CPAN
488489490491492493494495496497498499500501502503504505506507508
hsplit {
...
}
class
=>
'some_hsplit'
;
hsplit {
...
}
classes
=> [
qw(other hsplit)
],
style
=> {
fg
=>
'green'
};
=cut
sub hsplit(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = do {
local $PARENT = 'Tickit::Widget::HSplit';
local @PENDING_CHILD;
$code->();
Tickit::Widget::HSplit->new(
top_child => $PENDING_CHILD[0],
bottom_child => $PENDING_CHILD[1],
%args
lib/Tickit/DSL.pm view on Meta::CPAN
522523524525526527528529530531532533534535536537538539540541542
my
$txt
= static
'a static widget'
,
'parent:label'
=>
'static'
;
entry {
$txt
->set_text(
$_
[1])
}
'parent:label'
=>
'entry widget'
,
'parent:left'
=> 1,
'parent:top'
=> 1;
};
=cut
sub desktop(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::Layout::Desktop->new(%args);
{
tickit->later(sub {
local @WIDGET_ARGS;
local $PARENT = $w;
$code->($w);
});
}
lib/Tickit/DSL.pm view on Meta::CPAN
545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
apply_widget(
$w
);
}
}
=head2 relative
See L</pane> for the details.
=cut
sub
relative(&@) {
my
(
$code
,
%args
) =
@_
;
my
%parent_args
=
map
{;
$_
=>
delete
$args
{
'parent:'
.
$_
} }
map
/^parent:(.*)/ ? $1 : (),
keys
%args
;
my
$w
= Tickit::Widget::Layout::Relative->new(
%args
);
{
local
@WIDGET_ARGS
;
local
$PARENT
=
$w
;
$code
->(
$w
);
}
{
local
@WIDGET_ARGS
= (
@WIDGET_ARGS
,
%parent_args
);
apply_widget(
$w
);
}
}
=head2 pane
A pane in a L</relative> layout.
=cut
sub
pane(&@) {
my
(
$code
,
%args
) =
@_
;
die
"pane should be used within a relative { ... } item"
unless
$PARENT
->isa(
'Tickit::Widget::Layout::Relative'
);
{
local
@WIDGET_ARGS
= (
@WIDGET_ARGS
,
%args
);
$code
->(
$PARENT
);
}
}
=head1 FUNCTIONS - Scrolling
lib/Tickit/DSL.pm view on Meta::CPAN
595596597598599600601602603604605606607608609610611612613614615Any additional parameters will be passed to the new L<Tickit::Widget::ScrollBox>
instance:
scrollbox {
...
}
class
=>
'some_hsplit'
;
=cut
sub scrollbox(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = do {
local $PARENT = 'Tickit::Widget::ScrollBox';
local @PENDING_CHILD;
$code->();
Tickit::Widget::ScrollBox->new(
child => $PENDING_CHILD[0],
%args
lib/Tickit/DSL.pm view on Meta::CPAN
629630631632633634635636637638639640641642643644645646647648649};
Passes any additional args to the constructor:
scroller {
scroller_text
'line '
.
$_
for
1..100;
}
gravity
=>
'bottom'
;
=cut
sub scroller(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::Scroller->new(%args);
{
local $PARENT = $w;
$code->($w);
}
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
}
lib/Tickit/DSL.pm view on Meta::CPAN
692693694695696697698699700701702703704705706707708709710711712although a future version may provide C< console_tab >
as a helper function
for
adding tabs to an existing
console.
Note that this will attempt to load L<Tickit::Console>
at runtime, so it may throw an exception
if
it is not
already installed.
=cut
sub console(&@) {
require "Tickit" . "/Console.pm";
my %args = (on_line => @_);
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Console->new(
%args
);
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
$w
}
lib/Tickit/DSL.pm view on Meta::CPAN
768769770771772773774775776777778779780781782783784785786787788
tabbed {
static
'some text'
'parent:label'
=>
'first tab'
;
static
'other text'
'parent:label'
=>
'second tab'
;
}
ribbon_class
=>
'Some::Ribbon::Class'
,
tab_position
=>
'top'
;
The C<ribbon_class> parameter may be undocumented.
=cut
sub tabbed(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::Tabbed->new(%args);
{
local $PARENT = $w;
$code->($w);
}
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
}
lib/Tickit/DSL.pm view on Meta::CPAN
798799800801802803804805806807808809810811812813814815816817818
button {
float {
static
'this is a float'
}
lines
=> 3,
top
=> -1,
left
=>
'-50%'
;
}
'Show'
;
}
}
=cut
sub floatbox(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::FloatBox->new(%args);
{
local $PARENT = $w;
$code->($w);
}
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
}
lib/Tickit/DSL.pm view on Meta::CPAN
827828829830831832833834835836837838839840841842843844845846847
button {
float {
static
'this is a float'
}
lines
=> 3,
top
=> -1,
left
=>
'-50%'
;
}
'Show'
;
}
}
=cut
sub float(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
# Work out which container to use - either the least-distant ancestor,
# or a specific floatbox if one was provided
my $floatbox = delete($args{container}) || $PARENT;
while($floatbox && !$floatbox->isa('Tickit::Widget::FloatBox')) {
$floatbox = $floatbox->parent;
}
die "No floatbox found for this float" unless $floatbox;
lib/Tickit/DSL.pm view on Meta::CPAN
858859860861862863864865866867868869870871872873874875876877878
$code
->(
$float
);
};
}
=head2 statusbar
A L<Tickit::Widget::Statusbar>. Not very exciting.
=cut
sub
statusbar(&@) {
my
(
$code
,
%args
) =
@_
;
my
%parent_args
=
map
{;
$_
=>
delete
$args
{
'parent:'
.
$_
} }
map
/^parent:(.*)/ ? $1 : (),
keys
%args
;
my
$w
= Tickit::Widget::Statusbar->new(
%args
);
{
local
$PARENT
=
$w
;
$code
->(
$w
);
}
local
@WIDGET_ARGS
= (
@WIDGET_ARGS
,
%parent_args
);
apply_widget(
$w
);
}
lib/Tickit/DSL.pm view on Meta::CPAN
928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984=head2 entry
A L<Tickit::Widget::Entry> input field. Takes a coderef as the first parameter
since the C<on_enter> handler seems like an important feature.
my $rslt = static 'result here';
entry { shift; $rslt->set_text(eval shift) } text => '1 + 3';
=cut
sub
entry(&@) {
my
%args
= (
on_enter
=>
@_
);
my
%parent_args
=
map
{;
$_
=>
delete
$args
{
'parent:'
.
$_
} }
map
/^parent:(.*)/ ? $1 : (),
keys
%args
;
my
$w
= Tickit::Widget::Entry->new(
%args
);
local
@WIDGET_ARGS
= (
@WIDGET_ARGS
,
%parent_args
);
apply_widget(
$w
);
}
=head2 checkbox
Checkbox (or checkbutton).
=cut
sub
checkbox(&@) {
my
%args
= (
on_toggle
=>
@_
);
my
%parent_args
=
map
{;
$_
=>
delete
$args
{
'parent:'
.
$_
} }
map
/^parent:(.*)/ ? $1 : (),
keys
%args
;
my
$w
= Tickit::Widget::CheckButton->new(
%args
);
local
@WIDGET_ARGS
= (
@WIDGET_ARGS
,
%parent_args
);
apply_widget(
$w
);
}
=head2 radiobutton
radiogroup {
radiobutton { } 'one';
radiobutton { } 'two';
radiobutton { } 'three';
};
=cut
sub
radiobutton(&@) {
my
$code
=
shift
;
die
"need a radiogroup"
unless
$RADIOGROUP
;
my
%args
= (
group
=>
$RADIOGROUP
,
label
=>
@_
);
my
%parent_args
=
map
{;
$_
=>
delete
$args
{
'parent:'
.
$_
} }
map
/^parent:(.*)/ ? $1 : (),
keys
%args
;
my
$w
= Tickit::Widget::RadioButton->new(
%args
);
$w
->set_on_toggle(
$code
);
{
lib/Tickit/DSL.pm view on Meta::CPAN
9869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
apply_widget(
$w
);
}
}
=head2 radiogroup
See L</radiobutton>.
=cut
sub
radiogroup(&@) {
my
$code
=
shift
;
my
%args
=
@_
;
# my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my
$group
= Tickit::Widget::RadioButton::Group->new;
$group
->set_on_changed(
delete
$args
{on_changed})
if
exists
$args
{on_changed};
{
local
$RADIOGROUP
=
$group
;
$code
->();
}
}
=head2 button
A button. First parameter is the code to run when activated,
second parameter is the label:
button { warn "Activated" } 'OK';
=cut
sub
button(&@) {
my
$code
=
shift
;
my
%args
= (
label
=>
@_
);
my
%parent_args
=
map
{;
$_
=>
delete
$args
{
'parent:'
.
$_
} }
map
/^parent:(.*)/ ? $1 : (),
keys
%args
;
my
$w
= Tickit::Widget::Button->new(
%args
);
$w
->set_on_click(
sub
{
local
$PARENT
=
$w
->parent;
lib/Tickit/DSL.pm view on Meta::CPAN
104610471048104910501051105210531054105510561057105810591060106110621063106410651066
node2
=> [
qw(more nodes in this one)
,
and
=> [
qw(this has a few child nodes too)
]
],
];
=cut
sub tree(&@) {
my %args = (on_activate => @_);
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::Tree->new(
%args
);
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
$w
}
lib/Tickit/DSL.pm view on Meta::CPAN
10741075107610771078107910801081108210831084108510861087108810891090109110921093}
data
=> [
[ 1,
'first line'
],
[ 2,
'second line'
],
],
columns
=> [
{
label
=>
'ID'
,
width
=> 9,
align
=>
'right'
},
{
label
=>
'Description'
},
];
=cut
sub table(&@) {
my %args = (on_activate => @_);
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::Table->new(
%args
);
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
$w
}
lib/Tickit/DSL.pm view on Meta::CPAN
10961097109810991100110111021103110411051106110711081109111011111112111311141115Provides a
"breadcrumb trail"
.
my
$bc
= breadcrumb {
warn
"crumb selected: @_"
;
};
$bc
->adapter->
push
([
qw(some path here)
]);
=cut
sub breadcrumb(&@) {
my %args = (on_activate => @_);
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::Breadcrumb->new(
%args
);
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
$w
}
lib/Tickit/DSL.pm view on Meta::CPAN
116511661167116811691170117111721173117411751176117711781179118011811182118311841185=head2 fileviewer
File viewer. Takes a code block and a file name. The code block is currently unused,
but eventually will be called when the current line is activated in the widget.
fileviewer { } 'somefile.txt';
=cut
sub
fileviewer(&;@) {
my
(
$code
,
$file
) =
splice
@_
, 0, 2;
my
%args
= (
@_
,
file
=>
$file
);
my
%parent_args
=
map
{;
$_
=>
delete
$args
{
'parent:'
.
$_
} }
map
/^parent:(.*)/ ? $1 : (),
keys
%args
;
my
$w
= Tickit::Widget::FileViewer->new(
%args
);
lib/Tickit/DSL.pm view on Meta::CPAN
122212231224122512261227122812291230123112321233123412351236123712381239124012411242
};
};
static
'plain text under the menubar'
;
}
};
=cut
# haxx. A menubar has no link back to the container.
our $MENU_PARENT;
sub menubar(&@) {
my ($code, %args) = @_;
my %parent_args = map {; $_ => delete $args{'parent:' . $_} } map /^parent:(.*)/ ? $1 : (), keys %args;
my $w = Tickit::Widget::MenuBar->new(%args);
local $MENU_PARENT = $PARENT;
{
local $PARENT = $w;
$code->($w);
}
local @WIDGET_ARGS = (@WIDGET_ARGS, %parent_args);
apply_widget($w);
lib/Tickit/DSL.pm view on Meta::CPAN
13091310131113121313131413151316131713181319132013211322132313241325132613271328as widget arguments, see L</widget>
for
details.
customwidget {
my
$tbl
= Tickit::Widget::Table::Paged->new;
$tbl
->add_column(...);
$tbl
;
}
expand
=> 1;
=cut
sub customwidget(&@) {
my ($code, @args) = @_;
my %args = @args;
local $PARENT = delete($args{parent}) || $PARENT;
my $w = $code->($PARENT);
{
local @WIDGET_ARGS = (@WIDGET_ARGS, %args);
apply_widget($w);
}
}
lib/Tickit/DSL.pm view on Meta::CPAN
13501351135213531354135513561357135813591360136113621363136413651366136713681369o the widgets themselves - the above example would
thus be:
vbox {
static
=>
'33%'
'parent:expand'
=> 1;
static
=>
'66%'
'parent:expand'
=> 2;
};
=cut
sub widget(&@) {
my ($code, %args) = @_;
local $PARENT = delete($args{parent}) || $PARENT;
{
local @WIDGET_ARGS = (@WIDGET_ARGS, %args);
$code->($PARENT);
}
}
=head2 apply_widget