From 859e7126387d9717342ebc53d9baa7bcb7d91c73 Mon Sep 17 00:00:00 2001 From: Philipp Mildenberger Date: Fri, 24 Nov 2023 22:58:44 +0100 Subject: [PATCH] xilem_web: factor pointer to root, as it can be used for all Elements and refactor slightly --- crates/xilem_web/src/interfaces.rs | 49 +++++++++---------- crates/xilem_web/src/lib.rs | 2 + crates/xilem_web/src/{svg => }/pointer.rs | 21 +++++--- crates/xilem_web/src/svg/common_attrs.rs | 6 +-- crates/xilem_web/src/svg/kurbo_shape.rs | 20 ++++---- crates/xilem_web/src/svg/mod.rs | 3 +- .../xilem_web/web_examples/svgtoy/src/main.rs | 3 +- 7 files changed, 56 insertions(+), 48 deletions(-) rename crates/xilem_web/src/{svg => }/pointer.rs (90%) diff --git a/crates/xilem_web/src/interfaces.rs b/crates/xilem_web/src/interfaces.rs index 056c7957c..0d86e16cb 100644 --- a/crates/xilem_web/src/interfaces.rs +++ b/crates/xilem_web/src/interfaces.rs @@ -1,4 +1,4 @@ -use crate::{View, ViewMarker}; +use crate::{Pointer, PointerMsg, View, ViewMarker}; use std::borrow::Cow; use gloo::events::EventListenerOptions; @@ -62,6 +62,10 @@ where OnEvent::new_with_options(self, event, handler, options) } + fn pointer(self, f: F) -> Pointer { + crate::pointer::pointer(self, f) + } + // TODO should the API be "functional" in the sense, that new attributes are wrappers around the type, // or should they modify the underlying instance (e.g. via the following methods)? // The disadvantage that "functional" brings in, is that elements are not modifiable (i.e. attributes can't be simply added etc.) @@ -412,12 +416,7 @@ dom_interface_macro_and_trait_definitions!( } }, SvgElement { - methods: { - // TODO consider stateful event views like this in general - fn pointer(self, f: F) -> crate::svg::pointer::Pointer { - crate::svg::pointer::pointer(self, f) - } - }, + methods: {}, child_interfaces: { SvgAnimationElement { methods: {}, @@ -454,23 +453,23 @@ dom_interface_macro_and_trait_definitions!( SvgForeignObjectElement { methods: {}, child_interfaces: {} }, SvgGeometryElement { methods: { - fn stroke(self, brush: impl Into, style: peniko::kurbo::Stroke) -> crate::svg::common_attrs::Stroke { - crate::svg::common_attrs::stroke(self, brush, style) + fn stroke(self, brush: impl Into, style: peniko::kurbo::Stroke) -> crate::svg::Stroke { + crate::svg::stroke(self, brush, style) } }, child_interfaces: { SvgCircleElement { methods: { - fn fill(self, brush: impl Into) -> crate::svg::common_attrs::Fill { - crate::svg::common_attrs::fill(self, brush) + fn fill(self, brush: impl Into) -> crate::svg::Fill { + crate::svg::fill(self, brush) } }, child_interfaces: {} }, SvgEllipseElement { methods: { - fn fill(self, brush: impl Into) -> crate::svg::common_attrs::Fill { - crate::svg::common_attrs::fill(self, brush) + fn fill(self, brush: impl Into) -> crate::svg::Fill { + crate::svg::fill(self, brush) } }, child_interfaces: {} @@ -478,32 +477,32 @@ dom_interface_macro_and_trait_definitions!( SvgLineElement { methods: {}, child_interfaces: {} }, SvgPathElement { methods: { - fn fill(self, brush: impl Into) -> crate::svg::common_attrs::Fill { - crate::svg::common_attrs::fill(self, brush) + fn fill(self, brush: impl Into) -> crate::svg::Fill { + crate::svg::fill(self, brush) } }, child_interfaces: {} }, SvgPolygonElement { methods: { - fn fill(self, brush: impl Into) -> crate::svg::common_attrs::Fill { - crate::svg::common_attrs::fill(self, brush) + fn fill(self, brush: impl Into) -> crate::svg::Fill { + crate::svg::fill(self, brush) } }, child_interfaces: {} }, SvgPolylineElement { methods: { - fn fill(self, brush: impl Into) -> crate::svg::common_attrs::Fill { - crate::svg::common_attrs::fill(self, brush) + fn fill(self, brush: impl Into) -> crate::svg::Fill { + crate::svg::fill(self, brush) } }, child_interfaces: {} }, SvgRectElement { methods: { - fn fill(self, brush: impl Into) -> crate::svg::common_attrs::Fill { - crate::svg::common_attrs::fill(self, brush) + fn fill(self, brush: impl Into) -> crate::svg::Fill { + crate::svg::fill(self, brush) } }, child_interfaces: {} @@ -514,11 +513,11 @@ dom_interface_macro_and_trait_definitions!( SvgSwitchElement { methods: {}, child_interfaces: {} }, SvgTextContentElement { methods: { - fn fill(self, brush: impl Into) -> crate::svg::common_attrs::Fill { - crate::svg::common_attrs::fill(self, brush) + fn fill(self, brush: impl Into) -> crate::svg::Fill { + crate::svg::fill(self, brush) } - fn stroke(self, brush: impl Into, style: peniko::kurbo::Stroke) -> crate::svg::common_attrs::Stroke { - crate::svg::common_attrs::stroke(self, brush, style) + fn stroke(self, brush: impl Into, style: peniko::kurbo::Stroke) -> crate::svg::Stroke { + crate::svg::stroke(self, brush, style) } }, child_interfaces: { diff --git a/crates/xilem_web/src/lib.rs b/crates/xilem_web/src/lib.rs index b31a74bf9..a4178cde7 100644 --- a/crates/xilem_web/src/lib.rs +++ b/crates/xilem_web/src/lib.rs @@ -17,6 +17,7 @@ pub mod events; pub mod interfaces; mod one_of; mod optional_action; +mod pointer; pub mod svg; mod vecmap; mod view; @@ -33,6 +34,7 @@ pub use one_of::{ OneSeqOf5, OneSeqOf6, OneSeqOf7, OneSeqOf8, }; pub use optional_action::{Action, OptionalAction}; +pub use pointer::{Pointer, PointerDetails, PointerMsg}; pub use view::{ memoize, static_view, Adapt, AdaptState, AdaptThunk, AnyView, Memoize, MemoizeState, Pod, View, ViewMarker, ViewSequence, diff --git a/crates/xilem_web/src/svg/pointer.rs b/crates/xilem_web/src/pointer.rs similarity index 90% rename from crates/xilem_web/src/svg/pointer.rs rename to crates/xilem_web/src/pointer.rs index 8c67d5e64..7c738a64f 100644 --- a/crates/xilem_web/src/svg/pointer.rs +++ b/crates/xilem_web/src/pointer.rs @@ -16,7 +16,7 @@ use crate::{ view::{DomNode, View, ViewMarker}, }; -pub struct Pointer { +pub struct Pointer { child: V, callback: F, phantom: PhantomData (T, A)>, @@ -64,7 +64,7 @@ impl PointerDetails { pub fn pointer>( child: V, callback: F, -) -> Pointer { +) -> Pointer { Pointer { child, callback, @@ -72,11 +72,20 @@ pub fn pointer>( } } -impl ViewMarker for Pointer {} +crate::interfaces::impl_dom_interfaces_for_ty!( + Element, + Pointer, + vars: , + vars_on_ty: , + bounds: { + F: Fn(&mut T, PointerMsg) -> A, + } +); + +impl ViewMarker for Pointer {} +impl crate::interfaces::sealed::Sealed for Pointer {} -impl A + Send, V: View> View - for Pointer -{ +impl A, V: View> View for Pointer { type State = PointerState; type Element = V::Element; diff --git a/crates/xilem_web/src/svg/common_attrs.rs b/crates/xilem_web/src/svg/common_attrs.rs index 6612c9f8c..e6c8a5199 100644 --- a/crates/xilem_web/src/svg/common_attrs.rs +++ b/crates/xilem_web/src/svg/common_attrs.rs @@ -9,9 +9,9 @@ use xilem_core::{Id, MessageResult}; use crate::interfaces::{ Element, SvgCircleElement, SvgElement, SvgEllipseElement, SvgGeometryElement, - SvgGraphicsElement, SvgPathElement, SvgPolygonElement, SvgPolylineElement, SvgRectElement, - SvgTextContentElement, SvgTextElement, SvgTextPathElement, SvgTextPositioningElement, - SvgtSpanElement, SvgLineElement, + SvgGraphicsElement, SvgLineElement, SvgPathElement, SvgPolygonElement, SvgPolylineElement, + SvgRectElement, SvgTextContentElement, SvgTextElement, SvgTextPathElement, + SvgTextPositioningElement, SvgtSpanElement, }; use crate::IntoAttributeValue; use crate::{ diff --git a/crates/xilem_web/src/svg/kurbo_shape.rs b/crates/xilem_web/src/svg/kurbo_shape.rs index d8a685956..ef4ef39e5 100644 --- a/crates/xilem_web/src/svg/kurbo_shape.rs +++ b/crates/xilem_web/src/svg/kurbo_shape.rs @@ -17,13 +17,13 @@ use crate::{ }; macro_rules! generate_dom_interface_impl { - ($dom_interface:ident, ($ty_name:ident, $t:ident, $a:ident)) => { - impl<$t, $a> $crate::interfaces::$dom_interface<$t, $a> for $ty_name {} + ($dom_interface:ident, ($ty_name:ident)) => { + impl $crate::interfaces::$dom_interface for $ty_name {} }; } -generate_dom_interface_impl!(SvgLineElement, (Line, T, A)); -crate::interfaces::for_all_svg_line_element_ancestors!(generate_dom_interface_impl, (Line, T, A)); +generate_dom_interface_impl!(SvgLineElement, (Line)); +crate::interfaces::for_all_svg_line_element_ancestors!(generate_dom_interface_impl, (Line)); impl ViewMarker for Line {} impl Sealed for Line {} @@ -68,8 +68,8 @@ impl View for Line { } } -generate_dom_interface_impl!(SvgRectElement, (Rect, T, A)); -crate::interfaces::for_all_svg_rect_element_ancestors!(generate_dom_interface_impl, (Rect, T, A)); +generate_dom_interface_impl!(SvgRectElement, (Rect)); +crate::interfaces::for_all_svg_rect_element_ancestors!(generate_dom_interface_impl, (Rect)); impl ViewMarker for Rect {} impl Sealed for Rect {} @@ -128,8 +128,8 @@ impl View for Rect { } } -generate_dom_interface_impl!(SvgCircleElement, (Circle, T, A)); -crate::interfaces::for_all_svg_circle_element_ancestors!(generate_dom_interface_impl, (Circle, T, A)); +generate_dom_interface_impl!(SvgCircleElement, (Circle)); +crate::interfaces::for_all_svg_circle_element_ancestors!(generate_dom_interface_impl, (Circle)); impl ViewMarker for Circle {} impl Sealed for Circle {} @@ -184,8 +184,8 @@ impl View for Circle { } } -generate_dom_interface_impl!(SvgPathElement, (BezPath, T, A)); -crate::interfaces::for_all_svg_path_element_ancestors!(generate_dom_interface_impl, (BezPath, T, A)); +generate_dom_interface_impl!(SvgPathElement, (BezPath)); +crate::interfaces::for_all_svg_path_element_ancestors!(generate_dom_interface_impl, (BezPath)); impl ViewMarker for BezPath {} impl Sealed for BezPath {} diff --git a/crates/xilem_web/src/svg/mod.rs b/crates/xilem_web/src/svg/mod.rs index d80f42c83..f1b2815c2 100644 --- a/crates/xilem_web/src/svg/mod.rs +++ b/crates/xilem_web/src/svg/mod.rs @@ -1,7 +1,6 @@ pub(crate) mod common_attrs; pub(crate) mod kurbo_shape; -pub mod pointer; +pub use common_attrs::{fill, stroke, Fill, Stroke}; pub use peniko; pub use peniko::kurbo; -pub use pointer::{PointerDetails, PointerMsg}; diff --git a/crates/xilem_web/web_examples/svgtoy/src/main.rs b/crates/xilem_web/web_examples/svgtoy/src/main.rs index f12480db9..fbefb395b 100644 --- a/crates/xilem_web/web_examples/svgtoy/src/main.rs +++ b/crates/xilem_web/web_examples/svgtoy/src/main.rs @@ -8,9 +8,8 @@ use xilem_web::{ svg::{ kurbo::{self, Rect}, peniko::Color, - PointerMsg, }, - App, View, + App, PointerMsg, View, }; #[derive(Default)]