[][src]Struct druid::widget::Scope

pub struct Scope<SP: ScopePolicy, W: Widget<SP::State>> { /* fields omitted */ }

A widget that allows encapsulation of application state.

This is useful in circumstances where

  • A (potentially reusable) widget is composed of a tree of multiple cooperating child widgets
  • Those widgets communicate amongst themselves using Druid's reactive data mechanisms
  • It is undesirable to complicate the surrounding application state with the internal details of the widget.

Examples include:

  • In a tabs widget composed of a tab bar, and a widget switching body, those widgets need to cooperate on which tab is selected. However not every user of a tabs widget wishes to encumber their application state with this internal detail - especially as many tabs widgets may reasonably exist in an involved application.
  • In a table/grid widget composed of various internal widgets, many things need to be synchronised. Scroll position, heading moves, drag operations, sort/filter operations. For many applications access to this internal data outside of the table widget isn't needed. For this reason it may be useful to use a Scope to establish private state.

A scope embeds some input state (from its surrounding application or parent scope) into a larger piece of internal state. This is controlled by a user provided policy.

The ScopePolicy needs to do two things a) Create a new scope from the initial value of its input, b) Provide two way synchronisation between the input and the state via a ScopeTransfer

Convenience methods are provided to make a policy from a function and a lens. It may sometimes be advisable to implement ScopePolicy directly if you need to mention the type of a Scope.

Examples

use druid::{Data, Lens, WidgetExt};
use druid::widget::{TextBox, Scope};
#[derive(Clone, Data, Lens)]
struct AppState {
    name: String,
}

#[derive(Clone, Data, Lens)]
struct PrivateState {
    text: String,
    other: u32,
}

impl PrivateState {
    pub fn new(text: String) -> Self {
        PrivateState { text, other: 0 }
    }
}

fn main() {
    let scope = Scope::from_lens(
        PrivateState::new,
        PrivateState::text,
        TextBox::new().lens(PrivateState::text),
    );
}

Implementations

impl<SP: ScopePolicy, W: Widget<SP::State>> Scope<SP, W>[src]

pub fn new(policy: SP, inner: W) -> Self[src]

Create a new scope from a policy and an inner widget

impl<F: Fn(Transfer::In) -> Transfer::State, Transfer: ScopeTransfer, W: Widget<Transfer::State>> Scope<DefaultScopePolicy<F, Transfer>, W>[src]

pub fn from_function(make_state: F, transfer: Transfer, inner: W) -> Self[src]

Create a new policy from a function creating the state, and a ScopeTransfer synchronising it

impl<In: Data, State: Data, F: Fn(In) -> State, L: Lens<State, In>, W: Widget<State>> Scope<DefaultScopePolicy<F, LensScopeTransfer<L, In, State>>, W>[src]

pub fn from_lens(make_state: F, lens: L, inner: W) -> Self[src]

Create a new policy from a function creating the state, and a Lens synchronising it

Trait Implementations

impl<SP: ScopePolicy, W: Widget<SP::State>> Widget<<SP as ScopePolicy>::In> for Scope<SP, W>[src]

Auto Trait Implementations

impl<SP, W> !RefUnwindSafe for Scope<SP, W>[src]

impl<SP, W> !Send for Scope<SP, W>[src]

impl<SP, W> !Sync for Scope<SP, W>[src]

impl<SP, W> Unpin for Scope<SP, W> where
    SP: Unpin,
    W: Unpin,
    <SP as ScopePolicy>::State: Unpin,
    <SP as ScopePolicy>::Transfer: Unpin
[src]

impl<SP, W> !UnwindSafe for Scope<SP, W>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> RoundFrom<T> for T

impl<T, U> RoundInto<U> for T where
    U: RoundFrom<T>, 

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.