【问题标题】:gjs/gnome-shell-extension: Updating button text every 60 secondsgjs/gnome-shell-extension:每 60 秒更新一次按钮文本
【发布时间】:2021-10-02 16:27:06
【问题描述】:

我想编写一个简单的 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);
}

【问题讨论】:

标签: gnome gnome-shell gnome-shell-extensions gjs


【解决方案1】:

如果你想拥有你的扩展approved by ego,你可能想要做更多这样的事情(破坏你在启用中创建的内容):

extension.js:

let timeout = null;

function enable():

    timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, (60 * 1000), () => {
        // ...do the right thing...
        return GLib.SOURCE_CONTINUE;
    });

function disable():

    if (timeout) {
        GLib.Source.remove(timeout);
        timeout = null;
    }

【讨论】:

    【解决方案2】:

    您需要使用GLib.timeout_add_seconds():

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

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

    【讨论】:

    • 这对我不起作用,它让我发疯。请检查下面的 sn-p:超时代码第一次运行,但以后永远不会运行。 ``` function init() { _extensionButton = new PanelMenu.Button(0.0, 'temp', false);让 panelButtonText = new St.Label({ text: "5/8", y_align: Clutter.ActorAlign.CENTER, }); _extensionButton.add_child(panelButtonText); GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 3, () => { log('不工作' + String(i)); panelButtonText.set_text(String(i)) i += 3; }); } ```
    • 我忘记了GLib.SOURCE_CONTINUE。请查看编辑
    • 谢谢谢谢谢谢...您没有意识到我一直在研究为什么我的timeout_add_seconds 实现从未真正运行第二次...再次感谢您。令人气愤的是,GNOME 或 GJS 文档中没有记录这些常见的东西..
    • 有据可查,虽然很容易漏掉:“函数被重复调用,直到返回false”(在JS中,返回undefined是一个假值)
    猜你喜欢
    • 2012-01-09
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多