-
Notifications
You must be signed in to change notification settings - Fork 184
add class and style interfaces #193
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8f31522
add class interface
richard-uk1 c2ca085
add style interface
richard-uk1 a00b158
remove parse style/class logic, and panic instead (in dev)
richard-uk1 c4faf53
combine attributes, styles, classes into single struct
richard-uk1 cbeef67
make `class` and `style` methods polymorphic
richard-uk1 76bb994
fix clippy
richard-uk1 6240b7f
Update Cargo.toml
richard-uk1 f29a402
Update crates/xilem_web/src/context.rs
richard-uk1 92f26a1
Update crates/xilem_web/src/context.rs
richard-uk1 678ced5
Update crates/xilem_web/src/class.rs
richard-uk1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| use std::{borrow::Cow, marker::PhantomData}; | ||
|
|
||
| use xilem_core::{Id, MessageResult}; | ||
|
|
||
| use crate::{ | ||
| interfaces::{sealed::Sealed, Element}, | ||
| ChangeFlags, Cx, View, ViewMarker, | ||
| }; | ||
|
|
||
| /// A trait to make the class adding functions generic over collection type | ||
| pub trait IntoClasses { | ||
| fn into_classes(self, classes: &mut Vec<Cow<'static, str>>); | ||
| } | ||
|
|
||
| impl IntoClasses for String { | ||
| fn into_classes(self, classes: &mut Vec<Cow<'static, str>>) { | ||
| classes.push(self.into()); | ||
| } | ||
| } | ||
|
|
||
| impl IntoClasses for &'static str { | ||
| fn into_classes(self, classes: &mut Vec<Cow<'static, str>>) { | ||
| classes.push(self.into()); | ||
| } | ||
| } | ||
|
|
||
| impl IntoClasses for Cow<'static, str> { | ||
| fn into_classes(self, classes: &mut Vec<Cow<'static, str>>) { | ||
| classes.push(self); | ||
| } | ||
| } | ||
|
|
||
| impl<T> IntoClasses for Option<T> | ||
| where | ||
| T: IntoClasses, | ||
| { | ||
| fn into_classes(self, classes: &mut Vec<Cow<'static, str>>) { | ||
| if let Some(t) = self { | ||
| t.into_classes(classes); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T> IntoClasses for Vec<T> | ||
| where | ||
| T: IntoClasses, | ||
| { | ||
| fn into_classes(self, classes: &mut Vec<Cow<'static, str>>) { | ||
| for itm in self { | ||
| itm.into_classes(classes); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| macro_rules! impl_tuple_intoclasses { | ||
| ($($name:ident : $type:ident),* $(,)?) => { | ||
| impl<$($type),*> IntoClasses for ($($type,)*) | ||
| where | ||
| $($type: IntoClasses),* | ||
| { | ||
| #[allow(unused_variables)] | ||
| fn into_classes(self, classes: &mut Vec<Cow<'static, str>>) { | ||
| let ($($name,)*) = self; | ||
| $( | ||
| $name.into_classes(classes); | ||
| )* | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| impl_tuple_intoclasses!(); | ||
| impl_tuple_intoclasses!(t1: T1); | ||
| impl_tuple_intoclasses!(t1: T1, t2: T2); | ||
| impl_tuple_intoclasses!(t1: T1, t2: T2, t3: T3); | ||
| impl_tuple_intoclasses!(t1: T1, t2: T2, t3: T3, t4: T4); | ||
|
|
||
| /// Applies a class to the underlying element. | ||
| pub struct Class<E, T, A> { | ||
| pub(crate) element: E, | ||
| pub(crate) class_names: Vec<Cow<'static, str>>, | ||
| pub(crate) phantom: PhantomData<fn() -> (T, A)>, | ||
| } | ||
|
|
||
| impl<E, T, A> ViewMarker for Class<E, T, A> {} | ||
| impl<E, T, A> Sealed for Class<E, T, A> {} | ||
|
|
||
| impl<E: Element<T, A>, T, A> View<T, A> for Class<E, T, A> { | ||
| type State = E::State; | ||
| type Element = E::Element; | ||
|
|
||
| fn build(&self, cx: &mut Cx) -> (Id, Self::State, Self::Element) { | ||
| for class_name in &self.class_names { | ||
| cx.add_class_to_element(class_name); | ||
| } | ||
| self.element.build(cx) | ||
| } | ||
|
|
||
| fn rebuild( | ||
| &self, | ||
| cx: &mut Cx, | ||
| prev: &Self, | ||
| id: &mut Id, | ||
| state: &mut Self::State, | ||
| element: &mut Self::Element, | ||
| ) -> ChangeFlags { | ||
| for class_name in &self.class_names { | ||
| cx.add_class_to_element(class_name); | ||
| } | ||
| self.element.rebuild(cx, &prev.element, id, state, element) | ||
| } | ||
|
|
||
| fn message( | ||
| &self, | ||
| id_path: &[Id], | ||
| state: &mut Self::State, | ||
| message: Box<dyn std::any::Any>, | ||
| app_state: &mut T, | ||
| ) -> MessageResult<A> { | ||
| self.element.message(id_path, state, message, app_state) | ||
| } | ||
| } | ||
|
|
||
| crate::interfaces::impl_dom_interfaces_for_ty!(Element, Class); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.