Repository: espanso/espanso (language: rust)

Issue:
Copy Paste loosing after uses 
Hi,

I use espanso with Windows 10. I loose paste function after using espanso.

To retrieve it, i need to reload config.

Thx


Discussion / hints:
Hey @jeff38100,

Thank you for reporting! Indeed I noticed this problem myself a couple of days ago. It doesn't always happen, but sometimes it does. Do you have any idea of what match could be triggering it? I wasn't able to reproduce it now

Let me know if you figure out the steps to reproduce it.

Cheers :)

Maybe related to this I got another bug.

I use "dt-" to get the actual date "2020-08-26" with espanso. 
This replacement works only the first time. The second try gives me "2dt-" instead of "2020-08-26". 
After "Reload config" it works as it should, however only at the first time. Next time the replacement shows "2dt-".
Same with ":espanso" - it gives at the second time "H:espanso" back. 

Windows 10 Pro Version 2004, 64bit.
@stdt thank you for the report. I'm not able to reproduce the issue reliably, but it does happen sometimes and it's pretty annoying. I'll make sure to investigate on this one asap. Could you please post the output of `espanso log` here?

Cheers :)

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index 08ff7f96..9f830302 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -371,7 +371,7 @@ dependencies = [
 
 [[package]]
 name = "espanso"
-version = "0.7.0"
+version = "0.7.1"
 dependencies = [
  "backtrace 0.3.37 (registry+https://github.com/rust-lang/crates.io-index)",
  "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index 10ea57fb..88e884cc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "espanso"
-version = "0.7.0"
+version = "0.7.1"
 authors = ["Federico Terzi <federicoterzi96@gmail.com>"]
 license = "GPL-3.0"
 description = "Cross-platform Text Expander written in Rust"
diff --git a/snapcraft.yaml b/snapcraft.yaml
index 11a25935..5af50174 100644
--- a/snapcraft.yaml
+++ b/snapcraft.yaml
@@ -1,5 +1,5 @@
 name: espanso
-version: 0.7.0
+version: 0.7.1
 summary: A Cross-platform Text Expander written in Rust
 description: |
   espanso is a Cross-platform, Text Expander written in Rust.
diff --git a/src/context/windows.rs b/src/context/windows.rs
index 69288509..f871f307 100644
--- a/src/context/windows.rs
+++ b/src/context/windows.rs
@@ -24,11 +24,11 @@ use crate::event::{ActionType, Event, KeyEvent, KeyModifier};
 use log::{debug, error, info};
 use std::ffi::c_void;
 use std::fs;
+use std::path::{Path, PathBuf};
 use std::sync::atomic::AtomicBool;
 use std::sync::atomic::Ordering::Acquire;
 use std::sync::mpsc::Sender;
 use std::sync::Arc;
-use std::path::{Path, PathBuf};
 use widestring::{U16CStr, U16CString};
 
 const BMP_BINARY: &[u8] = include_bytes!("../res/win/espanso.bmp");
diff --git a/src/engine.rs b/src/engine.rs
index 3e2a5341..34061815 100644
--- a/src/engine.rs
+++ b/src/engine.rs
@@ -25,7 +25,10 @@ use crate::keyboard::KeyboardManager;
 use crate::matcher::{Match, MatchReceiver};
 use crate::protocol::{send_command_or_warn, IPCCommand, Service};
 use crate::render::{RenderResult, Renderer};
-use crate::ui::{MenuItem, MenuItemType, UIManager};
+use crate::{
+    guard::InjectGuard,
+    ui::{MenuItem, MenuItemType, UIManager},
+};
 use log::{debug, error, info, warn};
 use regex::Regex;
 use std::cell::RefCell;
@@ -209,7 +212,7 @@ impl<
         }
 
         // Block espanso from reinterpreting its own actions
-        self.is_injecting.store(true, Release);
+        let _inject_guard = InjectGuard::new(self.is_injecting.clone(), &config);
 
         let char_count = if trailing_separator.is_none() {
             m.triggers[trigger_offset].chars().count() as i32
@@ -307,18 +310,6 @@ impl<
                 .set_clipboard(&previous_clipboard_content);
         }
 
-        // On macOS, because the keyinjection is async, we need to wait a bit before
-        // giving back the control. Otherwise, the injected actions will be handled back
-        // by espanso itself.
-        if cfg!(target_os = "macos") {
-            std::thread::sleep(std::time::Duration::from_millis(
-                config.mac_post_inject_delay,
-            ));
-        }
-
-        // Re-allow espanso to interpret actions
-        self.is_injecting.store(false, Release);
-
         expansion_data
     }
 }
@@ -349,6 +340,9 @@ impl<
             return;
         }
 
+        // Block espanso from reinterpreting its own actions
+        let _inject_guard = InjectGuard::new(self.is_injecting.clone(), &config);
+
         let last_expansion_data = self.last_expansion_data.borrow();
         if let Some(ref last_expansion_data) = *last_expansion_data {
             let (trigger_string, injected_text_len) = last_expansion_data;
diff --git a/src/extension/form.rs b/src/extension/form.rs
index 79133f67..c0a3361a 100644
--- a/src/extension/form.rs
+++ b/src/extension/form.rs
@@ -61,7 +61,10 @@ impl super::Extension for FormExtension {
         }
 
         if let Some(icon_path) = crate::context::get_icon_path() {
-            form_config.insert(Value::from("icon"), Value::from(icon_path.to_string_lossy().to_string()));
+            form_config.insert(
+                Value::from("icon"),
+                Value::from(icon_path.to_string_lossy().to_string()),
+            );
         }
 
         let serialized_config: String =
diff --git a/src/guard.rs b/src/guard.rs
new file mode 100644
index 00000000..2f5b1f6e
--- /dev/null
+++ b/src/guard.rs
@@ -0,0 +1,39 @@
+use crate::config::Configs;
+use log::debug;
+use std::sync::atomic::Ordering::Release;
+use std::sync::{atomic::AtomicBool, Arc};
+
+pub struct InjectGuard {
+    is_injecting: Arc<AtomicBool>,
+    mac_post_inject_delay: u64,
+}
+
+impl InjectGuard {
+    pub fn new(is_injecting: Arc<AtomicBool>, config: &Configs) -> Self {
+        debug!("enabling inject guard");
+
+        // Enable the injecting block
+        is_injecting.store(true, Release);
+
+        Self {
+            is_injecting,
+            mac_post_inject_delay: config.mac_post_inject_delay,
+        }
+    }
+}
+
+impl Drop for InjectGuard {
+    fn drop(&mut self) {
+        debug!("releasing inject guard");
+
+        // On macOS, because the keyinjection is async, we need to wait a bit before
+        // giving back the control. Otherwise, the injected actions will be handled back
+        // by espanso itself.
+        if cfg!(target_os = "macos") {
+            std::thread::sleep(std::time::Duration::from_millis(self.mac_post_inject_delay));
+        }
+
+        // Re-allow espanso to interpret actions
+        self.is_injecting.store(false, Release);
+    }
+}
diff --git a/src/main.rs b/src/main.rs
index bb9c0edf..6158d389 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -63,6 +63,7 @@ mod edit;
 mod engine;
 mod event;
 mod extension;
+mod guard;
 mod keyboard;
 mod matcher;
 mod package;
diff --git a/src/ui/modulo/mod.rs b/src/ui/modulo/mod.rs
index 008956d0..1a19d2d9 100644
--- a/src/ui/modulo/mod.rs
+++ b/src/ui/modulo/mod.rs
@@ -71,7 +71,8 @@ impl ModuloManager {
 
         if let Some(ref modulo_path) = self.modulo_path {
             let mut command = Command::new(modulo_path);
-            command.args(args)   
+            command
+                .args(args)
                 .stdin(std::process::Stdio::piped())
                 .stdout(std::process::Stdio::piped())
                 .stderr(std::process::Stdio::piped());


Test patch: