use core::any::type_name;
use core::fmt::Debug;
use core::marker::PhantomData;
use crate::{MessageCtx, MessageResult, Mut, View, ViewMarker, ViewPathTracker};
#[must_use = "View values do nothing unless provided to Xilem."]
pub struct Lens<CF, V, F, ParentState, ChildState, Action, Context> {
access_state: F,
child_component: CF,
phantom: PhantomData<fn(ParentState) -> (ChildState, Action, Context, V)>,
}
impl<CF, V, F, ParentState, ChildState, Action, Context> Debug
for Lens<CF, V, F, ParentState, ChildState, Action, Context>
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Lens")
.field("from", &type_name::<ParentState>())
.field("to", &type_name::<ChildState>())
.finish_non_exhaustive()
}
}
pub fn lens<ParentState, Action, Context, ChildState, StateF, InnerView, Component>(
component: Component,
access_state: StateF,
) -> Lens<Component, InnerView, StateF, ParentState, ChildState, Action, Context>
where
ChildState: 'static,
ParentState: 'static,
Component: Fn(&mut ChildState) -> InnerView + 'static,
StateF: (for<'a> Fn(&'a mut ParentState) -> &'a mut ChildState) + 'static,
InnerView: View<ChildState, Action, Context>,
Context: ViewPathTracker,
Lens<Component, InnerView, StateF, ParentState, ChildState, Action, Context>:
View<ParentState, Action, Context>,
{
Lens {
child_component: component,
access_state,
phantom: PhantomData,
}
}
impl<Component, V, StateF, ParentState, ChildState, Action, Context> ViewMarker
for Lens<Component, V, StateF, ParentState, ChildState, Action, Context>
{
}
impl<Component, ParentState, ChildState, Action, Context, InnerView, StateF>
View<ParentState, Action, Context>
for Lens<Component, InnerView, StateF, ParentState, ChildState, Action, Context>
where
ParentState: 'static,
ChildState: 'static,
InnerView: View<ChildState, Action, Context>,
Component: Fn(&mut ChildState) -> InnerView + 'static,
StateF: (for<'a> Fn(&'a mut ParentState) -> &'a mut ChildState) + 'static,
Action: 'static,
Context: ViewPathTracker + 'static,
{
type ViewState = (InnerView, InnerView::ViewState);
type Element = InnerView::Element;
fn build(
&self,
ctx: &mut Context,
app_state: &mut ParentState,
) -> (Self::Element, Self::ViewState) {
let child_state = (self.access_state)(app_state);
let child = (self.child_component)(child_state);
let (element, child_view_state) = child.build(ctx, (self.access_state)(app_state));
(element, (child, child_view_state))
}
fn rebuild(
&self,
_prev: &Self,
view_state: &mut Self::ViewState,
ctx: &mut Context,
element: Mut<'_, Self::Element>,
app_state: &mut ParentState,
) {
let child_state = (self.access_state)(app_state);
let child = (self.child_component)(&mut *child_state);
child.rebuild(&view_state.0, &mut view_state.1, ctx, element, child_state);
view_state.0 = child;
}
fn teardown(
&self,
(child, child_view_state): &mut Self::ViewState,
ctx: &mut Context,
element: Mut<'_, Self::Element>,
) {
child.teardown(child_view_state, ctx, element);
}
fn message(
&self,
(child, child_view_state): &mut Self::ViewState,
message: &mut MessageCtx,
element: Mut<'_, Self::Element>,
app_state: &mut ParentState,
) -> MessageResult<Action> {
child.message(
child_view_state,
message,
element,
(self.access_state)(app_state),
)
}
}