From c63dcbdf042d8c177e6419aada637b1819549506 Mon Sep 17 00:00:00 2001 From: keith Date: Mon, 24 Aug 2020 00:40:25 -0700 Subject: [PATCH] change register command to be UiCommand rather than direct handler --- src/bridge/handler.rs | 29 +++++------------------------ src/bridge/mod.rs | 38 +++++++++++++++++++------------------- src/bridge/ui_commands.rs | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/src/bridge/handler.rs b/src/bridge/handler.rs index 11b161e..dd33a8d 100644 --- a/src/bridge/handler.rs +++ b/src/bridge/handler.rs @@ -1,18 +1,15 @@ use async_trait::async_trait; -use log::{error, trace}; +use log::trace; use nvim_rs::{compat::tokio::Compat, Handler, Neovim}; use rmpv::Value; use tokio::process::ChildStdin; use tokio::task; use super::events::handle_redraw_event_group; +use super::ui_commands::UiCommand; +use super::BRIDGE; use crate::settings::SETTINGS; -#[cfg(windows)] -use crate::settings::windows_registry::{ - register_rightclick_directory, register_rightclick_file, unregister_rightclick, -}; - #[derive(Clone)] pub struct NeovimHandler(); @@ -35,26 +32,10 @@ impl Handler for NeovimHandler { SETTINGS.handle_changed_notification(arguments); } "neovide.register_right_click" => { - #[cfg(windows)] - { - if unregister_rightclick() { - error!("Setup of Windows Registry failed during unregister. Try running as Admin?"); - } - if !register_rightclick_directory() { - error!("Setup of Windows Registry failed during directory registration. Try running as Admin?"); - } - if !register_rightclick_file() { - error!("Setup of Windows Registry failed during file registration. Try running as Admin?"); - } - } + BRIDGE.queue_command(UiCommand::RegisterRightClick); } "neovide.unregister_right_click" => { - #[cfg(windows)] - { - if !unregister_rightclick() { - error!("Removal of Windows Registry failed, probably no Admin"); - } - } + BRIDGE.queue_command(UiCommand::UnregisterRightClick); } _ => {} }) diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index 44d111e..40a0fac 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -80,6 +80,25 @@ fn build_nvim_cmd() -> Command { } } +pub fn build_neovide_command(channel: u64, num_args: u64, command: &str, event: &str) -> String { + let nargs: String = if num_args > 1 { + "+".to_string() + } else { + num_args.to_string() + }; + if num_args == 0 { + return format!( + "command! -nargs={} -complete=expression {} call rpcnotify({}, 'neovide.{}')", + nargs, command, channel, event + ); + } else { + return format!( + "command! -nargs={} -complete=expression {} call rpcnotify({}, 'neovide.{}', )", + nargs, command, channel, event + ); + }; +} + pub fn create_nvim_command() -> Command { let mut cmd = build_nvim_cmd(); @@ -105,25 +124,6 @@ async fn drain(receiver: &mut UnboundedReceiver) -> Option String { - let nargs: String = if num_args > 1 { - "+".to_string() - } else { - num_args.to_string() - }; - if num_args == 0 { - return format!( - "command! -nargs={} -complete=expression {} call rpcnotify({}, 'neovide.{}')", - nargs, command, channel, event - ); - } else { - return format!( - "command! -nargs={} -complete=expression {} call rpcnotify({}, 'neovide.{}', )", - nargs, command, channel, event - ); - }; -} - async fn start_process(mut receiver: UnboundedReceiver) { let (width, height) = window_geometry_or_default(); let (mut nvim, io_handler, _) = diff --git a/src/bridge/ui_commands.rs b/src/bridge/ui_commands.rs index 07139ee..2c30c1d 100644 --- a/src/bridge/ui_commands.rs +++ b/src/bridge/ui_commands.rs @@ -1,9 +1,13 @@ -use log::trace; +use log::{error, trace}; use nvim_rs::compat::tokio::Compat; use nvim_rs::Neovim; use tokio::process::ChildStdin; use crate::editor::EDITOR; +#[cfg(windows)] +use crate::settings::windows_registry::{ + register_rightclick_directory, register_rightclick_file, unregister_rightclick, +}; #[derive(Debug, Clone)] pub enum UiCommand { @@ -25,6 +29,10 @@ pub enum UiCommand { FocusLost, FocusGained, Quit, + #[cfg(windows)] + RegisterRightClick, + #[cfg(windows)] + UnregisterRightClick, } impl UiCommand { @@ -79,6 +87,32 @@ impl UiCommand { UiCommand::FileDrop(path) => { nvim.command(format!("e {}", path).as_str()).await.ok(); } + #[cfg(windows)] + UiCommand::RegisterRightClick => { + if unregister_rightclick() { + let msg = "Could not unregister previous menu item. Possibly already registered or not running as Admin?"; + nvim.err_writeln(msg).await.ok(); + error!("{}", msg); + } + if !register_rightclick_directory() { + let msg = "Could not register directory context menu item. Possibly already registered or not running as Admin?"; + nvim.err_writeln(msg).await.ok(); + error!("{}", msg); + } + if !register_rightclick_file() { + let msg = "Could not register file context menu item. Possibly already registered or not running as Admin?"; + nvim.err_writeln(msg).await.ok(); + error!("{}", msg); + } + } + #[cfg(windows)] + UiCommand::UnregisterRightClick => { + if !unregister_rightclick() { + let msg = "Could not remove context menu items. Possibly already removed or not running as Admin?"; + nvim.err_writeln(msg).await.ok(); + error!("{}", msg); + } + } } }