Writing a panel applet for Cinnamon: The basics
Introduction
What I wanted: A simple applet on Cinnamon, which allows me to turn a service on and off (hostapd, a Wifi hotspot). I first went for Argos catch-all extension, and learned that Cinnamon isn’t gnome-shell, and in particular that extensions for gnome-shell don’t (necessarily?) work with Cinnamon.
Speaking of which, my system is Linux Mint 19 on an x86_64, with
$ cinnamon --version Cinnamon 3.8.9
So I went for writing the applet myself. Given the so-so level of difficulty, I should have done that to begin with.
Spoiler: I’m not going to dive into the details of that, because my hostapd-firewall-DHCP daemon setting is quite specific. Rather, I’ll discuss about some general aspects of writing an applet.
So what is it like? Well, quite similar to writing something useful in JavaScript for a web page. Cinnamon’s applets are in fact written in JavaScript, and it feels pretty much the same. In particular, this thing about nothing happening when there’s an error, now go figure what it was. And yes, there’s an error log console which helps with syntax errors (reminds browsers’ error log, discussed below) but often run-time errors just lead to nothing. A situation that is familiar to anyone with JavaScript experience.
And I also finally understand why the cinnamon process hogs CPU all the time. OK, it’s usually just a few percents, and still, what is it doing all that time with no user activity? Answer: Running some JavaScript, I suppose.
But all in all, if you’re good with JavaScript and understand the concepts of GUI programming and events + fairly OK with object oriented programming, it’s quite fun. And there’s another thing you better be good at:
Read The Source
As of December 2018, the API for Cinnamon applets is hardly documented, and it’s somewhat messy. So after reading a couple of tutorials (See “References” at the bottom of this post), the best way to grasp how to get X done is by reading the sources of existing applets:
- System-installed: /usr/share/cinnamon/applets
- User-installed: ~/.local/share/cinnamon/applets
- Cinnamon’s core JavaScript sources: /usr/share/cinnamon/js
Each of these contains several subdirectories, typically with the form name@creator, one for each applet that is available for adding to the panels. Each of these has at least two files, which are also those to supply for your own applet:
- metadata.json, which contains some basic info on the applet (probably used while selecting applets to add).
- applet.js, which contains the JavaScript code for the applet.
It doesn’t matter if they’re executable, even though they often are.
There may also be additional *.js files.
Also, there might also be a po/ directory, which often contains .po and .pot files that are intended for localizing the text displayed to the user. These go along with the _() function in the JavaScript code. For the purposes of a simple applet, these are not necessary. Ignore these _(“Something”) things in the JavaScript code, and read them as just “Something”.
Some applets allow parameter setting. The runtime values for these are at ~/.cinnamon, which contains configuration data etc.
Two ways to object orient
Unfortunately, there are two styles for defining the applet class, both of which are used. This is a matter of minor confusion if you read the code of a few applets, and therefore worthy to note: Some of the applets use JavaScript class declarations (extending a built-in class), e.g.
class CinnamonSoundApplet extends Applet.TextIconApplet { constructor(metadata, orientation, panel_height, instanceId) { super(orientation, panel_height, instanceId);
and others use the “prototype” syntax:
MyApplet.prototype = { __proto__: Applet.IconApplet.prototype,
and so on. I guess they’re equivalent, despite the difference in syntax. Note that in the latter format, the constructor is a function called _init().
This way or another, all classes that employ timeout callbacks should have a destroy() method (no underscore prefix) to cancel them before quitting.
I wasn’t aware of these two syntax possibilities, and therefore started from the first applet I got my hands on. It happened to be written in the “prototype” syntax, which is probably the less preferable choice. I’m therefore not so sure my example below is a good starter.
Getting Started
It’s really about three steps to get an applet up and running.
- Create a directory in ~/.local/share/cinnamon/applets/ and put the two files there: metadata.json and applet.js.
- Restart Cinnamon. No, it’s not as bad as it sounds. See below.
- Install the applet to some panel, just like any other applet.
The development cycle (or: how to “run”)
None of the changes made in the applet’s directory (well, almost none) take any effect until Cinnamon is restarted, and when it is, everything is in sync. It’s not like a reboot, and it’s fine to do on the computer you’re working on, really. All windows remain in their workspaces (even though the windows’ tabs at the panel may change order). No reason to avoid this, even if you have a lot of windows opened. Done it a gazillion times.
So how to restart Cinnamon: ALT-F2, type “r” and Enter. Then cringe as your desktop fades away and be overwhelmed when it returns, and nothing bad happened.
If something is wrong with your applet (or otherwise), there a notification saying “Problems during Cinnamon startup” elaborating that “Cinnamon started successfully, but one or more applets, desklets or extensions failed to load”. From my own experience, that’s as bad as it gets: The applet wasn’t loaded, or doesn’t run properly.
Press Win+L (or ALT-F2, then type “lg” and Enter, or type “cinnamon-looking-glass” at shell prompt as non-root user) to launch the Looking Glass tool (called “Melange”). The Log tab is helpful with detailed error messages (colored red, that helps). Alternatively, look for the detailed error message in .xsession-errors in your home directory.
Note that the error message often appears before the line saying that the relevant applet was loaded.
OK, so now to some more specific topics.
Custom icons
Icons are referenced by their file name, without extension, in the JavaScript code as well as the metadata.json file (as “icon” assignment). The search path is the applet’s own icons/ subdirectory and the system icons, present at /usr/share/icons/.
My own experience is that creating an icons/ directory side-by-side with applet.js, and putting a PNG file named wifi-icon-off.png there makes a command like
this.set_applet_icon_name("wifi-icon-off");
work for setting the applet’s main icon on the panel. The PNG’s transparency is honored. The official file format is SVG, but who’s got patience for that.
Same goes with something menu items with icons:
item = new PopupMenu.PopupIconMenuItem("Access point off", "wifi-icon-off", St.IconType.FULLCOLOR); item.connect('activate', Lang.bind(this, function() { Main.Util.spawnCommandLine("/usr/local/bin/access-point-ctl off"); })); this.menu.addMenuItem(item);
My own experience with the menu items is that if the icon file isn’t found, Cinnamon silently puts an empty slot instead. JavaScript-style no fussing.
I didn’t manage to achieve something similar with the “icon” assignment in metadata.json, so the choices are either to save the icon in /usr/share/icons/, or use one of the system icons, or eliminate the “icon” assignment altogether from the JSON file. I went to the last option. This resulted in a dull default icon when installing the applet, but this is of zero importance for an applet I’ve written myself.
Running shell commands from JavaScript
The common way to execute a shell command is e.g.
const Main = imports.ui.main; Main.Util.spawnCommandLine("gnome-terminal");
The assignment of Main is typically done once, and at the top of the script, of course.
When the output of the command is of interest, it becomes slightly more difficult. The following function implements the parallel of the Perl backtick operator: Run the command, and return the result as a string. Note that unlike its bash counterpart, newlines remain newlines, and are not translated into spaces:
const GLib = imports.gi.GLib; function backtick(command) { try { let [result, stdout, stderr] = GLib.spawn_command_line_sync(command); if (stdout != null) { return stdout.toString(); } } catch (e) { global.logError(e); } return ""; }
and then one can go e.g.
let output = backtick("/bin/systemctl is-active hostapd");
after which output is a string containing the result of the execution (with a trailing newline, by the way).
As of December 2018, there’s no proper documentation of Cinnamon’s Glib wrapper, however the documentation of the C library can give an idea.
My example applet
OK, so here’s a skeleton applet for getting started with.
Its pros:
- It’s short, quite minimal, and keeps the mumbo-jumbo to a minimum
- It shows a simple drop-down menu display applet, which allows running a different shell command from each entry.
- It’s written in the less-preferable “prototype” syntax for defining objects.
- It does nothing useful. In particular, the shell commands it executes exist only on my computer.
- It depends on a custom icon (see “Custom Icons” above). Maybe this is an advantage…?
So if you want to give it a go, create a directory named ‘wifier@eli’ (or anything else?) in ~/.local/share/cinnamon/applets/, and put this as metadata.json:
{ "description": "Turn Wifi Access Point on and off", "uuid": "wifier@eli", "name": "Wifier" }
And this as applet.js:
const Applet = imports.ui.applet; const Lang = imports.lang; const St = imports.gi.St; const Main = imports.ui.main; const PopupMenu = imports.ui.popupMenu; const UUID = 'wifier@eli'; function ConfirmDialog(){ this._init(); } function MyApplet(orientation, panelHeight, instanceId) { this._init(orientation, panelHeight, instanceId); } MyApplet.prototype = { __proto__: Applet.IconApplet.prototype, _init: function(orientation, panelHeight, instanceId) { Applet.IconApplet.prototype._init.call(this, orientation, panelHeight, instanceId); try { this.set_applet_icon_name("wifi-icon-off"); this.set_applet_tooltip("Control Wifi access point"); this.menuManager = new PopupMenu.PopupMenuManager(this); this.menu = new Applet.AppletPopupMenu(this, orientation); this.menuManager.addMenu(this.menu); this._contentSection = new PopupMenu.PopupMenuSection(); this.menu.addMenuItem(this._contentSection); // First item: Turn on let item = new PopupMenu.PopupIconMenuItem("Access point on", "wifi-icon-on", St.IconType.FULLCOLOR); item.connect('activate', Lang.bind(this, function() { Main.Util.spawnCommandLine("/usr/local/bin/access-point-ctl on"); })); this.menu.addMenuItem(item); // Second item: Turn off item = new PopupMenu.PopupIconMenuItem("Access point off", "wifi-icon-off", St.IconType.FULLCOLOR); item.connect('activate', Lang.bind(this, function() { Main.Util.spawnCommandLine("/usr/local/bin/access-point-ctl off"); })); this.menu.addMenuItem(item); } catch (e) { global.logError(e); } }, on_applet_clicked: function(event) { this.menu.toggle(); }, }; function main(metadata, orientation, panelHeight, instanceId) { let myApplet = new MyApplet(orientation, panelHeight, instanceId); return myApplet; }
Next, create an “icons” subdirectory (e.g. ~/.local/share/cinnamon/applets/wifier@eli/icons/) and put a small (32 x 32 ?) PNG image there as wifi-icon-off.png, which functions as the applet’s top icon. Possibly download mine from here.
Anyhow, be sure to have an icon file. Otherwise there will be nothing on the panel.
Finally, restart Cinnamon, as explained above. You will get errors when trying the menu items (failed execution), but don’t worry — nothing bad will happen.
References
- A JavaScript object reference (there’s probably some “original” out there, but where?)
- The main Cinnamon reference page (don’t expect too much)
- Creating an Applet
- Writing an Applet
- Lang.bind() explained here.
Reader Comments
After you made changes to the applet code there is no need to restart Cinnamon. In Looking Glass (Extensions tab) just right click on your applet and select “Reload Code”. This is much faster than restarting Cinnamon. :-)
great work
could you put an example with an update loop? I can’t figure it out. Thanks!