forked from linebender/xilem
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xilem_html: Introduce dom-attributes
- Loading branch information
Showing
20 changed files
with
697 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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,49 @@ | ||
use super::create_dom_attribute_view; | ||
use crate::{interfaces::for_all_dom_interfaces, ChangeFlags}; | ||
use std::borrow::Cow; | ||
|
||
#[derive(PartialEq, Clone, Debug, PartialOrd)] | ||
pub enum ElementAttr { | ||
Class(Cow<'static, str>), | ||
} | ||
|
||
// TODO currently something like el.class("class1").class("class2") will result in "class2" (i.e. overwrite previous uses of class()) which is maybe not what we want. | ||
// There should probably be a way to add/remove classes when composing the element. | ||
create_dom_attribute_view!(class, Cow<'static, str>, Element); | ||
|
||
macro_rules! impl_dom_interface_for_element_dom_attributes { | ||
($dom_interface:ident) => { | ||
impl<T, A, E: $crate::interfaces::$dom_interface<T, A>> | ||
$crate::interfaces::$dom_interface<T, A> for ElementClass<E> | ||
{ | ||
} | ||
}; | ||
} | ||
|
||
// necessary to be different? | ||
for_all_dom_interfaces!(impl_dom_interface_for_element_dom_attributes); | ||
|
||
pub(crate) fn build_dom_attribute(el: &web_sys::Element, attr: &ElementAttr) { | ||
match attr { | ||
ElementAttr::Class(class) => { | ||
// benches show, that className is the fastest way to set the class: (https://www.measurethat.net/Benchmarks/Show/5918/0/classname-vs-setattribute-vs-classlist) | ||
el.set_class_name(class); | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn rebuild_dom_attribute( | ||
el: &web_sys::Element, | ||
old: &ElementAttr, | ||
new: &ElementAttr, | ||
) -> ChangeFlags { | ||
match (old, new) { | ||
(ElementAttr::Class(old_class), ElementAttr::Class(new_class)) | ||
if old_class != new_class => | ||
{ | ||
el.set_class_name(new_class); | ||
ChangeFlags::OTHER_CHANGE | ||
} | ||
_ => ChangeFlags::empty(), | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
crates/xilem_html/src/dom_attributes/html_canvas_element.rs
This file contains 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,42 @@ | ||
use super::create_dom_attribute_view; | ||
use crate::ChangeFlags; | ||
|
||
#[derive(PartialEq, Clone, Debug, PartialOrd)] | ||
pub enum HtmlCanvasElementAttr { | ||
Width(u32), | ||
Height(u32), | ||
} | ||
|
||
create_dom_attribute_view!(width, u32, HtmlCanvasElement); | ||
create_dom_attribute_view!(height, u32, HtmlCanvasElement); | ||
|
||
// TODO there may be less boilerplate heavy (but still flexible and non-mentally challenging/complex ways to express the stuff below...) | ||
|
||
pub(crate) fn build_dom_attribute(el: &web_sys::HtmlCanvasElement, attr: &HtmlCanvasElementAttr) { | ||
match attr { | ||
HtmlCanvasElementAttr::Width(width) => el.set_width(*width), | ||
HtmlCanvasElementAttr::Height(height) => el.set_height(*height), | ||
} | ||
} | ||
|
||
pub(crate) fn rebuild_dom_attribute( | ||
el: &web_sys::HtmlCanvasElement, | ||
old: &HtmlCanvasElementAttr, | ||
new: &HtmlCanvasElementAttr, | ||
) -> ChangeFlags { | ||
match (old, new) { | ||
(HtmlCanvasElementAttr::Width(old_width), HtmlCanvasElementAttr::Width(new_width)) | ||
if old_width != new_width => | ||
{ | ||
el.set_width(*new_width); | ||
ChangeFlags::OTHER_CHANGE | ||
} | ||
(HtmlCanvasElementAttr::Height(old_height), HtmlCanvasElementAttr::Height(new_height)) | ||
if old_height != new_height => | ||
{ | ||
el.set_height(*new_height); | ||
ChangeFlags::OTHER_CHANGE | ||
} | ||
_ => ChangeFlags::empty(), | ||
} | ||
} |
Oops, something went wrong.