微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我想编写一个简单的 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 转换为字符串。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。