繁体   English   中英

gjs/gnome-shell-extension:每 60 秒更新一次按钮文本

[英]gjs/gnome-shell-extension: Updating button text every 60 seconds

我想编写一个简单的 gnome 扩展,从文本文件在我的顶部栏上打印一些文本。 我设法打印了文本,但我无法每 60 秒更新一次。 甚至可以使用 gjs 吗?

这就是我想出的:

const {St, Clutter} = imports.gi;
const Main = imports.ui.main;
const GLib = imports.gi.GLib;



let panelButton;

function init () {
// Create a Button with "Hello World" text
panelButton = new St.Bin({
    style_class : "panel-button",
});

let fileContents = String(GLib.file_get_contents("path/to/myfile.txt")[1]);

let panelButtonText = new St.Label({
    text : fileContents,
    y_align: Clutter.ActorAlign.CENTER,
});
panelButton.set_child(panelButtonText);
}

function enable () {
// Add the button to the panel
Main.panel._centerBox.insert_child_at_index(panelButton, 2);
}

function disable () {
// Remove the added button from panel
Main.panel._centerBox.remove_child(panelButton);
}

你需要使用GLib.timeout_add_seconds()

GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 60, () => {
   updateLabel(newText);
});

顺便说一句,您可能应该使用ByteArray.toString()将从文件中获得的 Uint8Array 转换为字符串。

暂无
暂无

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

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