framework_benchmark.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. use dioxus::prelude::*;
  2. use rand::prelude::*;
  3. fn main() {
  4. dioxus::web::launch(App);
  5. // dioxus::desktop::launch(App);
  6. }
  7. #[derive(Clone, PartialEq)]
  8. struct Label {
  9. key: usize,
  10. labels: [&'static str; 3],
  11. }
  12. impl Label {
  13. fn new_list(num: usize) -> Vec<Self> {
  14. let mut rng = SmallRng::from_entropy();
  15. let mut labels = Vec::with_capacity(num);
  16. for _ in 0..num {
  17. labels.push(Label {
  18. key: 0,
  19. labels: [
  20. ADJECTIVES.choose(&mut rng).unwrap(),
  21. COLOURS.choose(&mut rng).unwrap(),
  22. NOUNS.choose(&mut rng).unwrap(),
  23. ],
  24. });
  25. }
  26. labels
  27. }
  28. }
  29. static App: Component<()> = |cx| {
  30. let mut items = use_ref(&cx, || vec![]);
  31. let mut selected = use_state(&cx, || None);
  32. cx.render(rsx! {
  33. div { class: "container"
  34. div { class: "jumbotron"
  35. div { class: "row"
  36. div { class: "col-md-6", h1 { "Dioxus" } }
  37. div { class: "col-md-6"
  38. div { class: "row"
  39. ActionButton { name: "Create 1,000 rows", id: "run",
  40. onclick: move || items.set(Label::new_list(1_000)),
  41. }
  42. ActionButton { name: "Create 10,000 rows", id: "runlots",
  43. onclick: move || items.set(Label::new_list(10_000)),
  44. }
  45. ActionButton { name: "Append 1,000 rows", id: "add",
  46. onclick: move || items.write().extend(Label::new_list(1_000)),
  47. }
  48. ActionButton { name: "Update every 10th row", id: "update",
  49. onclick: move || items.write().iter_mut().step_by(10).for_each(|item| item.labels[2] = "!!!"),
  50. }
  51. ActionButton { name: "Clear", id: "clear",
  52. onclick: move || items.write().clear(),
  53. }
  54. ActionButton { name: "Swap rows", id: "swaprows",
  55. onclick: move || items.write().swap(0, 998),
  56. }
  57. }
  58. }
  59. }
  60. }
  61. table {
  62. tbody {
  63. {items.read().iter().enumerate().map(|(id, item)| {
  64. let is_in_danger = if (*selected).map(|s| s == id).unwrap_or(false) {"danger"} else {""};
  65. rsx!(tr { class: "{is_in_danger}"
  66. td { class:"col-md-1" }
  67. td { class:"col-md-1", "{item.key}" }
  68. td { class:"col-md-1", onclick: move |_| selected.set(Some(id)),
  69. a { class: "lbl", {item.labels} }
  70. }
  71. td { class: "col-md-1"
  72. a { class: "remove", onclick: move |_| { items.write().remove(id); },
  73. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  74. }
  75. }
  76. td { class: "col-md-6" }
  77. })
  78. })}
  79. }
  80. }
  81. // span { class: "preloadicon glyphicon glyphicon-remove" aria_hidden: "true" }
  82. }
  83. })
  84. };
  85. #[derive(Props)]
  86. struct ActionButtonProps<'a> {
  87. name: &'static str,
  88. id: &'static str,
  89. onclick: &'a dyn Fn(),
  90. }
  91. fn ActionButton<'a>(cx: Scope<'a, ActionButtonProps<'a>>) -> Element {
  92. rsx!(cx, div { class: "col-sm-6 smallpad"
  93. button { class:"btn btn-primary btn-block", r#type: "button", id: "{cx.props.id}", onclick: move |_| (props.onclick)(),
  94. "{cx.props.name}"
  95. }
  96. })
  97. }
  98. static ADJECTIVES: &[&str] = &[
  99. "pretty",
  100. "large",
  101. "big",
  102. "small",
  103. "tall",
  104. "short",
  105. "long",
  106. "handsome",
  107. "plain",
  108. "quaint",
  109. "clean",
  110. "elegant",
  111. "easy",
  112. "angry",
  113. "crazy",
  114. "helpful",
  115. "mushy",
  116. "odd",
  117. "unsightly",
  118. "adorable",
  119. "important",
  120. "inexpensive",
  121. "cheap",
  122. "expensive",
  123. "fancy",
  124. ];
  125. static COLOURS: &[&str] = &[
  126. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  127. "orange",
  128. ];
  129. static NOUNS: &[&str] = &[
  130. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  131. "pizza", "mouse", "keyboard",
  132. ];
  133. // #[derive(PartialEq, Props)]
  134. // struct RowProps<'a> {
  135. // row_id: usize,
  136. // label: &'a Label,
  137. // }
  138. // fn Row(cx: Context, props: &RowProps) -> Element {
  139. // rsx!(cx, tr {
  140. // td { class:"col-md-1", "{cx.props.row_id}" }
  141. // td { class:"col-md-1", onclick: move |_| { /* run onselect */ }
  142. // a { class: "lbl", {cx.props.label.labels} }
  143. // }
  144. // td { class: "col-md-1"
  145. // a { class: "remove", onclick: move |_| {/* remove */}
  146. // span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  147. // }
  148. // }
  149. // td { class: "col-md-6" }
  150. // })
  151. // }