Skip to main content

PersistentState

Struct PersistentState 

Source
pub struct PersistentState<'f, T>
where T: Serialize + DeserializeOwned + Clone + Default + Send + Sync + 'static,
{ /* private fields */ }
Expand description

State that outlives the app, saved with egui’s persisted memory.

eframe writes it to disk natively and to localStorage on the web. Like localStorage, there is one value per type for the whole app, shared by every view that extracts it.

Use it for preferences: a theme, a sort order, a collapsed sidebar. It is created with T::default(), read at the start of the view and written back when the view returns (the last view to write wins), so keep it small. Outside of views, use PersistentState::get and PersistentState::set.

Saving needs eframe’s persistence feature. Without it, the value still lives in egui’s memory, for as long as the app runs.

use egui::Ui;
use egui_kit::extract::PersistentState;
use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Serialize, Deserialize)]
struct Preferences {
    compact: bool,
}

fn view(ui: &mut Ui, mut preferences: PersistentState<Preferences>) {
    ui.checkbox(&mut preferences.compact, "Compact");
}

Implementations§

Source§

impl<T> PersistentState<'_, T>
where T: Serialize + DeserializeOwned + Clone + Default + Send + Sync + 'static,

Outside of views, e.g. to apply a theme before the router draws.

Source

pub fn get(ctx: &Context) -> T

The value.

fn apply_theme(ctx: &egui::Context) {
    let preferences = PersistentState::<Preferences>::get(ctx);
    ctx.set_theme(if preferences.dark { egui::Theme::Dark } else { egui::Theme::Light });
}
Source

pub fn set(ctx: &Context, value: T)

Replaces the value.

Source

pub fn id() -> Id

Where the value is kept in egui’s memory.

Trait Implementations§

Source§

impl<T> Debug for PersistentState<'_, T>
where T: Serialize + DeserializeOwned + Clone + Default + Send + Sync + 'static + Debug,

Source§

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

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

impl<T> Deref for PersistentState<'_, T>
where T: Serialize + DeserializeOwned + Clone + Default + Send + Sync + 'static,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for PersistentState<'_, T>
where T: Serialize + DeserializeOwned + Clone + Default + Send + Sync + 'static,

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T> Drop for PersistentState<'_, T>
where T: Serialize + DeserializeOwned + Clone + Default + Send + Sync + 'static,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<S: 'static, D, T> FromView<S, D> for PersistentState<'_, T>
where T: Serialize + DeserializeOwned + Clone + Default + Send + Sync + 'static,

Source§

type Item<'f> = PersistentState<'f, T>

Source§

fn from_view<'f>(cx: &'f ViewContext<'_, S>) -> PersistentState<'f, T>

Auto Trait Implementations§

§

impl<'f, T> !RefUnwindSafe for PersistentState<'f, T>

§

impl<'f, T> !UnwindSafe for PersistentState<'f, T>

§

impl<'f, T> Forget for PersistentState<'f, T>

§

impl<'f, T> Freeze for PersistentState<'f, T>
where Option<T>: Freeze,

§

impl<'f, T> Move for PersistentState<'f, T>

§

impl<'f, T> Send for PersistentState<'f, T>
where Option<T>: Send,

§

impl<'f, T> Sync for PersistentState<'f, T>
where Option<T>: Sync,

§

impl<'f, T> Unpin for PersistentState<'f, T>
where Option<T>: Unpin,

§

impl<'f, T> UnsafeUnpin for PersistentState<'f, T>
where Option<T>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromRef<S> for S

Source§

fn from_ref(state: &S) -> &S

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> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.