JS API: how to detect when window was closed?

In my extension I keep additional settings per each window in shared object and I would like to garbage collect settings when window is closed. What is the best event to listen to?

The unmanaged signal seemed like good idea, but this event is fired only when window is closed in preview mode.

let meta = this._windowClone.metaWindow;
meta.connect('unmanaged', function(window) {}

What is the right way to capture the event of window being closed?

If this can help anyone, here’s the source code of the extension.

1 Like

Maybe you want Shell.WM::destroy?

Side note, you will want to be disconnecting your signals by passing the returned handler id, not the signal name:

const handlerId = object.connect('signal-name', () => {}); 

// Correct
object.disconnect(handlerId);

// Incorrect
object.disconnect('signal-name');

Thanks for the advice with disconnecting signals!

As for the destroy signal, I tried connecting to Shell.WM::destroy, but this one is sent every time I enter the list of the windows (press the super key). Window manager must be destroying all the windows in the workspace and creating their previews I think.

But how to reach the application that is running those windows? Should I check PID of the application? Yet this would seem too low level in my opinion, so there must be a better way.

It looks likes the windowClone actor also has a ::destroy signal, so maybe try that one?

No, MetaWindow doesn’t know or care about the overview, so the unmanaged signal is emitted whenever the window is unmanaged.

But as your extension extends the window overlay in the overview, I assume you disconnect the signal in _onDestroy()? Both window previews and their overlays are destroyed after leaving the overview, so that would explain the behavior you are seeing.

Nice, that was indeed the reason. As @fmuellner mentioned, connecting to destroy signal in WindowOverlay constructor was ineffective, because whole overlay was destroyed when leaving overview. The binding didn’t live long enough.

Connecting to signal in the enable() and disconnecting in disable() solves my problem.

function enable() {
    // (...)
    let wm = global.window_manager;
    wmHandler = wm.connect('destroy', function(_shellwm, actor) {
        let metaName = actor.meta_window.toString();
        delete windowConfigs[metaName];
    });

Thanks everyone for help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.