wip: threadsafe
This commit is contained in:
1 parent
71f0df6374
commit
82953f2ac3
22 files changed
+414
-545
No files matched your search
@@ -2,6 +2,7 @@
|
||||
//!
|
||||
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
use dioxus_core::{
|
||||
events::{
|
||||
@@ -21,7 +22,7 @@ pub fn trigger_from_serialized(val: serde_json::Value) -> UserEvent {
|
||||
let mut data: Vec<ImEvent> = serde_json::from_value(val).unwrap();
|
||||
let data = data.drain(..).next().unwrap();
|
||||
|
||||
let event = SyntheticEvent::MouseEvent(MouseEvent(Rc::new(WebviewMouseEvent)));
|
||||
let event = SyntheticEvent::MouseEvent(MouseEvent(Arc::new(WebviewMouseEvent)));
|
||||
let scope = ScopeId(data.scope as usize);
|
||||
let mounted_dom_id = Some(ElementId(data.mounted_dom_id as usize));
|
||||
UserEvent {
|
||||
|
||||
+170
-189
@@ -1,5 +1,11 @@
|
||||
//! Dioxus Desktop Renderer
|
||||
//!
|
||||
//! Render the Dioxus VirtualDom using the platform's native WebView implementation.
|
||||
//!
|
||||
|
||||
use std::borrow::BorrowMut;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::mpsc::channel;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
@@ -21,7 +27,6 @@ mod cfg;
|
||||
mod dom;
|
||||
mod escape;
|
||||
mod events;
|
||||
use events::*;
|
||||
|
||||
static HTML_CONTENT: &'static str = include_str!("./index.html");
|
||||
|
||||
@@ -31,20 +36,15 @@ pub fn launch(
|
||||
) -> anyhow::Result<()> {
|
||||
launch_with_props(root, (), config_builder)
|
||||
}
|
||||
pub fn launch_with_props<P: Properties + 'static>(
|
||||
|
||||
pub fn launch_with_props<P: Properties + 'static + Send + Sync>(
|
||||
root: FC<P>,
|
||||
props: P,
|
||||
builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
|
||||
) -> anyhow::Result<()> {
|
||||
WebviewRenderer::run(root, props, builder)
|
||||
run(root, props, builder)
|
||||
}
|
||||
|
||||
/// The `WebviewRenderer` provides a way of rendering a Dioxus Virtual DOM through a bridge to a Webview instance.
|
||||
/// Components used in WebviewRenderer instances can directly use system libraries, access the filesystem, and multithread with ease.
|
||||
pub struct WebviewRenderer<T> {
|
||||
/// The root component used to render the Webview
|
||||
root: FC<T>,
|
||||
}
|
||||
enum RpcEvent<'a> {
|
||||
Initialize { edits: Vec<DomEdit<'a>> },
|
||||
}
|
||||
@@ -55,184 +55,165 @@ struct Response<'a> {
|
||||
edits: Vec<DomEdit<'a>>,
|
||||
}
|
||||
|
||||
impl<T: Properties + 'static> WebviewRenderer<T> {
|
||||
pub fn run(
|
||||
root: FC<T>,
|
||||
props: T,
|
||||
user_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
|
||||
) -> anyhow::Result<()> {
|
||||
Self::run_with_edits(root, props, user_builder, None)
|
||||
}
|
||||
|
||||
pub fn run_with_edits<
|
||||
F: for<'a, 'b> FnOnce(&'a mut DesktopConfig<'b>) -> &'a mut DesktopConfig<'b>,
|
||||
>(
|
||||
root: FC<T>,
|
||||
props: T,
|
||||
user_builder: F,
|
||||
redits: Option<Vec<DomEdit<'static>>>,
|
||||
) -> anyhow::Result<()> {
|
||||
log::info!("hello edits");
|
||||
let event_loop = EventLoop::new();
|
||||
|
||||
let mut cfg = DesktopConfig::new();
|
||||
user_builder(&mut cfg);
|
||||
|
||||
let DesktopConfig {
|
||||
window,
|
||||
manual_edits,
|
||||
pre_rendered,
|
||||
} = cfg;
|
||||
|
||||
let window = window.build(&event_loop)?;
|
||||
|
||||
let mut vir = VirtualDom::new_with_props(root, props);
|
||||
|
||||
let channel = vir.get_event_sender();
|
||||
struct WebviewBridge {}
|
||||
// impl RealDom for WebviewBridge {
|
||||
// fn raw_node_as_any(&self) -> &mut dyn std::any::Any {
|
||||
// todo!()
|
||||
// }
|
||||
|
||||
// fn must_commit(&self) -> bool {
|
||||
// false
|
||||
// }
|
||||
|
||||
// fn commit_edits<'a>(&mut self, edits: &mut Vec<DomEdit<'a>>) {}
|
||||
|
||||
// fn wait_until_ready<'s>(
|
||||
// &'s mut self,
|
||||
// ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + 's>> {
|
||||
// //
|
||||
// Box::pin(async {
|
||||
// //
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
let mut real_dom = WebviewBridge {};
|
||||
// async_std::task::spawn_local(vir.run(&mut real_dom));
|
||||
|
||||
// todo: combine these or something
|
||||
let vdom = Arc::new(RwLock::new(vir));
|
||||
// let registry = Arc::new(RwLock::new(Some(WebviewRegistry::new())));
|
||||
|
||||
let webview = WebViewBuilder::new(window)?
|
||||
.with_url(&format!("data:text/html,{}", HTML_CONTENT))?
|
||||
// .with_rpc_handler(move |_window: &Window, mut req: RpcRequest| {
|
||||
// match req.method.as_str() {
|
||||
// "initiate" => {
|
||||
// let edits = if let Some(edits) = &redits {
|
||||
// serde_json::to_value(edits).unwrap()
|
||||
// } else {
|
||||
// let mut lock = vdom.write().unwrap();
|
||||
// // let mut reg_lock = registry.write().unwrap();
|
||||
// // Create the thin wrapper around the registry to collect the edits into
|
||||
// let mut real = dom::WebviewDom::new();
|
||||
// let pre = pre_rendered.clone();
|
||||
// let response = match pre {
|
||||
// Some(content) => {
|
||||
// lock.rebuild_in_place().unwrap();
|
||||
// Response {
|
||||
// edits: Vec::new(),
|
||||
// pre_rendered: Some(content),
|
||||
// }
|
||||
// }
|
||||
// None => {
|
||||
// //
|
||||
// let edits = {
|
||||
// // let mut edits = Vec::new();
|
||||
// todo!()
|
||||
// // lock.rebuild(&mut real, &mut edits).unwrap();
|
||||
// // edits
|
||||
// };
|
||||
// Response {
|
||||
// edits,
|
||||
// pre_rendered: None,
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// serde_json::to_value(&response).unwrap()
|
||||
// };
|
||||
// // Return the edits into the webview runtime
|
||||
// Some(RpcResponse::new_result(req.id.take(), Some(edits)))
|
||||
// }
|
||||
// "user_event" => {
|
||||
// log::debug!("User event received");
|
||||
// // let registry = registry.clone();
|
||||
// let vdom = vdom.clone();
|
||||
// let response = async_std::task::block_on(async move {
|
||||
// let mut lock = vdom.write().unwrap();
|
||||
// // let mut reg_lock = registry.write().unwrap();
|
||||
// // a deserialized event
|
||||
// let data = req.params.unwrap();
|
||||
// log::debug!("Data: {:#?}", data);
|
||||
// let event = trigger_from_serialized(data);
|
||||
// // lock.queue_event(event);
|
||||
// // Create the thin wrapper around the registry to collect the edits into
|
||||
// let mut real = dom::WebviewDom::new();
|
||||
// // Serialize the edit stream
|
||||
// //
|
||||
// let mut edits = Vec::new();
|
||||
// // lock.run(&mut real, &mut edits)
|
||||
// // .await
|
||||
// // .expect("failed to progress");
|
||||
// let response = Response {
|
||||
// edits,
|
||||
// pre_rendered: None,
|
||||
// };
|
||||
// let response = serde_json::to_value(&response).unwrap();
|
||||
// // Give back the registry into its slot
|
||||
// // *reg_lock = Some(real.consume());
|
||||
// // Return the edits into the webview runtime
|
||||
// Some(RpcResponse::new_result(req.id.take(), Some(response)))
|
||||
// });
|
||||
// response
|
||||
// // spawn a task to clean up the garbage
|
||||
// }
|
||||
// _ => todo!("this message failed"),
|
||||
// }
|
||||
// })
|
||||
.build()?;
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
*control_flow = ControlFlow::Wait;
|
||||
|
||||
match event {
|
||||
Event::WindowEvent {
|
||||
event, window_id, ..
|
||||
} => match event {
|
||||
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
||||
WindowEvent::Resized(_) | WindowEvent::Moved(_) => {
|
||||
let _ = webview.resize();
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
|
||||
Event::MainEventsCleared => {
|
||||
webview.resize();
|
||||
// window.request_redraw();
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Create a new text renderer from an existing Virtual DOM.
|
||||
/// This will progress the existing VDom's events to completion.
|
||||
pub fn from_vdom() -> Self {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Pass new args to the root function
|
||||
pub fn update(&mut self, _new_val: T) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Modify the root function in place, forcing a re-render regardless if the props changed
|
||||
pub fn update_mut(&mut self, _modifier: impl Fn(&mut T)) {
|
||||
todo!()
|
||||
}
|
||||
pub fn run<T: Properties + 'static + Send + Sync>(
|
||||
root: FC<T>,
|
||||
props: T,
|
||||
user_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
|
||||
) -> anyhow::Result<()> {
|
||||
run_with_edits(root, props, user_builder, None)
|
||||
}
|
||||
|
||||
pub fn run_with_edits<
|
||||
F: for<'a, 'b> FnOnce(&'a mut DesktopConfig<'b>) -> &'a mut DesktopConfig<'b>,
|
||||
T: Properties + 'static + Send + Sync,
|
||||
>(
|
||||
root: FC<T>,
|
||||
props: T,
|
||||
user_builder: F,
|
||||
redits: Option<Vec<DomEdit<'static>>>,
|
||||
) -> anyhow::Result<()> {
|
||||
/*
|
||||
|
||||
|
||||
*/
|
||||
|
||||
let mut cfg = DesktopConfig::new();
|
||||
user_builder(&mut cfg);
|
||||
let DesktopConfig {
|
||||
window,
|
||||
manual_edits,
|
||||
pre_rendered,
|
||||
} = cfg;
|
||||
|
||||
let event_loop = EventLoop::new();
|
||||
let window = window.build(&event_loop)?;
|
||||
|
||||
let (event_tx, mut event_rx) = tokio::sync::mpsc::unbounded_channel::<String>();
|
||||
|
||||
// Spawn the virtualdom onto its own thread
|
||||
// if it wants to spawn multithreaded tasks, it can use the executor directly
|
||||
std::thread::spawn(move || {
|
||||
//
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
runtime.block_on(async move {
|
||||
let mut vir = VirtualDom::new_with_props(root, props);
|
||||
let channel = vir.get_event_sender();
|
||||
loop {
|
||||
vir.wait_for_work().await;
|
||||
let edits = vir.run_with_deadline(|| false);
|
||||
let edit_string = serde_json::to_string(&edits[0].edits).unwrap();
|
||||
event_tx.send(edit_string).unwrap();
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
let dioxus_requsted = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let webview = WebViewBuilder::new(window)?
|
||||
.with_url(&format!("data:text/html,{}", HTML_CONTENT))?
|
||||
.with_rpc_handler(move |_window: &Window, mut req: RpcRequest| {
|
||||
match req.method.as_str() {
|
||||
"initiate" => {}
|
||||
"user_event" => {}
|
||||
_ => todo!("this message failed"),
|
||||
}
|
||||
todo!()
|
||||
})
|
||||
.build()?;
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
*control_flow = ControlFlow::Wait;
|
||||
|
||||
match event {
|
||||
Event::WindowEvent {
|
||||
event, window_id, ..
|
||||
} => match event {
|
||||
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
||||
WindowEvent::Resized(_) | WindowEvent::Moved(_) => {
|
||||
let _ = webview.resize();
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
|
||||
Event::MainEventsCleared => {
|
||||
webview.resize();
|
||||
// window.request_redraw();
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// let edits = if let Some(edits) = &redits {
|
||||
// serde_json::to_value(edits).unwrap()
|
||||
// } else {
|
||||
// let mut lock = vdom.write().unwrap();
|
||||
// // let mut reg_lock = registry.write().unwrap();
|
||||
// // Create the thin wrapper around the registry to collect the edits into
|
||||
// let mut real = dom::WebviewDom::new();
|
||||
// let pre = pre_rendered.clone();
|
||||
// let response = match pre {
|
||||
// Some(content) => {
|
||||
// lock.rebuild_in_place().unwrap();
|
||||
// Response {
|
||||
// edits: Vec::new(),
|
||||
// pre_rendered: Some(content),
|
||||
// }
|
||||
// }
|
||||
// None => {
|
||||
// //
|
||||
// let edits = {
|
||||
// // let mut edits = Vec::new();
|
||||
// todo!()
|
||||
// // lock.rebuild(&mut real, &mut edits).unwrap();
|
||||
// // edits
|
||||
// };
|
||||
// Response {
|
||||
// edits,
|
||||
// pre_rendered: None,
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
// serde_json::to_value(&response).unwrap()
|
||||
// };
|
||||
// // Return the edits into the webview runtime
|
||||
// Some(RpcResponse::new_result(req.id.take(), Some(edits)))
|
||||
|
||||
// log::debug!("User event received");
|
||||
// // let registry = registry.clone();
|
||||
// let vdom = vdom.clone();
|
||||
// let response = async_std::task::block_on(async move {
|
||||
// let mut lock = vdom.write().unwrap();
|
||||
// // let mut reg_lock = registry.write().unwrap();
|
||||
// // a deserialized event
|
||||
// let data = req.params.unwrap();
|
||||
// log::debug!("Data: {:#?}", data);
|
||||
// let event = trigger_from_serialized(data);
|
||||
// // lock.queue_event(event);
|
||||
// // Create the thin wrapper around the registry to collect the edits into
|
||||
// let mut real = dom::WebviewDom::new();
|
||||
// // Serialize the edit stream
|
||||
// //
|
||||
// let mut edits = Vec::new();
|
||||
// // lock.run(&mut real, &mut edits)
|
||||
// // .await
|
||||
// // .expect("failed to progress");
|
||||
// let response = Response {
|
||||
// edits,
|
||||
// pre_rendered: None,
|
||||
// };
|
||||
// let response = serde_json::to_value(&response).unwrap();
|
||||
// // Give back the registry into its slot
|
||||
// // *reg_lock = Some(real.consume());
|
||||
// // Return the edits into the webview runtime
|
||||
// Some(RpcResponse::new_result(req.id.take(), Some(response)))
|
||||
// });
|
||||
// response
|
||||
// // spawn a task to clean up the garbage
|
||||
Reference in new issue
Block a user