dioxus/packages/desktop/src/lib.rs

285 lines
9.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-05 07:37:15 +00:00
use std::cell::{Cell, RefCell};
2021-11-16 06:25:38 +00:00
use std::collections::{HashMap, VecDeque};
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-07-13 03:44:20 +00:00
use dioxus_core::*;
2021-08-06 02:23:41 +00:00
use serde::{Deserialize, Serialize};
2021-10-04 05:28:04 +00:00
2021-07-15 22:40:12 +00:00
pub use wry;
2021-07-13 03:44:20 +00:00
2021-10-25 19:05:17 +00:00
use wry::application::accelerator::{Accelerator, SysMods};
2021-11-03 19:13:50 +00:00
use wry::application::event::{ElementState, Event, StartCause, WindowEvent};
2021-11-16 06:25:38 +00:00
use wry::application::event_loop::{self, ControlFlow, EventLoop, EventLoopWindowTarget};
2021-11-03 19:13:50 +00:00
use wry::application::keyboard::{Key, KeyCode, ModifiersState};
2021-10-25 19:05:17 +00:00
use wry::application::menu::{MenuBar, MenuItem, MenuItemAttributes};
2021-11-16 06:25:38 +00:00
use wry::application::window::{Fullscreen, WindowId};
2021-10-04 05:28:04 +00:00
use wry::webview::{WebView, WebViewBuilder};
2021-07-08 16:01:31 +00:00
use wry::{
2021-10-25 19:05:17 +00:00
application::menu,
2021-07-08 16:01:31 +00:00
application::window::{Window, WindowBuilder},
webview::{RpcRequest, RpcResponse},
};
mod cfg;
2021-10-05 07:37:15 +00:00
mod desktop_context;
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>,
2021-10-05 07:37:15 +00:00
) {
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-10-05 07:37:15 +00:00
) {
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
}
#[derive(Serialize)]
struct Response<'a> {
pre_rendered: Option<String>,
edits: Vec<DomEdit<'a>>,
}
2021-10-05 07:37:15 +00:00
pub fn run<T: 'static + Send + Sync>(
2021-10-01 06:07:12 +00:00
root: FC<T>,
props: T,
user_builder: impl for<'a, 'b> FnOnce(&'b mut DesktopConfig<'a>) -> &'b mut DesktopConfig<'a>,
2021-10-05 07:37:15 +00:00
) {
// Generate the config
2021-10-01 06:07:12 +00:00
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;
2021-10-05 07:37:15 +00:00
// All of our webview windows are stored in a way that we can look them up later
// The "DesktopContext" will provide functionality for spawning these windows
2021-11-16 06:25:38 +00:00
let mut webviews = HashMap::<WindowId, WebView>::new();
2021-10-01 06:07:12 +00:00
let event_loop = EventLoop::new();
2021-10-05 07:37:15 +00:00
let props_shared = Cell::new(Some(props));
2021-10-04 05:28:04 +00:00
2021-11-03 19:13:50 +00:00
// create local modifier state
2021-11-16 06:25:38 +00:00
let modifiers = ModifiersState::default();
2021-11-03 19:13:50 +00:00
let quit_hotkey = Accelerator::new(SysMods::Cmd, KeyCode::KeyQ);
2021-11-16 06:25:38 +00:00
let edit_queue = Arc::new(RwLock::new(VecDeque::new()));
let is_ready: Arc<AtomicBool> = Default::default();
event_loop.run(move |window_event, event_loop, control_flow| {
2021-10-05 07:37:15 +00:00
*control_flow = ControlFlow::Wait;
2021-10-04 05:28:04 +00:00
2021-11-16 06:25:38 +00:00
match window_event {
2021-10-05 07:37:15 +00:00
Event::NewEvents(StartCause::Init) => {
2021-11-16 06:25:38 +00:00
let window = create_window(event_loop);
2021-10-05 07:37:15 +00:00
let window_id = window.id();
2021-11-16 06:25:38 +00:00
let sender =
launch_vdom_with_tokio(root, props_shared.take().unwrap(), edit_queue.clone());
let webview = create_webview(window, is_ready.clone(), sender);
2021-10-05 07:37:15 +00:00
webviews.insert(window_id, webview);
}
Event::WindowEvent {
event, window_id, ..
} => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
2021-11-03 19:13:50 +00:00
WindowEvent::Destroyed { .. } => {
webviews.remove(&window_id);
if webviews.is_empty() {
*control_flow = ControlFlow::Exit;
}
}
2021-11-16 06:25:38 +00:00
WindowEvent::Moved(pos) => {
//
}
2021-11-03 19:13:50 +00:00
WindowEvent::KeyboardInput { event, .. } => {
if quit_hotkey.matches(&modifiers, &event.physical_key) {
webviews.remove(&window_id);
if webviews.is_empty() {
*control_flow = ControlFlow::Exit;
}
}
}
2021-10-05 07:37:15 +00:00
WindowEvent::Resized(_) | WindowEvent::Moved(_) => {
if let Some(view) = webviews.get_mut(&window_id) {
let _ = view.resize();
2021-10-04 05:28:04 +00:00
}
}
2021-10-05 07:37:15 +00:00
// TODO: we want to shuttle all of these events into the user's app
_ => {}
},
2021-10-04 05:28:04 +00:00
2021-11-16 06:25:38 +00:00
Event::MainEventsCleared => {
// I hate this ready hack but it's needed to wait for the "onload" to occur
// We can't run any initializion scripts because the window isn't ready yet?
if is_ready.load(std::sync::atomic::Ordering::Relaxed) {
let mut queue = edit_queue.write().unwrap();
let (id, view) = webviews.iter_mut().next().unwrap();
while let Some(edit) = queue.pop_back() {
view.evaluate_script(&format!("window.interpreter.handleEdits({})", edit))
.unwrap();
}
}
}
2021-10-05 07:37:15 +00:00
Event::Resumed => {}
Event::Suspended => {}
Event::LoopDestroyed => {}
2021-10-01 06:07:12 +00:00
2021-10-05 07:37:15 +00:00
_ => {}
}
})
2021-10-04 05:28:04 +00:00
}
// Create a new tokio runtime on a dedicated thread and then launch the apps VirtualDom.
2021-10-05 07:37:15 +00:00
pub(crate) fn launch_vdom_with_tokio<P: Send + 'static>(
root: FC<P>,
props: P,
2021-11-16 06:25:38 +00:00
edit_queue: Arc<RwLock<VecDeque<String>>>,
2021-10-04 05:28:04 +00:00
) -> futures_channel::mpsc::UnboundedSender<SchedulerMsg> {
let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>();
2021-10-05 07:37:15 +00:00
let return_sender = sender.clone();
2021-10-04 05:28:04 +00:00
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-11-16 06:25:38 +00:00
let mut dom = VirtualDom::new_with_props_and_scheduler(root, props, sender, receiver);
let edits = dom.rebuild();
2021-10-04 05:28:04 +00:00
2021-11-16 06:25:38 +00:00
edit_queue
.write()
.unwrap()
.push_front(serde_json::to_string(&edits.edits).unwrap());
2021-10-04 05:28:04 +00:00
2021-11-16 06:25:38 +00:00
loop {
dom.wait_for_work().await;
let mut muts = dom.work_with_deadline(|| false);
while let Some(edit) = muts.pop() {
edit_queue
.write()
.unwrap()
.push_front(serde_json::to_string(&edit.edits).unwrap());
}
2021-10-04 05:28:04 +00:00
}
2021-11-16 06:25:38 +00:00
})
});
2021-10-04 05:28:04 +00:00
2021-11-16 06:25:38 +00:00
return_sender
}
2021-10-05 07:37:15 +00:00
2021-11-16 06:25:38 +00:00
fn build_menu() -> MenuBar {
// create main menubar menu
let mut menu_bar_menu = MenuBar::new();
2021-10-04 05:28:04 +00:00
2021-11-16 06:25:38 +00:00
// create `first_menu`
let mut first_menu = MenuBar::new();
2021-10-05 07:37:15 +00:00
2021-11-16 06:25:38 +00:00
first_menu.add_native_item(MenuItem::About("Todos".to_string()));
first_menu.add_native_item(MenuItem::Services);
first_menu.add_native_item(MenuItem::Separator);
first_menu.add_native_item(MenuItem::Hide);
first_menu.add_native_item(MenuItem::HideOthers);
first_menu.add_native_item(MenuItem::ShowAll);
2021-10-05 07:37:15 +00:00
2021-11-16 06:25:38 +00:00
first_menu.add_native_item(MenuItem::Quit);
first_menu.add_native_item(MenuItem::CloseWindow);
2021-10-22 05:16:39 +00:00
2021-11-16 06:25:38 +00:00
// create second menu
let mut second_menu = MenuBar::new();
2021-10-22 05:16:39 +00:00
2021-11-16 06:25:38 +00:00
// second_menu.add_submenu("Sub menu", true, my_sub_menu);
second_menu.add_native_item(MenuItem::Copy);
second_menu.add_native_item(MenuItem::Paste);
second_menu.add_native_item(MenuItem::SelectAll);
menu_bar_menu.add_submenu("First menu", true, first_menu);
menu_bar_menu.add_submenu("Second menu", true, second_menu);
menu_bar_menu
}
2021-10-22 05:16:39 +00:00
2021-11-16 06:25:38 +00:00
fn create_window(event_loop: &EventLoopWindowTarget<()>) -> Window {
WindowBuilder::new()
.with_maximized(true)
.with_menu(build_menu())
.with_title("Dioxus App")
.build(event_loop)
.unwrap()
}
fn create_webview(
window: Window,
is_ready: Arc<AtomicBool>,
sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
) -> WebView {
WebViewBuilder::new(window)
.unwrap()
.with_url("wry://index.html")
.unwrap()
.with_rpc_handler(move |_window: &Window, mut req: RpcRequest| {
match req.method.as_str() {
"user_event" => {
let event = events::trigger_from_serialized(req.params.unwrap());
log::debug!("User event: {:?}", event);
sender.unbounded_send(SchedulerMsg::UiEvent(event)).unwrap();
}
"initialize" => {
is_ready.store(true, std::sync::atomic::Ordering::Relaxed);
}
_ => {}
2021-10-01 06:07:12 +00:00
}
2021-10-04 05:28:04 +00:00
2021-11-16 06:25:38 +00:00
None
})
// Any content that that uses the `wry://` scheme will be shuttled through this handler as a "special case"
// For now, we only serve two pieces of content which get included as bytes into the final binary.
.with_custom_protocol("wry".into(), move |request| {
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),
};
wry::http::ResponseBuilder::new().mimetype(meta).body(data)
})
.build()
.unwrap()
2021-02-25 23:44:00 +00:00
}