Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xilem_web: Add a few boolean attribute modifiers for HtmlInputElement and factor element state flags out of these modifiers into ElementFlags #717

Merged

Conversation

Philipp-M
Copy link
Contributor

@Philipp-M Philipp-M commented Oct 26, 2024

This PR starts with supporting specialized elements (i.e. more than just the Element DOM interface), starting with boolean attributes in HtmlInputElement (checked, default_checked, disabled, required, multiple).
With a general OverwriteBool modifier which is optimized by encoding boolean flags into a u32, thus not requiring allocations (compared to the other modifiers we currently have), but is limited to max 32 overwrites, which I don't think should be a real-world limitation (i.e. input_el.checked().checked()...checked() 32 times makes no sense), I also started adding tests for this, as it juggles around with bit-operations (and we should generally start writing more tests).
I have originally planned to feature-flag this (i.e. have a feature like HtmlInputElement).
But I'd like to see how far we can go without this, I haven't yet noticed significant (binary-size) regressions in the todomvc example (which uses input elements) that justifies the worse DX that additional features introduce.
Having features for this is also not optimal for another reason: It changes the API (e.g. DomNode::Props is props::HtmlInputElement instead of props::Element.

It also factors the element state flags (was_created, in_hydration) into a separate ElementFlags which is shared within a Modifier<M> struct (similar as PodMut or WidgetMut in masonry), so that we don't have to duplicate that state in every modifier. Additionally a new flag needs_update is introduced, which indicates that the element in general needs to update any modifier, and is entirely an optimization to avoid checking every modifier whether it has changed (not yet that important, but when we have a lot of modifiers per element, having to check every modifier is less efficient, it's also already slightly visible in the js-framework-benchmark).
For this, it unfortunately suffers similar as #705 from the free-form syntax by being a little bit more verbose (which may be reverted after arbitrary_self_types are stable).

It should also fix #716

…anages general element flags (was_created/in_hydration etc.), to avoid duplicating this state in every modifier.
@Philipp-M Philipp-M force-pushed the xilem_web-input-element-modifiers branch from 14ed3b2 to fe23251 Compare October 26, 2024 16:16
@flosse flosse added the web label Oct 26, 2024
Comment on lines 825 to 838
/// See <https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/multiple> for more details.
///
/// # Examples
///
/// ```
/// use xilem_web::{interfaces::{Element, HtmlInputElement}, elements::html::input};
///
/// # fn component() -> impl HtmlInputElement<()> {
/// input(()).required(true) // results in <input required></input>
/// # }
/// ```
fn multiple(self, required: bool) -> html_input_element::view::Multiple<Self, State, Action> {
html_input_element::view::Multiple::new(self, required)
}
Copy link
Contributor

@RagibHasin RagibHasin Oct 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a copy-paste artifact. That is the parameter should be named multiple as well and the doc is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this, I went over the rest of the doc-comments and have found a few inconsistencies as well.
I hope I have now found and fixed all...
I've also renamed With to WithModifier as it seems more appropriate, and it's likely summed up in these alias traits anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also renamed With to WithModifier as it seems more appropriate

👍 It looks like a small change, but from my point of view it is enormously helpful for readability! 👍

Copy link
Contributor

@flosse flosse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only skimmed the code, but at least nothing obviously problematic caught my eye (as far as i can judge at all).

xilem_web/src/interfaces.rs Outdated Show resolved Hide resolved
@Philipp-M
Copy link
Contributor Author

Thanks for the reviews :)

@Philipp-M Philipp-M added this pull request to the merge queue Oct 28, 2024
Merged via the queue into linebender:main with commit f32277a Oct 28, 2024
17 checks passed
@Philipp-M Philipp-M deleted the xilem_web-input-element-modifiers branch October 28, 2024 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

xilem_web svg regression: Missing stroke attribute
3 participants