Getting "button pressed" and "released" events for a GtkScale in GTK4

Hi. I am attempting to get “button pressed” and “button released” events for a GtkScale, in order to implement a nice-to-use progress bar with seeking functionality for a music application. Specifically, I want to use the signals to disable and later re-enable the notifications from the application backend that update the slider position periodically.

In GTK3, I did this with button-press-event and button-release-event, and it worked well. When attempting to build this again with GTK4, I was directed to the migration guide, and tried to use an event controller as per its recommendations.

However, I am having some trouble with this. Specifically, my event controller does not get the “released” event, only the “pressed” one.

I don’t know C, so I prepared some examples that reproduce both behaviors using the Rust bindings:

GTK 3
[dependencies]
gtk = "0.15.4"
use gtk::prelude::*;

fn main() {
    let app = gtk::Application::builder()
        .application_id("org.gtk-rs.example")
        .build();

    app.connect_activate(build_ui);

    app.run();
}

fn build_ui(app: &gtk::Application) {
    let adj = gtk::Adjustment::new(0.0, 0.0, 100.0, 0.0, 0.0, 0.0);
    let scale = gtk::Scale::new(gtk::Orientation::Horizontal, Some(&adj));
    scale.set_visible(true);

    scale.connect_button_press_event(|_, _| {
        println!("pressed");
        gtk::Inhibit(false)
    });

    scale.connect_button_release_event(|_, _| {
        println!("released");
        gtk::Inhibit(false)
    });

    let window = gtk::ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .default_width(400)
        .default_height(100)
        .child(&scale)
        .build();

    window.present();
}

GTK 4
[dependencies]
gtk4 = "0.4.7"
use gtk4::prelude::*;

fn main() {
    let app = gtk4::Application::builder()
        .application_id("org.gtk-rs.example")
        .build();

    app.connect_activate(build_ui);

    app.run();
}

fn build_ui(app: &gtk4::Application) {
    let adj = gtk4::Adjustment::new(0.0, 0.0, 100.0, 0.0, 0.0, 0.0);
    let scale = gtk4::Scale::new(gtk4::Orientation::Horizontal, Some(&adj));

    let gesture = gtk4::GestureClick::new();

    gesture.connect_pressed(|_, _, _, _| {
        println!("pressed");
    });

    gesture.connect_released(|_, _, _, _| {
        println!("released");
    });

    scale.add_controller(&gesture);

    let window = gtk4::ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .default_width(400)
        .default_height(100)
        .child(&scale)
        .build();

    window.present();
}

I am also not the only one impacted by this, the progress bar from GNOME Music has the same problem: https://gitlab.gnome.org/GNOME/gnome-music/-/issues/517.

I would appreciate any suggestions on how to fix this.

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