ltk::nav_stack

Struct NavStack

Source
pub struct NavStack<R> { /* private fields */ }
Expand description

The screens the user went through to get where they are, root first.

A plain value the app keeps in its model — the runtime never sees it. It exists so the two things that must agree are one object: what App::on_back walks and what App::save_state persists. It serialises whenever the route type does, and it is never empty: there is always a root to fall back to, including after deserialising.

use ltk::{ App, BackResponse, Element, NavStack, text };
use serde::{ Deserialize, Serialize };

#[ derive( Clone, PartialEq, Serialize, Deserialize ) ]
enum Route { Inbox, Thread( u64 ), Settings }

#[ derive( Clone ) ]
enum Msg { Open( Route ), Back }

struct Mail { nav: NavStack<Route> }

impl App for Mail
{
    type Message = Msg;

    fn app_id( &self ) -> &str { "net.example.Mail" }

    fn view( &self ) -> Element<Msg>
    {
        let screen = match self.nav.current()
        {
            Route::Inbox        => text( "inbox" ),
            Route::Thread( id ) => text( format!( "thread {id}" ) ),
            Route::Settings     => text( "settings" ),
        };
        screen.into()
    }

    fn update( &mut self, msg: Msg )
    {
        match msg
        {
            Msg::Open( route ) => self.nav.push( route ),
            Msg::Back          => { self.nav.pop(); }
        }
    }

    fn on_back( &mut self ) -> BackResponse<Msg> { self.nav.back( Msg::Back ) }

    fn save_state( &self ) -> Option<Vec<u8>> { serde_json::to_vec( &self.nav ).ok() }

    fn restore_state( &mut self, state: Vec<u8> )
    {
        if let Ok( nav ) = serde_json::from_slice( &state ) { self.nav = nav; }
    }
}

Implementations§

Source§

impl<R> NavStack<R>

Source

pub fn new(root: R) -> Self

Source

pub fn current(&self) -> &R

The route on screen.

Source

pub fn current_mut(&mut self) -> &mut R

Source

pub fn push(&mut self, route: R)

Go to route, keeping the current one underneath.

Source

pub fn pop(&mut self) -> Option<R>

Go back one route. None at the root, which stays.

Source

pub fn replace(&mut self, route: R) -> R

Swap the current route for route without growing the stack — a sideways move (tab to tab, step to step) back should not retrace. Returns the route it replaced.

Source

pub fn pop_to_root(&mut self)

Source

pub fn reset(&mut self, root: R)

Start over from a new root.

Source

pub fn can_pop(&self) -> bool

Source

pub fn depth(&self) -> usize

Number of routes, the root included.

Source

pub fn iter(&self) -> impl Iterator<Item = &R>

Routes from the root up to the current one.

Source

pub fn back<M>(&self, pop: M) -> BackResponse<M>

The answer for App::on_back: pop when there is a route to go back to, BackResponse::Unhandled at the root. pop is the app’s message that ends up calling Self::pop.

Trait Implementations§

Source§

impl<R: Clone> Clone for NavStack<R>

Source§

fn clone(&self) -> NavStack<R>

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<R: Debug> Debug for NavStack<R>

Source§

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

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

impl<'de, R> Deserialize<'de> for NavStack<R>
where R: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<R: PartialEq> PartialEq for NavStack<R>

Source§

fn eq(&self, other: &NavStack<R>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<R> Serialize for NavStack<R>
where R: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<R: Eq> Eq for NavStack<R>

Source§

impl<R> StructuralPartialEq for NavStack<R>

Auto Trait Implementations§

§

impl<R> Freeze for NavStack<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for NavStack<R>
where R: RefUnwindSafe,

§

impl<R> Send for NavStack<R>
where R: Send,

§

impl<R> Sync for NavStack<R>
where R: Sync,

§

impl<R> Unpin for NavStack<R>
where R: Unpin,

§

impl<R> UnwindSafe for NavStack<R>
where R: UnwindSafe,

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

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

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

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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

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

§

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

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

§

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