赞
踩
示例代码:
#![cfg_attr( all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows" )] use freya::prelude::*; fn main() { launch_with_props(app, "Counter", (400.0, 350.0)); } fn app() -> Element { let mut count = use_signal(|| 0); rsx!( rect { height: "50%", width: "100%", main_align: "center", cross_align: "center", background: "rgb(0, 119, 182)", color: "white", shadow: "0 4 20 5 rgb(0, 0, 0, 80)", label { font_size: "75", font_weight: "bold", "{count}" } } rect { height: "50%", width: "100%", main_align: "center", cross_align: "center", direction: "horizontal", Button { onclick: move |_| count += 1, label { "Increase" } } Button { onclick: move |_| count -= 1, label { "Decrease" } } } ) }
显示效果:
rsx!宏展开后,等价代码:
use freya::prelude::*; fn main() { launch_with_props(app, "Counter", (400.0, 350.0)); } fn app() -> Element { let mut count = use_signal(|| 0); Some({ static TEMPLATE: dioxus_core::Template = dioxus_core::Template { name: "examples/counter.rs:15:5:283", roots: &[ dioxus_core::TemplateNode::Element { tag: dioxus_elements::rect::TAG_NAME, namespace: dioxus_elements::rect::NAME_SPACE, attrs: &[ dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::height.0, namespace: dioxus_elements::rect::height.1, value: "50%", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::width.0, namespace: dioxus_elements::rect::width.1, value: "100%", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::main_align.0, namespace: dioxus_elements::rect::main_align.1, value: "center", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::cross_align.0, namespace: dioxus_elements::rect::cross_align.1, value: "center", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::background.0, namespace: dioxus_elements::rect::background.1, value: "rgb(0, 119, 182)", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::color.0, namespace: dioxus_elements::rect::color.1, value: "white", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::shadow.0, namespace: dioxus_elements::rect::shadow.1, value: "0 4 20 5 rgb(0, 0, 0, 80)", }, ], children: &[ dioxus_core::TemplateNode::Element { tag: dioxus_elements::label::TAG_NAME, namespace: dioxus_elements::label::NAME_SPACE, attrs: &[ dioxus_core::TemplateAttribute::Static { name: dioxus_elements::label::font_size.0, namespace: dioxus_elements::label::font_size.1, value: "75", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::label::font_weight.0, namespace: dioxus_elements::label::font_weight.1, value: "bold", }, ], children: &[ dioxus_core::TemplateNode::DynamicText { id: 0usize, }, ], }, ], }, dioxus_core::TemplateNode::Element { tag: dioxus_elements::rect::TAG_NAME, namespace: dioxus_elements::rect::NAME_SPACE, attrs: &[ dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::height.0, namespace: dioxus_elements::rect::height.1, value: "50%", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::width.0, namespace: dioxus_elements::rect::width.1, value: "100%", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::main_align.0, namespace: dioxus_elements::rect::main_align.1, value: "center", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::cross_align.0, namespace: dioxus_elements::rect::cross_align.1, value: "center", }, dioxus_core::TemplateAttribute::Static { name: dioxus_elements::rect::direction.0, namespace: dioxus_elements::rect::direction.1, value: "horizontal", }, ], children: &[ dioxus_core::TemplateNode::Dynamic { id: 1usize, }, dioxus_core::TemplateNode::Dynamic { id: 2usize, }, ], }, ], node_paths: &[&[0u8, 0u8, 0u8], &[1u8, 0u8], &[1u8, 1u8]], attr_paths: &[], }; dioxus_core::VNode::new( None, TEMPLATE, Box::new([ dioxus_core::DynamicNode::Text( dioxus_core::VText::new((count).to_string().to_string()), ), dioxus_core::DynamicNode::Component({ use dioxus_core::prelude::Properties; (fc_to_builder(Button) .onclick(EventHandler::new(move |_| count += 1)) .children( Some({ static TEMPLATE: dioxus_core::Template = dioxus_core::Template { name: "examples/counter.rs:15:5:933", roots: &[ dioxus_core::TemplateNode::Element { tag: dioxus_elements::label::TAG_NAME, namespace: dioxus_elements::label::NAME_SPACE, attrs: &[], children: &[ dioxus_core::TemplateNode::Text { text: "Increase", }, ], }, ], node_paths: &[], attr_paths: &[], }; dioxus_core::VNode::new( None, TEMPLATE, Box::new([]), Box::new([]), ) }), ) .build()) .into_vcomponent(Button, "Button") }), dioxus_core::DynamicNode::Component({ use dioxus_core::prelude::Properties; (fc_to_builder(Button) .onclick(EventHandler::new(move |_| count -= 1)) .children( Some({ static TEMPLATE: dioxus_core::Template = dioxus_core::Template { name: "examples/counter.rs:15:5:1051", roots: &[ dioxus_core::TemplateNode::Element { tag: dioxus_elements::label::TAG_NAME, namespace: dioxus_elements::label::NAME_SPACE, attrs: &[], children: &[ dioxus_core::TemplateNode::Text { text: "Decrease", }, ], }, ], node_paths: &[], attr_paths: &[], }; dioxus_core::VNode::new( None, TEMPLATE, Box::new([]), Box::new([]), ) }), ) .build()) .into_vcomponent(Button, "Button") }), ]), Box::new([]), ) }) }
最终数据结构:
Some( VNode { vnode: VNodeInner { key: None, template: Cell { value: Template { name: "examples/counter.rs:15:16:294", roots: [ Element { tag: "rect", namespace: None, attrs: [ Static { name: "height", value: "50%", namespace: None, }, Static { name: "width", value: "100%", namespace: None, }, Static { name: "main_align", value: "center", namespace: None, }, Static { name: "cross_align", value: "center", namespace: None, }, Static { name: "background", value: "rgb(0, 119, 182)", namespace: None, }, Static { name: "color", value: "white", namespace: None, }, Static { name: "shadow", value: "0 4 20 5 rgb(0, 0, 0, 80)", namespace: None, }, ], children: [ Element { tag: "label", namespace: None, attrs: [ Static { name: "font_size", value: "75", namespace: None, }, Static { name: "font_weight", value: "bold", namespace: None, }, ], children: [ DynamicText { id: 0, }, ], }, ], }, Element { tag: "rect", namespace: None, attrs: [ Static { name: "height", value: "50%", namespace: None, }, Static { name: "width", value: "100%", namespace: None, }, Static { name: "main_align", value: "center", namespace: None, }, Static { name: "cross_align", value: "center", namespace: None, }, Static { name: "direction", value: "horizontal", namespace: None, }, ], children: [ Dynamic { id: 1, }, Dynamic { id: 2, }, ], }, ], node_paths: [ [ 0, 0, 0, ], [ 1, 0, ], [ 1, 1, ], ], attr_paths: [], }, }, dynamic_nodes: [ Text( VText { value: "0", }, ), Component( VComponent { name: "Button", }, ), Component( VComponent { name: "Button", }, ), ], dynamic_attrs: [], }, mount: Cell { value: MountId( 18446744073709551615, ), }, }, )
类似于JSON的数据结构:
{ "Some": { "VNode": { "vnode": { "key": null, "template": { "value": { "name": "examples/counter.rs:15:16:294", "roots": [ { "tag": "rect", "namespace": null, "attrs": [ { "name": "height", "value": "50%", "namespace": null }, { "name": "width", "value": "100%", "namespace": null }, { "name": "main_align", "value": "center", "namespace": null }, { "name": "cross_align", "value": "center", "namespace": null }, { "name": "background", "value": "rgb(0, 119, 182)", "namespace": null }, { "name": "color", "value": "white", "namespace": null }, { "name": "shadow", "value": "0 4 20 5 rgb(0, 0, 0, 80)", "namespace": null } ], "children": [ { "tag": "label", "namespace": null, "attrs": [ { "name": "font_size", "value": "75", "namespace": null }, { "name": "font_weight", "value": "bold", "namespace": null } ], "children": [ { "id": 0 } ] } ] }, { "tag": "rect", "namespace": null, "attrs": [ { "name": "height", "value": "50%", "namespace": null }, { "name": "width", "value": "100%", "namespace": null }, { "name": "main_align", "value": "center", "namespace": null }, { "name": "cross_align", "value": "center", "namespace": null }, { "name": "direction", "value": "horizontal", "namespace": null } ], "children": [ { "id": 1 }, { "id": 2 } ] } ], "node_paths": [ [ 0, 0, 0 ], [ 1, 0 ], [ 1, 1 ] ], "attr_paths": [] } }, "dynamic_nodes": [ { "Text": { "value": "0" } }, { "Component": { "name": "Button" } }, { "Component": { "name": "Button" } } ], "dynamic_attrs": [] }, "mount": { "value": { "MountId": 18446744073709551615 } } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。