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 perform_layout: Cell<bool>,
    pub flags: u64,
}
Expand description

Message is 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 very simple and reliable mechanism that effectively decouples widgets from each other. There is no direct way of modify something during runtime, you have to use messages to change 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 nor Send or Sync. User interface is a single-thread thing, as well as its messages.

§Examples

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

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

impl MyWidgetMessage {
    define_constructor!(MyWidgetMessage:DoSomething => fn do_something(), layout: false);
    define_constructor!(MyWidgetMessage:Foo => fn foo(u32), layout: false);
    define_constructor!(MyWidgetMessage:Bar => fn bar(foo: u32, baz: u8), layout: false);
}

fn using_messages(my_widget: Handle<UiNode>, ui: &UserInterface) {
    // Send MyWidgetMessage::DoSomething
    ui.send_message(MyWidgetMessage::do_something(
        my_widget,
        MessageDirection::ToWidget,
    ));
    // Send MyWidgetMessage::Foo
    ui.send_message(MyWidgetMessage::foo(
        my_widget,
        MessageDirection::ToWidget,
        5,
    ));
    // Send MyWidgetMessage::Bar
    ui.send_message(MyWidgetMessage::bar(
        my_widget,
        MessageDirection::ToWidget,
        1,
        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). T

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

§perform_layout: Cell<bool>

Whether or not message requires layout to be calculated first.

§Motivation

Some of message handling routines uses layout info, but message loop performed right after layout pass, but some of messages may change layout and this flag tells UI to perform layout before passing message further. In ideal case we’d perform layout after each message, but since layout pass is super heavy we should do it only when it is actually needed.

§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 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_perform_layout(self, perform_layout: bool) -> UiMessage

Sets the desired perform layout flag of the message.

Source

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

Sets the desired routing strategy.

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 Value message. When the input field receives Value message with MessageDirection::ToWidget it checks if value needs to be changed and if it does, it re-sends 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 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 set_perform_layout(&self, value: bool)

Sets perform layout flag.

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 copy 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 as_any(&self) -> &(dyn Any + 'static)

Converts self reference as a reference to Any. Could be used to downcast a trait object to a particular type.
Source§

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

Converts self reference as a reference to Any. Could be used to downcast a trait object to a particular type.
Source§

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

Source§

impl<T> FieldValue for T
where T: 'static,

Source§

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

Casts self to a &dyn Any
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<T> MessageData for T
where T: 'static + Debug + PartialEq + Any + Send + Clone,

Source§

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

Casts self as Any reference.
Source§

fn compare(&self, other: &(dyn MessageData + 'static)) -> bool

Compares this message data with some other.
Source§

fn clone_box(&self) -> Box<dyn MessageData>

Clones self as boxed value.
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<T> ScriptMessagePayload for T
where T: 'static + Send + Debug,

Source§

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

Returns self as &dyn Any
Source§

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

Returns self as &dyn Any
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,