1
0

framework_benchmark.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use rand::prelude::*;
  4. fn main() {
  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 x in 0..num {
  17. labels.push(Label {
  18. key: x,
  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. fn app(cx: Scope) -> Element {
  30. let items = use_ref(cx, Vec::new);
  31. let 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[0]}{item.labels[1]}{item.labels[2]}" }
  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: &'a str,
  88. id: &'a str,
  89. onclick: EventHandler<'a>,
  90. }
  91. fn ActionButton<'a>(cx: Scope<'a, ActionButtonProps<'a>>) -> Element {
  92. cx.render(rsx! {
  93. div {
  94. class: "col-sm-6 smallpad",
  95. button {
  96. class:"btn btn-primary btn-block",
  97. r#type: "button",
  98. id: "{cx.props.id}",
  99. onclick: move |_| cx.props.onclick.call(()),
  100. "{cx.props.name}"
  101. }
  102. }
  103. })
  104. }
  105. static ADJECTIVES: &[&str] = &[
  106. "pretty",
  107. "large",
  108. "big",
  109. "small",
  110. "tall",
  111. "short",
  112. "long",
  113. "handsome",
  114. "plain",
  115. "quaint",
  116. "clean",
  117. "elegant",
  118. "easy",
  119. "angry",
  120. "crazy",
  121. "helpful",
  122. "mushy",
  123. "odd",
  124. "unsightly",
  125. "adorable",
  126. "important",
  127. "inexpensive",
  128. "cheap",
  129. "expensive",
  130. "fancy",
  131. ];
  132. static COLOURS: &[&str] = &[
  133. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  134. "orange",
  135. ];
  136. static NOUNS: &[&str] = &[
  137. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  138. "pizza", "mouse", "keyboard",
  139. ];