Skip to content

Commit

Permalink
xilem_web: Add WithProps alias traits, to remove verbosity in publi…
Browse files Browse the repository at this point in the history
…c API
  • Loading branch information
Philipp-M committed Oct 26, 2024
1 parent 5e09d29 commit 14ed3b2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
30 changes: 6 additions & 24 deletions xilem_web/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ use std::borrow::Cow;

use crate::{
events,
modifiers::{
Attr, Attributes, Class, ClassIter, Classes, Rotate, Scale, ScaleValue, Style, StyleIter,
Styles, With,
},
modifiers::{Attr, Class, ClassIter, Rotate, Scale, ScaleValue, Style, StyleIter, With},
props::{WithElementProps, WithHtmlInputElementProps},
DomNode, DomView, IntoAttributeValue, OptionalAction, Pointer, PointerMsg,
};
use wasm_bindgen::JsCast;
Expand Down Expand Up @@ -51,13 +49,7 @@ macro_rules! event_handler_mixin {
}

pub trait Element<State, Action = ()>:
Sized
+ DomView<
State,
Action,
DomNode: DomNode<Props: With<Attributes> + With<Classes> + With<Styles>>
+ AsRef<web_sys::Element>,
>
Sized + DomView<State, Action, DomNode: DomNode<Props: WithElementProps> + AsRef<web_sys::Element>>
{
/// Set an attribute for an [`Element`]
///
Expand Down Expand Up @@ -331,7 +323,7 @@ pub trait Element<State, Action = ()>:
impl<State, Action, T> Element<State, Action> for T
where
T: DomView<State, Action>,
<T::DomNode as DomNode>::Props: With<Attributes> + With<Classes> + With<Styles>,
<T::DomNode as DomNode>::Props: WithElementProps,
T::DomNode: AsRef<web_sys::Element>,
{
}
Expand Down Expand Up @@ -764,13 +756,7 @@ pub trait HtmlInputElement<State, Action = ()>:
HtmlElement<
State,
Action,
DomNode: DomNode<
Props: With<html_input_element::Checked>
+ With<html_input_element::DefaultChecked>
+ With<html_input_element::Disabled>
+ With<html_input_element::Required>
+ With<html_input_element::Multiple>,
> + AsRef<web_sys::HtmlInputElement>,
DomNode: DomNode<Props: WithHtmlInputElementProps> + AsRef<web_sys::HtmlInputElement>,
>
{
/// See <https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/checked> for more details.
Expand Down Expand Up @@ -857,11 +843,7 @@ impl<State, Action, T> HtmlInputElement<State, Action> for T
where
T: HtmlElement<State, Action>,
T::DomNode: AsRef<web_sys::HtmlInputElement>,
<T::DomNode as DomNode>::Props: With<html_input_element::Checked>
+ With<html_input_element::DefaultChecked>
+ With<html_input_element::Disabled>
+ With<html_input_element::Required>
+ With<html_input_element::Multiple>,
<T::DomNode as DomNode>::Props: WithHtmlInputElementProps,
{
}

Expand Down
7 changes: 7 additions & 0 deletions xilem_web/src/props/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl ElementFlags {
self.0 |= Self::NEEDS_UPDATE;
}
}

// Lazy access to attributes etc. to avoid allocating unnecessary memory when it isn't needed
// Benchmarks have shown, that this can significantly increase performance and reduce memory usage...
/// This holds all the state for a DOM [`Element`](`crate::interfaces::Element`), it is used for [`DomNode::Props`](`crate::DomNode::Props`)
Expand Down Expand Up @@ -175,3 +176,9 @@ impl With<Styles> for Element {
Modifier::new(modifier, &mut self.flags)
}
}

pub trait WithElementProps:
With<Attributes> + With<Children> + With<Classes> + With<Styles>
{
}
impl<T: With<Attributes> + With<Children> + With<Classes> + With<Styles>> WithElementProps for T {}
22 changes: 22 additions & 0 deletions xilem_web/src/props/html_input_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::modifiers::{Modifier, With};
use crate::{props, FromWithContext, Pod, ViewCtx};
use wasm_bindgen::JsCast as _;

use super::WithElementProps;

/// Props specific to an input element.
pub struct HtmlInputElement {
element_props: props::Element,
Expand Down Expand Up @@ -113,3 +115,23 @@ impl With<Multiple> for HtmlInputElement {
Modifier::new(&mut self.multiple, &mut self.element_props.flags)
}
}

pub trait WithHtmlInputElementProps:
WithElementProps
+ With<Checked>
+ With<DefaultChecked>
+ With<Disabled>
+ With<Required>
+ With<Multiple>
{
}
impl<
T: WithElementProps
+ With<Checked>
+ With<DefaultChecked>
+ With<Disabled>
+ With<Required>
+ With<Multiple>,
> WithHtmlInputElementProps for T
{
}

0 comments on commit 14ed3b2

Please sign in to comment.