dioxus/examples/simple_desktop.rs

83 lines
1.9 KiB
Rust
Raw Normal View History

2022-12-16 10:55:20 +00:00
#![allow(non_snake_case)]
2022-12-13 02:31:30 +00:00
use dioxus::prelude::*;
2022-12-16 10:55:20 +00:00
use dioxus_router::prelude::*;
2022-12-13 02:31:30 +00:00
fn main() {
simple_logger::SimpleLogger::new()
.with_level(log::LevelFilter::Debug)
.with_module_level("dioxus", log::LevelFilter::Trace)
.init()
.unwrap();
dioxus_desktop::launch(App);
2022-12-13 02:31:30 +00:00
}
#[component]
fn App(cx: Scope) -> Element {
2023-06-02 17:33:47 +00:00
render! {
2023-07-26 01:14:48 +00:00
Router::<Route> {}
2023-06-02 17:33:47 +00:00
}
}
2022-12-16 10:55:20 +00:00
2023-06-02 17:33:47 +00:00
#[derive(Routable, Clone)]
#[rustfmt::skip]
enum Route {
#[layout(NavBar)]
#[route("/")]
Home {},
#[nest("/new")]
#[route("/")]
BlogList {},
#[route("/:post")]
BlogPost {
post: String,
},
#[end_nest]
#[route("/oranges")]
Oranges {},
}
#[component]
2023-06-02 17:33:47 +00:00
fn NavBar(cx: Scope) -> Element {
2022-12-16 10:55:20 +00:00
render! {
h1 { "Your app here" }
ul {
2023-07-24 18:57:56 +00:00
li { Link { to: Route::Home {}, "home" } }
li { Link { to: Route::BlogList {}, "blog" } }
li { Link { to: Route::BlogPost { post: "tim".into() }, "tims' blog" } }
li { Link { to: Route::BlogPost { post: "bill".into() }, "bills' blog" } }
li { Link { to: Route::BlogPost { post: "james".into() }, "james amazing' blog" } }
2022-12-13 02:31:30 +00:00
}
2023-07-26 01:14:48 +00:00
Outlet::<Route> {}
2022-12-16 10:55:20 +00:00
}
2022-12-13 02:31:30 +00:00
}
#[component]
2022-12-13 02:31:30 +00:00
fn Home(cx: Scope) -> Element {
log::debug!("rendering home {:?}", cx.scope_id());
2022-12-16 10:55:20 +00:00
render! { h1 { "Home" } }
2022-12-13 02:31:30 +00:00
}
#[component]
2022-12-13 02:31:30 +00:00
fn BlogList(cx: Scope) -> Element {
log::debug!("rendering blog list {:?}", cx.scope_id());
2022-12-16 10:55:20 +00:00
render! { div { "Blog List" } }
2022-12-13 02:31:30 +00:00
}
#[component]
2023-06-02 17:33:47 +00:00
fn BlogPost(cx: Scope, post: String) -> Element {
log::debug!("rendering blog post {}", post);
2022-12-13 02:31:30 +00:00
2022-12-16 10:55:20 +00:00
render! {
2022-12-13 22:44:47 +00:00
div {
2023-06-02 17:33:47 +00:00
h3 { "blog post: {post}" }
2023-07-24 18:57:56 +00:00
Link { to: Route::BlogList {}, "back to blog list" }
2022-12-13 22:44:47 +00:00
}
2022-12-16 10:55:20 +00:00
}
}
#[component]
2022-12-16 10:55:20 +00:00
fn Oranges(cx: Scope) -> Element {
render!("Oranges are not apples!")
2022-12-13 02:31:30 +00:00
}