4

我一直在与可怕的 Gnome API 文档作斗争,并想出了这个扩展:

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

这应该读取hello文件中的任何内容并将其显示在顶部面板中。但是,如果我更改hello文件的内容,我必须重新启动 Gnome 才能显示新内容。现在,肯定有一种方法可以动态地执行此操作,但我在文档中找不到任何内容。面板中的消息基本上应该始终反映文件中的任何内容。任何想法如何做到这一点?

4

2 回答 2

5

您需要获取文件的Gio.File句柄hello,然后对其进行监视

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
});
于 2013-09-28T05:57:06.483 回答
0

这对我有用。它将每 30 秒刷新一次标签值。

  • 添加以下导入

    const Mainloop = 进口.mainloop;

  • 在你的 init 方法中

    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});
    
于 2016-02-15T12:27:33.860 回答