compiling but quits on startup

macos-click-through
Keith Simmons 3 years ago
parent b5ff3556fe
commit 0ef1521a18

@ -6,7 +6,6 @@ mod ui_commands;
use std::path::Path; use std::path::Path;
use std::process::Stdio; use std::process::Stdio;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use log::{error, info, warn}; use log::{error, info, warn};
@ -17,6 +16,7 @@ use tokio::runtime::Runtime;
use tokio::sync::mpsc::UnboundedReceiver; use tokio::sync::mpsc::UnboundedReceiver;
use crate::channel_utils::*; use crate::channel_utils::*;
use crate::running_tracker::*;
use crate::settings::*; use crate::settings::*;
use crate::{cmd_line::CmdLineSettings, error_handling::ResultPanicExplanation}; use crate::{cmd_line::CmdLineSettings, error_handling::ResultPanicExplanation};
pub use events::*; pub use events::*;
@ -158,7 +158,6 @@ async fn start_neovim_runtime(
ui_command_sender: LoggingTx<UiCommand>, ui_command_sender: LoggingTx<UiCommand>,
ui_command_receiver: UnboundedReceiver<UiCommand>, ui_command_receiver: UnboundedReceiver<UiCommand>,
redraw_event_sender: LoggingTx<RedrawEvent>, redraw_event_sender: LoggingTx<RedrawEvent>,
running: Arc<AtomicBool>,
) { ) {
let handler = NeovimHandler::new(ui_command_sender.clone(), redraw_event_sender.clone()); let handler = NeovimHandler::new(ui_command_sender.clone(), redraw_event_sender.clone());
let (nvim, io_handler) = match connection_mode() { let (nvim, io_handler) = match connection_mode() {
@ -172,7 +171,6 @@ async fn start_neovim_runtime(
std::process::exit(-1); std::process::exit(-1);
} }
let close_watcher_running = running.clone();
tokio::spawn(async move { tokio::spawn(async move {
info!("Close watcher started"); info!("Close watcher started");
match io_handler.await { match io_handler.await {
@ -184,7 +182,7 @@ async fn start_neovim_runtime(
} }
Ok(Ok(())) => {} Ok(Ok(())) => {}
}; };
close_watcher_running.store(false, Ordering::Relaxed); RUNNING_TRACKER.quit("neovim processed failed");
}); });
match nvim.command_output("echo has('nvim-0.4')").await.as_deref() { match nvim.command_output("echo has('nvim-0.4')").await.as_deref() {
@ -284,7 +282,7 @@ async fn start_neovim_runtime(
let nvim = Arc::new(nvim); let nvim = Arc::new(nvim);
start_ui_command_handler(running.clone(), ui_command_receiver, nvim.clone()); start_ui_command_handler(ui_command_receiver, nvim.clone());
SETTINGS.read_initial_values(&nvim).await; SETTINGS.read_initial_values(&nvim).await;
SETTINGS.setup_changed_listeners(&nvim).await; SETTINGS.setup_changed_listeners(&nvim).await;
} }
@ -297,14 +295,12 @@ pub fn start_bridge(
ui_command_sender: LoggingTx<UiCommand>, ui_command_sender: LoggingTx<UiCommand>,
ui_command_receiver: UnboundedReceiver<UiCommand>, ui_command_receiver: UnboundedReceiver<UiCommand>,
redraw_event_sender: LoggingTx<RedrawEvent>, redraw_event_sender: LoggingTx<RedrawEvent>,
running: Arc<AtomicBool>,
) -> Bridge { ) -> Bridge {
let runtime = Runtime::new().unwrap(); let runtime = Runtime::new().unwrap();
runtime.spawn(start_neovim_runtime( runtime.spawn(start_neovim_runtime(
ui_command_sender, ui_command_sender,
ui_command_receiver, ui_command_receiver,
redraw_event_sender, redraw_event_sender,
running,
)); ));
Bridge { _runtime: runtime } Bridge { _runtime: runtime }
} }

@ -18,6 +18,7 @@ use tokio::sync::mpsc::{
}; };
use crate::bridge::TxWrapper; use crate::bridge::TxWrapper;
use crate::running_tracker::RUNNING_TRACKER;
#[cfg(windows)] #[cfg(windows)]
use crate::windows_utils::{ use crate::windows_utils::{
register_rightclick_directory, register_rightclick_file, unregister_rightclick, register_rightclick_directory, register_rightclick_file, unregister_rightclick,
@ -198,52 +199,38 @@ impl From<ParallelCommand> for UiCommand {
} }
} }
pub fn start_ui_command_handler(running: Arc<AtomicBool>, mut ui_command_receiver: UnboundedReceiver<UiCommand>, nvim: Arc<Neovim<TxWrapper>>) { pub fn start_ui_command_handler(mut ui_command_receiver: UnboundedReceiver<UiCommand>, nvim: Arc<Neovim<TxWrapper>>) {
let serial_tx = start_serial_command_handler(running.clone(), nvim.clone()); let (serial_tx, mut serial_rx) = unbounded_channel::<SerialCommand>();
let ui_command_nvim = nvim.clone();
tokio::spawn(async move { tokio::spawn(async move {
loop { while RUNNING_TRACKER.is_running() {
if !running.load(Ordering::Relaxed) {
break;
}
match ui_command_receiver.recv().await { match ui_command_receiver.recv().await {
Some(UiCommand::Serial(serial_command)) => Some(UiCommand::Serial(serial_command)) =>
serial_tx.send(serial_command).expect("Could not send serial ui command"), serial_tx.send(serial_command).expect("Could not send serial ui command"),
Some(UiCommand::Parallel(parallel_command)) => { Some(UiCommand::Parallel(parallel_command)) => {
let nvim = nvim.clone(); let ui_command_nvim = ui_command_nvim.clone();
tokio::spawn(async move { tokio::spawn(async move {
parallel_command.execute(&nvim).await; parallel_command.execute(&ui_command_nvim).await;
}); });
} }
None => { None => {
running.store(false, Ordering::Relaxed); RUNNING_TRACKER.quit("ui command channel failed");
break;
} }
} }
} }
}); });
}
pub fn start_serial_command_handler(running: Arc<AtomicBool>, nvim: Arc<Neovim<TxWrapper>>) -> UnboundedSender<SerialCommand> { let serial_command_nvim = nvim.clone();
let (serial_tx, mut serial_rx) = unbounded_channel::<SerialCommand>();
tokio::spawn(async move { tokio::spawn(async move {
loop { while RUNNING_TRACKER.is_running() {
if !running.load(Ordering::Relaxed) {
break;
}
match serial_rx.recv().await { match serial_rx.recv().await {
Some(serial_command) => { Some(serial_command) => {
serial_command.execute(&nvim).await; serial_command.execute(&serial_command_nvim).await;
}, },
None => { None => {
running.store(false, Ordering::Relaxed); RUNNING_TRACKER.quit("serial ui command channel failed");
break;
}, },
} }
} }
}); });
serial_tx
} }

@ -21,8 +21,9 @@ mod redraw_scheduler;
mod renderer; mod renderer;
mod settings; mod settings;
mod utils; mod utils;
mod running_tracker;
mod window; mod window;
pub mod windows_utils; mod windows_utils;
#[macro_use] #[macro_use]
extern crate derive_new; extern crate derive_new;
@ -42,6 +43,8 @@ use settings::SETTINGS;
use window::{create_window, KeyboardSettings, WindowSettings}; use window::{create_window, KeyboardSettings, WindowSettings};
pub use channel_utils::*; pub use channel_utils::*;
pub use running_tracker::*;
pub use windows_utils::*;
fn main() { fn main() {
// ----------- // -----------
@ -137,8 +140,6 @@ fn main() {
CursorSettings::register(); CursorSettings::register();
KeyboardSettings::register(); KeyboardSettings::register();
let running = Arc::new(AtomicBool::new(true));
let (redraw_event_sender, redraw_event_receiver) = unbounded_channel(); let (redraw_event_sender, redraw_event_receiver) = unbounded_channel();
let logging_redraw_event_sender = let logging_redraw_event_sender =
LoggingTx::attach(redraw_event_sender, "redraw_event".to_owned()); LoggingTx::attach(redraw_event_sender, "redraw_event".to_owned());
@ -161,7 +162,6 @@ fn main() {
logging_ui_command_sender.clone(), logging_ui_command_sender.clone(),
ui_command_receiver, ui_command_receiver,
logging_redraw_event_sender, logging_redraw_event_sender,
running.clone(),
); );
start_editor( start_editor(
redraw_event_receiver, redraw_event_receiver,
@ -172,7 +172,6 @@ fn main() {
batched_draw_command_receiver, batched_draw_command_receiver,
window_command_receiver, window_command_receiver,
logging_ui_command_sender, logging_ui_command_sender,
running,
); );
} }

@ -0,0 +1,34 @@
use std::sync::{
Arc,
atomic::{
AtomicBool,
Ordering,
},
};
use log::info;
lazy_static! {
pub static ref RUNNING_TRACKER: RunningTracker = RunningTracker::new();
}
pub struct RunningTracker {
running: Arc<AtomicBool>
}
impl RunningTracker {
fn new() -> Self {
Self {
running: Arc::new(AtomicBool::new(true))
}
}
pub fn quit(&self, reason: &str) {
self.running.store(false, Ordering::Relaxed);
info!("Quit {}", reason);
}
pub fn is_running(&self) -> bool {
self.running.load(Ordering::Relaxed)
}
}

@ -3,11 +3,7 @@ mod mouse_manager;
mod renderer; mod renderer;
use std::{ use std::{
sync::{ sync::mpsc::Receiver,
atomic::{AtomicBool, Ordering},
mpsc::Receiver,
Arc,
},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
@ -33,6 +29,7 @@ use crate::{
editor::WindowCommand, editor::WindowCommand,
redraw_scheduler::REDRAW_SCHEDULER, redraw_scheduler::REDRAW_SCHEDULER,
renderer::Renderer, renderer::Renderer,
running_tracker::*,
settings::{maybe_save_window_size, SETTINGS}, settings::{maybe_save_window_size, SETTINGS},
utils::Dimensions, utils::Dimensions,
}; };
@ -99,13 +96,13 @@ impl GlutinWindowWrapper {
self.windowed_context.window().set_title(&self.title); self.windowed_context.window().set_title(&self.title);
} }
pub fn handle_quit(&mut self, running: &Arc<AtomicBool>) { pub fn handle_quit(&mut self) {
if SETTINGS.get::<CmdLineSettings>().remote_tcp.is_none() { if SETTINGS.get::<CmdLineSettings>().remote_tcp.is_none() {
self.ui_command_sender self.ui_command_sender
.send(ParallelCommand::Quit.into()) .send(ParallelCommand::Quit.into())
.expect("Could not send quit command to bridge"); .expect("Could not send quit command to bridge");
} else { } else {
running.store(false, Ordering::Relaxed); RUNNING_TRACKER.quit("window closed");
} }
} }
@ -118,7 +115,7 @@ impl GlutinWindowWrapper {
REDRAW_SCHEDULER.queue_next_frame(); REDRAW_SCHEDULER.queue_next_frame();
} }
pub fn handle_event(&mut self, event: Event<()>, running: &Arc<AtomicBool>) { pub fn handle_event(&mut self, event: Event<()>) {
self.keyboard_manager.handle_event(&event); self.keyboard_manager.handle_event(&event);
self.mouse_manager.handle_event( self.mouse_manager.handle_event(
&event, &event,
@ -128,13 +125,13 @@ impl GlutinWindowWrapper {
); );
match event { match event {
Event::LoopDestroyed => { Event::LoopDestroyed => {
self.handle_quit(running); self.handle_quit();
} }
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => { } => {
self.handle_quit(running); self.handle_quit();
} }
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::ScaleFactorChanged { scale_factor, .. }, event: WindowEvent::ScaleFactorChanged { scale_factor, .. },
@ -242,7 +239,6 @@ pub fn create_window(
batched_draw_command_receiver: Receiver<Vec<DrawCommand>>, batched_draw_command_receiver: Receiver<Vec<DrawCommand>>,
window_command_receiver: Receiver<WindowCommand>, window_command_receiver: Receiver<WindowCommand>,
ui_command_sender: LoggingTx<UiCommand>, ui_command_sender: LoggingTx<UiCommand>,
running: Arc<AtomicBool>,
) { ) {
let icon = { let icon = {
let icon = load_from_memory(ICON).expect("Failed to parse icon data"); let icon = load_from_memory(ICON).expect("Failed to parse icon data");
@ -312,7 +308,7 @@ pub fn create_window(
let mut previous_frame_start = Instant::now(); let mut previous_frame_start = Instant::now();
event_loop.run(move |e, _window_target, control_flow| { event_loop.run(move |e, _window_target, control_flow| {
if !running.load(Ordering::Relaxed) { if RUNNING_TRACKER.is_running() {
maybe_save_window_size(window_wrapper.saved_grid_size); maybe_save_window_size(window_wrapper.saved_grid_size);
std::process::exit(0); std::process::exit(0);
} }
@ -321,7 +317,7 @@ pub fn create_window(
window_wrapper.handle_window_commands(); window_wrapper.handle_window_commands();
window_wrapper.synchronize_settings(); window_wrapper.synchronize_settings();
window_wrapper.handle_event(e, &running); window_wrapper.handle_event(e);
let refresh_rate = { SETTINGS.get::<WindowSettings>().refresh_rate as f32 }; let refresh_rate = { SETTINGS.get::<WindowSettings>().refresh_rate as f32 };
let expected_frame_length_seconds = 1.0 / refresh_rate; let expected_frame_length_seconds = 1.0 / refresh_rate;

Loading…
Cancel
Save