From b94c807230342aadad705d88a1f881d26eb2a0ff Mon Sep 17 00:00:00 2001 From: James Carl Date: Tue, 21 Mar 2023 17:25:02 -0400 Subject: [PATCH] Implements #1571 --- Cargo.lock | 1 - crates/fj-host/Cargo.toml | 1 - crates/fj-host/src/host.rs | 5 ++--- crates/fj-host/src/host_thread.rs | 11 +++++------ crates/fj-window/src/run.rs | 17 ++++++++++++++++- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d4e2b50e7..f6d2aba8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1088,7 +1088,6 @@ dependencies = [ "notify", "thiserror", "tracing", - "winit", ] [[package]] diff --git a/crates/fj-host/Cargo.toml b/crates/fj-host/Cargo.toml index 67f8ddf43..c3b920e8f 100644 --- a/crates/fj-host/Cargo.toml +++ b/crates/fj-host/Cargo.toml @@ -21,4 +21,3 @@ libloading = "0.7.4" notify = "5.1.0" thiserror = "1.0.40" tracing = "0.1.37" -winit = "0.28.3" diff --git a/crates/fj-host/src/host.rs b/crates/fj-host/src/host.rs index c973a0ec1..7789739d6 100644 --- a/crates/fj-host/src/host.rs +++ b/crates/fj-host/src/host.rs @@ -2,7 +2,6 @@ use std::thread::JoinHandle; use crossbeam_channel::Sender; use fj_operations::shape_processor::ShapeProcessor; -use winit::event_loop::EventLoopProxy; use crate::{EventLoopClosed, HostThread, Model, ModelEvent}; @@ -18,10 +17,10 @@ impl Host { /// loop. pub fn new( shape_processor: ShapeProcessor, - event_loop_proxy: EventLoopProxy, + model_event_tx: Sender, ) -> Self { let (command_tx, host_thread) = - HostThread::spawn(shape_processor, event_loop_proxy); + HostThread::spawn(shape_processor, model_event_tx); Self { command_tx, diff --git a/crates/fj-host/src/host_thread.rs b/crates/fj-host/src/host_thread.rs index 638b3df05..4cd7937ca 100644 --- a/crates/fj-host/src/host_thread.rs +++ b/crates/fj-host/src/host_thread.rs @@ -3,7 +3,6 @@ use std::thread::{self, JoinHandle}; use crossbeam_channel::{self, Receiver, Sender}; use fj_interop::processed_shape::ProcessedShape; use fj_operations::shape_processor::ShapeProcessor; -use winit::event_loop::EventLoopProxy; use crate::{Error, HostCommand, Model, Watcher}; @@ -14,7 +13,7 @@ pub(crate) struct EventLoopClosed; pub(crate) struct HostThread { shape_processor: ShapeProcessor, - event_loop_proxy: EventLoopProxy, + model_event_tx: Sender, command_tx: Sender, command_rx: Receiver, } @@ -23,14 +22,14 @@ impl HostThread { // Spawn a background thread that will process models for an event loop. pub(crate) fn spawn( shape_processor: ShapeProcessor, - event_loop_proxy: EventLoopProxy, + event_loop_proxy: Sender, ) -> (Sender, JoinHandle>) { let (command_tx, command_rx) = crossbeam_channel::unbounded(); let command_tx_2 = command_tx.clone(); let host_thread = Self { shape_processor, - event_loop_proxy, + model_event_tx: event_loop_proxy, command_tx, command_rx, }; @@ -117,8 +116,8 @@ impl HostThread { // Send a message to the event loop. fn send_event(&mut self, event: ModelEvent) -> Result<(), EventLoopClosed> { - self.event_loop_proxy - .send_event(event) + self.model_event_tx + .send(event) .map_err(|_| EventLoopClosed)?; Ok(()) diff --git a/crates/fj-window/src/run.rs b/crates/fj-window/src/run.rs index c089cac2d..94df90d71 100644 --- a/crates/fj-window/src/run.rs +++ b/crates/fj-window/src/run.rs @@ -6,6 +6,7 @@ use std::{ error, fmt::{self, Write}, + thread, }; use fj_host::{Host, Model, ModelEvent}; @@ -32,7 +33,21 @@ pub fn run( let egui_winit_state = egui_winit::State::new(&event_loop); - let mut host = Host::new(shape_processor, event_loop.create_proxy()); + let (model_event_tx, model_event_rx) = crossbeam_channel::unbounded(); + let event_proxy = event_loop.create_proxy(); + + let _event_relay_join_handle = thread::Builder::new() + .name("event_relay".to_string()) + .spawn(move || { + for event in model_event_rx { + if event_proxy.send_event(event).is_err() { + // Looks like the main window closed. + break; + } + } + }); + + let mut host = Host::new(shape_processor, model_event_tx); if let Some(model) = model { host.load_model(model);