I'm looking to execute some code just after my Gtk3 window closes. I've tried delete-event
, destroy
, and destroy-event
which seems to not fire at all.
I get that there's some difference between Gdk and Gtk, but I'm not exactly sure about what that is. I assume I'm asking about the right thing.
I'm creating the window with the node.js binding of gtk_window_new()
, and closing it with gtk_window_close()
. When doing that, and using the events mentioned above, the event handlers fire while the window is still on the screen.
edit:
As requested, some example code:
const gi = require('node-gtk')
const Gtk = gi.require('Gtk', '3.0');
gi.startLoop();
Gtk.init();
const win = new Gtk.Window();
win.on('destroy', () => Gtk.mainQuit());
win.on('delete-event', () => false);
const button = new Gtk.Button({ label: 'Do it' });
win.add(button);
button.on('clicked', function () {win.close();});
win.showAll();
Gtk.main();
shell_command("wmctrl -lG");
function shell_command(cmd_str) {
const execSync = require('child_process').execSync;
code = execSync(cmd_str);
return code.toString();
}
On my Linux Mint 19.3 system, the above code creates a small window with 1 button. Clicking the button crashes execution with the following terminal error:
Error: Command failed: wmctrl -lG
X Error of failed request: BadWindow (invalid Window parameter)
Major opcode of failed request: 20 (X_GetProperty)
Resource id in failed request: 0x6c00003
Serial number of failed request: 26
Current serial number in output stream: 26
The window id 0x6c00003
is the window id of the window created by the above code, as proven by running wmctrl after running the script, but before pressing the button.
If I simply put the the call to wmctrl
in a 50ms timer, it works without error.
setTimeout(function () {shell_command("wmctrl -lG");}, 50);
This is what leads me to believe it's a race condition. I think wmctrl
is getting window ids when my window still exists, but then further querying against those ids after my window has closed, and it causes the above error. That's my theory, and if it's correct, Gtk.main()
returns before the window is truly gone (and the same is true of the other events mentioned).