| 1 | package App::TimeTracker::Gtk3StatusIcon; | ||
| 2 | |||
| 3 | # ABSTRACT: Show TimeTracker status as a GTK3 StatusIcon in the system tray | ||
| 4 | our $VERSION = '1.000'; # VERSION | ||
| 5 | |||
| 6 | 1 | ▶ | use v5.24; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 0 | ▶ | use Gtk3; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use IO::Async::File; | ||
| 12 | use IO::Async::Loop::Glib; | ||
| 13 | use Lock::File; | ||
| 14 | |||
| 15 | use App::TimeTracker::Proto 3.100; | ||
| 16 | use App::TimeTracker::Data::Task; | ||
| 17 | use File::Share qw(dist_file); | ||
| 18 | use Clipboard; | ||
| 19 | |||
| 20 | my %ICONS = ( | ||
| 21 | lazy => dist_file( 'App-TimeTracker-Gtk3StatusIcon', 'lazy.png' ), | ||
| 22 | busy => dist_file( 'App-TimeTracker-Gtk3StatusIcon', 'busy.png' ), | ||
| 23 | ); | ||
| 24 | |||
| 25 | my $TRACKER_HOME = App::TimeTracker::Proto->new->home; | ||
| 26 | |||
| 27 | sub init { | ||
| 28 | my ($class, $run) = @_; | ||
| 29 | |||
| 30 | my @caller = caller(); | ||
| 31 | my $lock; | ||
| 32 | if ($caller[1] =~ /tracker_gtk3statusicon.pl$/) { | ||
| 33 | $lock = Lock::File->new($TRACKER_HOME.'/tracker_gtk3statusicon.lock', { blocking=>0 }); | ||
| 34 | unless ($lock) { | ||
| 35 | say "tracker_gtk3statusicon.pl seems to be running already..."; | ||
| 36 | exit 0; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | Gtk3->init; | ||
| 41 | my $menu = Gtk3::Menu->new(); | ||
| 42 | my $task = get_current_task(); | ||
| 43 | my $icon = Gtk3::StatusIcon->new_from_file($ICONS{$task->{status}}); | ||
| 44 | my @items; | ||
| 45 | for my $line ($task->{lines}->@*) { | ||
| 46 | my $item = Gtk3::MenuItem->new($line); | ||
| 47 | $item->signal_connect( activate => sub { | ||
| 48 | Clipboard->copy($item->get_label) if $task->{status} eq 'busy'; | ||
| 49 | } ); | ||
| 50 | $menu->append($item); | ||
| 51 | push(@items, $item); | ||
| 52 | } | ||
| 53 | |||
| 54 | my $quit = Gtk3::ImageMenuItem->new_from_stock('gtk-quit'); | ||
| 55 | $quit->signal_connect( activate => sub { Gtk3->main_quit } ); | ||
| 56 | $menu->append($quit); | ||
| 57 | $menu->show_all(); | ||
| 58 | |||
| 59 | $icon->signal_connect( 'activate' => sub { $menu->popup_at_pointer } ); | ||
| 60 | |||
| 61 | my $loop = IO::Async::Loop::Glib->new(); | ||
| 62 | |||
| 63 | my $file = IO::Async::File->new( | ||
| 64 | filename => $TRACKER_HOME, | ||
| 65 | on_mtime_changed => sub { | ||
| 66 | my ( $self ) = @_; | ||
| 67 | my $task = get_current_task(); | ||
| 68 | $icon->set_from_file($ICONS{$task->{status}}); | ||
| 69 | for my $i (0 .. 2) { | ||
| 70 | $items[$i]->set_label($task->{lines}[$i]); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | ); | ||
| 74 | $loop->add( $file ); | ||
| 75 | |||
| 76 | Gtk3->main if $run; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub get_current_task() { | ||
| 80 | my $task = App::TimeTracker::Data::Task->current($TRACKER_HOME); | ||
| 81 | if ($task) { | ||
| 82 | return { | ||
| 83 | status => 'busy', | ||
| 84 | lines => [ | ||
| 85 | $task->project, | ||
| 86 | $task->id || 'no id', | ||
| 87 | $task->description || 'no description', | ||
| 88 | ], | ||
| 89 | } | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | return { | ||
| 93 | status => 'lazy', | ||
| 94 | lines => [qw(currently doing nothing)], | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ | ||