From 060da4560e3678c86ca89b2d030691a7bb3f9a75 Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Mon, 22 Sep 2008 16:11:25 +1200 Subject: [PATCH] Switch the MPD plugin to using the new Notifications module. --- lib/Display/Notifications.pm | 30 ++++++++++++++++++------- lib/Display/Plugin.pm | 3 ++- lib/Display/Plugins/MPD.pm | 43 +----------------------------------- picture.pl | 10 ++++++--- 4 files changed, 32 insertions(+), 54 deletions(-) diff --git a/lib/Display/Notifications.pm b/lib/Display/Notifications.pm index 934677d..6c6d836 100644 --- a/lib/Display/Notifications.pm +++ b/lib/Display/Notifications.pm @@ -3,17 +3,22 @@ package Display::Notifications; use Clutter; use Set::Object; -my $delay = 15; +my $delay = 5; sub new { - my $proto = shift; + my ($proto, $kernel, $session, $stage) = @_; my $class = ref($proto) || $proto; - my $self = {}; + my $self = { + 'kernel' => $kernel, + 'session' => $session, + 'stage' => $stage, + }; $self->{'blocks'} = Set::Object->new(); bless ($self, $class); + $self->init(); return $self; } @@ -21,9 +26,10 @@ sub add { my ($self, $notification) = @_; my $block = Clutter::Group->new(); + $block->set_opacity(0); my $bg = Clutter::Rectangle->new(Clutter::Color->parse('Black')); - $bg->set_width($self->{'stage'}->get_width() - 40); + $bg->set_width($self->{'stage'}->get_width() - 20); $bg->set_opacity(100); $block->add($bg); @@ -35,10 +41,11 @@ sub add { $notification->set_anchor_point(1, $notification->get_height()); $notification->set_position(20, $self->{'stage'}->get_height() - 20); + $notification->set_width($bg->get_width() - 20); my $expire = time() + $delay; - $self->{'blocks'}->add( { + $self->{'blocks'}->insert( { 'expire' => $expire, 'block' => $block }); @@ -46,13 +53,14 @@ sub add { $self->{'stage'}->add($block); my $effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_inc'); my $timeline = Clutter::Effect->fade($effect, $block, 255); - $timeline->start(); - $self->{'stage'}->delay_add(notification_expire, $expire, $self); + $self->{'kernel'}->delay_add('notifications_expire', $delay); + + $timeline->start(); } sub expire { - my $self = shift; + my ($self) = @_[OBJECT]; for my $block ($self->{'blocks'}->members()) { if ($block->expire <= time()) { @@ -62,4 +70,10 @@ sub expire { } } +sub init { + my $self = shift; + + $self->{'kernel'}->state('notifications_expire', $self, 'expire'); +} + 1; diff --git a/lib/Display/Plugin.pm b/lib/Display/Plugin.pm index 9df460e..b8e9540 100644 --- a/lib/Display/Plugin.pm +++ b/lib/Display/Plugin.pm @@ -4,12 +4,13 @@ package Display::Plugin; use Clutter; sub new { - my ($class,$kernel,$session,$stage) = @_; + my ($class,$kernel,$session,$stage,$notifications) = @_; my $self = { 'kernel' => $kernel, 'session' => $session, 'stage' => $stage, + 'notifications' => $notifications, }; bless ($self, $class); diff --git a/lib/Display/Plugins/MPD.pm b/lib/Display/Plugins/MPD.pm index 8a7362d..edb62c7 100644 --- a/lib/Display/Plugins/MPD.pm +++ b/lib/Display/Plugins/MPD.pm @@ -5,8 +5,6 @@ use Audio::MPD; use POSIX qw/strftime/; use base ('Display::Plugin'); -my $delay = 15; - sub new { my $proto = shift; my $class = ref($proto) || $proto; @@ -37,57 +35,18 @@ sub display { $self->{'file'} = $current->file(); - $self->{'bg'}->set_height($self->{'status'}->get_height() + 10); - $self->{'bg'}->set_anchor_point(1, $self->{'bg'}->get_height()); - $self->{'bg'}->set_position(10, $self->{'stage'}->get_height() - 10); - - $self->{'status'}->set_anchor_point(1, $self->{'status'}->get_height()); - $self->{'status'}->set_position(20, $self->{'stage'}->get_height() - 20); - - if (! $self->{'active'}) { - $self->{'block'}->set_opacity(0); - $self->{'stage'}->add($self->{'block'}); - my $effect = Clutter::EffectTemplate->new_for_duration(1000, 'main::smoothstep_inc'); - my $timeline = Clutter::Effect->fade($effect, $self->{'block'}, 255); - $timeline->start(); - - $self->{'active'} = time() + $delay; - } - - Glib::Timeout->add_seconds($delay, $self->can('hide_display'), $self); + $self->{'notifications'}->add($self->{'status'}); } return 1; } -sub hide_display { - my $self = shift; - - if ($self->{'active'} <= time()) { - $self->{'stage'}->remove($self->{'block'}); - - $self->{'active'} = 0; - } - - return 0; -} - sub init { my $self = shift; - $self->{'block'} = Clutter::Group->new(); - - $self->{'bg'} = Clutter::Rectangle->new(Clutter::Color->parse('Black')); - $self->{'bg'}->set_width($self->{'stage'}->get_width() - 20); - $self->{'bg'}->set_opacity(100); - $self->{'status'} = Clutter::Label->new('Sans 20', "Song\nAlbum"); $self->{'status'}->set_color(Clutter::Color->parse('White')); $self->{'status'}->set_ellipsize('end'); - $self->{'status'}->set_width($self->{'bg'}->get_width() - 20); - - $self->{'block'}->add($self->{'bg'}); - $self->{'block'}->add($self->{'status'}); Glib::Timeout->add_seconds(5, $self->can('display'), $self); } diff --git a/picture.pl b/picture.pl index a8a783b..e3e1402 100755 --- a/picture.pl +++ b/picture.pl @@ -10,6 +10,8 @@ use Getopt::Long; use lib "$FindBin::Bin/lib"; +use Display::Notifications; + my $full_screen = 1; GetOptions( @@ -39,14 +41,16 @@ sub start { $stage->set_size(800, 600); } - my @modules = loadModules($kernel, $session, $stage); + my $notifications = Display::Notifications->new($kernel, $session, $stage); + + my @modules = loadModules($kernel, $session, $stage, $notifications); $stage->show_all(); } sub loadModules { - my ($kernel, $session, $stage) = @_; + my ($kernel, $session, $stage, $notifications) = @_; my @modules; for my $module (plugins()) { @@ -55,7 +59,7 @@ sub loadModules { if ($@) { warn "Failed to load plugin: $module ($@)\n"; } else { - push @modules, $module->new($kernel, $session, $stage); + push @modules, $module->new($kernel, $session, $stage, $notifications); } } -- 2.30.2