rust-native-ui / libui-rs

Rust bindings to the minimalist, native, cross-platform UI toolkit `libui`
Apache License 2.0
932 stars 73 forks source link

[usage] Externally trigger UI update #113

Open EinDev opened 3 years ago

EinDev commented 3 years ago

How can i trigger a simple Label to update from another thread? As soon as i enter the event loop the thread becomes blocking, until a event is called. But as far as i can see these events are only internal events such as mouse move events.

If i want to use EventLoop#next_tick, i need the UI which is a Rc<iui::ui::UIToken which i can only call from the owning thread. If i want to update the Label, the same occurs. How can i change it?

Maybe i did understand something wrong, if so feel free to correct me :)

Also another question: The Label seems to flash quickly as i update it. Is this expected behaviour?

duffrecords commented 3 years ago

I use Tokio watch channels to communicate between tasks. I spawn an asynchronous task and pass the sender half of the channel as one of its arguments, and keep the receiver half in the UI event loop. In the loop, I use a Tokio select macro with a branch that checks the channel for changes (usually several of these, depending on the UI design) and a default branch that sleeps asynchronously for 5 ms and then executes the next tick in the event loop. The updated UI component will retain its value until something new is sent through the channel. I haven't had to use any reference counters with this pattern.