lvgl / lv_binding_rust

LVGL bindings for Rust. A powerful and easy-to-use embedded GUI with many widgets, advanced visual effects (opacity, antialiasing, animations) and low memory requirements (16K RAM, 64K Flash).
MIT License
687 stars 71 forks source link

Button callbacks #5

Closed justinmoon closed 4 years ago

justinmoon commented 4 years ago

How can I define a button "on-click" callback?

I'm trying to add a button example and I'm stuck trying to type the callback to lvgl_sys::lvgl_obj_set_event_cb

justinmoon commented 4 years ago

It seems like lvgl_sys wants the callback to be an unsafe extern "C" function, which doesn't really make sense if I'm providing this callback from Rust ...

rafaelcaricio commented 4 years ago

Hi @justinmoon,

It is flattering that you have interest in using lvgl-rs. I just wanted to tell you that this project is in the very early stages of development. I would really appreciate help of others, like you, to push this project forward. Most things are still missing and I'm still pondering about how to handle the global state dependency in lvgl library rely.

justinmoon commented 4 years ago

No problem. I'll try to help out wherever I can.

rafaelcaricio commented 4 years ago

In general, I'm trying to follow the recommendations from those articles:

justinmoon commented 4 years ago

Thanks for sharing.

I'm trying to figure out how to get these event callbacks working. This post seems to explain roughly what is needed, but still working on it

justinmoon commented 4 years ago

On a related note, how were you able to get a button to look like this? image

Mine look like this image

Might help if you could add whatever code produced your screenshot as an example.

rafaelcaricio commented 4 years ago

@justinmoon that screenshot was in an earlier version of the demo app, https://github.com/rafaelcaricio/lvgl-rs/blob/7bf1ac0cae3b1867b4687efaf1ccbd9e321df54b/examples/demo/src/main.rs I was using SDL directly.

rafaelcaricio commented 4 years ago

Hey @justinmoon , I just implemented a way to add event callbacks. So you can do things when buttons are clicked, hovered, etc. Please look the example:

let mut button = Button::new(&mut screen);
button.on_event(|this, event| {
    if let lvgl::Event::Clicked = event {
        println!("Clicked!");
    }
});

https://github.com/rafaelcaricio/lvgl-rs/blob/master/examples/bar.rs#L48-L52

rafaelcaricio commented 4 years ago

I will close this ticket now, as this is now supported. If you have other questions. Please open other issue.