i_slint_core::api

Function invoke_from_event_loop

Source
pub fn invoke_from_event_loop(
    func: impl FnOnce() + Send + 'static,
) -> Result<(), EventLoopError>
Expand description

Adds the specified function to an internal queue, notifies the event loop to wake up. Once woken up, any queued up functors will be invoked.

This function is thread-safe and can be called from any thread, including the one running the event loop. The provided functors will only be invoked from the thread that started the event loop.

You can use this to set properties or use any other Slint APIs from other threads, by collecting the code in a functor and queuing it up for invocation within the event loop.

If you want to capture non-Send types to run in the next event loop iteration, you can use the slint::spawn_local function instead.

See also Weak::upgrade_in_event_loop.

ยงExample

slint::slint! { export component MyApp inherits Window { in property <int> foo; /* ... */ } }
let handle = MyApp::new().unwrap();
let handle_weak = handle.as_weak();
let thread = std::thread::spawn(move || {
    // ... Do some computation in the thread
    let foo = 42;
     // now forward the data to the main thread using invoke_from_event_loop
    let handle_copy = handle_weak.clone();
    slint::invoke_from_event_loop(move || handle_copy.unwrap().set_foo(foo));
});
handle.run().unwrap();