How to hide a window?

I’m using a Gtk::Window. I want the ability to hide the entire window with the click of a button. I tried hide() as well as iconify() on the window but it did not hide?

Calling gtk_widget_hide() will hide any widget, including a top level like a GtkWindow.

If your window is not being hidden then it might mean you’re stopping the main loop from iterating to the next frame.

I was able to get it to work in this quick demo using window().unwrap().iconify(), which should work on any widget inside that window:

use gtk::prelude::*;
fn main() {
    let application =
        gtk::Application::new(Some("com.demo.demo"), Default::default());
    application.connect_activate(|app| {
        let window = gtk::ApplicationWindow::new(app);
        let button = gtk::Button::with_label("Click me!");
        button.connect_clicked(|b| b.window().unwrap().iconify());
        window.add(&button);
        window.show_all();
    });
    application.run();
}

Hey thanks for replying.
I am not sure why this is happening but @jfrancis, your code isn’t really working for me either. I had to make some changes since it wouldn’t compile otherwise so let me know if the error was in me translating your code.

use gtk::prelude::*;
use gdk::WindowExt;
use gtk::ButtonExt;
use gio::ApplicationExt;
use gio::prelude::ApplicationExtManual;

fn main() {
    let application =
        gtk::Application::new(Some("com.demo.demo"), Default::default()).unwrap();
    application.connect_activate(|app| {
        let window = gtk::ApplicationWindow::new(app);
        let button = gtk::Button::with_label("Click me!");
        button.connect_clicked(|b| b.get_event_window().unwrap().iconify());
        window.add(&button);
        window.show_all();
    });
    application.run(&["test".to_string()]);
}

The above code does not hide the window when I click the button.
For some more context, I’m running: Linux 5.12.1-arch1-1, Arch Linux. and this is my Cargo.toml dependencies:

[dependencies]
gdk = "0.13.2"
gio = "0.9.1"
gtk = "0.9.2"

Let me know if there’s any more info I can provide on my side.

Oh sorry, I was using this as my Cargo.toml dependency because the versioned crates seem to be out of date:

gtk = { git = "https://github.com/gtk-rs/gtk3-rs" }

In any case, don’t use get_event_window, the function you want should be called either window or get_window.

The get_event_window() method does not do what you think it does: it returns the gdk::Window that the gtk::Button uses for the event handling. You’re trying to hide the input surface, not the top level window that contains the button.

If you want to hide the top level window that contains the button, use get_toplevel().

The get_window() method on gtk::WidgetExt returns the gdk::Window used by the widget, which is definitely not what you want to hide.

Hey @ebassi, just a follow up question. Is this hide function non blocking? If yes, is there any callback I can get for when the window is hidden?

Hiding is instantaneous from the toolkit perspective, but the request to not draw the window is sent to the window manager, which will decide what to do with it according to its own redraw loop.

There is no notification for the application.

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