Cant create own applet

Please post suggestions for improvement of Cinnamon on:
https://github.com/linuxmint/Cinnamon
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
fehlfarbe

Cant create own applet

Post by fehlfarbe »

Hi,

I'm trying to create a simple Cinnamon panel applet that shows the current process of my Octoprint jobs. I tried the tutorial from http://projects.linuxmint.com/referenc ... pplet.html and modified it but I just can't get it work.

There is always this error message in ~/.xsession-errors when I add the applet to a panel:

Code: Select all

[progress@octoprint]: Failed to evaluate 'main' function on applet: progress@octoprint/100
Cjs-Message: 23:36:55.289: JS LOG: [LookingGlass/trace] 
<----------------
_getPanelInfo@/usr/share/cinnamon/js/ui/applet.js:219:19
_init@/usr/share/cinnamon/js/ui/applet.js:197:13
_init@/usr/share/cinnamon/js/ui/applet.js:659:9
anonymous/MyApplet.prototype._init@/home/kolbe/.local/share/cinnamon/applets/progress@octoprint/applet.js:18:9
MyApplet@/home/kolbe/.local/share/cinnamon/applets/progress@octoprint/applet.js:11:5
main@/home/kolbe/.local/share/cinnamon/applets/progress@octoprint/applet.js:53:12
createApplet@/usr/share/cinnamon/js/ui/appletManager.js:581:18
addAppletToPanels@/usr/share/cinnamon/js/ui/appletManager.js:356:22
onEnabledAppletsChanged@/usr/share/cinnamon/js/ui/appletManager.js:296:9
---------------->
Cjs-Message: 23:36:55.296: JS LOG: [LookingGlass/error] 
[progress@octoprinte]: [Applet] Unable to find definition for applet 30
[progress@octoprinte]: Failed to evaluate 'main' function on applet: progress@octoprinte/106
Cjs-Message: 23:36:55.296: JS LOG: [LookingGlass/trace] 
<----------------
_getPanelInfo@/usr/share/cinnamon/js/ui/applet.js:219:19
_init@/usr/share/cinnamon/js/ui/applet.js:197:13
_init@/usr/share/cinnamon/js/ui/applet.js:789:9
anonymous/MyApplet.prototype._init@/home/kolbe/.local/share/cinnamon/applets/progress@octoprinte/applet.js:68:9
MyApplet@/home/kolbe/.local/share/cinnamon/applets/progress@octoprinte/applet.js:61:5
main@/home/kolbe/.local/share/cinnamon/applets/progress@octoprinte/applet.js:104:20
createApplet@/usr/share/cinnamon/js/ui/appletManager.js:581:18
addAppletToPanels@/usr/share/cinnamon/js/ui/appletManager.js:356:22
onEnabledAppletsChanged@/usr/share/cinnamon/js/ui/appletManager.js:296:9
---------------->
Cjs-Message: 23:36:55.828: JS LOG: Unknown network device type, is 14
Cjs-Message: 23:36:55.843: JS LOG: Unknown network device type, is 13
My code is at: ~/.local/share/cinnamon/applets/progress@octoprinte/

applet.js:

Code: Select all

const Applet = imports.ui.applet;
const GLib = imports.gi.GLib;
const Mainloop = imports.mainloop;
const PopupMenu = imports.ui.popupMenu;
const Settings = imports.ui.settings;
const UUID = "progress@octoprinte";
const Util = imports.misc.util;


function MyApplet(metadata, orientation, panelHeight, instance_id) {
    this._init(metadata, orientation, panelHeight, instance_id);
}

MyApplet.prototype = {
    __proto__: Applet.TextApplet.prototype,

    _init: function (metadata, orientation, panelHeight, instance_id) {
        Applet.TextApplet.prototype._init.call(this, metadata, orientation, panelHeight, instance_id);

        // settings
        try {
            this.settings = new Settings.AppletSettings(this, UUID, instance_id);

            this.settings.bindProperty(Settings.BindingDirection.IN,
                "server",
                "server",
                this.on_settings_changed,
                null);
            this.settings.bindProperty(Settings.BindingDirection.IN,
                "basic_auth",
                "basic_auth",
                this.on_settings_changed,
                null);
        } catch (e) {
            global.logError("Cannot load settings");
        }

        this.set_applet_tooltip(("Progress"));
    },

    on_applet_clicked: function() {
        Util.spawn('nemo');
    },

    on_settings_changed: function () {
        global.logError("Settings changed");
    },
};

function main(metadata, orientation, panelHeight, instance_id) {
    let myApplet = new MyApplet(metadata, orientation, panelHeight, instance_id);
    return myApplet;
}
metadata.json:

Code: Select all

{
    "uuid": "progress@octoprinte",
    "name": "Octoprint Progress",
    "version": "0.1",
    "description": "OctoPrint progress",
    "icon": "force-exit",
    "max-instances": "-1",
    "multiversion": true
}
settings-schema.json:

Code: Select all

{
  "server": {
    "type" : "entry",
    "default" : "http://141.56.7.88",
    "description" : "Server"
  },
  "api_key": {
    "type" : "entry",
    "default" : "XXXXXXXXXXXXXXXXXX",
    "description" : "API key"
  },
  "basic_auth": {
    "type" : "entry",
    "default" : "xxx:xxx",
    "description": "Credentials for basic authentication"
  }
}
I'm running Linux Mint 19.1 with Cinnamon 4.0.9

Any idea what's wrong with my code?
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.
Hoser Rob
Level 20
Level 20
Posts: 11796
Joined: Sat Dec 15, 2012 8:57 am

Re: Cant create own applet

Post by Hoser Rob »

What mods did you make? Have you written javascript code before? If not I don't think this'd be the best instro.
For every complex problem there is an answer that is clear, simple, and wrong - H. L. Mencken
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Cant create own applet

Post by rene »

fehlfarbe wrote: Wed Jan 16, 2019 6:55 pm Any idea what's wrong with my code?
You are as a difference with the example code passing "metadata" up from main() and passing it to Applet.TextApplet.prototype._init which however of course is not prepared for that. Certainly you are free to pass "metadata" to MyApplet.prototype._init (supposedly to parse the uuid out?) but you can not just expect an existing function to dynamically update its prototype.

You'll by the way also need to say Util.spawn(['nemo']) rather than simply Util.spawn('nemo'); spawn() expects an argv-type array of strings.

That as, as a unidiff,

Code: Select all

rene@t5500:~$ diff -u .local/share/cinnamon/applets/progress@octoprinte/applet.js{.orig,}
--- .local/share/cinnamon/applets/progress@octoprinte/applet.js.orig	2019-01-17 17:59:18.040572328 +0100
+++ .local/share/cinnamon/applets/progress@octoprinte/applet.js	2019-01-17 17:57:04.238484276 +0100
@@ -15,7 +15,7 @@
     __proto__: Applet.TextApplet.prototype,
 
     _init: function (metadata, orientation, panelHeight, instance_id) {
-        Applet.TextApplet.prototype._init.call(this, metadata, orientation, panelHeight, instance_id);
+        Applet.TextApplet.prototype._init.call(this, orientation, panelHeight, instance_id);
 
         // settings
         try {
@@ -39,7 +39,7 @@
     },
 
     on_applet_clicked: function() {
-        Util.spawn('nemo');
+        Util.spawn(['nemo']);
     },
 
     on_settings_changed: function () {
Although it with these changes works for me, I believe I should also comment on the UUID in theory wanting to be a valid URI; i.e., that you should make it be progress@octoprint.org or more likely progress@fehlfarbe.local or some such. The "force-exit" icon doesn't exist for me on 19.1 by the way.

NOTE: I while editing experienced that removing and re-adding the applet was not enough for changes to be picked up; I needed to log out and back in after editing applet.js.
fehlfarbe

Re: Cant create own applet

Post by fehlfarbe »

I used JavaScript for different webprojects some years ago so I'm a bit out of practice.

Thanks @rene - it's working when I change to:

Code: Select all

Applet.TextApplet.prototype._init.call(this, orientation, panelHeight, instance_id);
I also renamed my project to octoprint@fehfarbe.local and the settings window is also working :)

Is there somewhere a more detailed documentation for the different Applet classes or more tutorials?
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Cant create own applet

Post by rene »

There is something here for 3.2.7:

http://lira.epac.to:8080/doc/cinnamon/c ... index.html

which should be here in its current form:

http://projects.linuxmint.com/referenc ... nnamon-js/

but isn't.
fehlfarbe

Re: Cant create own applet

Post by fehlfarbe »

Thanks again! I also found some infos about directory structure and so on at https://github.com/linuxmint/cinnamon-spices-applets

If someone is interested, my applet is available at: https://github.com/fehlfarbe/octoprint-applet
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: Cant create own applet

Post by rene »

Looking nice.

In the context of the original issue, I do see you are not in fact using "metadata" in MyApplet; note then that you can/could just not pass that up from main through MyApplet() and through to _init(). Not important either though...
Locked

Return to “Cinnamon”