Javascript 参考「;“窗口”;在Gnome外壳中用于Gnome扩展

Javascript 参考「;“窗口”;在Gnome外壳中用于Gnome扩展,javascript,gnome-shell,gnome-3,gnome-shell-extensions,gjs,Javascript,Gnome Shell,Gnome 3,Gnome Shell Extensions,Gjs,我想了解具有属性MetaWindow的变量“window”如何在不同的函数和变量中使用,而没有明确定义,例如let app=this.\u tracker.get\u window\u app(window)

我想了解具有属性
MetaWindow
的变量“window”如何在不同的函数和变量中使用,而没有明确定义,例如
let app=this.\u tracker.get\u window\u app(window)
就像这里:

var WindowAttentionHandler = class {
    constructor() {
        this._tracker = Shell.WindowTracker.get_default();
        this._windowDemandsAttentionId = global.display.connect('window-demands-attention',
                                                                this._onWindowDemandsAttention.bind(this));
        this._windowMarkedUrgentId = global.display.connect('window-marked-urgent',
                                                            this._onWindowDemandsAttention.bind(this));
    }

    _getTitleAndBanner(app, window) {
        let title = app.get_name();
        let banner = _("“%s” is ready").format(window.get_title());
        return [title, banner];
    }

    _onWindowDemandsAttention(display, window) {
        // We don't want to show the notification when the window is already focused,
        // because this is rather pointless.
        // Some apps (like GIMP) do things like setting the urgency hint on the
        // toolbar windows which would result into a notification even though GIMP itself is
        // focused.
        // We are just ignoring the hint on skip_taskbar windows for now.
        // (Which is the same behaviour as with metacity + panel)

        if (!window || window.has_focus() || window.is_skip_taskbar())
            return;

        let app = this._tracker.get_window_app(window);
        let source = new WindowAttentionSource(app, window);
        Main.messageTray.add(source);

        let [title, banner] = this._getTitleAndBanner(app, window);

        let notification = new MessageTray.Notification(source, title, banner);
        notification.connect('activated', () => {
            source.open();
        });
        notification.setForFeedback(true);

        source.showNotification(notification);

        source.signalIDs.push(window.connect('notify::title', () => {
            [title, banner] = this._getTitleAndBanner(app, window);
            notification.update(title, banner);
        }));
    }
};

变量
窗口
在建议的代码中未定义为
元窗口
,因为它使用方法
。从模块
全局显示中的侦听信号
窗口要求注意绑定
。方法
.bind
负责将类型
元窗口
作为
发送,以允许在其他函数中使用该类型,该窗口首先触发函数
\u onWindowDemandAttention

global.display.connect('window-demands-attention',this.\u onWindowDemandsAttention.bind(this))

该方法允许对象从中借用方法 没有复制该方法的另一个对象。这被称为 JavaScript中的函数借用

下面是一个基于建议代码的示例,用于在窗口需要注意时获得焦点:

var GetFocus = class {
    constructor() {
      this.focusID = global.display.connect('window-demands-attention', this._onWindowDemandsAttention.bind(this));
    }

    _onWindowDemandsAttention(display, window) {
         Main.activateWindow(window);
    }

    _destroy() {
        global.display.disconnect(this.focusID);
    }
}

函数以前不起作用,因为我缺少
.bind

你说的“而不是在代码中的任何地方定义[d]是什么意思?”?它就在参数列表中。@SebastianSimon,就像
let app=this.\u tracker.get\u window\u app(window)
在函数中定义[d],并通过回调传递给其他函数。我只是想知道“窗口”是从哪里来的。您的评论无助于澄清它。但是,
窗口
可以从参数列表中获得,就像
a
b
可以在
函数和(a,b){return a+b;}
中获得一样。运行时调用这些函数的任何部分都会提供适当的参数。如果调用
sum
,则调用
sum(3,5)
以获得
8
。你不会考虑<代码> < <代码> > <代码> b>代码>“未被定义”,是吗?同样,如果主机环境(例如Gnome外壳)调用
WindowAttentionHandler.prototype.\u onWindowDemandAttention
,它将使用适当的上下文和参数调用该函数。@SebastianSimon,就像我可以将
a
分配到
window
而不获取
错误一样:参数“metawin”应为MetaWindow类型的对象,但获取了未定义的类型。此错误来自何处?我看不出这有什么关系。
bind
只是创建了新函数;它还没有使用变为
window
的参数调用它
global.display.connect负责执行此操作。这就是错误的来源;你在之前的评论中没有解释这一点。删除
bind
会从函数中删除上下文。换句话说,
this
(在
this.\u onWindowDemandAttention
)将不再引用该
值。这就是
.bind
的目的。但是,这不是函数调用本身;它不是提供
显示
窗口
参数的东西。这就是我在之前的评论中所说的。删除
.bind
并不能证明什么<代码>绑定
不解释
窗口
。我也“不知道你出了什么问题”,但请遵循。顺便说一句,当传递包含上下文的方法时,这个
context.method.bind(context)
模式是必需的。塞巴斯蒂安西蒙用英语解释了这一点,更礼貌的是结束了这个问题。当所述副本没有解释如何将
window
类型对象定义为
MetaWindow
时,为什么要关闭该问题
.bind
是原因,这就是我想知道的。您当前的问题不包括任何有关错误或类型的内容。
.bind
调用只能确保在方法中使用正确的
this
。它不会在此处设置
窗口
参数。错误似乎与
窗口
参数无关
metawin
是另一回事。重复目标解释了两个参数
display
window
的来源:
global.display.connect
需要一个回调函数作为其第二个参数
global.display.connect
然后调用该函数;通过调用它,提供了两个参数。