feat(config): add preserve clipboard config fields

This commit is contained in:
Federico Terzi 2021-06-05 11:36:55 +02:00
parent 28d8194b4a
commit dadc71728c
5 changed files with 52 additions and 5 deletions

View File

@ -54,8 +54,20 @@ pub trait Config: Send {
// Defines the key that disables/enables espanso when double pressed
fn toggle_key(&self) -> Option<ToggleKey>;
// If true, instructs the daemon process to restart the worker (and refresh
// the configuration) after a configuration file change is detected on disk.
fn auto_restart(&self) -> bool;
// If true, espanso will attempt to preserve the previous clipboard content
// after an expansion has taken place (when using the Clipboard backend).
fn preserve_clipboard(&self) -> bool;
// The number of milliseconds to wait before restoring the previous clipboard
// content after an expansion. This is needed as without this delay, sometimes
// the target application detects the previous clipboard content instead of
// the expansion content.
fn restore_clipboard_delay(&self) -> usize;
fn is_match<'a>(&self, app: &AppProperties<'a>) -> bool;
}

View File

@ -30,8 +30,10 @@ pub(crate) struct ParsedConfig {
pub backend: Option<String>,
pub clipboard_threshold: Option<usize>,
pub auto_restart: Option<bool>,
pub preserve_clipboard: Option<bool>,
pub pre_paste_delay: Option<usize>,
pub restore_clipboard_delay: Option<usize>,
pub toggle_key: Option<String>,

View File

@ -45,6 +45,12 @@ pub(crate) struct YAMLConfig {
#[serde(default)]
pub auto_restart: Option<bool>,
#[serde(default)]
pub preserve_clipboard: Option<bool>,
#[serde(default)]
pub restore_clipboard_delay: Option<usize>,
#[serde(default)]
pub includes: Option<Vec<String>>,
@ -96,8 +102,10 @@ impl TryFrom<YAMLConfig> for ParsedConfig {
backend: yaml_config.backend,
clipboard_threshold: yaml_config.clipboard_threshold,
auto_restart: yaml_config.auto_restart,
preserve_clipboard: yaml_config.preserve_clipboard,
pre_paste_delay: yaml_config.pre_paste_delay,
restore_clipboard_delay: yaml_config.restore_clipboard_delay,
toggle_key: yaml_config.toggle_key,
@ -130,6 +138,8 @@ mod tests {
pre_paste_delay: 300
toggle_key: CTRL
auto_restart: false
preserve_clipboard: false
restore_clipboard_delay: 400
use_standard_includes: true
includes: ["test1"]
@ -154,6 +164,8 @@ mod tests {
backend: Some("clipboard".to_string()),
clipboard_threshold: Some(200),
auto_restart: Some(false),
preserve_clipboard: Some(false),
restore_clipboard_delay: Some(400),
pre_paste_delay: Some(300),

View File

@ -177,7 +177,7 @@ impl Config for ResolvedConfig {
Some("right_ctrl") => Some(ToggleKey::RightCtrl),
Some("right_alt") => Some(ToggleKey::RightAlt),
Some("right_shift") => Some(ToggleKey::RightShift),
Some("right_meta") | Some("right_cmd")=> Some(ToggleKey::RightMeta),
Some("right_meta") | Some("right_cmd") => Some(ToggleKey::RightMeta),
Some("left_ctrl") => Some(ToggleKey::LeftCtrl),
Some("left_alt") => Some(ToggleKey::LeftAlt),
Some("left_shift") => Some(ToggleKey::LeftShift),
@ -185,11 +185,22 @@ impl Config for ResolvedConfig {
Some("off") => None,
None => Some(ToggleKey::Alt),
err => {
error!("invalid toggle_key specified {:?}, falling back to ALT", err);
error!(
"invalid toggle_key specified {:?}, falling back to ALT",
err
);
Some(ToggleKey::Alt)
}
}
}
fn preserve_clipboard(&self) -> bool {
self.parsed.preserve_clipboard.unwrap_or(true)
}
fn restore_clipboard_delay(&self) -> usize {
self.parsed.restore_clipboard_delay.unwrap_or(300)
}
}
impl ResolvedConfig {
@ -250,6 +261,8 @@ impl ResolvedConfig {
clipboard_threshold,
auto_restart,
pre_paste_delay,
preserve_clipboard,
restore_clipboard_delay,
toggle_key,
includes,
excludes,

View File

@ -281,9 +281,17 @@ impl Config for LegacyInteropConfig {
model::KeyModifier::RIGHT_META => Some(crate::config::ToggleKey::RightMeta),
model::KeyModifier::LEFT_SHIFT => Some(crate::config::ToggleKey::LeftShift),
model::KeyModifier::RIGHT_SHIFT => Some(crate::config::ToggleKey::RightShift),
model::KeyModifier::CAPS_LOCK => None
model::KeyModifier::CAPS_LOCK => None,
}
}
fn preserve_clipboard(&self) -> bool {
self.config.preserve_clipboard
}
fn restore_clipboard_delay(&self) -> usize {
self.config.restore_clipboard_delay.try_into().unwrap()
}
}
struct LegacyMatchGroup {