antoyo / relm

Idiomatic, GTK+-based, GUI library, inspired by Elm, written in Rust
MIT License
2.43k stars 78 forks source link

"Clicked" on Label #210

Closed MGlolenstine closed 4 years ago

MGlolenstine commented 4 years ago

How would one detect clicks on Label? There's no connect_clicked and there isn't one in the EventBox either.

Is there another widget I could use to detect the button press on a Label?

emmanueltouzery commented 4 years ago

I believe you'll need a gtk eventbox for that.

MGlolenstine commented 4 years ago

I figured as much, that's why I mentioned it... I did try to use it, but as it has no connect_xxx functions, I don't know how to use it.

Edit

I did ask on the gtk-rs github, and they answered that there must exist a function like this, but I don't see it or I'm missing something.

emmanueltouzery commented 4 years ago

hehe, I totally missed that you already mentioned eventbox.

I used that code in the past, worked fine:

    event_box
        .connect("button-press-event", false, |_| {
            println!("clicked");
            Some(true.to_value())
        })
        .unwrap();

there may be a nicer way though...

emmanueltouzery commented 4 years ago

I'm sorry I believe that code was raw gtk-rs.

This is relm:

    event_box
        .connect_local(
            // i don't understand the diff between connect and connect_local!!
            // but connect wants some multithread things that connect_local doesn't require
            "button-press-event",
            false,
            glib::clone!(@strong event => move |_| {
                clicked(&event);
                Some(true.to_value())
            }),
        )
        .unwrap();
MGlolenstine commented 4 years ago

Oh, so if the connect doesn't exist, we have to connect them manually.

Thanks!

MGlolenstine commented 4 years ago

From the connect method, I found that we can call 'static methods, but what if I wanted to call a method inside self -> The custom widget? I'd probably have to implement a custom Clone trait, but I'm wondering if there's another way without having to use the glib::clone!?

emmanueltouzery commented 4 years ago

sorry that's over my head at this point. I don't use event_box in my code anymore, too, i just copy-pasted some of my old code.

MGlolenstine commented 4 years ago

No matter, thanks for your help! If nothing else, you've pointed me in the right direction ;)

antoyo commented 4 years ago

@MGlolenstine: There's an example doing that.

MGlolenstine commented 4 years ago

Darn! I'm really getting lousy skimming through examples...

Thanks for your help!

antoyo commented 4 years ago

@MGlolenstine: If you have an idea for making them more discoverable, I'd be happy to make that change. One idea I had for the tutorial was to have one page describing the whole syntax of the view! {} macro with links to the respective tutorials. Maybe I could have a version of that now that points to the examples. But still, that would not have helped to find that specific example.

MGlolenstine commented 4 years ago

I'll think about it, but I'm not sure what the best way to do it would be.

One thing, that could be done (but it would take a lot of effort), is to have all RELM/gtk-rs widgets/elements and point them to their usage in the examples, like link them to their line with some explanation of the code surrounding it.