Search code examples
javascriptgnome-3gnome-shell-extensions

How to make Gnome Shell extension query for changes


I've been battling the horrendous Gnome API documentation and came up with this extension:

const St = imports.gi.St;
const Main = imports.ui.main;
const Tweener = imports.ui.tweener;
const GLib = imports.gi.GLib;

let label;

function init() {
    label = new St.Bin({ style_class: 'panel-label' });

    let stuff = GLib.spawn_command_line_sync("cat /home/user/temp/hello")[1].toString();
    let text = new St.Label({ text: stuff });

    label.set_child(text);
}

function enable() {
    Main.panel._rightBox.insert_child_at_index(label, 0);
}

function disable() {
    Main.panel._rightBox.remove_child(label);
}

This should read whatever is in the hello file and display it in the top panel. However, if I change the contents of the hello file, I have to restart Gnome for that new content to be shown. Now, surely there is a way to do this dynamically but I just couldn't find anything in the documentation. The message in the panel should basically always mirror whatever is in the file. Any ideas how to do this?


Solution

  • You'll want to obtain a Gio.File handle for your hello file, and then monitor it:

    let helloFile = Gio.File.new_for_path('/home/user/temp/hello');
    let monitor = helloFile.monitor(Gio.FileMonitorFlags.NONE, null);
    monitor.connect('changed', function (file, otherFile, eventType) {
        // change your UI here
    });