Unify names/locations of memoize and adapt.

Also use adapt macro to generate adapt view for xilem.
This commit is contained in:
Richard Dodd 2023-07-05 18:16:34 +01:00
parent 00dd6c15f3
commit f99baa3969
8 changed files with 5 additions and 99 deletions

View File

@ -17,7 +17,6 @@
mod any_view;
mod id;
mod memoize;
mod message;
mod sequence;
mod vec_splice;

View File

@ -1,5 +1,5 @@
#[macro_export]
macro_rules! impl_adapt_view {
macro_rules! generate_adapt_view {
($viewtrait:ident, $cx:ty, $changeflags:ty) => {
/// A view that wraps a child view and modifies the state that callbacks have access to.
///

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
mod adapt;
mod memoize;
/// Create the `View` trait for a particular xilem context (e.g. html, native, ...).
///

View File

@ -101,7 +101,7 @@ xilem_core::generate_view_trait! {View, DomNode, Cx, ChangeFlags;}
xilem_core::generate_viewsequence_trait! {ViewSequence, View, ViewMarker, DomNode, Cx, ChangeFlags, Pod;}
xilem_core::generate_anyview_trait! {AnyView, View, ViewMarker, Cx, ChangeFlags, AnyNode, BoxedView;}
xilem_core::generate_memoize_view! {Memoize, MemoizeState, View, ViewMarker, Cx, ChangeFlags, s, memoize}
xilem_core::impl_adapt_view! {View, Cx, ChangeFlags}
xilem_core::generate_adapt_view! {View, Cx, ChangeFlags}
/// This view container can switch between two views.
///

View File

@ -1,94 +0,0 @@
// Copyright 2022 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::{any::Any, marker::PhantomData};
use crate::{event::EventResult, id::Id};
use super::{Cx, View};
pub struct Adapt<T, A, U, B, F: Fn(&mut T, AdaptThunk<U, B, C>) -> EventResult<A>, C: View<U, B>> {
f: F,
child: C,
phantom: PhantomData<fn() -> (T, A, U, B)>,
}
/// A "thunk" which dispatches an event to an adapt node's child.
///
/// The closure passed to Adapt should call this thunk with the child's
/// app state.
pub struct AdaptThunk<'a, U, B, C: View<U, B>> {
child: &'a C,
state: &'a mut C::State,
id_path: &'a [Id],
event: Box<dyn Any>,
}
impl<T, A, U, B, F: Fn(&mut T, AdaptThunk<U, B, C>) -> EventResult<A>, C: View<U, B>>
Adapt<T, A, U, B, F, C>
{
pub fn new(f: F, child: C) -> Self {
Adapt {
f,
child,
phantom: Default::default(),
}
}
}
impl<'a, U, B, C: View<U, B>> AdaptThunk<'a, U, B, C> {
pub fn call(self, app_state: &mut U) -> EventResult<B> {
self.child
.event(self.id_path, self.state, self.event, app_state)
}
}
impl<T, A, U, B, F: Fn(&mut T, AdaptThunk<U, B, C>) -> EventResult<A> + Send, C: View<U, B>>
View<T, A> for Adapt<T, A, U, B, F, C>
{
type State = C::State;
type Element = C::Element;
fn build(&self, cx: &mut Cx) -> (Id, Self::State, Self::Element) {
self.child.build(cx)
}
fn rebuild(
&self,
cx: &mut Cx,
prev: &Self,
id: &mut Id,
state: &mut Self::State,
element: &mut Self::Element,
) -> bool {
self.child.rebuild(cx, &prev.child, id, state, element)
}
fn event(
&self,
id_path: &[Id],
state: &mut Self::State,
event: Box<dyn Any>,
app_state: &mut T,
) -> EventResult<A> {
let thunk = AdaptThunk {
child: &self.child,
state,
id_path,
event,
};
(self.f)(app_state, thunk)
}
}

View File

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// mod adapt;
// mod async_list;
mod button;
// mod layout_observer;
@ -29,4 +28,4 @@ pub use xilem_core::{Id, IdPath, VecSplice};
pub use button::button;
pub use linear_layout::{h_stack, v_stack, LinearLayout};
pub use list::{list, List};
pub use view::{Cx, View, ViewMarker, ViewSequence};
pub use view::{Adapt, Cx, Memoize, View, ViewMarker, ViewSequence};

View File

@ -27,6 +27,7 @@ xilem_core::generate_view_trait! {View, Widget, Cx, ChangeFlags; : Send}
xilem_core::generate_viewsequence_trait! {ViewSequence, View, ViewMarker, Widget, Cx, ChangeFlags, Pod; : Send}
xilem_core::generate_anyview_trait! {AnyView, View, ViewMarker, Cx, ChangeFlags, AnyWidget, BoxedView; + Send}
xilem_core::generate_memoize_view! {Memoize, MemoizeState, View, ViewMarker, Cx, ChangeFlags, s, memoize}
xilem_core::generate_adapt_view! {View, Cx, ChangeFlags}
#[derive(Clone)]
pub struct Cx {