Skip to content

Commit

Permalink
xilem_web: factor pointer to root, as it can be used for all Elements…
Browse files Browse the repository at this point in the history
… and refactor slightly
  • Loading branch information
Philipp-M committed Nov 24, 2023
1 parent e8be32b commit 859e712
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 48 deletions.
49 changes: 24 additions & 25 deletions crates/xilem_web/src/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{View, ViewMarker};
use crate::{Pointer, PointerMsg, View, ViewMarker};
use std::borrow::Cow;

use gloo::events::EventListenerOptions;
Expand Down Expand Up @@ -62,6 +62,10 @@ where
OnEvent::new_with_options(self, event, handler, options)
}

fn pointer<F: Fn(&mut T, PointerMsg)>(self, f: F) -> Pointer<Self, T, A, F> {
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.)
Expand Down Expand Up @@ -412,12 +416,7 @@ dom_interface_macro_and_trait_definitions!(
}
},
SvgElement {
methods: {
// TODO consider stateful event views like this in general
fn pointer<F: Fn(&mut T, crate::svg::pointer::PointerMsg)>(self, f: F) -> crate::svg::pointer::Pointer<T, A, Self, F> {
crate::svg::pointer::pointer(self, f)
}
},
methods: {},
child_interfaces: {
SvgAnimationElement {
methods: {},
Expand Down Expand Up @@ -454,56 +453,56 @@ dom_interface_macro_and_trait_definitions!(
SvgForeignObjectElement { methods: {}, child_interfaces: {} },
SvgGeometryElement {
methods: {
fn stroke(self, brush: impl Into<peniko::Brush>, style: peniko::kurbo::Stroke) -> crate::svg::common_attrs::Stroke<Self, T, A> {
crate::svg::common_attrs::stroke(self, brush, style)
fn stroke(self, brush: impl Into<peniko::Brush>, style: peniko::kurbo::Stroke) -> crate::svg::Stroke<Self, T, A> {
crate::svg::stroke(self, brush, style)
}
},
child_interfaces: {
SvgCircleElement {
methods: {
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::common_attrs::Fill<Self, T, A> {
crate::svg::common_attrs::fill(self, brush)
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::Fill<Self, T, A> {
crate::svg::fill(self, brush)
}
},
child_interfaces: {}
},
SvgEllipseElement {
methods: {
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::common_attrs::Fill<Self, T, A> {
crate::svg::common_attrs::fill(self, brush)
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::Fill<Self, T, A> {
crate::svg::fill(self, brush)
}
},
child_interfaces: {}
},
SvgLineElement { methods: {}, child_interfaces: {} },
SvgPathElement {
methods: {
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::common_attrs::Fill<Self, T, A> {
crate::svg::common_attrs::fill(self, brush)
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::Fill<Self, T, A> {
crate::svg::fill(self, brush)
}
},
child_interfaces: {}
},
SvgPolygonElement {
methods: {
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::common_attrs::Fill<Self, T, A> {
crate::svg::common_attrs::fill(self, brush)
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::Fill<Self, T, A> {
crate::svg::fill(self, brush)
}
},
child_interfaces: {}
},
SvgPolylineElement {
methods: {
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::common_attrs::Fill<Self, T, A> {
crate::svg::common_attrs::fill(self, brush)
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::Fill<Self, T, A> {
crate::svg::fill(self, brush)
}
},
child_interfaces: {}
},
SvgRectElement {
methods: {
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::common_attrs::Fill<Self, T, A> {
crate::svg::common_attrs::fill(self, brush)
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::Fill<Self, T, A> {
crate::svg::fill(self, brush)
}
},
child_interfaces: {}
Expand All @@ -514,11 +513,11 @@ dom_interface_macro_and_trait_definitions!(
SvgSwitchElement { methods: {}, child_interfaces: {} },
SvgTextContentElement {
methods: {
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::common_attrs::Fill<Self, T, A> {
crate::svg::common_attrs::fill(self, brush)
fn fill(self, brush: impl Into<peniko::Brush>) -> crate::svg::Fill<Self, T, A> {
crate::svg::fill(self, brush)
}
fn stroke(self, brush: impl Into<peniko::Brush>, style: peniko::kurbo::Stroke) -> crate::svg::common_attrs::Stroke<Self, T, A> {
crate::svg::common_attrs::stroke(self, brush, style)
fn stroke(self, brush: impl Into<peniko::Brush>, style: peniko::kurbo::Stroke) -> crate::svg::Stroke<Self, T, A> {
crate::svg::stroke(self, brush, style)
}
},
child_interfaces: {
Expand Down
2 changes: 2 additions & 0 deletions crates/xilem_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
view::{DomNode, View, ViewMarker},
};

pub struct Pointer<T, A, V, F> {
pub struct Pointer<V, T, A, F> {
child: V,
callback: F,
phantom: PhantomData<fn() -> (T, A)>,
Expand Down Expand Up @@ -64,19 +64,28 @@ impl PointerDetails {
pub fn pointer<T, A, F: Fn(&mut T, PointerMsg), V: Element<T, A>>(
child: V,
callback: F,
) -> Pointer<T, A, V, F> {
) -> Pointer<V, T, A, F> {
Pointer {
child,
callback,
phantom: Default::default(),
}
}

impl<T, A, V, F> ViewMarker for Pointer<T, A, V, F> {}
crate::interfaces::impl_dom_interfaces_for_ty!(
Element,
Pointer,
vars: <F,>,
vars_on_ty: <F,>,
bounds: {
F: Fn(&mut T, PointerMsg) -> A,
}
);

impl<V, T, A, F> ViewMarker for Pointer<V, T, A, F> {}
impl<V, T, A, F> crate::interfaces::sealed::Sealed for Pointer<V, T, A, F> {}

impl<T, A, F: Fn(&mut T, PointerMsg) -> A + Send, V: View<T, A>> View<T, A>
for Pointer<T, A, V, F>
{
impl<T, A, F: Fn(&mut T, PointerMsg) -> A, V: View<T, A>> View<T, A> for Pointer<V, T, A, F> {
type State = PointerState<V::State>;
type Element = V::Element;

Expand Down
6 changes: 3 additions & 3 deletions crates/xilem_web/src/svg/common_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
20 changes: 10 additions & 10 deletions crates/xilem_web/src/svg/kurbo_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, A> $crate::interfaces::$dom_interface<T, A> 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 {}
Expand Down Expand Up @@ -68,8 +68,8 @@ impl<T, A> View<T, A> 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 {}
Expand Down Expand Up @@ -128,8 +128,8 @@ impl<T, A> View<T, A> 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 {}
Expand Down Expand Up @@ -184,8 +184,8 @@ impl<T, A> View<T, A> 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 {}
Expand Down
3 changes: 1 addition & 2 deletions crates/xilem_web/src/svg/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
3 changes: 1 addition & 2 deletions crates/xilem_web/web_examples/svgtoy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use xilem_web::{
svg::{
kurbo::{self, Rect},
peniko::Color,
PointerMsg,
},
App, View,
App, PointerMsg, View,
};

#[derive(Default)]
Expand Down

0 comments on commit 859e712

Please sign in to comment.