I am new to Cinnamon development and this forum.
I have been trying to write an applet for Cinnamon but have struck rock and need some advise.
The applet I am trying to build should show me the time of any given location.
Since there is no real documentation I could find, I started my work from netspeed@adec applet. Everything went smooth until I started to add menu to add location to the settings.json. Somehow I am not able to add menu until now.
I would really appreciate if someone can help me with that please.
Here is the code of applet.js
Code: Select all
const Applet = imports.ui.applet;
const Cinnamon = imports.gi.Cinnamon;
const GLib = imports.gi.GLib;
const GTop = imports.gi.GTop;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const PopupMenu = imports.ui.popupMenu;
const St = imports.gi.St;
const Main = imports.ui.main;
function MyMenu(launcher, orientation) {
this._init(launcher, orientation);
}
MyMenu.prototype = {
__proto__: PopupMenu.PopupMenu.prototype,
_init: function(launcher, orientation) {
this._launcher = launcher;
PopupMenu.PopupMenu.prototype._init.call(this, launcher.actor, 0.0, orientation, 0);
Main.uiGroup.add_actor(this.actor);
this.actor.hide();
}
}
function MyApplet(orientation) {
this._init(orientation);
}
MyApplet.prototype = {
__proto__: Applet.TextApplet.prototype,
_init: function(metadata, orientation) {
Applet.TextApplet.prototype._init.call(this, orientation);
this.metadata = metadata;
this.path = this.metadata.path;
this.settingsFile = "settings.json";
try {
this.loadSettings();
this.menuManager = new PopupMenu.PopupMenuManager(this);
this.menu = new MyMenu(this, orientation);
this.menuManager.addMenu(this.menu);
this.makeMenu();
this.buildContextMenu();
this.update();
}
catch (e) {
global.logError(e);
}
},
makeMenu: function() {
//this.menu.removeAll();
//this.menuitemInfo = new PopupMenu.PopupMenuItem("This is test Message", { reactive: false });
this.menu.addMenuItem(new PopupMenu.PopupMenuItem("Test Message", { reactive: false }));
},
buildContextMenu: function() {
this._applet_context_menu.removeAll();
this._applet_context_menu.addMenuItem(new PopupMenu.PopupMenuItem("Select the interface to be monitored:", { reactive: false }));
},
on_applet_clicked: function(event) {
if(!this.menu.isOpen) {
this.makeMenu();
}
this.menu.toggle();
},
update:function () {
var d_now = new Date();
var d_offset = new Date(d_now);
d_offset.setHours(d_now.getHours() + this.settings.HoursOffset);
this.set_applet_label(this.settings.Location + ":" + d_offset.getDate()+"/"+(d_offset.getMonth()+1)+" "+d_offset.getHours()+":"+
d_offset.getMinutes()+":"+d_offset.getSeconds());
Mainloop.timeout_add(1000, Lang.bind(this, this.update));
},
loadSettings: function() {
try {
global.logError("Setting path: " + this.path);
var dir = Gio.file_new_for_path(this.path);
global.logError("Setting filepath: " + this.settingsFile);
var prefsFile = dir.get_child(this.settingsFile);
global.logError("Reading File" + prefsFile.get_path());
var settingData = Cinnamon.get_file_contents_utf8_sync(prefsFile.get_path());
global.logError("PArsing JSON");
this.settings = JSON.parse(settingData);
} catch(e) {
global.logError(e);
global.logError("Settings file not found. Using default values.");
this.settings = JSON.parse('{"Location":"Sydney","HoursOffset":8,"MinutesOffset":0}');
}
}
};
function main(metadata, orientation) {
//imports.searchPath.push(metadata.path);
let myApplet = new MyApplet(metadata,orientation);
return myApplet;
}