webview.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #![allow(non_upper_case_globals, non_snake_case)]
  12. use dioxus::{events::on::MouseEvent, prelude::*};
  13. fn main() -> anyhow::Result<()> {
  14. env_logger::init();
  15. dioxus::desktop::launch(App, |c| c)
  16. }
  17. static App: FC<()> = |cx, props| {
  18. let state = use_state(cx, || String::from("hello"));
  19. let clear_text = state == "hello";
  20. dbg!("rednering parent");
  21. cx.render(rsx! {
  22. div {
  23. h1 {"{state}"}
  24. CalculatorKey { name: "key-clear", onclick: move |_| state.modify().push_str("hello"), "{clear_text}" }
  25. CalculatorKey { name: "key-sign", onclick: move |_| { state.modify().pop(); }, "±"}
  26. }
  27. })
  28. };
  29. #[derive(Props)]
  30. struct CalculatorKeyProps<'a> {
  31. name: &'static str,
  32. onclick: &'a dyn Fn(MouseEvent),
  33. }
  34. fn CalculatorKey<'a>(cx: Context<'a>, props: &'a CalculatorKeyProps) -> DomTree<'a> {
  35. cx.render(rsx! {
  36. button {
  37. class: "calculator-key {props.name}"
  38. onclick: {props.onclick}
  39. {cx.children()}
  40. }
  41. })
  42. }