Add preserve_clipboard mode

This commit is contained in:
Federico Terzi 2019-12-13 23:17:53 +01:00
parent fc532d1a9c
commit 47f2cc1661
2 changed files with 33 additions and 2 deletions

View File

@ -53,6 +53,7 @@ fn default_use_system_agent() -> bool { true }
fn default_config_caching_interval() -> i32 { 800 } fn default_config_caching_interval() -> i32 { 800 }
fn default_word_separators() -> Vec<char> { vec![' ', ',', '.', '\r', '\n', 22u8 as char] } fn default_word_separators() -> Vec<char> { vec![' ', ',', '.', '\r', '\n', 22u8 as char] }
fn default_toggle_interval() -> u32 { 230 } fn default_toggle_interval() -> u32 { 230 }
fn default_preserve_clipboard() -> bool {false}
fn default_backspace_limit() -> i32 { 3 } fn default_backspace_limit() -> i32 { 3 }
fn default_exclude_default_matches() -> bool {false} fn default_exclude_default_matches() -> bool {false}
fn default_matches() -> Vec<Match> { Vec::new() } fn default_matches() -> Vec<Match> { Vec::new() }
@ -98,6 +99,9 @@ pub struct Configs {
#[serde(default = "default_toggle_interval")] #[serde(default = "default_toggle_interval")]
pub toggle_interval: u32, pub toggle_interval: u32,
#[serde(default = "default_preserve_clipboard")]
pub preserve_clipboard: bool,
#[serde(default)] #[serde(default)]
pub paste_shortcut: PasteShortcut, pub paste_shortcut: PasteShortcut,
@ -145,6 +149,7 @@ impl Configs {
validate_field!(result, self.backspace_limit, default_backspace_limit()); validate_field!(result, self.backspace_limit, default_backspace_limit());
validate_field!(result, self.ipc_server_port, default_ipc_server_port()); 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.use_system_agent, default_use_system_agent());
validate_field!(result, self.preserve_clipboard, default_preserve_clipboard());
result result
} }

View File

@ -95,6 +95,19 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
menu menu
} }
fn return_content_if_preserve_clipboard_is_enabled(&self) -> Option<String> {
// 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! { lazy_static! {
@ -119,6 +132,8 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
self.keyboard_manager.delete_string(char_count); self.keyboard_manager.delete_string(char_count);
let mut previous_clipboard_content : Option<String> = None;
// Manage the different types of matches // Manage the different types of matches
match &m.content { match &m.content {
// Text Match // Text Match
@ -205,10 +220,12 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
} }
}, },
BackendType::Clipboard => { 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.clipboard_manager.set_clipboard(&target_string);
self.keyboard_manager.trigger_paste(&config.paste_shortcut); 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) => { MatchContentType::Image(content) => {
// Make sure the image exist beforehand // Make sure the image exist beforehand
if content.path.exists() { 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.clipboard_manager.set_clipboard_image(&content.path);
self.keyboard_manager.trigger_paste(&config.paste_shortcut); self.keyboard_manager.trigger_paste(&config.paste_shortcut);
}else{ }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) { fn on_enable_update(&self, status: bool) {