繁体   English   中英

如何使Gnome Shell扩展查询更改

[英]How to make Gnome Shell extension query for changes

我一直在与可怕的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才能显示新内容。 现在,肯定有一种方法可以动态地执行此操作,但我在文档中找不到任何内容。 面板中的消息基本上应始终镜像文件中的任何内容。 任何想法如何做到这一点?

您将需要获取hello文件的Gio.File句柄,然后监视它:

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

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

  • 添加以下导入

    const Mainloop = imports.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}); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM