X-Git-Url: https://adrianiainlam.tk/git/?p=indicator-lunar-calendar.git;a=blobdiff_plain;f=indicator-lunar-calendar.js;h=16af28c21887712407cd473e8a8c2e4f98c9a8d1;hp=77b778da3b98acd29f735106f2e80f8f8493681e;hb=9ee9956267315fa231fd54727e2e92a585a43bb3;hpb=c2c14655e4da36c46f245fe9ad421d3b290ae600 diff --git a/indicator-lunar-calendar.js b/indicator-lunar-calendar.js index 77b778d..16af28c 100755 --- a/indicator-lunar-calendar.js +++ b/indicator-lunar-calendar.js @@ -23,26 +23,27 @@ */ /* import dependencies */ -var GNode = require('node-gtk'); -var Gtk = GNode.importNS('Gtk'); -var AppIndicator3 = GNode.importNS('AppIndicator3'); +const gi = require('node-gtk'); +const Gtk = gi.require('Gtk', '3.0'); +const AppIndicator3 = gi.require('AppIndicator3'); var CronJob = require('cron').CronJob; var LunarCalendar = require('lunar-calendar-zh'); +var DBus = require('dbus-native'); /* setup indicator object */ -GNode.startLoop(); -Gtk.init(null); +gi.startLoop(); +Gtk.init(); var indicator = AppIndicator3.Indicator.new( "lunar-indicator", - __dirname + '/鼠.svg', + __dirname + '/icons/鼠.svg', AppIndicator3.IndicatorCategory.APPLICATION_STATUS ); -indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE); +indicator.setStatus(AppIndicator3.IndicatorStatus.ACTIVE); var menu = new Gtk.Menu(); var item = new Gtk.MenuItem(); menu.append(item); -indicator.set_menu(menu); -menu.show_all(); +indicator.setMenu(menu); +menu.showAll(); function update_indicator() { /* get current time at UTC+8, add 1 to date if after 23:00 (子時) */ @@ -70,9 +71,17 @@ function update_indicator() { long_date += '\n' + lunar.hour + '時'; /* output to indicator */ - indicator.set_icon(__dirname + '/' + lunar.zodiac + '.svg'); - indicator.set_label(compact_date, ''); - item.set_label(long_date); + indicator.setIcon(__dirname + '/icons/' + lunar.zodiac + '.svg'); + indicator.setLabel(compact_date, ''); + item.setLabel(long_date); + + console.log('Indicator updated. ' + lunar.hour + ' Time: ' + new Date()); + /* DO NOT REMOVE THE ABOVE LINE. + * I have no absolutely no idea why but the indicator doesn't get + * updated if this line is removed. + * It wasn't needed though before implementing the DBus section below. + * So perhaps some kind of conflict between the two? + */ } var job = new CronJob({ @@ -82,4 +91,23 @@ var job = new CronJob({ }); update_indicator(); + +/* Detect resume from suspend and update date/time */ +var bus = DBus.systemBus(); +var service = bus.getService('org.freedesktop.login1'); +service.getInterface( + '/org/freedesktop/login1', + 'org.freedesktop.login1.Manager', + function(err, nm) { + nm.addListener('PrepareForSleep', function(arg) { + // PrepareForSleep returns false when resuming from suspend + if(!arg) { + job.stop(); // force cronjob to recalculate time + job.start(); + update_indicator(); + } + }); + } +); + Gtk.main();