Tighten passive expansion process:

- Don't abort if original clipboard is None (why should we?)
- Don't abort if original clipboard has the same text as that selected
- If original clipboard has text, restore that text before render
- If original clipboard has text, restore that text in case of aborted expansion

Note: will unfortunately remove non-text data (image) from clipboard if no text
is selected when passive expansion is triggered

Fixes #372

Fixes #365 (supersedes #368)
This commit is contained in:
Andy Kluger 2020-07-15 00:08:47 -04:00
parent 44322a74a7
commit e784b94792

View File

@ -346,11 +346,14 @@ impl<
// In order to avoid pasting previous clipboard contents, we need to check if // In order to avoid pasting previous clipboard contents, we need to check if
// a new clipboard was effectively copied. // a new clipboard was effectively copied.
// See issue: https://github.com/federico-terzi/espanso/issues/213 // See issue: https://github.com/federico-terzi/espanso/issues/213
let previous_clipboard = self.clipboard_manager.get_clipboard(); let previous_clipboard = self.clipboard_manager.get_clipboard().unwrap_or_default();
// Sleep for a while, giving time to effectively copy the text // Sleep for a while, giving time to effectively copy the text
std::thread::sleep(std::time::Duration::from_millis(100)); // TODO: avoid hardcoding std::thread::sleep(std::time::Duration::from_millis(100)); // TODO: avoid hardcoding
// Clear the clipboard, for new-content detection later
self.clipboard_manager.set_clipboard("");
// Trigger a copy shortcut to transfer the content of the selection to the clipboard // Trigger a copy shortcut to transfer the content of the selection to the clipboard
self.keyboard_manager.trigger_copy(&config); self.keyboard_manager.trigger_copy(&config);
@ -360,15 +363,13 @@ impl<
// Then get the text from the clipboard and render the match output // Then get the text from the clipboard and render the match output
let clipboard = self.clipboard_manager.get_clipboard(); let clipboard = self.clipboard_manager.get_clipboard();
// Restore original clipboard now, in case expansion doesn't happen at all
self.clipboard_manager.set_clipboard(&previous_clipboard);
if let Some(clipboard) = clipboard { if let Some(clipboard) = clipboard {
// Don't expand empty clipboards, as usually they are the result of an empty passive selection // Don't expand empty clipboards, as usually they are the result of an empty passive selection
if clipboard.trim().is_empty() { if clipboard.trim().is_empty() {
info!("Avoiding passive expansion, as the user didn't select anything"); info!("Avoiding passive expansion, as the user didn't select anything");
} else {
if let Some(previous_content) = previous_clipboard {
// Because of issue #213, we need to make sure the user selected something.
if clipboard == previous_content {
info!("Avoiding passive expansion, as the user didn't select anything");
} else { } else {
info!("Passive mode activated"); info!("Passive mode activated");
@ -381,13 +382,13 @@ impl<
std::thread::sleep(std::time::Duration::from_millis(100)); // TODO: avoid hardcoding std::thread::sleep(std::time::Duration::from_millis(100)); // TODO: avoid hardcoding
self.keyboard_manager.trigger_paste(&config); self.keyboard_manager.trigger_paste(&config);
self.clipboard_manager.set_clipboard(&previous_clipboard);
} }
_ => warn!("Cannot expand passive match"), _ => warn!("Cannot expand passive match"),
} }
} }
} }
}
}
// Re-allow espanso to interpret actions // Re-allow espanso to interpret actions
self.is_injecting.store(false, Release); self.is_injecting.store(false, Release);