implement for web and desktop

This commit is contained in:
= committed 2022-12-15 21:16:37 -06:00
1 parent ab743e01b7
commit 0d9c350d5e
8 files changed
+69 -30

No files matched your search

+18 -5
View File
@@ -1,7 +1,7 @@
use crate::desktop_context::{DesktopContext, UserWindowEvent};
use crate::events::{decode_event, EventMessage};
use dioxus_core::*;
use futures_channel::mpsc::{unbounded, UnboundedSender};
use futures_channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use futures_util::StreamExt;
#[cfg(target_os = "ios")]
use objc::runtime::Object;
@@ -24,6 +24,8 @@ pub(super) struct DesktopController {
pub(super) is_ready: Arc<AtomicBool>,
pub(super) proxy: EventLoopProxy<UserWindowEvent>,
pub(super) event_tx: UnboundedSender<serde_json::Value>,
#[cfg(debug_assertions)]
pub(super) templates_tx: UnboundedSender<Template<'static>>,
#[cfg(target_os = "ios")]
pub(super) views: Vec<*mut Object>,
@@ -39,6 +41,7 @@ impl DesktopController {
) -> Self {
let edit_queue = Arc::new(Mutex::new(Vec::new()));
let (event_tx, mut event_rx) = unbounded();
let (templates_tx, mut templates_rx) = unbounded();
let proxy2 = proxy.clone();
let pending_edits = edit_queue.clone();
@@ -64,6 +67,18 @@ impl DesktopController {
loop {
tokio::select! {
template = {
#[allow(unused)]
fn maybe_future<'a>(templates_rx: &'a mut UnboundedReceiver<Template<'static>>) -> impl Future<Output = dioxus_core::Template<'static>> + 'a {
#[cfg(debug_assertions)]
return templates_rx.select_next_some();
#[cfg(not(debug_assertions))]
return std::future::pending();
}
maybe_future(&mut templates_rx)
} => {
dom.replace_template(template);
}
_ = dom.wait_for_work() => {}
Some(json_value) = event_rx.next() => {
if let Ok(value) = serde_json::from_value::<EventMessage>(json_value) {
@@ -93,6 +108,8 @@ impl DesktopController {
quit_app_on_close: true,
proxy: proxy2,
event_tx,
#[cfg(debug_assertions)]
templates_tx,
#[cfg(target_os = "ios")]
views: vec![],
}
@@ -123,8 +140,4 @@ impl DesktopController {
}
}
}
pub(crate) fn set_template(&self, _serialized_template: String) {
todo!("hot reloading currently WIP")
}
}
-6
View File
@@ -171,11 +171,6 @@ pub enum UserWindowEvent {
DragWindow,
FocusWindow,
/// Set a new Dioxus template for hot-reloading
///
/// Is a no-op in release builds. Must fit the right format for templates
SetTemplate(String),
Visible(bool),
Minimize(bool),
Maximize(bool),
@@ -223,7 +218,6 @@ impl DesktopController {
match user_event {
Initialize | EditsReady => self.try_load_ready_webviews(),
SetTemplate(template) => self.set_template(template),
CloseWindow => *control_flow = ControlFlow::Exit,
DragWindow => {
// if the drag_window has any errors, we don't do anything
+5 -7
View File
@@ -1,6 +1,6 @@
#![allow(dead_code)]
use dioxus_core::VirtualDom;
use dioxus_core::Template;
use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
use std::io::{BufRead, BufReader};
@@ -13,7 +13,7 @@ fn handle_error(connection: std::io::Result<LocalSocketStream>) -> Option<LocalS
.ok()
}
pub(crate) fn init(_dom: &VirtualDom) {
pub(crate) fn init(proxy: futures_channel::mpsc::UnboundedSender<Template<'static>>) {
let latest_in_connection: Arc<Mutex<Option<BufReader<LocalSocketStream>>>> =
Arc::new(Mutex::new(None));
@@ -36,11 +36,9 @@ pub(crate) fn init(_dom: &VirtualDom) {
let mut buf = String::new();
match conn.read_line(&mut buf) {
Ok(_) => {
todo!()
// let msg: SetTemplateMsg = serde_json::from_str(&buf).unwrap();
// channel
// .start_send(SchedulerMsg::SetTemplate(Box::new(msg)))
// .unwrap();
let msg: Template<'static> =
serde_json::from_str(Box::leak(buf.into_boxed_str())).unwrap();
proxy.unbounded_send(msg).unwrap();
}
Err(err) => {
if err.kind() != std::io::ErrorKind::WouldBlock {
+3
View File
@@ -106,6 +106,9 @@ pub fn launch_with_props<P: 'static + Send>(root: Component<P>, props: P, mut cf
let event_loop = EventLoop::with_user_event();
let mut desktop = DesktopController::new_on_tokio(root, props, event_loop.create_proxy());
#[cfg(debug_assertions)]
hot_reload::init(desktop.templates_tx.clone());
event_loop.run(move |window_event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;