Window managment hidding

using:

gtkmm-4.0: Version: 4.20.0

glibmm-2.68: Version: 2.84.0

sigc+±3.0: Version: 3.6.0

gdk-pixbuf-2.0: Version: 2.42.12

gegl-0.4: Version: 0.4.62

babl-0.1: Version: 0.1.114

vips: Version: 8.16.1

exiv2: Version: 0.28.5

tbb: Version: 2022.1.0

i would like to create a window, do something, then hide/delete/close, then create another window.

my issue is when i hide/delete/close the app closes.

it seams that hiding and closing is the same in the window manager i am using.

where can i go to learn about how the manager works or a short sample code would be nice.

Thanks!!

1 Like

Hi,

When a GtkApplication doesn’t have windows anymore, it will automatically terminate.

Specifically this:

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.

1 Like

Thank you!

While working through this issue i was looking at trapping a notification of when the/a window is/has been minimized, is there a way to do this also??

Thanks again!!

1 Like

It’s a bit convoluted…

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 :slight_smile:

1 Like

i will give this a try and let you know what happens!!

Translation to C++/gtkmm.

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();
}
2 Likes

You don’t need that at all. Use the “notify” signal with these properties:

1 Like

Oh, thanks, I wasn’t aware of this suspended feature.

“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.

1 Like

it seams that hidding a window is the same a closing??, i tried hidding and the app closes, am i missing/not doing something correctly??

And Thanks for taking the time to replay and explain!!

How exactly do you hide the window?
Do you use a GtkApplication? if yes, do you assign the window to the application?

void Application::create_window(const Glib::RefPtr<Gio::File> &file) {

    g_print("Application: create window started\\n");

g_main_window = new Window();

add_window(*g_main_window);

g_main_window->signal_hide().connect([this]() { g_main_window->set_visible(false); });

g_print("Application: window added to application\\n");
}

i used prints to show the logic and it does see the state changes, so yes it works!!

THANKS!!

so i am doing a little more playing with the window hiding, minimize, and app closing.

i rain across ‘set_auto_exit(false);‘ is this working in gtkmm 4.20??

the AI says it should work???

or should i use the hold()??

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?

1 Like

Well, i am using vscode, and copilot chat gpt4.1, sometimes it gets a mind of its own.

It told me about set_auto_exit, and that was how it started!!

I have found using the hold() in the app works for this purpose.

I am still trying to learn now about using the notify Bassi talked about, which somewhere lead me to toplevel commands and that got really confusing.

all this makes me wonder if vscode is a good place to be, any thoughts??

Thanks for the time and responce!!

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.

I am actually talking about GtkWindow properties; there’s very little reason to ever go into GDK code to begin with.

Sorry, I misread. It was gwillems that mentioned GdkToplevel’s notify::state signal.

so just clearing out the squirrels,

i create a window as in:

Application::on_activate{

    g_main_window = new Window();

    add_window(*g_main_window);

}

somewhere in the dark places:

if(g_main_window.fullscreened/maximized/suspended) …. this will check/return true/flase??

is this the correct way??