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>
impl<SP: ScopePolicy, W: Widget<SP::State>> Scope<SP, W>
sourcepub fn state(&self) -> Option<&SP::State>
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.
sourcepub fn state_mut(&mut self) -> Option<&mut SP::State>
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>
impl<F: FnOnce(Transfer::In) -> Transfer::State, Transfer: ScopeTransfer, W: Widget<Transfer::State>> Scope<DefaultScopePolicy<F, Transfer>, W>
sourcepub fn from_function(make_state: F, transfer: Transfer, inner: W) -> Self
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>
impl<In: Data, State: Data, F: Fn(In) -> State, L: Lens<State, In>, W: Widget<State>> Scope<DefaultScopePolicy<F, LensScopeTransfer<L, In, State>>, W>
Trait Implementations§
source§impl<SP: ScopePolicy, W: Widget<SP::State>> Widget<<SP as ScopePolicy>::In> for Scope<SP, W>
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
)
fn event( &mut self, ctx: &mut EventCtx<'_, '_>, event: &Event, data: &mut SP::In, env: &Env )
source§fn lifecycle(
&mut self,
ctx: &mut LifeCycleCtx<'_, '_>,
event: &LifeCycle,
data: &SP::In,
env: &Env
)
fn lifecycle( &mut self, ctx: &mut LifeCycleCtx<'_, '_>, event: &LifeCycle, data: &SP::In, env: &Env )
source§fn update(
&mut self,
ctx: &mut UpdateCtx<'_, '_>,
_old_data: &SP::In,
data: &SP::In,
env: &Env
)
fn update( &mut self, ctx: &mut UpdateCtx<'_, '_>, _old_data: &SP::In, data: &SP::In, env: &Env )
source§fn layout(
&mut self,
ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
data: &SP::In,
env: &Env
) -> Size
fn layout( &mut self, ctx: &mut LayoutCtx<'_, '_>, bc: &BoxConstraints, data: &SP::In, env: &Env ) -> Size
source§fn paint(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &SP::In, env: &Env)
fn paint(&mut self, ctx: &mut PaintCtx<'_, '_, '_>, data: &SP::In, env: &Env)
source§fn compute_max_intrinsic(
&mut self,
axis: Axis,
ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
data: &T,
env: &Env
) -> f64
fn compute_max_intrinsic( &mut self, axis: Axis, ctx: &mut LayoutCtx<'_, '_>, bc: &BoxConstraints, data: &T, env: &Env ) -> f64
source§impl<SP: ScopePolicy, W: Widget<SP::State>> WidgetWrapper for Scope<SP, W>
impl<SP: ScopePolicy, W: Widget<SP::State>> WidgetWrapper for Scope<SP, W>
§type Wrapped = W
type Wrapped = W
Widget<impl Data>
(if existential bounds were supported).
Any other scheme leads to T
being unconstrained in unification at some point