pub struct UiRunner<T> { /* private fields */ }
Expand description
Run code on the UI thread from another thread.
Allows you to mix non-blocking threaded code, with code that reads and updates your widget in the UI thread.
This can be copied and passed around.
Implementations§
Source§impl<T: 'static> UiRunner<T>
impl<T: 'static> UiRunner<T>
Sourcepub fn new(key: usize) -> Self
pub fn new(key: usize) -> Self
Create a new UiRunner
that dispatches functions as global actions but
differentiates them by the provided key
.
If used in a widget, prefer using your_widget.ui_runner()
.
If used in your app main, prefer using your_app_main.ui_runner()
.
Sourcepub fn handle(
self,
cx: &mut Cx,
event: &Event,
scope: &mut Scope<'_, '_>,
target: &mut T,
)
pub fn handle( self, cx: &mut Cx, event: &Event, scope: &mut Scope<'_, '_>, target: &mut T, )
Handle all functions scheduled with the key
of this UiRunner
.
You should call this once from your handle_event
method, like:
fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
// ... handle other stuff ...
self.ui_runner().handle(cx, event, scope, self);
}
Once a function has been handled, it will never run again.
Sourcepub fn defer(self, f: impl DeferCallback<T>)
pub fn defer(self, f: impl DeferCallback<T>)
Schedule the provided closure to run on the UI thread.
Sourcepub fn block_on<R: Send + 'static>(
self,
f: impl FnOnce(&mut T, &mut Cx, &mut Scope<'_, '_>) -> R + Send + 'static,
) -> R
pub fn block_on<R: Send + 'static>( self, f: impl FnOnce(&mut T, &mut Cx, &mut Scope<'_, '_>) -> R + Send + 'static, ) -> R
Like defer
, but blocks the current thread until the UI awakes, processes
the closure, and returns the result.
Generally, you should prefer to use defer
if you don’t need to communicate
a value back. This method may wait a long time if the UI thread is busy so you
should not use it in tight loops.