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?

有帮助吗?

解决方案

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
});

其他提示

This worked for me. It will refresh the label value each 30 seconds.

  • Add the following import

    const Mainloop = imports.mainloop;

  • In your init method

    Mainloop.timeout_add(30000, function () { 
     let stuff = GLib.spawn_command_line_sync("your_command")[1].toString();
     let label = new St.Label({ text: stuff });
     button.set_child(label);return true});
    
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top