0

I am writing a Rust screen capture program using the gtk4 crate. It is very similar in concept to the Windows screen capture tool. The main problem is the function draw_area.

fn draw_area(window: &Window) -> Arc<Mutex<Coordinates>> {
    let draw_ctrl = GestureDrag::new();

    let coor: Arc<Mutex<Coordinates>> = Arc::new(Mutex::new(Coordinates {
        start_x: 0.0,
        start_y: 0.0,
        end_x: 0.0,
        end_y: 0.0,
    }));
    let condvar = Arc::new(Condvar::new());

    let thread_condvar = Arc::clone(&condvar);
    let thread_coor_begin = Arc::clone(&coor);
    draw_ctrl.connect_drag_begin(move |_, x, y| {
        let mut coor = thread_coor_begin.lock().unwrap();
        coor.start_x = x;
        coor.start_y = y;
        /*println!("START");
        println!(
            "start_x: {:?} start_y: {:?} end_x: {:?} end_y: {:?}",
            coor.start_x, coor.start_y, coor.end_x, coor.end_y
        );*/
        thread_condvar.notify_one();
    });

    let thread_condvar = Arc::clone(&condvar);
    let thread_coor_end = Arc::clone(&coor);
    draw_ctrl.connect_drag_end(move |_, x, y| {
        let mut coor = thread_coor_end.lock().unwrap();
        coor.end_x = coor.start_x + x;
        coor.end_y = coor.start_y + y;
        /*println!("END");
        println!(
            "start_x: {:?} start_y: {:?} end_x: {:?} end_y: {:?}",
            coor.start_x, coor.start_y, coor.end_x, coor.end_y
        );*/
        thread_condvar.notify_one();
    });

    window.child().unwrap().add_controller(draw_ctrl);
    coor
}

This function stores the click and drag coordinates of the cursor onto a window which works fine. The problem is that this function is called on a Button connect_clicked callback like this

button_new.connect_clicked(move |_| {
    let full_window = build_fullscreen_window();
    let coor = draw_area(&full_window);
    // other stuff
}

and, obviously, it does not wait if coor had been populated or not. What I want to do is having these callbacks being called exactly once each and having the // other stuff wait for coor to be populated.

I tried with counters on the struct Coordinates itself and having timeout/sleep (to check the counter) right after the let coor = draw_area(&full_window); line but it crashed the program or simply were not working.

0 Answers0