Merge pull request #373 from AndydeCleyre/bugfix/372

Tighten passive expansion process
This commit is contained in:
Federico Terzi 2020-08-13 19:18:18 +02:00 committed by GitHub
commit a2a73efe74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 19 deletions

View File

@ -95,6 +95,9 @@ fn default_passive_arg_delimiter() -> char {
fn default_passive_arg_escape() -> char {
'\\'
}
fn default_passive_delay() -> u64 {
100
}
fn default_passive_key() -> KeyModifier {
KeyModifier::OFF
}
@ -215,6 +218,9 @@ pub struct Configs {
#[serde(default = "default_passive_key")]
pub passive_key: KeyModifier,
#[serde(default = "default_passive_delay")]
pub passive_delay: u64,
#[serde(default = "default_enable_passive")]
pub enable_passive: bool,

View File

@ -388,16 +388,22 @@ impl<
// In order to avoid pasting previous clipboard contents, we need to check if
// a new clipboard was effectively copied.
// 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
std::thread::sleep(std::time::Duration::from_millis(100)); // TODO: avoid hardcoding
std::thread::sleep(std::time::Duration::from_millis(config.passive_delay));
// Clear the clipboard, for new-content detection later
self.clipboard_manager.set_clipboard("");
// Sleep for a while, giving time to effectively copy the text
std::thread::sleep(std::time::Duration::from_millis(config.passive_delay));
// Trigger a copy shortcut to transfer the content of the selection to the clipboard
self.keyboard_manager.trigger_copy(&config);
// 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(config.passive_delay));
// Then get the text from the clipboard and render the match output
let clipboard = self.clipboard_manager.get_clipboard();
@ -407,30 +413,31 @@ impl<
if clipboard.trim().is_empty() {
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 {
info!("Passive mode activated");
info!("Passive mode activated");
let rendered = self.renderer.render_passive(&clipboard, &config);
// Restore original clipboard in case it's used during render
self.clipboard_manager.set_clipboard(&previous_clipboard);
match rendered {
RenderResult::Text(payload) => {
// Paste back the result in the field
self.clipboard_manager.set_clipboard(&payload);
let rendered = self.renderer.render_passive(&clipboard, &config);
std::thread::sleep(std::time::Duration::from_millis(100)); // TODO: avoid hardcoding
self.keyboard_manager.trigger_paste(&config);
}
_ => warn!("Cannot expand passive match"),
}
match rendered {
RenderResult::Text(payload) => {
// Paste back the result in the field
self.clipboard_manager.set_clipboard(&payload);
std::thread::sleep(std::time::Duration::from_millis(config.passive_delay));
self.keyboard_manager.trigger_paste(&config);
}
_ => warn!("Cannot expand passive match"),
}
}
}
std::thread::sleep(std::time::Duration::from_millis(config.passive_delay));
// Restore original clipboard
self.clipboard_manager.set_clipboard(&previous_clipboard);
// Re-allow espanso to interpret actions
self.is_injecting.store(false, Release);
}