use dioxus::core::{
Attribute, AttributeValue, DynamicNode, Element, Template, TemplateAttribute, TemplateNode,
VNode,
};
use crate::IconProps;
type AttributeDescription = (&'static str, Option<&'static str>, bool);
const SVG_NAMESPACE: Option<&'static str> = Some("http://www.w3.org/2000/svg");
const ICON_ATTR_PATHS: &[&[u8]] = &[&[0u8]];
static DEFAULT_SVG_ATTRS: [TemplateAttribute; 1] = [dynamic_attr(0)];
const XMLNS: AttributeDescription = ("xmlns", None, false);
const WIDTH: AttributeDescription = ("width", None, false);
const HEIGHT: AttributeDescription = ("height", None, false);
const VIEW_BOX: AttributeDescription = ("viewBox", None, false);
#[inline]
pub(crate) const fn icon_template(roots: &'static [TemplateNode]) -> Template {
Template {
roots,
node_paths: &[],
attr_paths: ICON_ATTR_PATHS,
}
}
#[inline]
pub(crate) const fn svg(children: &'static [TemplateNode]) -> TemplateNode {
element_with_attrs("svg", &DEFAULT_SVG_ATTRS, children)
}
#[inline]
pub(crate) const fn element(
tag: &'static str,
attrs: &'static [TemplateAttribute],
children: &'static [TemplateNode],
) -> TemplateNode {
element_with_attrs(tag, attrs, children)
}
#[inline]
const fn element_with_attrs(
tag: &'static str,
attrs: &'static [TemplateAttribute],
children: &'static [TemplateNode],
) -> TemplateNode {
TemplateNode::Element {
tag,
namespace: SVG_NAMESPACE,
attrs,
children,
}
}
#[inline]
pub(crate) const fn attr(name: &'static str, value: &'static str) -> TemplateAttribute {
TemplateAttribute::Static {
name,
value,
namespace: None,
}
}
#[inline]
const fn dynamic_attr(id: usize) -> TemplateAttribute {
TemplateAttribute::Dynamic { id }
}
#[inline]
fn icon_attr(
(name, namespace, volatile): AttributeDescription,
value: AttributeValue,
) -> Attribute {
Attribute {
name,
value,
namespace,
volatile,
}
}
#[inline]
pub(crate) fn icon_element(
template: Template,
view_box: &'static str,
root_static_attrs: &'static [(&'static str, &'static str)],
props: IconProps,
) -> Element {
let IconProps { size, attributes } = props;
let size = size.into_value();
let mut root_attributes = Vec::with_capacity(attributes.len() + root_static_attrs.len() + 4);
push_default_attr(
&mut root_attributes,
&attributes,
XMLNS,
"http://www.w3.org/2000/svg",
);
push_default_attr_value(&mut root_attributes, &attributes, WIDTH, size.clone());
push_default_attr_value(&mut root_attributes, &attributes, HEIGHT, size);
push_default_attr(&mut root_attributes, &attributes, VIEW_BOX, view_box);
for &(name, value) in root_static_attrs {
push_default_attr(&mut root_attributes, &attributes, (name, None, false), value);
}
root_attributes.extend(attributes);
let dynamic_attributes = Box::new([root_attributes.into_boxed_slice()]);
let dynamic_nodes: Box<[DynamicNode]> = Box::new([]);
Ok(VNode::new(None, template, dynamic_nodes, dynamic_attributes))
}
#[inline]
fn push_default_attr(
output: &mut Vec<Attribute>,
attributes: &[Attribute],
description: AttributeDescription,
value: &str,
) {
if !has_attr(attributes, description) {
push_default_attr_value(
output,
attributes,
description,
AttributeValue::Text(value.to_owned()),
);
}
}
#[inline]
fn push_default_attr_value(
output: &mut Vec<Attribute>,
attributes: &[Attribute],
description: AttributeDescription,
value: AttributeValue,
) {
if !has_attr(attributes, description) {
output.push(icon_attr(description, value));
}
}
#[inline]
fn has_attr(attributes: &[Attribute], (name, namespace, _): AttributeDescription) -> bool {
attributes
.iter()
.any(|attribute| attribute.name == name && attribute.namespace == namespace)
}