web_tick.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #![allow(non_upper_case_globals, non_snake_case)]
  2. //! Example: Webview Renderer
  3. //! -------------------------
  4. //!
  5. //! This example shows how to use the dioxus_desktop crate to build a basic desktop application.
  6. //!
  7. //! Under the hood, the dioxus_desktop crate bridges a native Dioxus VirtualDom with a custom prebuit application running
  8. //! in the webview runtime. Custom handlers are provided for the webview instance to consume patches and emit user events
  9. //! into the native VDom instance.
  10. //!
  11. //! Currently, NodeRefs won't work properly, but all other event functionality will.
  12. use dioxus::prelude::*;
  13. fn main() {
  14. #[cfg(target_arch = "wasm32")]
  15. intern_strings();
  16. dioxus::web::launch(App, |c| c);
  17. }
  18. static App: FC<()> = |cx, props| {
  19. let mut rng = SmallRng::from_entropy();
  20. let rows = (0..1_000).map(|f| {
  21. let label = Label::new(&mut rng);
  22. rsx! {
  23. Row {
  24. row_id: f,
  25. label: label
  26. }
  27. }
  28. });
  29. cx.render(rsx! {
  30. table {
  31. tbody {
  32. {rows}
  33. }
  34. }
  35. })
  36. };
  37. #[derive(PartialEq, Props)]
  38. struct RowProps {
  39. row_id: usize,
  40. label: Label,
  41. }
  42. fn Row<'a>(cx: Context<'a>, props: &'a RowProps) -> DomTree<'a> {
  43. let [adj, col, noun] = props.label.0;
  44. cx.render(rsx! {
  45. tr {
  46. td { class:"col-md-1", "{props.row_id}" }
  47. td { class:"col-md-1", onclick: move |_| { /* run onselect */ }
  48. a { class: "lbl", "{adj}" "{col}" "{noun}" }
  49. }
  50. td { class: "col-md-1"
  51. a { class: "remove", onclick: move |_| {/* remove */}
  52. span { class: "glyphicon glyphicon-remove remove" aria_hidden: "true" }
  53. }
  54. }
  55. td { class: "col-md-6" }
  56. }
  57. })
  58. }
  59. use rand::prelude::*;
  60. #[derive(PartialEq)]
  61. struct Label([&'static str; 3]);
  62. impl Label {
  63. fn new(rng: &mut SmallRng) -> Self {
  64. Label([
  65. ADJECTIVES.choose(rng).unwrap(),
  66. COLOURS.choose(rng).unwrap(),
  67. NOUNS.choose(rng).unwrap(),
  68. ])
  69. }
  70. }
  71. static ADJECTIVES: &[&str] = &[
  72. "pretty",
  73. "large",
  74. "big",
  75. "small",
  76. "tall",
  77. "short",
  78. "long",
  79. "handsome",
  80. "plain",
  81. "quaint",
  82. "clean",
  83. "elegant",
  84. "easy",
  85. "angry",
  86. "crazy",
  87. "helpful",
  88. "mushy",
  89. "odd",
  90. "unsightly",
  91. "adorable",
  92. "important",
  93. "inexpensive",
  94. "cheap",
  95. "expensive",
  96. "fancy",
  97. ];
  98. static COLOURS: &[&str] = &[
  99. "red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black",
  100. "orange",
  101. ];
  102. static NOUNS: &[&str] = &[
  103. "table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger",
  104. "pizza", "mouse", "keyboard",
  105. ];
  106. #[cfg(target_arch = "wasm32")]
  107. fn intern_strings() {
  108. for adj in ADJECTIVES {
  109. wasm_bindgen::intern(adj);
  110. }
  111. for col in COLOURS {
  112. wasm_bindgen::intern(col);
  113. }
  114. for no in NOUNS {
  115. wasm_bindgen::intern(no);
  116. }
  117. wasm_bindgen::intern("col-md-1");
  118. wasm_bindgen::intern("col-md-6");
  119. wasm_bindgen::intern("glyphicon glyphicon-remove remove");
  120. wasm_bindgen::intern("remove");
  121. wasm_bindgen::intern("dioxus");
  122. wasm_bindgen::intern("lbl");
  123. wasm_bindgen::intern("true");
  124. }