Catalyst-Blinker

 view release on metacpan or  search on metacpan

Blinker.pm  view on Meta::CPAN

        *Catalyst::Restarter::Forking::_fork_and_start = sub {
            $child = $$;
            $crf_fork->(@_);
        };
    }

    my $pid = fork;
    die "fork error:$!" unless defined $pid;
    if ( $pid ) {
        close $r;
        return;
    }

    # child here
    eval "use Prima qw(Application);";
    die $@ if $@;

    close $w;
    my $size = $opt{size};
    my $center = $size / 2;
    my $countdown_to_hide = 0;
    my $circle  = Prima::Image->new(
        width     => $size,
        height    => $size,
        type      => 1,
        color     => 0xffffff,
        backColor => 0,
    );
    $circle->begin_paint;
    $circle->clear;
    $circle->fill_ellipse($center,$center,$size-2,$size-2);
    $circle->end_paint;
    my $dx = ( $opt{x} <= 0 ) ? $::application->width-$size-2+$opt{x} : $opt{x};
    my $dy = ( $opt{y} <= 0 ) ? $::application->height-$size-2+$opt{y} : $opt{y};
    my $blinker = Prima::Widget->new(
        origin      => [ $dx, $dy ],
        size        => [ $size, $size ],
        visible     => 1,
        syncPaint   => 1, 
        backColor   => cl::LightRed(),
        selectable  => 0,
        shape       => $circle,
        buffered    => 1,
        onPaint     => sub {
            my ( $self, $canvas ) = @_;
            $canvas->clear;
            $canvas->lineWidth(4);
            my ( $c, $x ) = ( 8, $size-2 );
            while ( $c < 16 ) {
                $canvas->color($canvas->backColor & ( $c * 0x101010) );
                $canvas->ellipse($center,$center,$x,$x);
                $canvas->lineWidth(2);
                $x--;
                $c++;
            }
        },
    );
    $blinker->bring_to_front;
    my $blinkstate = 0;
    my $blinkcolor = 16;
    my $timer = Prima::Timer->new(
        timeout   => 100,
        onTick    => sub {
            my $self = shift;
            if ( $blinkcolor == 8 && $countdown_to_hide > 0 ) {
                # green waits and hides the blinker
                unless (--$countdown_to_hide) {
                    $self->stop;
                    $blinker->hide;
                }
            }

            my $c;
            if ( $countdown_to_hide < 8 ) {
                # normal up-down blinking
                $blinkstate = 0 if ++$blinkstate > 15;
            } else {
                # green glows steadily, then nicely fades out
                $blinkstate = 7;
            }

            $c = ( $blinkstate > 7 ) ? ( 15 - $blinkstate ) : $blinkstate;
            $c = (( $c << 4 ) | 0x80) << $blinkcolor;
            $c |= ( $c >> 8 ) if $c > 0x10000; # makes it yellow, as red doesn't blink at all
            $blinker->backColor($c);
        },
    );
    $timer->start;
    my $reader = Prima::File->new(
        file   => $r,
        mask   => fe::Read(),
        onRead => sub {
            my $cmd = <$r>;
            chomp $cmd;
            if ( $cmd eq 'start' ) {
                $blinkstate = 0;
                $countdown_to_hide = 32;
                $blinkcolor = 8;
            } elsif ( $cmd eq 'stop') {
                $timer->start;
                $blinkstate = 0;
                $blinkcolor = 16;
                $blinker->backColor(0x808000);
                $blinker->show;
                $blinker->bring_to_front;
            } elsif ( $cmd eq 'fail') {
                $blinker->backColor(cl::LightRed());
                $blinker->show;
                $blinker->bring_to_front;
                $timer->stop;
            }
        },
    );
    run Prima;
    exit;
}

1;

=head1 AUTHOR

Dmitry Karasik <dmitry@karasik.eu.org>

=head1 LICENSE

Copyright (C) 2013, Novozymes.

=cut



( run in 0.596 second using v1.01-cache-2.11-cpan-437f7b0c052 )