diff --git a/Cargo.lock b/Cargo.lock index d46dd6d..825affd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1476,6 +1476,7 @@ dependencies = [ "euclid", "font-kit", "image", + "lazy_static", "lru", "msgbox", "nvim-rs", diff --git a/Cargo.toml b/Cargo.toml index 4917eeb..57bd8b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ image = "0.22.3" nvim-rs = { git = "https://github.com/KillTheMule/nvim-rs", branch = "futures", features = [ "use_tokio" ] } tokio = { version = "0.2.9", features = [ "blocking", "process", "time" ] } async-trait = "0.1.18" +lazy_static = "1.4.0" [build-dependencies] winres = "0.1.11" diff --git a/src/bridge/handler.rs b/src/bridge/handler.rs index eb51270..0f3def0 100644 --- a/src/bridge/handler.rs +++ b/src/bridge/handler.rs @@ -6,11 +6,11 @@ use async_trait::async_trait; use tokio::process::ChildStdin; use crate::error_handling::ResultPanicExplanation; -use crate::editor::Editor; +use crate::editor::{EDITOR, Editor}; use super::events::parse_neovim_event; #[derive(Clone)] -pub struct NeovimHandler(pub Arc>); +pub struct NeovimHandler { } #[async_trait] impl Handler for NeovimHandler { @@ -20,7 +20,7 @@ impl Handler for NeovimHandler { let parsed_events = parse_neovim_event(event_name, arguments) .unwrap_or_explained_panic("Could not parse event", "Could not parse event from neovim"); for event in parsed_events { - let mut editor = self.0.lock().unwrap(); + let mut editor = EDITOR.lock().unwrap(); editor.handle_redraw_event(event); } } diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index 2922bd9..c85f597 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -15,10 +15,15 @@ use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; pub use events::*; pub use keybindings::*; pub use ui_commands::UiCommand; -use crate::editor::Editor; +use crate::editor::{EDITOR, Editor}; use crate::error_handling::ResultPanicExplanation; +use crate::INITIAL_DIMENSIONS; use handler::NeovimHandler; +lazy_static! { + pub static ref BRIDGE: Bridge = Bridge::new(); +} + #[cfg(target_os = "windows")] fn set_windows_creation_flags(cmd: &mut Command) { cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW @@ -49,9 +54,9 @@ async fn drain(receiver: &mut UnboundedReceiver) -> Option>, mut receiver: UnboundedReceiver, grid_dimensions: (u64, u64)) { - let (width, height) = grid_dimensions; - let (mut nvim, io_handler, _) = create::new_child_cmd(&mut create_nvim_command(), NeovimHandler(editor)).await +async fn start_process(mut receiver: UnboundedReceiver) { + let (width, height) = INITIAL_DIMENSIONS; + let (mut nvim, io_handler, _) = create::new_child_cmd(&mut create_nvim_command(), NeovimHandler { }).await .unwrap_or_explained_panic("Could not create nvim process", "Could not locate or start the neovim process"); tokio::spawn(async move { @@ -96,19 +101,22 @@ pub struct Bridge { } impl Bridge { - pub fn new(editor: Arc>, grid_dimensions: (u64, u64)) -> Bridge { + pub fn new() -> Bridge { let runtime = Runtime::new().unwrap(); let (sender, receiver) = unbounded_channel::(); runtime.spawn(async move { - start_process(editor, receiver, grid_dimensions).await; + start_process(receiver).await; }); + println!("Bridge created."); + Bridge { _runtime: runtime, sender } } - pub fn queue_command(&mut self, command: UiCommand) { - self.sender.send(command) + pub fn queue_command(&self, command: UiCommand) { + let mut sender = self.sender.clone(); + sender.send(command) .unwrap_or_explained_panic( "Could Not Send UI Command", "Could not send UI command from the window system to the neovim process."); diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 0ec8d80..87ef674 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -2,7 +2,7 @@ mod cursor; mod style; use std::collections::HashMap; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use skulpin::skia_safe::colors; use skulpin::winit::window::Window; @@ -10,6 +10,12 @@ use skulpin::winit::window::Window; pub use cursor::{Cursor, CursorShape, CursorMode}; pub use style::{Colors, Style}; use crate::bridge::{GridLineCell, GuiOption, RedrawEvent}; +use crate::redraw_scheduler::REDRAW_SCHEDULER; +use crate::INITIAL_DIMENSIONS; + +lazy_static! { + pub static ref EDITOR: Arc> = Arc::new(Mutex::new(Editor::new())); +} pub type GridCell = Option<(char, Option