Struct finestra::State

source ·
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>

source

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 :)

source

pub fn set(&self, value: impl Into<T>)

Set the new value of the state, which will notify the listeners.

source

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}");
});
source

pub fn clone_inner(&self) -> T
where 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();
source

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}");
});
source

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> Clone for State<T>

source§

fn clone(&self) -> Self

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<T> Debug for State<T>

source§

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

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

impl<T> Default for State<T>
where T: Default,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T> From<&State<T>> for StateOrRaw<T>

source§

fn from(value: &State<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<State<T>> for StateOrRaw<T>

source§

fn from(value: State<T>) -> Self

Converts to this type from the input type.
source§

impl<T> Send for State<T>

source§

impl<T> Sync for State<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for State<T>

§

impl<T> Unpin for State<T>

§

impl<T> UnwindSafe for State<T>

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> 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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.