0

I want to write a simple gnome extension that prints some text on my top bar from a text file. I managed to print the text but i'm having trouble with updating it every 60 seconds. Is it even possible with gjs?

this is what i came up with:

const {St, Clutter} = imports.gi;
const Main = imports.ui.main;
const GLib = imports.gi.GLib;



let panelButton;

function init () {
// Create a Button with "Hello World" text
panelButton = new St.Bin({
    style_class : "panel-button",
});

let fileContents = String(GLib.file_get_contents("path/to/myfile.txt")[1]);

let panelButtonText = new St.Label({
    text : fileContents,
    y_align: Clutter.ActorAlign.CENTER,
});
panelButton.set_child(panelButtonText);
}

function enable () {
// Add the button to the panel
Main.panel._centerBox.insert_child_at_index(panelButton, 2);
}

function disable () {
// Remove the added button from panel
Main.panel._centerBox.remove_child(panelButton);
}
karol
  • 1
  • if not going deep into the Question. there is a extension already. https://unix.stackexchange.com/a/659786/383311 – PRATAP Jul 27 '21 at 12:19

2 Answers2

2

You'll need to use GLib.timeout_add_seconds():

GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 60, () => {
   updateLabel(newText);
   return GLib.SOURCE_CONTINUE;
});

By the way, you should probably use ByteArray.toString() to convert the Uint8Array that you get from the file into a string.

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • This isn't working for me, and its driving me insane. Please check the below snippet: the timeout code runs the first time, but never afterwards ever. ``` function init() { _extensionButton = new PanelMenu.Button(0.0, 'temp', false); let panelButtonText = new St.Label({ text: "5/8", y_align: Clutter.ActorAlign.CENTER, }); _extensionButton.add_child(panelButtonText); GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 3, () => { log('Doesnt work' + String(i)); panelButtonText.set_text(String(i)) i += 3; }); } ``` – Nirmal Khedkar Feb 03 '22 at 12:46
  • 1
    I forgot `GLib.SOURCE_CONTINUE`. Please see the edit – ptomato Feb 05 '22 at 18:20
  • Thank you thank you thank you ... You dont realise how much I've been looking into why my `timeout_add_seconds` implementation never really ran the second time ... Thank you once more. Its so infuriating that such common stuff isnt documented in GNOME or GJS docs .. – Nirmal Khedkar Feb 06 '22 at 12:37
  • It is documented, though it's easy to miss: "The function is called repeatedly until it returns `false`" (in JS, returning `undefined` is a falsey value) – ptomato Feb 06 '22 at 14:33
1

If you want to have your extension approved by ego you'd possibly want to do it more like this (destroy in disable what you create in enable):

in extension.js:

let timeout = null;

in function enable():

    timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, (60 * 1000), () => {
        // ...do the right thing...
        return GLib.SOURCE_CONTINUE;
    });

in function disable():

    if (timeout) {
        GLib.Source.remove(timeout);
        timeout = null;
    }
Zappo-II
  • 21
  • 5