Help with Cinnamon applet dev

Chat about anything related to Linux Mint
Forum rules
Do not post support questions here. Before you post read the forum rules. Topics in this forum are automatically closed 6 months after creation.
Locked
nabsha

Help with Cinnamon applet dev

Post by nabsha »

Hi All,

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;
}
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
remoulder
Level 17
Level 17
Posts: 7621
Joined: Fri May 09, 2008 1:14 pm

Re: Help with Cinnamon applet dev

Post by remoulder »

This is not a developers forum, I suggest you try https://github.com/linuxmint/Cinnamon
[Edit] your original post and add [SOLVED] once your question is resolved.

“The people are my God” stressing the factor determining man’s destiny lies within man not in anything outside man, and thereby defining man as the dominator and remoulder of the world.
nabsha

Re: Help with Cinnamon applet dev

Post by nabsha »

Hi All,

Just wanted to update that I kinnda rewrote the thing and it worked.

You can get the working applet via github.
https://github.com/nabsha/cinnamon-applet-world-times

I tried to upload it on cinnamon-spices but I could not find the link so if someone can help me to get it on cinnamon-spices, that would be great.
http://cinnamon-spices.linuxmint.com/applets

It was a good experiment and I am sure that a lot more can be done for cinnamon applets.

Cheers.
Kamikaze

Re: Help with Cinnamon applet dev

Post by Kamikaze »

Hi Nabsha,

I, like you, recently developed my first Cinnamon applet. Sounds like you had the same troubles I did (lack of doco mainly, and difficulty finding how to upload the applet to spices).
I still haven't found great doco, though am now a bit more familiar with translating some of the API doco available from the GNOME site for use via the gjs bindings which helped when I was recently looking into using GSettings schemas.

Anyway, I can help you with uploading the applet to the spices site - here's how you do it:
* On visiting the page http://cinnamon-spices.linuxmint.com/applets you'll see 'Sign In' text in the very top right of the page
* Register an account and then log-in (note that it appears to send your p/w over straight HTTP so don't use any password you care about)
* Once you've logged in, you should see new text in the very top LEFT of the page, called '+ New'.
* Highlight the '+ New' and it should drop down so you can select the relevant thing you've made be it an applet/extension/theme
* Fill out the details required in the form and submit.

Note that it's very picky on wanting PNGs for pics and a ZIP of the applet, nothing else was accepted for me (I originally tried a .tar.gz I think).
Hope this helps.

Cheers
Locked

Return to “Chat about Linux Mint”