pub struct StateManager { /* private fields */ }
Expand description
StateManager
is used to manage the mutable state associated with a particular component’s ID.
Because components technically only exist while a layout tree is being calculated, this is
where a component is expected to store all it’s durable state that must survive through the
next layout pass. The StateManager also requires that all components implement
StateMachineChild
, even if they are stateless. A derive macro is provided for this case.
Likewise, the internal state object must implement event::EventRouter
, even if it doesn’t process
any events, in which case it can simply set Input and Output to std::convert::Infallible
All components can access their own state by calling StateManager::get
with their ID and
the component::StateMachine
wrapper type around their internal state object, which should have been
created earlier by feather automatically calling StateMachineChild::init
.
§Examples
use feather_ui::{SourceID, StateManager, component::StateMachine, event::EventRouter};
use std::sync::Arc;
use std::convert::Infallible;
#[derive(Clone, PartialEq)]
struct FooBar {
i: i32
}
impl EventRouter for FooBar {
type Input = Infallible;
type Output = Infallible;
}
fn layout(id: Arc<SourceID>, manager: &mut StateManager) {
let outer = manager.get_mut::<StateMachine<FooBar, 0>>(&id).unwrap();
outer.state.i = 3;
}
A component can even retrieve a different component’s internal state as long as it has the ID and knows the type of the inner state. This is how most feather components get the DPI for the current window.
use feather_ui::{SourceID, StateManager, component::StateMachine, event::EventRouter};
use std::sync::Arc;
use std::convert::Infallible;
use feather_ui::component::window::WindowStateMachine;
#[derive(Clone, PartialEq)]
struct FooBar {
i: i32
}
impl EventRouter for FooBar {
type Input = Infallible;
type Output = Infallible;
}
fn layout(
manager: &mut StateManager,
window: &Arc<SourceID>,
) {
let dpi = manager
.get::<WindowStateMachine>(window)
.map(|x| x.state.dpi)
.unwrap_or(feather_ui::BASE_DPI);
println!("{dpi:?}");
}
Implementations§
Source§impl StateManager
impl StateManager
pub fn register_pointer<T>( &mut self, p: *const T, id: Arc<SourceID>, ) -> Option<Arc<SourceID>>
pub fn invalidate_pointer<T>(&mut self, p: *const T) -> Option<Arc<SourceID>>
pub fn mutate_pointer<T>(&mut self, p: *const T)
Sourcepub fn get<'a, State: 'static + StateMachineWrapper>(
&'a self,
id: &SourceID,
) -> Result<&'a State>
pub fn get<'a, State: 'static + StateMachineWrapper>( &'a self, id: &SourceID, ) -> Result<&'a State>
Gets a reference to a state for the given id
. Returns an error if the state doesn’t
exist or if the requested type doesn’t match.
Sourcepub fn get_mut<'a, State: 'static + StateMachineWrapper>(
&'a mut self,
id: &SourceID,
) -> Result<&'a mut State>
pub fn get_mut<'a, State: 'static + StateMachineWrapper>( &'a mut self, id: &SourceID, ) -> Result<&'a mut State>
Gets a mutable reference to a state for the given id
. Returns an error if the
state doesn’t exist or if the requested type doesn’t match.
Trait Implementations§
Source§impl Default for StateManager
impl Default for StateManager
Source§fn default() -> StateManager
fn default() -> StateManager
Auto Trait Implementations§
impl Freeze for StateManager
impl !RefUnwindSafe for StateManager
impl !Send for StateManager
impl !Sync for StateManager
impl Unpin for StateManager
impl !UnwindSafe for StateManager
Blanket Implementations§
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> 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> 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<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.