diff --git a/src/main.rs b/src/main.rs index 0dcf4b2..8ffd37f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,9 +12,7 @@ mod ui_commands; #[macro_use] extern crate rust_embed; use std::sync::{Arc, Mutex}; -//use std::thread; use std::process::Stdio; -use std::sync::mpsc::{channel, Receiver}; use async_trait::async_trait; use rmpv::Value; @@ -22,6 +20,7 @@ use nvim_rs::{create::tokio as create, Neovim, UiAttachOptions, Handler, compat: use tokio::process::{ChildStdin, Command}; use tokio::task::spawn_blocking; use tokio::runtime::Runtime; +use tokio::sync::mpsc::{unbounded_channel, UnboundedSender, UnboundedReceiver}; use window::ui_loop; use editor::Editor; @@ -51,14 +50,9 @@ fn create_nvim_command() -> Command { cmd } +#[derive(Clone)] struct NeovimHandler(Arc>); -impl Clone for NeovimHandler { - fn clone(&self) -> Self { - NeovimHandler(Arc::clone(&self.0)) - } -} - #[async_trait] impl Handler for NeovimHandler { type Writer = Compat; @@ -73,7 +67,7 @@ impl Handler for NeovimHandler { } } -async fn start_nvim(editor: Arc>, receiver: Arc>>) { +async fn start_nvim(editor: Arc>, mut receiver: UnboundedReceiver) { let (mut nvim, io_handler, _) = create::new_child_cmd(&mut create_nvim_command(), NeovimHandler(editor.clone())).await .unwrap_or_explained_panic("Could not create nvim process", "Could not locate or start the neovim process"); @@ -95,16 +89,21 @@ async fn start_nvim(editor: Arc>, receiver: Arc(); + let (sender, receiver) = unbounded_channel::(); let editor_clone = editor.clone(); - let receiver = Arc::new(Mutex::new(receiver)); rt.spawn(async move { start_nvim(editor_clone, receiver).await; }); diff --git a/src/ui_commands.rs b/src/ui_commands.rs index f5d6a9a..bbe824e 100644 --- a/src/ui_commands.rs +++ b/src/ui_commands.rs @@ -2,7 +2,7 @@ use nvim_rs::Neovim; use nvim_rs::compat::tokio::Compat; use tokio::process::ChildStdin; -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum UiCommand { Resize { width: i64, height: i64 }, Keyboard(String), diff --git a/src/window.rs b/src/window.rs index 6da9bbf..ca5377d 100644 --- a/src/window.rs +++ b/src/window.rs @@ -8,7 +8,7 @@ use skulpin::winit::dpi::LogicalSize; use skulpin::winit::event::{ElementState, Event, MouseScrollDelta, StartCause, WindowEvent}; use skulpin::winit::event_loop::{ControlFlow, EventLoop}; use skulpin::winit::window::{Icon, WindowBuilder}; -use std::sync::mpsc::Sender; +use tokio::sync::mpsc::UnboundedSender; use crate::editor::Editor; use crate::keybindings::construct_keybinding_string; @@ -21,7 +21,7 @@ struct Asset; const EXTRA_LIVE_FRAMES: usize = 10; -fn handle_new_grid_size(new_size: LogicalSize, renderer: &Renderer, command_channel: &mut Sender) { +fn handle_new_grid_size(new_size: LogicalSize, renderer: &Renderer, command_channel: &mut UnboundedSender) { if new_size.width > 0.0 && new_size.height > 0.0 { let new_width = ((new_size.width + 1.0) as f32 / renderer.font_width) as u64; let new_height = ((new_size.height + 1.0) as f32 / renderer.font_height) as u64; @@ -30,7 +30,7 @@ fn handle_new_grid_size(new_size: LogicalSize, renderer: &Renderer, command_chan } } -pub fn ui_loop(editor: Arc>, mut command_channel: Sender, initial_size: (u64, u64)) { +pub fn ui_loop(editor: Arc>, mut command_channel: UnboundedSender, initial_size: (u64, u64)) { let mut renderer = Renderer::new(editor.clone()); let event_loop = EventLoop::<()>::with_user_event(); @@ -41,7 +41,6 @@ pub fn ui_loop(editor: Arc>, mut command_channel: Sender>()); let icon_data = Asset::get("nvim.ico").expect("Failed to read icon data"); let icon = load_from_memory(&icon_data).expect("Failed to parse icon data"); let (width, height) = icon.dimensions();