dioxus/packages/desktop/src/lib.rs

244 lines
7.5 KiB
Rust
Raw Normal View History

2021-10-01 06:07:12 +00:00
//! Dioxus Desktop Renderer
//!
//! Render the Dioxus VirtualDom using the platform's native WebView implementation.
//!
2021-07-08 16:01:31 +00:00
use std::borrow::BorrowMut;
2021-10-04 05:28:04 +00:00
use std::cell::RefCell;
2021-07-09 03:25:27 +00:00
use std::ops::{Deref, DerefMut};
2021-10-04 05:28:04 +00:00
use std::rc::Rc;
2021-10-01 06:07:12 +00:00
use std::sync::atomic::AtomicBool;
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-10-04 05:28:04 +00:00
use dioxus_core::scheduler::SchedulerMsg;
2021-07-13 03:44:20 +00:00
use dioxus_core::*;
2021-10-04 05:28:04 +00:00
// use futures_channel::mpsc::UnboundedSender;
2021-08-06 02:23:41 +00:00
use serde::{Deserialize, Serialize};
2021-10-04 05:28:04 +00:00
mod logging;
pub use logging::set_up_logging;
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};
2021-10-04 05:28:04 +00:00
use wry::application::event_loop::{self, ControlFlow, EventLoop};
2021-07-13 20:48:11 +00:00
use wry::application::window::Fullscreen;
2021-10-04 05:28:04 +00:00
use wry::webview::{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;
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<()>,
2021-09-25 00:11:30 +00:00
config_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
) -> anyhow::Result<()> {
2021-09-25 00:11:30 +00:00
launch_with_props(root, (), config_builder)
}
2021-10-01 06:07:12 +00:00
pub fn launch_with_props<P: Properties + 'static + Send + Sync>(
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<()> {
2021-10-01 06:07:12 +00:00
run(root, props, builder)
2021-01-21 16:10:31 +00:00
}
2021-10-04 05:28:04 +00:00
#[derive(Serialize)]
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
}
2021-10-04 05:28:04 +00:00
enum BridgeEvent {
Initialize(serde_json::Value),
Update(serde_json::Value),
}
#[derive(Serialize)]
struct Response<'a> {
pre_rendered: Option<String>,
edits: Vec<DomEdit<'a>>,
}
2021-10-01 06:07:12 +00:00
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<()> {
let mut cfg = DesktopConfig::new();
user_builder(&mut cfg);
let DesktopConfig {
window,
manual_edits,
pre_rendered,
2021-10-04 05:28:04 +00:00
..
2021-10-01 06:07:12 +00:00
} = cfg;
let event_loop = EventLoop::new();
let window = window.build(&event_loop)?;
2021-10-04 05:28:04 +00:00
let (event_tx, mut event_rx) = tokio::sync::mpsc::unbounded_channel();
let sender = launch_vdom_with_tokio(root, props, event_tx.clone());
let locked_receiver = Rc::new(RefCell::new(event_rx));
let webview = WebViewBuilder::new(window)?
.with_url("wry://index.html")?
2021-10-04 05:28:04 +00:00
.with_rpc_handler(move |_window: &Window, mut req: RpcRequest| {
2021-10-04 06:54:20 +00:00
//
2021-10-04 05:28:04 +00:00
match req.method.as_str() {
"initiate" => {
let mut rx = (*locked_receiver).borrow_mut();
match rx.try_recv() {
Ok(BridgeEvent::Initialize(edits)) => {
Some(RpcResponse::new_result(req.id.take(), Some(edits)))
}
_ => None,
}
}
"user_event" => {
let data = req.params.unwrap();
let event = events::trigger_from_serialized(data);
sender.unbounded_send(SchedulerMsg::UiEvent(event)).unwrap();
let mut rx = (*locked_receiver).borrow_mut();
match rx.blocking_recv() {
Some(BridgeEvent::Update(edits)) => {
Some(RpcResponse::new_result(req.id.take(), Some(edits)))
}
2021-10-04 06:54:20 +00:00
_ => None,
2021-10-04 05:28:04 +00:00
}
}
_ => todo!("this message failed"),
}
})
// this isn't quite portable unfortunately :(
// todo: figure out a way to allow us to create the index.html with the index.js file separately
// it's a bit easier to hack with
.with_custom_protocol("wry".into(), move |request| {
use std::fs::{canonicalize, read};
use wry::http::ResponseBuilder;
// Remove url scheme
let path = request.uri().replace("wry://", "");
let (data, meta) = match path.as_str() {
"index.html" => (include_bytes!("./index.html").to_vec(), "text/html"),
"index.html/index.js" => (include_bytes!("./index.js").to_vec(), "text/javascript"),
_ => unimplemented!("path {}", path),
2021-10-04 05:28:04 +00:00
};
ResponseBuilder::new().mimetype(meta).body(data)
})
.build()?;
run_event_loop(event_loop, webview, event_tx);
2021-10-01 06:07:12 +00:00
2021-10-04 05:28:04 +00:00
Ok(())
}
pub fn start<P: 'static + Send>(
root: FC<P>,
config_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
) -> ((), ()) {
//
((), ())
}
// Create a new tokio runtime on a dedicated thread and then launch the apps VirtualDom.
fn launch_vdom_with_tokio<C: Send + 'static>(
root: FC<C>,
props: C,
event_tx: tokio::sync::mpsc::UnboundedSender<BridgeEvent>,
) -> futures_channel::mpsc::UnboundedSender<SchedulerMsg> {
2021-10-01 06:07:12 +00:00
// Spawn the virtualdom onto its own thread
// if it wants to spawn multithreaded tasks, it can use the executor directly
2021-10-04 05:28:04 +00:00
let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>();
let sender_2 = sender.clone();
2021-10-01 06:07:12 +00:00
std::thread::spawn(move || {
2021-10-04 05:28:04 +00:00
// We create the runtim as multithreaded, so you can still "spawn" onto multiple threads
let runtime = tokio::runtime::Builder::new_multi_thread()
2021-10-01 06:07:12 +00:00
.enable_all()
.build()
.unwrap();
runtime.block_on(async move {
2021-10-04 05:28:04 +00:00
let mut vir = VirtualDom::new_with_props_and_scheduler(root, props, sender, receiver);
let _ = vir.get_event_sender();
let edits = vir.rebuild();
2021-10-04 06:54:20 +00:00
// the receiving end expects something along these lines
2021-10-04 05:28:04 +00:00
#[derive(Serialize)]
struct Evt<'a> {
edits: Vec<DomEdit<'a>>,
}
let edit_string = serde_json::to_value(Evt { edits: edits.edits }).unwrap();
match event_tx.send(BridgeEvent::Initialize(edit_string)) {
Ok(_) => {}
Err(_) => {}
}
2021-10-01 06:07:12 +00:00
loop {
vir.wait_for_work().await;
2021-10-04 05:28:04 +00:00
let mut muts = vir.run_with_deadline(|| false);
while let Some(edit) = muts.pop() {
let edit_string = serde_json::to_value(Evt { edits: edit.edits }).unwrap();
match event_tx.send(BridgeEvent::Update(edit_string)) {
Ok(_) => {}
Err(er) => {
log::error!("Sending should not fail {}", er);
}
}
}
2021-10-01 06:07:12 +00:00
2021-10-04 05:28:04 +00:00
log::info!("mutations sent on channel");
2021-10-01 06:07:12 +00:00
}
})
2021-10-04 05:28:04 +00:00
});
sender_2
}
2021-10-01 06:07:12 +00:00
2021-10-04 05:28:04 +00:00
fn run_event_loop(
event_loop: EventLoop<()>,
webview: WebView,
event_tx: tokio::sync::mpsc::UnboundedSender<BridgeEvent>,
) {
let _ = event_tx.clone();
event_loop.run(move |event, target, control_flow| {
2021-10-01 06:07:12 +00:00
*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();
}
2021-08-06 02:23:41 +00:00
_ => {}
2021-10-01 06:07:12 +00:00
},
Event::MainEventsCleared => {
webview.resize();
// window.request_redraw();
2021-07-08 16:01:31 +00:00
}
2021-10-01 06:07:12 +00:00
_ => {}
}
2021-10-04 05:28:04 +00:00
})
2021-02-25 23:44:00 +00:00
}