rustgtkgtk4gtk-rs

Triggering code after the window has been shown


I'm using gtk-rs. I want to trigger some code after the window has been shown (has displayed on the screen):

window.connect_show(clone!(@weak window => move |_| {
    let command = format!("sleep 0.1; wmctrl -r \"CSS\" -e 1,640,100,680,768");
    println!("2");
    run_command(&command);
}));

println!("1");
window.show();
println!("3");

This will print: 1, 2, 3. Meaning that connect_show is triggering right before window.show();

This won't let the command wmctrl -r \"CSS\" -e 1,640,100,680,768" reposition and resize the window. A delay is needed to accomplish that:

"sleep 0.1; wmctrl -r \"CSS\" -e 1,640,100,680,768"

Is there another way to make the command work without having to use sleep 0.1?

Here is the whole code.


Solution

  • For some reason, the wmctrl commands is successful if I use glib::idle_add:

    window.connect_show(clone!(@weak window => move |_| {
        glib::idle_add(|| {
            // the code
        glib::Continue(false)
    });