diff --git a/src/config/mod.rs b/src/config/mod.rs index 64463d4..c849527 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -53,6 +53,7 @@ fn default_use_system_agent() -> bool { true } fn default_config_caching_interval() -> i32 { 800 } fn default_word_separators() -> Vec { vec![' ', ',', '.', '\r', '\n', 22u8 as char] } fn default_toggle_interval() -> u32 { 230 } +fn default_preserve_clipboard() -> bool {false} fn default_backspace_limit() -> i32 { 3 } fn default_exclude_default_matches() -> bool {false} fn default_matches() -> Vec { Vec::new() } @@ -98,6 +99,9 @@ pub struct Configs { #[serde(default = "default_toggle_interval")] pub toggle_interval: u32, + #[serde(default = "default_preserve_clipboard")] + pub preserve_clipboard: bool, + #[serde(default)] pub paste_shortcut: PasteShortcut, @@ -145,6 +149,7 @@ impl Configs { validate_field!(result, self.backspace_limit, default_backspace_limit()); validate_field!(result, self.ipc_server_port, default_ipc_server_port()); validate_field!(result, self.use_system_agent, default_use_system_agent()); + validate_field!(result, self.preserve_clipboard, default_preserve_clipboard()); result } diff --git a/src/engine.rs b/src/engine.rs index 151835d..8f49a5c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -95,6 +95,19 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa menu } + + fn return_content_if_preserve_clipboard_is_enabled(&self) -> Option { + // If the preserve_clipboard option is enabled, first save the current + // clipboard content in order to restore it later. + if self.config_manager.default_config().preserve_clipboard { + match self.clipboard_manager.get_clipboard() { + Some(clipboard) => {Some(clipboard)}, + None => {None}, + } + }else { + None + } + } } lazy_static! { @@ -119,6 +132,8 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa self.keyboard_manager.delete_string(char_count); + let mut previous_clipboard_content : Option = None; + // Manage the different types of matches match &m.content { // Text Match @@ -205,10 +220,12 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa } }, BackendType::Clipboard => { - let previous_clipboard_content = self.clipboard_manager.get_clipboard().unwrap_or(String::from("")); + // If the preserve_clipboard option is enabled, save the current + // clipboard content to restore it later. + previous_clipboard_content = self.return_content_if_preserve_clipboard_is_enabled(); + self.clipboard_manager.set_clipboard(&target_string); self.keyboard_manager.trigger_paste(&config.paste_shortcut); - self.clipboard_manager.set_clipboard(&previous_clipboard_content); }, } @@ -222,6 +239,10 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa MatchContentType::Image(content) => { // Make sure the image exist beforehand if content.path.exists() { + // If the preserve_clipboard option is enabled, save the current + // clipboard content to restore it later. + previous_clipboard_content = self.return_content_if_preserve_clipboard_is_enabled(); + self.clipboard_manager.set_clipboard_image(&content.path); self.keyboard_manager.trigger_paste(&config.paste_shortcut); }else{ @@ -229,6 +250,11 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa } }, } + + // Restore previous clipboard content + if let Some(previous_clipboard_content) = previous_clipboard_content { + self.clipboard_manager.set_clipboard(&previous_clipboard_content); + } } fn on_enable_update(&self, status: bool) {