diff --git a/crates/xilem_core/src/lib.rs b/crates/xilem_core/src/lib.rs index 98ad9ef2..657ff0e7 100644 --- a/crates/xilem_core/src/lib.rs +++ b/crates/xilem_core/src/lib.rs @@ -17,7 +17,6 @@ mod any_view; mod id; -mod memoize; mod message; mod sequence; mod vec_splice; diff --git a/crates/xilem_core/src/view/adapt.rs b/crates/xilem_core/src/view/adapt.rs index 27ae79cc..6a8bb27a 100644 --- a/crates/xilem_core/src/view/adapt.rs +++ b/crates/xilem_core/src/view/adapt.rs @@ -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. /// diff --git a/crates/xilem_core/src/memoize.rs b/crates/xilem_core/src/view/memoize.rs similarity index 100% rename from crates/xilem_core/src/memoize.rs rename to crates/xilem_core/src/view/memoize.rs diff --git a/crates/xilem_core/src/view.rs b/crates/xilem_core/src/view/mod.rs similarity index 99% rename from crates/xilem_core/src/view.rs rename to crates/xilem_core/src/view/mod.rs index 53115747..3fc4837e 100644 --- a/crates/xilem_core/src/view.rs +++ b/crates/xilem_core/src/view/mod.rs @@ -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, ...). /// diff --git a/crates/xilem_html/src/view.rs b/crates/xilem_html/src/view.rs index 5c7d74ff..f1df0684 100644 --- a/crates/xilem_html/src/view.rs +++ b/crates/xilem_html/src/view.rs @@ -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. /// diff --git a/src/view/adapt.rs b/src/view/adapt.rs deleted file mode 100644 index 86a861fc..00000000 --- a/src/view/adapt.rs +++ /dev/null @@ -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) -> EventResult, C: View> { - f: F, - child: C, - phantom: PhantomData (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> { - child: &'a C, - state: &'a mut C::State, - id_path: &'a [Id], - event: Box, -} - -impl) -> EventResult, C: View> - Adapt -{ - pub fn new(f: F, child: C) -> Self { - Adapt { - f, - child, - phantom: Default::default(), - } - } -} - -impl<'a, U, B, C: View> AdaptThunk<'a, U, B, C> { - pub fn call(self, app_state: &mut U) -> EventResult { - self.child - .event(self.id_path, self.state, self.event, app_state) - } -} - -impl) -> EventResult + Send, C: View> - View for Adapt -{ - 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, - app_state: &mut T, - ) -> EventResult { - let thunk = AdaptThunk { - child: &self.child, - state, - id_path, - event, - }; - (self.f)(app_state, thunk) - } -} diff --git a/src/view/mod.rs b/src/view/mod.rs index b3a7425f..8bf92e82 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -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}; diff --git a/src/view/view.rs b/src/view/view.rs index 8865074a..92469767 100644 --- a/src/view/view.rs +++ b/src/view/view.rs @@ -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 {