Skip to main content

UiMessage

Struct UiMessage 

Source
pub struct UiMessage {
    pub handled: Cell<bool>,
    pub data: Box<dyn MessageData>,
    pub destination: Handle<UiNode>,
    pub direction: MessageDirection,
    pub routing_strategy: RoutingStrategy,
    pub delivery_mode: DeliveryMode,
    pub flags: u64,
}
Expand description

Message is a basic communication element that is used to deliver information to widget or to user code.

§Motivation

This UI library uses message passing mechanism to communicate with widgets. This is a very simple and reliable mechanism that effectively decouples widgets from each other. There is no direct way of modifying something during runtime, you have to use messages to change the state of ui elements.

§Direction

Each message marked with “Direction” field, which means supported routes for message. For example crate::button::ButtonMessage::Click has “Direction: To/From UI” which means that it can be sent either from internals of library or from user code. However crate::widget::WidgetMessage::Focus has “Direction: From UI” which means that only internal library code can send such messages without a risk of breaking anything.

§Threading

UiMessage is Send, it can be sent from another thread to a user interface.

§Examples

use fyrox_ui::{
    core::pool::Handle, message::MessageDirection, message::UiMessage, UiNode,
    UserInterface, message::MessageData,
};

// Message must be debuggable and comparable.
#[derive(Debug, PartialEq, Clone)]
enum MyWidgetMessage {
    DoSomething,
    Foo(u32),
    Bar { foo: u32, baz: u8 },
}
impl MessageData for MyWidgetMessage{}

fn using_messages(my_widget: Handle<UiNode>, ui: &UserInterface) {
    // Send MyWidgetMessage::DoSomething
    ui.send(my_widget, MyWidgetMessage::DoSomething);
    // Send MyWidgetMessage::Foo
    ui.send(my_widget, MyWidgetMessage::Foo(5));
    // Send MyWidgetMessage::Bar
    ui.send(my_widget, MyWidgetMessage::Bar {foo: 1, baz: 2});
}

Fields§

§handled: Cell<bool>

Useful flag to check if a message was already handled. It could be used to mark messages as “handled” to prevent any further responses to them. It is especially useful in bubble message routing, when a message is passed through the entire chain of parent nodes starting from current. In this, you can mark a message as “handled” and also check if it is handled or not. For example, this is used in crate::tree::Tree implementation, to prevent double-click to close all the parent trees from current.

§data: Box<dyn MessageData>

Actual message data. Use UiMessage::data method to try to downcast the internal data to a specific type.

§destination: Handle<UiNode>

Handle of node that will receive message. Please note that all nodes in hierarchy will also receive this message, order is “up-on-tree” (so-called “bubble” message routing).

§direction: MessageDirection

Indicates the direction of the message. See MessageDirection docs for more info.

§routing_strategy: RoutingStrategy

Defines a way of how the message will behave in the widget tree after it was delivered to the destination node. Default is bubble routing. See RoutingStrategy for more info.

§delivery_mode: DeliveryMode

Message delivery mode. See DeliveryMode docs for more info.

§flags: u64

A custom user flags. Use it if handled flag is not enough.

Implementations§

Source§

impl UiMessage

Source

pub fn with_data<T>(data: T) -> UiMessage
where T: MessageData,

Creates new UI message with desired data.

Source

pub fn for_widget( handle: Handle<impl ObjectOrVariant<UiNode>>, data: impl MessageData, ) -> UiMessage

Creates a new UI message with the given data for the specified widget.

Source

pub fn from_widget( handle: Handle<impl ObjectOrVariant<UiNode>>, data: impl MessageData, ) -> UiMessage

Creates a new UI message with the given data to be posted from the name of the specified widget.

Source

pub fn with_destination(self, destination: Handle<UiNode>) -> UiMessage

Sets the desired destination of the message.

Source

pub fn with_direction(self, direction: MessageDirection) -> UiMessage

Sets the desired direction of the message.

Source

pub fn with_handled(self, handled: bool) -> UiMessage

Sets the desired handled flag of the message.

Source

pub fn with_routing_strategy( self, routing_strategy: RoutingStrategy, ) -> UiMessage

Sets the desired routing strategy.

Source

pub fn with_delivery_mode(self, delivery_mode: DeliveryMode) -> UiMessage

Sets the desired delivery mode for the message.

Source

pub fn with_flags(self, flags: u64) -> UiMessage

Sets the desired flags of the message.

Source

pub fn reverse(&self) -> UiMessage

Creates a new copy of the message with reversed direction. Typical use case is to re-send messages to create “response” in a widget. For example, you have a float input field, and it has a Value message. When the input field receives a Value message with MessageDirection::ToWidget it checks if value needs to be changed and if it does, it re-sends the same message, but with reversed direction back to message queue so every “listener” can reach properly. The input field won’t react at MessageDirection::FromWidget message so there will be no infinite message loop.

Source

pub fn data_from<T>( &self, handle: Handle<impl ObjectOrVariant<UiNode>>, ) -> Option<&T>
where T: MessageData,

Checks if the message comes from the specified widget (via Self::is_from) and the data type matches the given type and returns a reference to the data.

§Example
if let Some(data) = message.data_from::<MyMessage>(widget_handle) {
    // Do something
}

This method call is essentially a shortcut for:

if message.destination() == widget_handle && message.direction() == MessageDirection::FromWidget {
    if let Some(data) = message.data::<MyMessage>() {
        // Do something
    }
}
Source

pub fn data_for<T>( &self, handle: Handle<impl ObjectOrVariant<UiNode>>, ) -> Option<&T>
where T: MessageData,

Checks if the message was sent to the specified widget (via Self::is_for) and the data type matches the given type and returns a reference to the data.

§Example
if let Some(data) = message.data_for::<MyMessage>(widget_handle) {
    // Do something
}

This method call is essentially a shortcut for:

if message.destination() == widget_handle && message.direction() == MessageDirection::ToWidget {
    if let Some(data) = message.data::<MyMessage>() {
        // Do something
    }
}
Source

pub fn is_from(&self, handle: Handle<impl ObjectOrVariant<UiNode>>) -> bool

Checks whether the message destination node handle matches the given one and the message direction is MessageDirection::FromWidget.

Source

pub fn is_for(&self, handle: Handle<impl ObjectOrVariant<UiNode>>) -> bool

Checks whether the message destination node handle matches the given one and the message direction is MessageDirection::ToWidget.

Source

pub fn destination(&self) -> Handle<UiNode>

Returns destination widget handle of the message.

Source

pub fn data<T>(&self) -> Option<&T>
where T: MessageData,

Tries to downcast current data of the message to a particular type.

Source

pub fn set_handled(&self, handled: bool)

Sets handled flag.

Source

pub fn handled(&self) -> bool

Returns handled flag.

Source

pub fn direction(&self) -> MessageDirection

Returns direction of the message.

Source

pub fn need_perform_layout(&self) -> bool

Returns perform layout flag.

Source

pub fn has_flags(&self, flags: u64) -> bool

Checks if the message has particular flags.

Trait Implementations§

Source§

impl Clone for UiMessage

Source§

fn clone(&self) -> UiMessage

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for UiMessage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for UiMessage

Source§

fn eq(&self, other: &UiMessage) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsyncTaskResult for T
where T: Any + Send + 'static,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

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 T
where U: From<T>,

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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T, U> ObjectOrVariant<T> for U

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,