dioxus/examples/webview.rs

49 lines
1.5 KiB
Rust
Raw Normal View History

2021-01-22 20:50:16 +00:00
//! Example: Webview Renderer
2021-01-26 19:02:35 +00:00
//! -------------------------
2021-01-22 20:50:16 +00:00
//!
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-01-22 20:50:16 +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-01-22 20:50:16 +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.
2021-06-24 15:09:38 +00:00
//!
//! Currently, NodeRefs won't work properly, but all other event functionality will.
#![allow(non_upper_case_globals, non_snake_case)]
2021-01-22 20:50:16 +00:00
2021-07-26 16:14:48 +00:00
use dioxus::{events::on::MouseEvent, prelude::*};
2021-01-21 16:10:31 +00:00
fn main() -> anyhow::Result<()> {
env_logger::init();
dioxus::desktop::launch(App, |c| c)
2021-06-24 15:09:38 +00:00
}
2021-01-22 20:50:16 +00:00
2021-06-24 15:09:38 +00:00
static App: FC<()> = |cx| {
2021-07-26 16:14:48 +00:00
let state = use_state(cx, || String::from("hello"));
let clear_text = state == "hello";
2021-06-24 15:09:38 +00:00
2021-07-26 16:14:48 +00:00
dbg!("rednering parent");
2021-06-24 15:09:38 +00:00
cx.render(rsx! {
div {
2021-07-26 16:14:48 +00:00
h1 {"{state}"}
CalculatorKey { name: "key-clear", onclick: move |_| state.get_mut().push_str("hello"), "{clear_text}" }
CalculatorKey { name: "key-sign", onclick: move |_| { state.get_mut().pop(); }, "±"}
2021-06-24 15:09:38 +00:00
}
})
};
2021-07-26 16:14:48 +00:00
#[derive(Props)]
struct CalculatorKeyProps<'a> {
name: &'static str,
onclick: &'a dyn Fn(MouseEvent),
}
fn CalculatorKey<'a, 'r>(cx: Context<'a, CalculatorKeyProps<'r>>) -> DomTree<'a> {
cx.render(rsx! {
button {
class: "calculator-key {cx.name}"
onclick: {cx.onclick}
{cx.children()}
}
})
}