Subscribe to active window changes

Is there a way to create a callback that will be called every time the active window changes, passing in information like class, title, etc.? I can find the current active window like this:

global.get_window_actors().find(x => x.get_meta_window().has_focus())

But I would like an event-based API to avoid having to call this repeatedly in a hot loop. Does such API exists?

That’s unnecessarily complicated. MetaDisplay has a focus-window property that you can use directly. You can use the notify signal to monitor changes:

global.display.connect('notify::focus-window',
    () => console.log(`Focus changed to ${global.display.focus_window}`));
1 Like

Thank you! Where can I find documentation about this? I can’t find them anywhere.

GJS docs: Meta.Display::focus-window

1 Like