our
$VERSION
=
'1.000'
;
my
%ICONS
= (
lazy
=> dist_file(
'App-TimeTracker-Gtk3StatusIcon'
,
'lazy.png'
),
busy
=> dist_file(
'App-TimeTracker-Gtk3StatusIcon'
,
'busy.png'
),
);
my
$TRACKER_HOME
= App::TimeTracker::Proto->new->home;
sub
init {
my
(
$class
,
$run
) =
@_
;
my
@caller
=
caller
();
my
$lock
;
if
(
$caller
[1] =~ /tracker_gtk3statusicon.pl$/) {
$lock
= Lock::File->new(
$TRACKER_HOME
.
'/tracker_gtk3statusicon.lock'
, {
blocking
=>0 });
unless
(
$lock
) {
say
"tracker_gtk3statusicon.pl seems to be running already..."
;
exit
0;
}
}
Gtk3->init;
my
$menu
= Gtk3::Menu->new();
my
$task
= get_current_task();
my
$icon
= Gtk3::StatusIcon->new_from_file(
$ICONS
{
$task
->{status}});
my
@items
;
for
my
$line
(
$task
->{lines}->@*) {
my
$item
= Gtk3::MenuItem->new(
$line
);
$item
->signal_connect(
activate
=>
sub
{
Clipboard->copy(
$item
->get_label)
if
$task
->{status} eq
'busy'
;
} );
$menu
->append(
$item
);
push
(
@items
,
$item
);
}
my
$quit
= Gtk3::ImageMenuItem->new_from_stock(
'gtk-quit'
);
$quit
->signal_connect(
activate
=>
sub
{ Gtk3->main_quit } );
$menu
->append(
$quit
);
$menu
->show_all();
$icon
->signal_connect(
'activate'
=>
sub
{
$menu
->popup_at_pointer } );
my
$loop
= IO::Async::Loop::Glib->new();
my
$file
= IO::Async::File->new(
filename
=>
$TRACKER_HOME
,
on_mtime_changed
=>
sub
{
my
(
$self
) =
@_
;
my
$task
= get_current_task();
$icon
->set_from_file(
$ICONS
{
$task
->{status}});
for
my
$i
(0 .. 2) {
$items
[
$i
]->set_label(
$task
->{lines}[
$i
]);
}
}
);
$loop
->add(
$file
);
Gtk3->main
if
$run
;
}
sub
get_current_task() {
my
$task
= App::TimeTracker::Data::Task->current(
$TRACKER_HOME
);
if
(
$task
) {
return
{
status
=>
'busy'
,
lines
=> [
$task
->project,
$task
->id ||
'no id'
,
$task
->description ||
'no description'
,
],
}
}
else
{
return
{
status
=>
'lazy'
,
lines
=> [
qw(currently doing nothing)
],
}
}
}
1;