dioxus/examples/slideshow.rs

113 lines
2.5 KiB
Rust
Raw Normal View History

2021-07-08 14:17:51 +00:00
//! Example: Webview Renderer
//! -------------------------
//!
2021-07-16 04:27:06 +00:00
//! This example shows how to use the dioxus_desktop crate to build a basic desktop application.
2021-07-08 14:17:51 +00:00
//!
2021-07-16 04:27:06 +00:00
//! Under the hood, the dioxus_desktop crate bridges a native Dioxus VirtualDom with a custom prebuit application running
2021-07-08 14:17:51 +00:00
//! in the webview runtime. Custom handlers are provided for the webview instance to consume patches and emit user events
//! into the native VDom instance.
//!
//! Currently, NodeRefs won't work properly, but all other event functionality will.
use dioxus::prelude::*;
fn main() {
dioxus::desktop::launch(App, |c| c);
}
static App: FC<()> = |cx| {
2021-07-09 03:25:27 +00:00
let slides = use_state(cx, SlideController::new);
2021-07-08 14:17:51 +00:00
2021-07-09 03:25:27 +00:00
let slide = match slides.slide_id {
2021-07-08 14:17:51 +00:00
0 => cx.render(rsx!(Title {})),
1 => cx.render(rsx!(Slide1 {})),
2 => cx.render(rsx!(Slide2 {})),
3 => cx.render(rsx!(Slide3 {})),
_ => cx.render(rsx!(End {})),
};
cx.render(rsx! {
div {
2021-07-09 03:25:27 +00:00
style: {
background_color: "red"
}
2021-07-08 14:17:51 +00:00
div {
div { h1 {"my awesome slideshow"} }
div {
2021-07-09 03:25:27 +00:00
button {"<-", onclick: move |_| slides.get_mut().go_forward()}
h3 { "{slides.slide_id}" }
button {"->" onclick: move |_| slides.get_mut().go_backward()}
2021-07-08 14:17:51 +00:00
}
}
{slide}
}
})
};
2021-07-09 03:25:27 +00:00
#[derive(Clone)]
struct SlideController {
slide_id: isize,
}
impl SlideController {
fn new() -> Self {
Self { slide_id: 0 }
}
fn can_go_forward(&self) -> bool {
false
}
fn can_go_backward(&self) -> bool {
true
}
fn go_forward(&mut self) {
if self.can_go_forward() {
self.slide_id += 1;
}
}
fn go_backward(&mut self) {
if self.can_go_backward() {
self.slide_id -= 1;
}
}
}
2021-07-08 14:17:51 +00:00
const Title: FC<()> = |cx| {
cx.render(rsx! {
div {
2021-07-09 03:25:27 +00:00
h1 { "Title" }
p {}
2021-07-08 14:17:51 +00:00
}
})
};
const Slide1: FC<()> = |cx| {
cx.render(rsx! {
div {
2021-07-09 03:25:27 +00:00
h1 { "Slide1" }
p {}
2021-07-08 14:17:51 +00:00
}
})
};
const Slide2: FC<()> = |cx| {
cx.render(rsx! {
div {
2021-07-09 03:25:27 +00:00
h1 { "Slide2" }
p {}
2021-07-08 14:17:51 +00:00
}
})
};
const Slide3: FC<()> = |cx| {
cx.render(rsx! {
div {
2021-07-09 03:25:27 +00:00
h1 { "Slide3" }
p {}
2021-07-08 14:17:51 +00:00
}
})
};
const End: FC<()> = |cx| {
cx.render(rsx! {
div {
2021-07-09 03:25:27 +00:00
h1 { "End" }
p {}
2021-07-08 14:17:51 +00:00
}
})
};