GApplication provides convenient life-cycle management by maintaining a “use count” for the primary application instance. The use count can be changed using g_application_hold() and g_application_release(). If it drops to zero, the application exits. Higher-level classes such as GtkApplication employ the use count to ensure that the application stays alive as long as it has any opened windows.
So you can use these hold/release APIs to keep your app alive when all windows are deleted.
You need to get the window’s GdkSurface with gtk_native_get_surface. This surface is actually a GdkToplevel , so you can connect to its notify::state signal to watch state changes, then in the signal callback check the GDK_TOPLEVEL_STATE_MINIMIZED flag in the result of gdk_toplevel_get_state.
I don’t know gtkmm so can’t translate this to c++, sorry
I haven’t tested this code. I don’t guarantee that it’s 100% correct.
Gtk::Window* window = ......;
Glib::RefPtr<Gdk::Surface> surface = window->get_surface();
// If surface implements the Gdk::Toplevel interface, it's actually a
// Gdk::ToplevelSurfaceImpl (a subclass of both Surface and Toplevel).
Glib::RefPtr<Gdk::Toplevel> toplevel = std::dynamic_pointer_cast<Gdk::Toplevel>(surface);
if (toplevel)
toplevel->property_state().signal_changed().connect([](){on_window_state_changed();});
else
something_wrong();
void on_window_state_changed()
{
if (toplevel->get_state() == Gdk::Toplevel::State::MINIMIZED)
handle_minimized();
}
“suspended” is not entirely useful; in GNOME, for instance, any “hidden” window will not be suspended, because the shell still needs to update the window contents in the overview, even if the window is, ostensibly, hidden.
When a window is hidden, gtkmm removes the window from the application.
This behavior is not what everyone expects. It was criticized in issue gtkmm#166.
Since gtkmm 4.21.2 you can use Gtk::Window::set_remove_on_hide(false) to avoid the removal.
set_auto_exit(false)? What’s that? Where did you find it?
When Emmanuele talks about GdkToplevel’s notify::state signal, and I talk about toplevel->property_state().signal_changed() we’re talking about the same signal,
one in C code, the other in C++ code.