Add back button to an action

Hello! I have been trying to add a back button to an accelerator in gtk4.
I am not sure if I am going in the right direction and would like some pointers here.

use gtk;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
use gtk::gio;

fn main() {
    let application = Application::builder()
        .application_id("com.example.FirstGtkApp")
        .build();

    application.connect_activate(|app| {
        let window = ApplicationWindow::builder()
            .application(app)
            .title("First GTK Program")
            .default_width(350)
            .default_height(70)
            .build();


        let back_action = gio::SimpleAction::new("back", None);
        back_action.connect_activate(|_, _| {
            eprintln!("Back!");
        });
        app.set_accels_for_action("app.back", &["<Alt>Left", "Back"]);
        app.add_action(&back_action);

        let button = Button::with_label("Click me!");
        button.connect_clicked(|_| {
            eprintln!("Clicked!");
        });
        window.set_child(Some(&button));
        window.show();
    });

    application.run();
}

I modified the example to include an action on the button press.
Here the action gets called if I use the Left combo, but not if I press the back button.
Should I go a different route binding it?

For me it is not easy to understand your post, I have no idea what a “back button” is.

When you just intent to connect a button and a key event to a gaction, you may follow the gaction0 example in

GTK4 for Graphical User Interfaces

Maybe you missed a line like

button.setActionName("win.save")

That example was based on a Python example, which may have vanished. In Rust all that is a bit more difficult, but should be similar.

Oh, I apologize! If you try it out yourself for a while it is hard to know what wont be evident for most people.

I want to bind the back button of the mouse basically.
In the example I bound the accellerator successfully to the action itself for the Left through:

 app.set_accels_for_action("app.back", &["<Alt>Left", "Back"]);

So If I press Alt+Left now it activates the action, but not on the back button.

I don’t want to connect a button, the button is just here because it is from the gtk4-rs example and I thought it would be easier for people to understand if it is something familiar.

This is a more minimal example:

use gtk;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
use gtk::gio;

fn main() {
    let application = Application::builder().build();

    application.connect_activate(|app| {
        let window = ApplicationWindow::builder()
            .application(app).build();
        let back_action = gio::SimpleAction::new("back", None);
        back_action.connect_activate(|_, _| {
            eprintln!("Back!");
        });
        app.set_accels_for_action("app.back", &["<Alt>Left", "Back"]);
        app.add_action(&back_action);

        window.show();
    });

    application.run();
}

I don’t think you can use set_accels_for_action to bind a mouse button event, you will have to add a gtk::GestureClick to the window.

1 Like

@jfrancis
Thank you so much, this is exactly what I was looking for!
Also thanks @StefanSalewski the blog is really nice!

The code as written probably still works just fine. It’s just that “back” is a keyboard button and not a mouse button.

E.g.: https://github.com/baedert/corebird/blob/master/src/Corebird.vala#L246

1 Like

Awesome, thanks for the info!

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