custom_element.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. use dioxus::prelude::*;
  2. use dioxus_native_core::{custom_element::CustomElement, prelude::*};
  3. use dioxus_native_core_macro::partial_derive_state;
  4. use shipyard::Component;
  5. use tokio::time::sleep;
  6. #[derive(Debug, Clone, PartialEq, Eq, Default, Component)]
  7. pub struct BlablaState {
  8. count: usize,
  9. }
  10. #[partial_derive_state]
  11. impl State for BlablaState {
  12. type ParentDependencies = (Self,);
  13. type ChildDependencies = ();
  14. type NodeDependencies = ();
  15. const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new()
  16. .with_attrs(AttributeMaskBuilder::Some(&["blabla"]))
  17. .with_element();
  18. fn update<'a>(
  19. &mut self,
  20. _: NodeView,
  21. _: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
  22. parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
  23. _: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
  24. _: &SendAnyMap,
  25. ) -> bool {
  26. if let Some((parent,)) = parent {
  27. if parent.count != 0 {
  28. self.count += 1;
  29. }
  30. }
  31. true
  32. }
  33. fn create<'a>(
  34. node_view: NodeView<()>,
  35. node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
  36. parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
  37. children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
  38. context: &SendAnyMap,
  39. ) -> Self {
  40. let mut myself = Self::default();
  41. myself.update(node_view, node, parent, children, context);
  42. myself
  43. }
  44. }
  45. mod dioxus_elements {
  46. macro_rules! builder_constructors {
  47. (
  48. $(
  49. $(#[$attr:meta])*
  50. $name:ident {
  51. $(
  52. $(#[$attr_method:meta])*
  53. $fil:ident: $vil:ident,
  54. )*
  55. };
  56. )*
  57. ) => {
  58. $(
  59. #[allow(non_camel_case_types)]
  60. $(#[$attr])*
  61. pub struct $name;
  62. impl $name {
  63. pub const TAG_NAME: &'static str = stringify!($name);
  64. pub const NAME_SPACE: Option<&'static str> = None;
  65. $(
  66. pub const $fil: (&'static str, Option<&'static str>, bool) = (stringify!($fil), None, false);
  67. )*
  68. }
  69. impl GlobalAttributes for $name {}
  70. )*
  71. }
  72. }
  73. pub trait GlobalAttributes {}
  74. pub trait SvgAttributes {}
  75. builder_constructors! {
  76. blabla {
  77. };
  78. testing132 {
  79. };
  80. }
  81. }
  82. #[test]
  83. fn native_core_is_okay() {
  84. use std::sync::{Arc, Mutex};
  85. use std::time::Duration;
  86. fn app(cx: Scope) -> Element {
  87. let colors = use_state(cx, || vec!["green", "blue", "red"]);
  88. let padding = use_state(cx, || 10);
  89. use_effect(cx, colors, |colors| async move {
  90. sleep(Duration::from_millis(1000)).await;
  91. colors.with_mut(|colors| colors.reverse());
  92. });
  93. use_effect(cx, padding, |padding| async move {
  94. sleep(Duration::from_millis(10)).await;
  95. padding.with_mut(|padding| {
  96. if *padding < 65 {
  97. *padding += 1;
  98. } else {
  99. *padding = 5;
  100. }
  101. });
  102. });
  103. let _big = colors[0];
  104. let _mid = colors[1];
  105. let _small = colors[2];
  106. cx.render(rsx! {
  107. blabla {
  108. blabla {
  109. testing132 {}
  110. }
  111. }
  112. })
  113. }
  114. let rt = tokio::runtime::Builder::new_current_thread()
  115. .enable_time()
  116. .build()
  117. .unwrap();
  118. rt.block_on(async {
  119. let rdom = Arc::new(Mutex::new(RealDom::new([BlablaState::to_type_erased()])));
  120. rdom.lock()
  121. .unwrap()
  122. .register_custom_element::<TestElement>();
  123. let mut dioxus_state = DioxusState::create(&mut rdom.lock().unwrap());
  124. let mut dom = VirtualDom::new(app);
  125. let mutations = dom.rebuild();
  126. dioxus_state.apply_mutations(&mut rdom.lock().unwrap(), mutations);
  127. let ctx = SendAnyMap::new();
  128. rdom.lock().unwrap().update_state(ctx);
  129. for _ in 0..10 {
  130. dom.wait_for_work().await;
  131. let mutations = dom.render_immediate();
  132. dioxus_state.apply_mutations(&mut rdom.lock().unwrap(), mutations);
  133. let ctx = SendAnyMap::new();
  134. rdom.lock().unwrap().update_state(ctx);
  135. }
  136. });
  137. }
  138. struct TestElement {
  139. root: NodeId,
  140. }
  141. impl CustomElement for TestElement {
  142. const NAME: &'static str = "blabla";
  143. fn create(dom: &mut RealDom<()>) -> Self {
  144. let root = dom.create_node(ElementNode {
  145. tag: "shadow_root".into(),
  146. namespace: None,
  147. attributes: Default::default(),
  148. listeners: Default::default(),
  149. });
  150. Self { root: root.id() }
  151. }
  152. fn root(&self) -> NodeId {
  153. self.root
  154. }
  155. fn attributes_changed(
  156. &mut self,
  157. _dom: &mut RealDom<()>,
  158. attributes: &dioxus_native_core::node_ref::AttributeMask,
  159. ) {
  160. println!("attributes_changed");
  161. println!("{:?}", attributes);
  162. }
  163. }