Skip to content

Commit

Permalink
xilem_web: Refactor unnecessary generic type parameter Props to ass…
Browse files Browse the repository at this point in the history
…ociated type (#619)

This was the initial plan of #403, I can't remember anymore why I didn't
made it to work back then. Anyway this should simplify the type
signature of `Pod` and `PodMut` a little bit.

This also includes a little bit of refactoring otherwise (mostly
aesthetic + removal of unnecessary wrapper `DynNode`)
  • Loading branch information
Philipp-M authored Sep 28, 2024
1 parent c0de26f commit 67f0043
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 219 deletions.
12 changes: 10 additions & 2 deletions xilem_web/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ impl WithAttributes for ElementProps {
}
}

impl<E: DomNode<P>, P: WithAttributes> WithAttributes for Pod<E, P> {
impl<N> WithAttributes for Pod<N>
where
N: DomNode,
N::Props: WithAttributes,
{
fn rebuild_attribute_modifier(&mut self) {
self.props.rebuild_attribute_modifier();
}
Expand All @@ -230,7 +234,11 @@ impl<E: DomNode<P>, P: WithAttributes> WithAttributes for Pod<E, P> {
}
}

impl<E: DomNode<P>, P: WithAttributes> WithAttributes for PodMut<'_, E, P> {
impl<N> WithAttributes for PodMut<'_, N>
where
N: DomNode,
N::Props: WithAttributes,
{
fn rebuild_attribute_modifier(&mut self) {
self.props.rebuild_attribute_modifier();
}
Expand Down
10 changes: 8 additions & 2 deletions xilem_web/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ impl WithClasses for ElementProps {
}
}

impl<E: DomNode<P>, P: WithClasses> WithClasses for Pod<E, P> {
impl<N: DomNode> WithClasses for Pod<N>
where
N::Props: WithClasses,
{
fn rebuild_class_modifier(&mut self) {
self.props.rebuild_class_modifier();
}
Expand All @@ -256,7 +259,10 @@ impl<E: DomNode<P>, P: WithClasses> WithClasses for Pod<E, P> {
}
}

impl<E: DomNode<P>, P: WithClasses> WithClasses for PodMut<'_, E, P> {
impl<N: DomNode> WithClasses for PodMut<'_, N>
where
N::Props: WithClasses,
{
fn rebuild_class_modifier(&mut self) {
self.props.rebuild_class_modifier();
}
Expand Down
2 changes: 1 addition & 1 deletion xilem_web/src/element_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl ElementProps {
}
}

impl Pod<web_sys::Element, ElementProps> {
impl Pod<web_sys::Element> {
/// Creates a new Pod with [`web_sys::Element`] as element and `ElementProps` as its [`DomView::Props`](`crate::DomView::Props`)
pub fn new_element(children: Vec<AnyPod>, ns: &str, elem_name: &str) -> Self {
let element = document()
Expand Down
18 changes: 9 additions & 9 deletions xilem_web/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ where
State: 'static,
Action: 'static,
Element: 'static,
Element: From<Pod<web_sys::Element, ElementProps>>,
Element: From<Pod<web_sys::Element>>,
{
let mut elements = AppendVec::default();
#[cfg(feature = "hydration")]
Expand All @@ -269,15 +269,15 @@ where
pub(crate) fn rebuild_element<'el, State, Action, Element>(
children: &dyn DomViewSequence<State, Action>,
prev_children: &dyn DomViewSequence<State, Action>,
element: Mut<'el, Pod<Element, ElementProps>>,
element: Mut<'el, Pod<Element>>,
state: &mut ElementState,
ctx: &mut ViewCtx,
) -> Mut<'el, Pod<Element, ElementProps>>
) -> Mut<'el, Pod<Element>>
where
State: 'static,
Action: 'static,
Element: 'static,
Element: DomNode<ElementProps>,
Element: DomNode<Props = ElementProps>,
{
let mut dom_children_splice = DomChildrenSplice::new(
&mut state.append_scratch,
Expand All @@ -300,14 +300,14 @@ where

pub(crate) fn teardown_element<State, Action, Element>(
children: &dyn DomViewSequence<State, Action>,
element: Mut<'_, Pod<Element, ElementProps>>,
element: Mut<'_, Pod<Element>>,
state: &mut ElementState,
ctx: &mut ViewCtx,
) where
State: 'static,
Action: 'static,
Element: 'static,
Element: DomNode<ElementProps>,
Element: DomNode<Props = ElementProps>,
{
let mut dom_children_splice = DomChildrenSplice::new(
&mut state.append_scratch,
Expand Down Expand Up @@ -353,7 +353,7 @@ where
State: 'static,
Action: 'static,
{
type Element = Pod<web_sys::HtmlElement, ElementProps>;
type Element = Pod<web_sys::HtmlElement>;

type ViewState = ElementState;

Expand Down Expand Up @@ -444,7 +444,7 @@ macro_rules! define_element {
State: 'static,
Action: 'static,
{
type Element = Pod<web_sys::$dom_interface, ElementProps>;
type Element = Pod<web_sys::$dom_interface>;

type ViewState = ElementState;

Expand Down Expand Up @@ -501,7 +501,7 @@ macro_rules! define_elements {
use super::{build_element, rebuild_element, teardown_element, DomViewSequence, ElementState};
use crate::{
core::{MessageResult, Mut, ViewId, ViewMarker},
DomFragment, DynMessage, ElementProps, Pod, View, ViewCtx,
DomFragment, DynMessage, Pod, View, ViewCtx,
};
$(define_element!(crate::$ns, $element_def);)*
};
Expand Down
19 changes: 12 additions & 7 deletions xilem_web/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

//! Opinionated extension traits roughly resembling their equivalently named DOM interfaces.
//!
//! It is used for DOM elements, e.g. created with [`html::span`](`crate::elements::html::span`) to modify the underlying element, such as [`Element::attr`] or [`HtmlElement::style`]
//!
//! These traits can also be used as return type of components to allow modifying the underlying DOM element that is returned.
Expand All @@ -18,7 +19,7 @@ use crate::{
class::{AsClassIter, Class, WithClasses},
events,
style::{IntoStyles, Style, WithStyle},
DomView, IntoAttributeValue, OptionalAction, Pointer, PointerMsg,
DomNode, DomView, IntoAttributeValue, OptionalAction, Pointer, PointerMsg,
};
use wasm_bindgen::JsCast;

Expand Down Expand Up @@ -50,7 +51,11 @@ macro_rules! event_handler_mixin {

pub trait Element<State, Action = ()>:
Sized
+ DomView<State, Action, Props: WithAttributes + WithClasses, DomNode: AsRef<web_sys::Element>>
+ DomView<
State,
Action,
DomNode: DomNode<Props: WithAttributes + WithClasses> + AsRef<web_sys::Element>,
>
{
/// Set an attribute for an [`Element`]
///
Expand Down Expand Up @@ -270,7 +275,7 @@ pub trait Element<State, Action = ()>:
impl<State, Action, T> Element<State, Action> for T
where
T: DomView<State, Action>,
T::Props: WithAttributes + WithClasses,
<T::DomNode as DomNode>::Props: WithAttributes + WithClasses,
T::DomNode: AsRef<web_sys::Element>,
{
}
Expand Down Expand Up @@ -495,7 +500,7 @@ where

// #[cfg(feature = "HtmlElement")]
pub trait HtmlElement<State, Action = ()>:
Element<State, Action, Props: WithStyle, DomNode: AsRef<web_sys::HtmlElement>>
Element<State, Action, DomNode: DomNode<Props: WithStyle> + AsRef<web_sys::HtmlElement>>
{
/// Set a style attribute
fn style(self, style: impl IntoStyles) -> Style<Self, State, Action> {
Expand All @@ -510,7 +515,7 @@ impl<State, Action, T> HtmlElement<State, Action> for T
where
T: Element<State, Action>,
T::DomNode: AsRef<web_sys::HtmlElement>,
T::Props: WithStyle,
<T::DomNode as DomNode>::Props: WithStyle,
{
}

Expand Down Expand Up @@ -1472,7 +1477,7 @@ where

// #[cfg(feature = "SvgElement")]
pub trait SvgElement<State, Action = ()>:
Element<State, Action, Props: WithStyle, DomNode: AsRef<web_sys::SvgElement>>
Element<State, Action, DomNode: DomNode<Props: WithStyle> + AsRef<web_sys::SvgElement>>
{
/// Set a style attribute
fn style(self, style: impl IntoStyles) -> Style<Self, State, Action> {
Expand All @@ -1487,7 +1492,7 @@ impl<State, Action, T> SvgElement<State, Action> for T
where
T: Element<State, Action>,
T::DomNode: AsRef<web_sys::SvgElement>,
T::Props: WithStyle,
<T::DomNode as DomNode>::Props: WithStyle,
{
}

Expand Down
Loading

0 comments on commit 67f0043

Please sign in to comment.