pub struct State<T> { /* private fields */ }
Expand description
The State
primitive is the tool to modify the characteristics of
Views
, by e.g. modifying a Label
when a Button
is clicked:
struct Application;
impl AppDelegate<AppState> for Application {
fn make_content_view(&mut self, state: &mut AppState, _: Window) -> impl finestra::View<Self, AppState> {
state.label.set("Clicked: 0");
Stack::vertical()
.with(Label::new(&state.label))
.with(Button::new("Press")
.with_on_click(|state: &mut AppState, _| {
state.count += 1;
state.label.set(format!("Clicked: {}", state.count));
}))
}
}
#[derive(Debug, Default)]
struct AppState {
count: usize,
label: TextValue,
}
For more information, see the crate documentation.
Implementations§
source§impl<T> State<T>
impl<T> State<T>
sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Create a new State
with the given value. Note: you can use
State::default()
if your wrapped value supports it :)
sourcepub fn set(&self, value: impl Into<T>)
pub fn set(&self, value: impl Into<T>)
Set the new value of the state, which will notify the
listeners
.
sourcepub fn with<F: FnOnce(&T) -> R, R>(&self, f: F) -> R
pub fn with<F: FnOnce(&T) -> R, R>(&self, f: F) -> R
Get the value within the state by using a visitor method.
let state = State::new("Hello, world!".to_string());
state.with(|value| {
println!("Value is: {value}");
});
sourcepub fn clone_inner(&self) -> Twhere
T: Clone,
pub fn clone_inner(&self) -> Twhere
T: Clone,
Clone the inner value. Note: you can also use State::with()
if you
only need a temporary reference.
let state = State::new("Hello, world!".to_string());
let cloned: String = state.clone_inner();
sourcepub fn with_mut<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R
pub fn with_mut<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R
Get a mutable reference to the value within the state by using a visitor method.
let state = State::new("Hello, world!".to_string());
state.with(|value| {
println!("Value is: {value}");
});
sourcepub fn add_listener<F: Fn(&T) + 'static>(&self, callback: F)
pub fn add_listener<F: Fn(&T) + 'static>(&self, callback: F)
Get subscribed by changes to the State. Note: the callback won’t get invoked for the initial value, only for changes.
Trait Implementations§
source§impl<T> From<&State<T>> for StateOrRaw<T>
impl<T> From<&State<T>> for StateOrRaw<T>
source§impl<T> From<State<T>> for StateOrRaw<T>
impl<T> From<State<T>> for StateOrRaw<T>
impl<T> Send for State<T>
impl<T> Sync for State<T>
Auto Trait Implementations§
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
Mutably borrows from an owned value. Read more