Override window close event to hide instead

I’m making an application that minimizes to the system tray. Is there a way to overrule the default window close button action and make it hide the window instead?

What speaks against closing the window?
If you intend to run the application without a window shown, you should split the application logic from the presentation in the window.

So when the window is closed you close the window, but let the application with the logic run in the background.

Some context: the application is designed around a GNOME Shell widget. The widget itself contains minimal logic and communicates with the application. The widget’s context menu contains an option to open the main window of the application. Users might close that window, which currently quits the host application and renders the widget useless.

What you suggest seems feels like a bit more hassle to me. If an interprocess call comes in to open the main window, it’s easier to unhide it than to possibly have to create a new one when the application receives a request to reveal the main window.

Well, cleanly splitting your code into “shell widget”, “GTK UI” and “application logic” would, in my view, still be the best design for the application.
Also because it will improve its maintainability later on.

That aside, AFAIK you can’t “hide” a window.
I also don’t think that neither Wayland nor X11 ever provided that in their protocols, so its not like GTK could just implement it.

That aside, AFAIK you can’t “hide” a window.
I also don’t think that neither Wayland nor X11 ever provided that in their protocols, so its not like GTK could just implement it.

I have successfully used ApplicationWindow’s set_visible used for this so far.

Well, cleanly splitting your code into “shell widget”, “GTK UI” and “application logic” would, in my view, still be the best design for the application.

This is already the case. The extension does a D-Bus call to reveal the window, the D-Bus server thread of the application forwards the request to a Sender, the GUI thread gets the Receiver end, it receives the event, it calls set_visible(true) on the window.

Use the GtkWindow::close-request signal: it’s exactly what you’re looking for.

Bingo, thanks! In my case, it was the Rust equivalent connect_close_request on the GtkWindowExt trait.