[−][src]Struct druid::widget::Scope
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]
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]
pub fn event(
&mut self,
ctx: &mut EventCtx<'_, '_>,
event: &Event,
data: &mut SP::In,
env: &Env
)
[src]
&mut self,
ctx: &mut EventCtx<'_, '_>,
event: &Event,
data: &mut SP::In,
env: &Env
)
pub fn lifecycle(
&mut self,
ctx: &mut LifeCycleCtx<'_, '_>,
event: &LifeCycle,
data: &SP::In,
env: &Env
)
[src]
&mut self,
ctx: &mut LifeCycleCtx<'_, '_>,
event: &LifeCycle,
data: &SP::In,
env: &Env
)
pub fn update(
&mut self,
ctx: &mut UpdateCtx<'_, '_>,
_old_data: &SP::In,
data: &SP::In,
env: &Env
)
[src]
&mut self,
ctx: &mut UpdateCtx<'_, '_>,
_old_data: &SP::In,
data: &SP::In,
env: &Env
)
pub fn layout(
&mut self,
ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
data: &SP::In,
env: &Env
) -> Size
[src]
&mut self,
ctx: &mut LayoutCtx<'_, '_>,
bc: &BoxConstraints,
data: &SP::In,
env: &Env
) -> Size
pub fn paint(
&mut self,
ctx: &mut PaintCtx<'_, '_, '_>,
data: &SP::In,
env: &Env
)
[src]
&mut self,
ctx: &mut PaintCtx<'_, '_, '_>,
data: &SP::In,
env: &Env
)
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]
SP: Unpin,
W: Unpin,
<SP as ScopePolicy>::State: Unpin,
<SP as ScopePolicy>::Transfer: Unpin,
impl<SP, W> !UnwindSafe for Scope<SP, W>
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> RoundFrom<T> for T
pub fn round_from(x: T) -> T
impl<T, U> RoundInto<U> for T where
U: RoundFrom<T>,
U: RoundFrom<T>,
pub fn round_into(self) -> U
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]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,