demo.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //! An example where the dioxus vdom is running in a native thread, interacting with webview
  2. use std::{
  3. borrow::BorrowMut,
  4. rc::Rc,
  5. sync::{mpsc::channel, Arc},
  6. };
  7. // use async_std::{channel, task::block_on};
  8. use dioxus_core::{dodriodiff::DiffMachine, prelude::bumpalo::Bump, prelude::*, scope};
  9. use scope::Scope;
  10. use web_view::Handle;
  11. static HTML_CONTENT: &'static str = include_str!("./index.html");
  12. enum InnerEvent {
  13. Initiate(Handle<()>),
  14. }
  15. // async_std::task::spawn(async {
  16. // #[async_std::main]
  17. fn main() -> anyhow::Result<()> {
  18. let (sender, receiver) = channel::<InnerEvent>();
  19. // let (sender, receiver) = channel::unbounded::<InnerEvent>();
  20. // let task = async_std::task::spawn(async move {
  21. let mut view = web_view::builder()
  22. .title("My Project")
  23. .content(web_view::Content::Html(HTML_CONTENT))
  24. .size(320, 480)
  25. .resizable(true)
  26. .debug(true)
  27. .user_data(())
  28. .invoke_handler(|view, arg| {
  29. // todo: handle events here
  30. println!("handling invoker");
  31. let handle = view.handle();
  32. sender.send(InnerEvent::Initiate(handle));
  33. Ok(())
  34. })
  35. .build()
  36. .unwrap();
  37. println!("building the diff");
  38. let bump = Bump::new();
  39. let mut diff_machine = DiffMachine::new(&bump);
  40. let old = html! {<div> </div>}(&bump);
  41. // let mut scope = Scope::new(TEST, (), None);
  42. // scope.run::<()>();
  43. let new = html! {
  44. <div>
  45. <div class="flex items-center justify-center flex-col">
  46. <div class="flex items-center justify-center">
  47. <div class="flex flex-col bg-white rounded p-4 w-full max-w-xs">
  48. // Title
  49. <div class="font-bold text-xl">
  50. "Jon's awesome site!!11"
  51. </div>
  52. // Subtext / description
  53. <div class="text-sm text-gray-500">
  54. "He worked so hard on it :)"
  55. </div>
  56. <div class="flex flex-row items-center justify-center mt-6">
  57. // Main number
  58. <div class="font-medium text-6xl">
  59. "1337"
  60. </div>
  61. </div>
  62. // Try another
  63. <div class="flex flex-row justify-between mt-6">
  64. // <a href=format!("http://localhost:8080/fib/{}", other_fib_to_try) class="underline">
  65. "Legit made my own React"
  66. // </a>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. </div>
  72. }(&bump);
  73. diff_machine.diff_node(&old, &new);
  74. let edits = diff_machine.consume();
  75. let ready_edits = serde_json::to_string(&edits)?;
  76. let ref_edits = Arc::new(ready_edits);
  77. loop {
  78. view.step().expect("should not fail");
  79. // if let Some(evt) = receiver.try_recv() {}
  80. if let Ok(event) = receiver.try_recv() {
  81. match event {
  82. InnerEvent::Initiate(handle) => {
  83. // println!("awesome, things worked");
  84. let ediits = ref_edits.clone();
  85. // println!("{}", ediits);
  86. handle
  87. .dispatch(move |view| {
  88. view.eval(format!("EditListReceived(`{}`);", ediits).as_str())?;
  89. Ok(())
  90. })
  91. .expect("Dispatch failed");
  92. // let g = handle.();
  93. }
  94. }
  95. }
  96. // let event = receiver.try_recv();
  97. // view.eval("alert('omg');")?;
  98. // view.step().expect("webview should not fail")?;
  99. }
  100. }
  101. // static TEST: FC<()> = |ctx, props| {
  102. // ctx.view(html! {
  103. // <div>
  104. // <div class="flex items-center justify-center flex-col">
  105. // <div class="flex items-center justify-center">
  106. // <div class="flex flex-col bg-white rounded p-4 w-full max-w-xs">
  107. // // Title
  108. // <div class="font-bold text-xl">
  109. // "Jon's awesome site!!11"
  110. // </div>
  111. // // Subtext / description
  112. // <div class="text-sm text-gray-500">
  113. // "He worked so hard on it :)"
  114. // </div>
  115. // <div class="flex flex-row items-center justify-center mt-6">
  116. // // Main number
  117. // <div class="font-medium text-6xl">
  118. // "1337"
  119. // </div>
  120. // </div>
  121. // // Try another
  122. // <div class="flex flex-row justify-between mt-6">
  123. // // <a href=format!("http://localhost:8080/fib/{}", other_fib_to_try) class="underline">
  124. // "Legit made my own React"
  125. // // </a>
  126. // </div>
  127. // </div>
  128. // </div>
  129. // </div>
  130. // </div>
  131. // })
  132. // };