GNOME Shell扩展,等待新窗口打开

时间:2018-09-28 16:40:53

标签: gtk centos7 gnome-shell gnome-shell-extensions

我正在用JavaScript编写GNOME Shell扩展,以修改所有应用程序窗口。

作为练习,我首先要获取一个打开的窗口的应用程序名称并将其显示在面板中。

现在,在Looking Glass中,我可以通过键入

来获取所有打开的窗口
>>> global.screen.get_active_workspace().list_windows()
r(0) = [object instance proxy GType:MetaWindowX11 ...], ...

然后我可以通过键入

获得列表中第一个窗口的名称
>>> Shell.WindowTracker.get_default().get_window_app(r(0)[0]).get_name()
r(1) = <name of application>

但是,当我尝试在扩展程序的extension.js文件中执行此操作,然后重新启动GNOME Shell时,由于global.screen.get_active_workspace().list_windows()的结果为{{1 }}。

我以为这可能是因为我的扩展程序是在创建窗口之前执行的,所以我开始研究如何等到创建窗口后再在窗口上执行操作。

这是我真正陷入困境的地方。

在扩展程序的undefined函数中,我试图添加一个事件侦听器,该事件侦听器在接收到init()信号时运行我的函数update()

window-created信号来自MetaDisplay对象。

这是到目前为止我的代码:

window-created

代码编译没有错误,但是在创建新窗口时未调用我的函数let display = global.display; display.connect('window-created', Lang.bind(this, this.update));

有人知道这里发生了什么吗?我的语法错误吗?我应该使用其他信号吗?

任何帮助将不胜感激。

完整的extension.js文件

update()

1 个答案:

答案 0 :(得分:0)

我最终想出了解决问题的方法。问题在于我在听哪些信号以及在哪里听。经过大量研究,我还注意到大多数扩展都是面向对象的,因此我更改了代码以反映这一点。下面是我的新的,有效的,面向对象的代码。

const St = imports.gi.St;
const Main = imports.ui.main;
const Lang = imports.lang;
const Shell = imports.gi.Shell;

let button;

function MyApplication() {
    this._init();
}

MyApplication.prototype = {
    _init: function() {
        button = new St.Bin({ style_class: 'panel-button', 
                              reactive: true, 
                              can_focus: true, 
                              x_fill: true, 
                              y_fill: false, 
                              track_hover: true });
    },

    _update: function() {
        text = new St.Label({ text: Shell.WindowTracker.get_default().focus_app.get_name() });
        button.set_child(text);
    },

    enable: function() {
        Main.panel._rightBox.insert_child_at_index(button, 0);
        let callbackID = global.display.connect('notify::focus-window', Lang.bind(this, this._update));
    },

    disable: function() {
        Main.panel._rightBox.remove_child(button);
    }
};

function init() {
    return new MyApplication();
}
相关问题