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

source

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.

Examples found in repository?
examples/blocking_function.rs (line 71)
65
66
67
68
69
70
71
72
73
74
fn wrapped_slow_function(sink: ExtEventSink, number: u32) {
    thread::spawn(move || {
        let number = slow_function(number);
        // Once the slow function is done we can use the event sink (the external handle).
        // This sends the `FINISH_SLOW_FUNCTION` command to the main thread and attach
        // the number as payload.
        sink.submit_command(FINISH_SLOW_FUNCTION, number, Target::Auto)
            .expect("command failed to submit");
    });
}
source

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?
examples/async_event.rs (lines 74-76)
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

source§

fn clone(&self) -> ExtEventSink

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more