dioxus/packages/desktop/src/lib.rs

284 lines
10 KiB
Rust
Raw Normal View History

2021-07-08 16:01:31 +00:00
use std::borrow::BorrowMut;
2021-07-09 03:25:27 +00:00
use std::ops::{Deref, DerefMut};
2021-02-25 23:44:00 +00:00
use std::sync::mpsc::channel;
2021-07-08 16:01:31 +00:00
use std::sync::{Arc, RwLock};
2021-02-25 23:44:00 +00:00
use cfg::DesktopConfig;
2021-07-13 03:44:20 +00:00
use dioxus_core::*;
2021-07-15 22:40:12 +00:00
pub use wry;
2021-07-13 03:44:20 +00:00
2021-07-13 20:48:11 +00:00
use wry::application::event::{Event, WindowEvent};
use wry::application::event_loop::{ControlFlow, EventLoop};
use wry::application::window::Fullscreen;
use wry::webview::WebViewBuilder;
2021-07-08 16:01:31 +00:00
use wry::{
application::window::{Window, WindowBuilder},
webview::{RpcRequest, RpcResponse},
};
mod cfg;
mod dom;
2021-07-09 03:25:27 +00:00
mod escape;
mod events;
use events::*;
2021-02-25 23:44:00 +00:00
static HTML_CONTENT: &'static str = include_str!("./index.html");
2021-01-21 16:10:31 +00:00
pub fn launch(
root: FC<()>,
builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
) -> anyhow::Result<()> {
2021-07-08 13:29:12 +00:00
launch_with_props(root, (), builder)
}
pub fn launch_with_props<P: Properties + 'static>(
root: FC<P>,
2021-07-08 13:29:12 +00:00
props: P,
builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
2021-02-25 23:44:00 +00:00
) -> anyhow::Result<()> {
WebviewRenderer::run(root, props, builder)
2021-01-21 16:10:31 +00:00
}
/// 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>,
}
2021-07-08 16:01:31 +00:00
enum RpcEvent<'a> {
2021-07-16 04:27:06 +00:00
Initialize { edits: Vec<DomEdit<'a>> },
2021-02-25 23:44:00 +00:00
}
#[derive(Serialize)]
struct Response<'a> {
pre_rendered: Option<String>,
edits: Vec<DomEdit<'a>>,
}
impl<T: Properties + 'static> WebviewRenderer<T> {
2021-02-25 23:44:00 +00:00
pub fn run(
root: FC<T>,
props: T,
user_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
2021-07-12 06:23:46 +00:00
) -> anyhow::Result<()> {
Self::run_with_edits(root, props, user_builder, None)
}
pub fn run_with_edits(
root: FC<T>,
props: T,
user_builder: impl for<'a, 'b> FnOnce(&'a mut DesktopConfig<'b>) -> &'a mut DesktopConfig<'b>,
2021-07-12 06:23:46 +00:00
redits: Option<Vec<DomEdit<'static>>>,
2021-02-25 23:44:00 +00:00
) -> anyhow::Result<()> {
log::info!("hello edits");
2021-07-08 16:01:31 +00:00
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)?;
2021-02-25 23:44:00 +00:00
2021-07-09 03:25:27 +00:00
let vir = VirtualDom::new_with_props(root, props);
// todo: combine these or something
let vdom = Arc::new(RwLock::new(vir));
2021-07-26 16:14:48 +00:00
// let registry = Arc::new(RwLock::new(Some(WebviewRegistry::new())));
2021-07-08 16:01:31 +00:00
let webview = WebViewBuilder::new(window)?
.with_url(&format!("data:text/html,{}", HTML_CONTENT))?
2021-07-13 20:48:11 +00:00
.with_rpc_handler(move |_window: &Window, mut req: RpcRequest| {
2021-07-09 03:25:27 +00:00
match req.method.as_str() {
"initiate" => {
2021-07-12 06:23:46 +00:00
let edits = if let Some(edits) = &redits {
serde_json::to_value(edits).unwrap()
} else {
let mut lock = vdom.write().unwrap();
2021-07-26 16:14:48 +00:00
// let mut reg_lock = registry.write().unwrap();
2021-07-09 03:25:27 +00:00
2021-07-12 06:23:46 +00:00
// Create the thin wrapper around the registry to collect the edits into
2021-07-26 16:14:48 +00:00
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();
lock.rebuild(&mut real, &mut edits).unwrap();
edits
};
Response {
edits,
pre_rendered: None,
}
}
2021-07-12 06:23:46 +00:00
};
2021-07-09 03:25:27 +00:00
serde_json::to_value(&response).unwrap()
2021-07-12 06:23:46 +00:00
};
2021-07-09 03:25:27 +00:00
// Return the edits into the webview runtime
Some(RpcResponse::new_result(req.id.take(), Some(edits)))
}
"user_event" => {
log::debug!("User event received");
2021-07-09 03:25:27 +00:00
2021-07-26 16:14:48 +00:00
// let registry = registry.clone();
let vdom = vdom.clone();
let response = async_std::task::block_on(async move {
let mut lock = vdom.write().unwrap();
2021-07-26 16:14:48 +00:00
// 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);
2021-07-09 03:25:27 +00:00
// Create the thin wrapper around the registry to collect the edits into
2021-07-26 16:14:48 +00:00
let mut real = dom::WebviewDom::new();
// Serialize the edit stream
//
let mut edits = Vec::new();
lock.progress_with_event(&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
2021-07-26 16:14:48 +00:00
// *reg_lock = Some(real.consume());
2021-07-09 03:25:27 +00:00
// Return the edits into the webview runtime
Some(RpcResponse::new_result(req.id.take(), Some(response)))
});
2021-07-09 03:25:27 +00:00
response
// spawn a task to clean up the garbage
2021-07-09 03:25:27 +00:00
}
_ => todo!("this message failed"),
}
})
2021-07-08 16:01:31 +00:00
.build()?;
event_loop.run(move |event, _, control_flow| {
2021-07-16 04:27:06 +00:00
*control_flow = ControlFlow::Wait;
2021-07-08 16:01:31 +00:00
match event {
2021-07-15 22:40:12 +00:00
Event::WindowEvent {
event, window_id, ..
} => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::Resized(_) | WindowEvent::Moved(_) => {
let _ = webview.resize();
2021-07-08 16:01:31 +00:00
}
2021-07-15 22:40:12 +00:00
_ => {}
},
Event::MainEventsCleared => {
webview.resize();
// window.request_redraw();
2021-07-08 16:01:31 +00:00
}
2021-07-15 22:40:12 +00:00
_ => {} // Event::WindowEvent { event, .. } => {
// //
// match event {
// WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
// _ => {}
// }
// }
// _ => {
// // let _ = webview.resize();
// }
2021-07-08 16:01:31 +00:00
}
});
2021-02-25 23:44:00 +00:00
}
2021-01-21 16:10:31 +00:00
/// Create a new text-renderer instance from a functional component root.
/// Automatically progresses the creation of the VNode tree to completion.
///
/// A VDom is automatically created. If you want more granular control of the VDom, use `from_vdom`
2021-07-08 16:01:31 +00:00
// pub fn new(root: FC<T>, builder: impl FnOnce() -> WVResult<WebView<'static, ()>>) -> Self {
// Self { root }
// }
2021-01-21 16:10:31 +00:00
/// 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
2021-07-13 20:48:11 +00:00
pub fn update(&mut self, _new_val: T) {
2021-01-21 16:10:31 +00:00
todo!()
2021-01-21 08:22:08 +00:00
}
2021-01-21 16:10:31 +00:00
/// Modify the root function in place, forcing a re-render regardless if the props changed
2021-07-13 20:48:11 +00:00
pub fn update_mut(&mut self, _modifier: impl Fn(&mut T)) {
2021-01-21 16:10:31 +00:00
todo!()
}
2021-02-25 23:44:00 +00:00
}
2021-01-21 16:10:31 +00:00
2021-07-08 16:01:31 +00:00
use serde::{Deserialize, Serialize};
use serde_json::Value;
2021-07-26 16:14:48 +00:00
// use crate::dom::WebviewRegistry;
2021-07-09 03:25:27 +00:00
2021-07-08 16:01:31 +00:00
#[derive(Debug, Serialize, Deserialize)]
struct MessageParameters {
message: String,
}
fn HANDLER(window: &Window, mut req: RpcRequest) -> Option<RpcResponse> {
let mut response = None;
if &req.method == "fullscreen" {
if let Some(params) = req.params.take() {
if let Ok(mut args) = serde_json::from_value::<Vec<bool>>(params) {
if !args.is_empty() {
if args.swap_remove(0) {
window.set_fullscreen(Some(Fullscreen::Borderless(None)));
} else {
window.set_fullscreen(None);
}
};
response = Some(RpcResponse::new_result(req.id.take(), None));
}
}
} else if &req.method == "send-parameters" {
if let Some(params) = req.params.take() {
if let Ok(mut args) = serde_json::from_value::<Vec<MessageParameters>>(params) {
let result = if !args.is_empty() {
let msg = args.swap_remove(0);
Some(Value::String(format!("Hello, {}!", msg.message)))
} else {
// NOTE: in the real-world we should send an error response here!
None
};
// Must always send a response as this is a `call()`
response = Some(RpcResponse::new_result(req.id.take(), result));
}
}
}
response
}