Struct druid::widget::Scope

source ·
pub struct Scope<SP: ScopePolicy, W: Widget<SP::State>> { /* private fields */ }
Expand description

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§

source§

impl<SP: ScopePolicy, W: Widget<SP::State>> Scope<SP, W>

source

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

Create a new scope from a policy and an inner widget

source

pub fn state(&self) -> Option<&SP::State>

A reference to the contents of the Scope’s state.

This allows you to access the content from outside the widget.

source

pub fn state_mut(&mut self) -> Option<&mut SP::State>

A mutable reference to the contents of the Scope’s state.

This allows you to mutably access the content of the Scope’ s state from outside the widget. Mainly useful for composite widgets.

Note:

If you modify the state through this reference, the Scope will not call update on its children until the next event it receives.

source§

impl<F: FnOnce(Transfer::In) -> Transfer::State, Transfer: ScopeTransfer, W: Widget<Transfer::State>> Scope<DefaultScopePolicy<F, Transfer>, W>

source

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

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

source§

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

source

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

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

Trait Implementations§

source§

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

source§

fn event( &mut self, ctx: &mut EventCtx<'_, '_>, event: &Event, data: &mut SP::In, env: &Env )

Handle an event. Read more
source§

fn lifecycle( &mut self, ctx: &mut LifeCycleCtx<'_, '_>, event: &LifeCycle, data: &SP::In, env: &Env )

Handle a life cycle notification. Read more
source§

fn update( &mut self, ctx: &mut UpdateCtx<'_, '_>, _old_data: &SP::In, data: &SP::In, env: &Env )

Update the widget’s appearance in response to a change in the app’s Data or Env. Read more
source§

fn layout( &mut self, ctx: &mut LayoutCtx<'_, '_>, bc: &BoxConstraints, data: &SP::In, env: &Env ) -> Size

Compute layout. Read more
source§

fn paint(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &SP::In, env: &Env)

Paint the widget appearance. Read more
source§

fn compute_max_intrinsic( &mut self, axis: Axis, ctx: &mut LayoutCtx<'_, '_>, bc: &BoxConstraints, data: &T, env: &Env ) -> f64

Computes max intrinsic/preferred dimension of a widget on the provided axis. Read more
source§

impl<SP: ScopePolicy, W: Widget<SP::State>> WidgetWrapper for Scope<SP, W>

§

type Wrapped = W

The type of the wrapped widget. Maybe we would like to constrain this to Widget<impl Data> (if existential bounds were supported). Any other scheme leads to T being unconstrained in unification at some point
source§

fn wrapped(&self) -> &Self::Wrapped

Get immutable access to the wrapped child
source§

fn wrapped_mut(&mut self) -> &mut Self::Wrapped

Get mutable access to the wrapped child

Auto Trait Implementations§

§

impl<SP, W> !RefUnwindSafe for Scope<SP, W>

§

impl<SP, W> !Send for Scope<SP, W>

§

impl<SP, W> !Sync for Scope<SP, W>

§

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

§

impl<SP, W> !UnwindSafe for Scope<SP, W>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

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

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

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 Twhere U: From<T>,

const: unstable · 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.

§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

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

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, W> TestWidgetExt<T> for Wwhere T: Data, W: Widget<T> + 'static,

source§

fn record(self, recording: &Recording) -> Recorder<Self>

source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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