driven.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. use dioxus_html::EventData;
  2. use dioxus_native_core::{
  3. node::{OwnedAttributeDiscription, OwnedAttributeValue, TextNode},
  4. prelude::*,
  5. real_dom::{NodeImmutable, NodeTypeMut},
  6. NodeId, Renderer,
  7. };
  8. use dioxus_tui::{self, render, Config};
  9. use std::sync::{Arc, RwLock};
  10. use std::{rc::Rc, sync::Mutex};
  11. use taffy::Taffy;
  12. struct Test([[usize; 10]; 10]);
  13. impl Renderer<Rc<EventData>> for Test {
  14. fn render(&mut self, mut root: dioxus_native_core::NodeMut) {
  15. // Set the root node to be a flexbox with a column direction.
  16. if let NodeTypeMut::Element(mut el) = root.node_type_mut() {
  17. el.set_attribute(
  18. OwnedAttributeDiscription {
  19. name: "display".into(),
  20. namespace: None,
  21. },
  22. OwnedAttributeValue::Text("flex".into()),
  23. );
  24. el.set_attribute(
  25. OwnedAttributeDiscription {
  26. name: "flex-direction".into(),
  27. namespace: None,
  28. },
  29. OwnedAttributeValue::Text("column".into()),
  30. );
  31. el.set_attribute(
  32. OwnedAttributeDiscription {
  33. name: "width".into(),
  34. namespace: None,
  35. },
  36. OwnedAttributeValue::Text("100%".into()),
  37. );
  38. el.set_attribute(
  39. OwnedAttributeDiscription {
  40. name: "height".into(),
  41. namespace: None,
  42. },
  43. OwnedAttributeValue::Text("100%".into()),
  44. );
  45. }
  46. let root_id = root.id();
  47. // Remove old grid. Frameworks should retain the grid and only update the values.
  48. let children_ids = root.child_ids().map(|ids| ids.to_vec());
  49. let rdom = root.real_dom_mut();
  50. if let Some(children) = children_ids {
  51. for child in children {
  52. rdom.get_mut(child).unwrap().remove();
  53. }
  54. }
  55. // create the grid
  56. for (x, row) in self.0.iter().copied().enumerate() {
  57. let row_node = rdom
  58. .create_node(NodeType::Element(ElementNode {
  59. tag: "div".to_string(),
  60. attributes: [
  61. (
  62. OwnedAttributeDiscription {
  63. name: "display".into(),
  64. namespace: None,
  65. },
  66. OwnedAttributeValue::Text("flex".into()),
  67. ),
  68. (
  69. OwnedAttributeDiscription {
  70. name: "flex-direction".into(),
  71. namespace: None,
  72. },
  73. OwnedAttributeValue::Text("row".into()),
  74. ),
  75. (
  76. OwnedAttributeDiscription {
  77. name: "width".into(),
  78. namespace: None,
  79. },
  80. OwnedAttributeValue::Text("100%".into()),
  81. ),
  82. (
  83. OwnedAttributeDiscription {
  84. name: "height".into(),
  85. namespace: None,
  86. },
  87. OwnedAttributeValue::Text("100%".into()),
  88. ),
  89. ]
  90. .into_iter()
  91. .collect(),
  92. ..Default::default()
  93. }))
  94. .id();
  95. for (y, count) in row.iter().copied().enumerate() {
  96. let node = rdom
  97. .create_node(NodeType::Text(TextNode::new(count.to_string())))
  98. .id();
  99. let mut button = rdom.create_node(NodeType::Element(ElementNode {
  100. tag: "div".to_string(),
  101. attributes: [
  102. (
  103. OwnedAttributeDiscription {
  104. name: "background-color".into(),
  105. namespace: None,
  106. },
  107. OwnedAttributeValue::Text(format!(
  108. "rgb({}, {}, {})",
  109. count * 10,
  110. 0,
  111. (x + y) * 10,
  112. )),
  113. ),
  114. (
  115. OwnedAttributeDiscription {
  116. name: "width".into(),
  117. namespace: None,
  118. },
  119. OwnedAttributeValue::Text("100%".into()),
  120. ),
  121. (
  122. OwnedAttributeDiscription {
  123. name: "height".into(),
  124. namespace: None,
  125. },
  126. OwnedAttributeValue::Text("100%".into()),
  127. ),
  128. (
  129. OwnedAttributeDiscription {
  130. name: "display".into(),
  131. namespace: None,
  132. },
  133. OwnedAttributeValue::Text("flex".into()),
  134. ),
  135. (
  136. OwnedAttributeDiscription {
  137. name: "flex-direction".into(),
  138. namespace: None,
  139. },
  140. OwnedAttributeValue::Text("row".into()),
  141. ),
  142. (
  143. OwnedAttributeDiscription {
  144. name: "justify-content".into(),
  145. namespace: None,
  146. },
  147. OwnedAttributeValue::Text("center".into()),
  148. ),
  149. (
  150. OwnedAttributeDiscription {
  151. name: "align-items".into(),
  152. namespace: None,
  153. },
  154. OwnedAttributeValue::Text("center".into()),
  155. ),
  156. ]
  157. .into_iter()
  158. .collect(),
  159. ..Default::default()
  160. }));
  161. button.add_event_listener("click");
  162. button.add_event_listener("wheel");
  163. button.add_child(node);
  164. let button_id = button.id();
  165. rdom.get_mut(row_node).unwrap().add_child(button_id);
  166. }
  167. rdom.get_mut(root_id).unwrap().add_child(row_node);
  168. }
  169. }
  170. fn handle_event(
  171. &mut self,
  172. node: dioxus_native_core::NodeMut<()>,
  173. event: &str,
  174. value: Rc<EventData>,
  175. bubbles: bool,
  176. ) {
  177. if let Some(parent) = node.parent() {
  178. let child_number = parent
  179. .child_ids()
  180. .unwrap()
  181. .iter()
  182. .position(|id| *id == node.id())
  183. .unwrap();
  184. if let Some(parents_parent) = parent.parent() {
  185. let parents_child_number = parents_parent
  186. .child_ids()
  187. .unwrap()
  188. .iter()
  189. .position(|id| *id == parent.id())
  190. .unwrap();
  191. self.0[parents_child_number][child_number] += 1;
  192. }
  193. }
  194. }
  195. fn poll_async(&mut self) -> std::pin::Pin<Box<dyn futures::Future<Output = ()> + Send>> {
  196. Box::pin(async move { tokio::time::sleep(std::time::Duration::from_millis(1000)).await })
  197. }
  198. }
  199. fn main() {
  200. render(Config::new(), |_, _, _| Test(Default::default())).unwrap();
  201. }