-
I am learning Rust because of Slint. After some experimentation I got this pattern to work in test Slint/Rust app. My question is, does Slint have built in facilities for that or is one expected to use I am currently using it like this (excerpt) : let ui = AppWindow::new()?;
let ui_a = Arc::new(ui);
let ui_handle = ui_a.as_weak();
let (tx, rx) = std::sync::mpsc::channel::<i32>();
let sender = Arc::new(Mutex::new(tx));
let mailbox_processor = async_std::task::block_on(
{
let sender = sender.clone();
mb(
Arc::new(
{
let sender = sender.clone();
move |state| {
let sender = sender.clone();
println!("State updated to: {}", state);
std::thread::spawn(move || {
sender.lock().unwrap().send(state).expect("Failed to send update");
});
}
}
)
)
}
);
// message polling thread
thread::spawn(move || {
for received in rx {
println!("Received: {}", received);
// send msg to main (thread)
let ui_handle_clone = ui_handle.clone();
let _ = slint::invoke_from_event_loop(move || {
if let Some(ui) = ui_handle_clone.upgrade() {
ui.set_counter(received);
}
});
}
}); Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The problem with the code you gave is that it doesn't seem to run the event loop in the main thread. You probably shouldn't call async_std::task::block_on in the main thread. |
Beta Was this translation helpful? Give feedback.
The problem with the code you gave is that it doesn't seem to run the event loop in the main thread. You probably shouldn't call async_std::task::block_on in the main thread.
Instead, you can use slint::spawn_local https://docs.rs/slint/latest/slint/fn.spawn_local.html to run a future in the main event loop. But don't forget to call slint::run_event_loop after spawning your thread