grid.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. use dioxus_native_core::{
  2. node::TextNode,
  3. prelude::*,
  4. real_dom::{NodeImmutable, NodeTypeMut},
  5. NodeId,
  6. };
  7. use rink::{render, Config, Driver, EventData};
  8. use rustc_hash::FxHashSet;
  9. use std::rc::Rc;
  10. use std::sync::{Arc, RwLock};
  11. const SIZE: usize = 20;
  12. #[derive(Default, Clone, Copy)]
  13. struct Node {
  14. container_id: Option<NodeId>,
  15. text_id: Option<NodeId>,
  16. count: usize,
  17. }
  18. struct Test {
  19. node_states: [[Node; SIZE]; SIZE],
  20. dirty: FxHashSet<(usize, usize)>,
  21. }
  22. impl Default for Test {
  23. fn default() -> Self {
  24. Self {
  25. node_states: [[Node {
  26. container_id: None,
  27. text_id: None,
  28. count: 0,
  29. }; SIZE]; SIZE],
  30. dirty: FxHashSet::default(),
  31. }
  32. }
  33. }
  34. impl Test {
  35. fn create(mut root: NodeMut) -> Self {
  36. let mut myself = Self::default();
  37. // Set the root node to be a flexbox with a column direction.
  38. if let NodeTypeMut::Element(mut el) = root.node_type_mut() {
  39. el.set_attribute("display".to_string(), "flex".to_string());
  40. el.set_attribute(("flex-direction", "style"), "column".to_string());
  41. el.set_attribute(("width", "style"), "100%".to_string());
  42. el.set_attribute(("height", "style"), "100%".to_string());
  43. }
  44. let root_id = root.id();
  45. let rdom = root.real_dom_mut();
  46. // create the grid
  47. for (x, row) in myself.node_states.iter_mut().enumerate() {
  48. let row_node = rdom
  49. .create_node(NodeType::Element(ElementNode {
  50. tag: "div".to_string(),
  51. attributes: [
  52. ("display".to_string().into(), "flex".to_string().into()),
  53. (("flex-direction", "style").into(), "row".to_string().into()),
  54. (("width", "style").into(), "100%".to_string().into()),
  55. (("height", "style").into(), "100%".to_string().into()),
  56. ]
  57. .into_iter()
  58. .collect(),
  59. ..Default::default()
  60. }))
  61. .id();
  62. for (y, node) in row.iter_mut().enumerate() {
  63. let count = node.count;
  64. let id = rdom
  65. .create_node(NodeType::Text(TextNode::new(count.to_string())))
  66. .id();
  67. let mut button = rdom.create_node(NodeType::Element(ElementNode {
  68. tag: "div".to_string(),
  69. attributes: [
  70. ("display".to_string().into(), "flex".to_string().into()),
  71. (
  72. ("background-color", "style").into(),
  73. format!("rgb({}, {}, {})", count * 10, 0, (x + y),).into(),
  74. ),
  75. (("width", "style").into(), "100%".to_string().into()),
  76. (("height", "style").into(), "100%".to_string().into()),
  77. (("flex-direction", "style").into(), "row".to_string().into()),
  78. (
  79. ("justify-content", "style").into(),
  80. "center".to_string().into(),
  81. ),
  82. (("align-items", "style").into(), "center".to_string().into()),
  83. ]
  84. .into_iter()
  85. .collect(),
  86. ..Default::default()
  87. }));
  88. button.add_event_listener("click");
  89. button.add_event_listener("wheel");
  90. button.add_child(id);
  91. let button_id = button.id();
  92. rdom.get_mut(row_node).unwrap().add_child(button_id);
  93. node.container_id = Some(button_id);
  94. node.text_id = Some(id);
  95. }
  96. rdom.get_mut(root_id).unwrap().add_child(row_node);
  97. }
  98. myself
  99. }
  100. }
  101. impl Driver for Test {
  102. fn update(&mut self, rdom: &Arc<RwLock<RealDom>>) {
  103. let mut rdom = rdom.write().unwrap();
  104. for (x, y) in self.dirty.drain() {
  105. let node = self.node_states[x][y];
  106. let node_id = node.container_id.unwrap();
  107. let mut container = rdom.get_mut(node_id).unwrap();
  108. if let NodeTypeMut::Element(mut el) = container.node_type_mut() {
  109. el.set_attribute(
  110. ("background-color", "style"),
  111. format!("rgb({}, {}, {})", node.count * 10, 0, (x + y),),
  112. );
  113. }
  114. let text_id = node.text_id.unwrap();
  115. let mut text = rdom.get_mut(text_id).unwrap();
  116. let type_mut = text.node_type_mut();
  117. if let NodeTypeMut::Text(mut text) = type_mut {
  118. *text = node.count.to_string();
  119. }
  120. }
  121. }
  122. fn handle_event(
  123. &mut self,
  124. rdom: &Arc<RwLock<RealDom>>,
  125. id: NodeId,
  126. _: &str,
  127. _: Rc<EventData>,
  128. _: bool,
  129. ) {
  130. let rdom = rdom.read().unwrap();
  131. let node = rdom.get(id).unwrap();
  132. if let Some(parent) = node.parent() {
  133. let child_number = parent
  134. .child_ids()
  135. .iter()
  136. .position(|id| *id == node.id())
  137. .unwrap();
  138. if let Some(parents_parent) = parent.parent() {
  139. let parents_child_number = parents_parent
  140. .child_ids()
  141. .iter()
  142. .position(|id| *id == parent.id())
  143. .unwrap();
  144. self.node_states[parents_child_number][child_number].count += 1;
  145. self.dirty.insert((parents_child_number, child_number));
  146. }
  147. }
  148. }
  149. fn poll_async(&mut self) -> std::pin::Pin<Box<dyn futures::Future<Output = ()> + '_>> {
  150. Box::pin(async move { tokio::time::sleep(std::time::Duration::from_millis(1000)).await })
  151. }
  152. }
  153. fn main() {
  154. render(Config::new(), |rdom, _, _| {
  155. let mut rdom = rdom.write().unwrap();
  156. let root = rdom.root_id();
  157. Test::create(rdom.get_mut(root).unwrap())
  158. })
  159. .unwrap();
  160. }