I want to understand how the variable "window" which has the attribute MetaWindow
can be used in different functions and vars while not being explicitly defined such as let app = this._tracker.get_window_app(window);
and then passed on to other functions through a callback. Reference code here: windowAttentionHandler.js
Like here:
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);
}));
}
};
The variable
window
is not defined asMetaWindow
in the suggested code because it borrows such status using the method.bind
from the listening signalwindow-demands-attention
in the moduleglobal.display
. The method.bind
is responsible for sending the typeMetaWindow
asthis
allowing it to be used in other functions with reference to the window which triggered the function_onWindowDemandsAttention
in the first place:global.display.connect('window-demands-attention', this._onWindowDemandsAttention.bind(this));
Here is an example based on the suggested code for getting the focus when the window demands the attention:
The function was not working before because I was missing the
.bind
.