slideshow.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //! Example: Webview Renderer
  2. //! -------------------------
  3. //!
  4. //! This example shows how to use the dioxus_desktop crate to build a basic desktop application.
  5. //!
  6. //! Under the hood, the dioxus_desktop crate bridges a native Dioxus VirtualDom with a custom prebuit application running
  7. //! in the webview runtime. Custom handlers are provided for the webview instance to consume patches and emit user events
  8. //! into the native VDom instance.
  9. //!
  10. //! Currently, NodeRefs won't work properly, but all other event functionality will.
  11. use dioxus::prelude::*;
  12. fn main() {
  13. dioxus::desktop::launch(App, |c| c);
  14. }
  15. static App: FC<()> = |cx| {
  16. let slides = use_state(cx, SlideController::new);
  17. let slide = match slides.slide_id {
  18. 0 => cx.render(rsx!(Title {})),
  19. 1 => cx.render(rsx!(Slide1 {})),
  20. 2 => cx.render(rsx!(Slide2 {})),
  21. 3 => cx.render(rsx!(Slide3 {})),
  22. _ => cx.render(rsx!(End {})),
  23. };
  24. cx.render(rsx! {
  25. div {
  26. style: {
  27. background_color: "red"
  28. }
  29. div {
  30. div { h1 {"my awesome slideshow"} }
  31. div {
  32. button {"<-", onclick: move |_| slides.get_mut().go_forward()}
  33. h3 { "{slides.slide_id}" }
  34. button {"->" onclick: move |_| slides.get_mut().go_backward()}
  35. }
  36. }
  37. {slide}
  38. }
  39. })
  40. };
  41. #[derive(Clone)]
  42. struct SlideController {
  43. slide_id: isize,
  44. }
  45. impl SlideController {
  46. fn new() -> Self {
  47. Self { slide_id: 0 }
  48. }
  49. fn can_go_forward(&self) -> bool {
  50. false
  51. }
  52. fn can_go_backward(&self) -> bool {
  53. true
  54. }
  55. fn go_forward(&mut self) {
  56. if self.can_go_forward() {
  57. self.slide_id += 1;
  58. }
  59. }
  60. fn go_backward(&mut self) {
  61. if self.can_go_backward() {
  62. self.slide_id -= 1;
  63. }
  64. }
  65. }
  66. const Title: FC<()> = |cx| {
  67. cx.render(rsx! {
  68. div {
  69. h1 { "Title" }
  70. p {}
  71. }
  72. })
  73. };
  74. const Slide1: FC<()> = |cx| {
  75. cx.render(rsx! {
  76. div {
  77. h1 { "Slide1" }
  78. p {}
  79. }
  80. })
  81. };
  82. const Slide2: FC<()> = |cx| {
  83. cx.render(rsx! {
  84. div {
  85. h1 { "Slide2" }
  86. p {}
  87. }
  88. })
  89. };
  90. const Slide3: FC<()> = |cx| {
  91. cx.render(rsx! {
  92. div {
  93. h1 { "Slide3" }
  94. p {}
  95. }
  96. })
  97. };
  98. const End: FC<()> = |cx| {
  99. cx.render(rsx! {
  100. div {
  101. h1 { "End" }
  102. p {}
  103. }
  104. })
  105. };