2013-09-26 31 views
2

我一直在爭奪可怕的侏儒API文檔以及與此延伸過來的Gnome外殼擴展查詢:如何使更改

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

你想獲得一個Gio.File手柄爲您hello文件,然後monitor它:

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

這是偉大的!我希望這個東西被清楚地記錄在某處:(還有一個問題:如何監視命令的輸出?假設我想運行ping並在頂部面板中顯示每個數據包的時間?感謝您的幫助! – mart1n

+0

這就是值得一個完整的問題和答案,但這個C代碼可能讓你開始:https://github.com/ptomato/gnome-inform7/blob/master/src/spawn.c#L215說到C代碼,不幸的是,您正確的GJS文檔的最佳選擇是閱讀C API:https://developer.gnome.org/gio/stable/GFileMonitor.html#GFileMonitor-changed – ptomato

0

這爲我工作。它會每30秒刷新一次標籤值。

  • 添加以下進口

    const的主循環= 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});