Struct druid::ExtEventSink
source · pub struct ExtEventSink { /* private fields */ }
Expand description
A thing that can move into other threads and be used to submit commands back to the running application.
This API is preliminary, and may be changed or removed without warning.
Implementations§
source§impl ExtEventSink
impl ExtEventSink
sourcepub fn submit_command<T: Any + Send>(
&self,
selector: Selector<T>,
payload: impl Into<Box<T>>,
target: impl Into<Target>
) -> Result<(), ExtEventError>
pub fn submit_command<T: Any + Send>( &self, selector: Selector<T>, payload: impl Into<Box<T>>, target: impl Into<Target> ) -> Result<(), ExtEventError>
Submit a Command
to the running application.
Command
is not thread safe, so you cannot submit it directly;
instead you have to pass the Selector
and the payload
separately, and it will be turned into a Command
when it is received.
The payload
must implement Any + Send
.
If the Target::Auto
is equivalent to Target::Global
.
sourcepub fn add_idle_callback<T: 'static + Data>(
&self,
cb: impl FnOnce(&mut T) + Send + 'static
)
pub fn add_idle_callback<T: 'static + Data>( &self, cb: impl FnOnce(&mut T) + Send + 'static )
Schedule an idle callback.
T
must be the application’s root Data
type (the type provided to AppLauncher::launch
).
Add an idle callback, which is called (once) when the message loop is empty. The idle callback will be run from the main UI thread.
Note: the name “idle” suggests that it will be scheduled with a lower priority than other UI events, but that’s not necessarily the case.
Examples found in repository?
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
fn generate_colors(event_sink: druid::ExtEventSink) {
// This function is called in a separate thread, and runs until the program ends.
// We take an `ExtEventSink` as an argument, we can use this event sink to send
// commands to the main thread. Every time we generate a new colour we send it
// to the main thread.
let start_time = Instant::now();
let mut color = Color::WHITE;
loop {
let time_since_start = (Instant::now() - start_time).as_nanos();
let (r, g, b, _) = color.as_rgba8();
// there is no logic here; it's a very silly way of mutating the color.
color = match (time_since_start % 2, time_since_start % 3) {
(0, _) => Color::rgb8(r.wrapping_add(3), g, b),
(_, 0) => Color::rgb8(r, g.wrapping_add(3), b),
(_, _) => Color::rgb8(r, g, b.wrapping_add(3)),
};
// schedule idle callback to change the data
event_sink.add_idle_callback(move |data: &mut Color| {
*data = color;
});
thread::sleep(Duration::from_millis(20));
}
}
Trait Implementations§
source§impl Clone for ExtEventSink
impl Clone for ExtEventSink
source§fn clone(&self) -> ExtEventSink
fn clone(&self) -> ExtEventSink
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more