Trouble w/ event handling in GTK-RS

โš“ Rust    ๐Ÿ“… 2025-08-30    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 4      

surdeus

OK, so ...

I'm starting to develop a GUI application that will involve some pretty complex mouse interaction. The following code is just a simple experiment to try to understand how event handling works in GTK-RS.

    let ectrl1 = GestureClick::new();
    ectrl1.connect_pressed(move |_, np, _, _| {
        if np == 1 {
             timeout_add_local_once(Duration::from_millis(500), move || {
                 println!("ยซSingleClickยป");
             });
        } else {
            println!("MultiClick");
        }
    });
    ectrl1.connect_released(move |_, np, _, _| {
        println!("Released ({})", np);
    });
    ectrl1.connect_stopped(move |_| {
        println!("Stopped");
    });
    ectrl1.connect_unpaired_release(move |ec, _, _, _, _| {
        println!("Released [X]");
    });

    let ectrl2 = GestureLongPress::new();
    ectrl2.connect_pressed(move |_, _, _| {
        println!("LongPress|Pressed");
    });
    ectrl2.connect_cancelled(move |_| {
        println!("LongPress|Cancelled");
    });
    ectrl2.connect_delay_factor_notify(move |_| {
        println!("LongPress|DFN");
    });
    ectrl2.group_with(&ectrl1);
    button.add_controller(ectrl1);
    button.add_controller(ectrl2);

So, to briefly explain, I want this widget (which is actually a Label rather than a Button, but I call it button based on its role in the UI), to respond to at least the following:

  • SIngle click
  • Double click
  • Long press

When the user invokes the double click or long press handler, I want the single click action to not happen. But both of other gestures start with a single click. So there's a problem ...

Conceptually, I think what I need to do is to have the single-click handler dispatch a delayed callback (e.g., using source::timeout_add_local_once as shown in my sample code), and then have the double click and long press handlers cancel the invocation of that callback.

Assuming that that's the right approach, I need to get a reference to the callback that I can pass to the other event handlers. But I don't see how to do that. I thought that possibly using a SourceId and calling remove() on it might work (though I can't find any explanation of the effects of calling SourceId::remove() ...), but I haven't figured out how to pass those other closures it due to lifetime issues.

Am I on the right track? Or does GTK/Glib provide some simpler way to handle this, that I just haven't discovered yet? Please advise.documentation that explains

1 post - 1 participant

Read full topic

๐Ÿท๏ธ Rust_feed