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: MessageDirectionIndicates the direction of the message. See MessageDirection docs for more info.
routing_strategy: RoutingStrategyDefines 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: DeliveryModeMessage delivery mode. See DeliveryMode docs for more info.
flags: u64A custom user flags. Use it if handled flag is not enough.
Implementations§
Source§impl UiMessage
impl UiMessage
Sourcepub fn with_data<T>(data: T) -> UiMessagewhere
T: MessageData,
pub fn with_data<T>(data: T) -> UiMessagewhere
T: MessageData,
Creates new UI message with desired data.
Sourcepub fn for_widget(
handle: Handle<impl ObjectOrVariant<UiNode>>,
data: impl MessageData,
) -> UiMessage
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.
Sourcepub fn from_widget(
handle: Handle<impl ObjectOrVariant<UiNode>>,
data: impl MessageData,
) -> UiMessage
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.
Sourcepub fn with_destination(self, destination: Handle<UiNode>) -> UiMessage
pub fn with_destination(self, destination: Handle<UiNode>) -> UiMessage
Sets the desired destination of the message.
Sourcepub fn with_direction(self, direction: MessageDirection) -> UiMessage
pub fn with_direction(self, direction: MessageDirection) -> UiMessage
Sets the desired direction of the message.
Sourcepub fn with_handled(self, handled: bool) -> UiMessage
pub fn with_handled(self, handled: bool) -> UiMessage
Sets the desired handled flag of the message.
Sourcepub fn with_routing_strategy(
self,
routing_strategy: RoutingStrategy,
) -> UiMessage
pub fn with_routing_strategy( self, routing_strategy: RoutingStrategy, ) -> UiMessage
Sets the desired routing strategy.
Sourcepub fn with_delivery_mode(self, delivery_mode: DeliveryMode) -> UiMessage
pub fn with_delivery_mode(self, delivery_mode: DeliveryMode) -> UiMessage
Sets the desired delivery mode for the message.
Sourcepub fn with_flags(self, flags: u64) -> UiMessage
pub fn with_flags(self, flags: u64) -> UiMessage
Sets the desired flags of the message.
Sourcepub fn reverse(&self) -> UiMessage
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.
Sourcepub fn data_from<T>(
&self,
handle: Handle<impl ObjectOrVariant<UiNode>>,
) -> Option<&T>where
T: MessageData,
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
}
}Sourcepub fn data_for<T>(
&self,
handle: Handle<impl ObjectOrVariant<UiNode>>,
) -> Option<&T>where
T: MessageData,
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
}
}Sourcepub fn is_from(&self, handle: Handle<impl ObjectOrVariant<UiNode>>) -> bool
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.
Sourcepub fn is_for(&self, handle: Handle<impl ObjectOrVariant<UiNode>>) -> bool
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.
Sourcepub fn destination(&self) -> Handle<UiNode>
pub fn destination(&self) -> Handle<UiNode>
Returns destination widget handle of the message.
Sourcepub fn data<T>(&self) -> Option<&T>where
T: MessageData,
pub fn data<T>(&self) -> Option<&T>where
T: MessageData,
Tries to downcast current data of the message to a particular type.
Sourcepub fn set_handled(&self, handled: bool)
pub fn set_handled(&self, handled: bool)
Sets handled flag.
Sourcepub fn direction(&self) -> MessageDirection
pub fn direction(&self) -> MessageDirection
Returns direction of the message.
Sourcepub fn need_perform_layout(&self) -> bool
pub fn need_perform_layout(&self) -> bool
Returns perform layout flag.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for UiMessage
impl !RefUnwindSafe for UiMessage
impl Send for UiMessage
impl !Sync for UiMessage
impl Unpin for UiMessage
impl !UnwindSafe for UiMessage
Blanket Implementations§
Source§impl<T> AsyncTaskResult for T
impl<T> AsyncTaskResult for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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 Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSend for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<T, U> ObjectOrVariant<T> for Uwhere
PhantomData<U>: ObjectOrVariantHelper<T, U>,
impl<T, U> ObjectOrVariant<T> for Uwhere
PhantomData<U>: ObjectOrVariantHelper<T, U>,
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.