feat(config): add enable option

This commit is contained in:
Federico Terzi 2021-08-21 09:21:59 +02:00
parent ce802bc72e
commit eb727abeec
5 changed files with 29 additions and 0 deletions

View File

@ -39,8 +39,21 @@ pub trait Config: Send + Sync {
fn id(&self) -> i32; fn id(&self) -> i32;
fn label(&self) -> &str; fn label(&self) -> &str;
fn match_paths(&self) -> &[String]; fn match_paths(&self) -> &[String];
// The mechanism used to perform the injection. Espanso can either
// inject text by simulating keypresses (Inject backend) or
// by using the clipboard (Clipboard backend). Both of them have pros
// and cons, so the "Auto" backend is used by default to automatically
// choose the most appropriate one based on the situation.
// If for whatever reason the Auto backend is not appropriate, you
// can change this option to override it.
fn backend(&self) -> Backend; fn backend(&self) -> Backend;
// If false, espanso will be disabled for the current configuration.
// This option can be used to selectively disable espanso when
// using a specific application (by creating an app-specific config).
fn enable(&self) -> bool;
// Number of chars after which a match is injected with the clipboard // Number of chars after which a match is injected with the clipboard
// backend instead of the default one. This is done for efficiency // backend instead of the default one. This is done for efficiency
// reasons, as injecting a long match through separate events becomes // reasons, as injecting a long match through separate events becomes

View File

@ -28,6 +28,7 @@ pub(crate) struct ParsedConfig {
pub label: Option<String>, pub label: Option<String>,
pub backend: Option<String>, pub backend: Option<String>,
pub enable: Option<bool>,
pub clipboard_threshold: Option<usize>, pub clipboard_threshold: Option<usize>,
pub auto_restart: Option<bool>, pub auto_restart: Option<bool>,
pub preserve_clipboard: Option<bool>, pub preserve_clipboard: Option<bool>,

View File

@ -34,6 +34,9 @@ pub(crate) struct YAMLConfig {
#[serde(default)] #[serde(default)]
pub backend: Option<String>, pub backend: Option<String>,
#[serde(default)]
pub enable: Option<bool>,
#[serde(default)] #[serde(default)]
pub clipboard_threshold: Option<usize>, pub clipboard_threshold: Option<usize>,
@ -150,6 +153,7 @@ impl TryFrom<YAMLConfig> for ParsedConfig {
Ok(Self { Ok(Self {
label: yaml_config.label, label: yaml_config.label,
backend: yaml_config.backend, backend: yaml_config.backend,
enable: yaml_config.enable,
clipboard_threshold: yaml_config.clipboard_threshold, clipboard_threshold: yaml_config.clipboard_threshold,
auto_restart: yaml_config.auto_restart, auto_restart: yaml_config.auto_restart,
toggle_key: yaml_config.toggle_key, toggle_key: yaml_config.toggle_key,
@ -210,6 +214,7 @@ mod tests {
r#" r#"
label: "test" label: "test"
backend: clipboard backend: clipboard
enable: false
clipboard_threshold: 200 clipboard_threshold: 200
pre_paste_delay: 300 pre_paste_delay: 300
toggle_key: CTRL toggle_key: CTRL
@ -268,6 +273,7 @@ mod tests {
label: Some("test".to_string()), label: Some("test".to_string()),
backend: Some("clipboard".to_string()), backend: Some("clipboard".to_string()),
enable: Some(false),
clipboard_threshold: Some(200), clipboard_threshold: Some(200),
auto_restart: Some(false), auto_restart: Some(false),
preserve_clipboard: Some(false), preserve_clipboard: Some(false),

View File

@ -158,6 +158,10 @@ impl Config for ResolvedConfig {
} }
} }
} }
fn enable(&self) -> bool {
self.parsed.enable.unwrap_or(true)
}
fn clipboard_threshold(&self) -> usize { fn clipboard_threshold(&self) -> usize {
self self
@ -364,6 +368,7 @@ impl ResolvedConfig {
// Fields // Fields
label, label,
backend, backend,
enable,
clipboard_threshold, clipboard_threshold,
auto_restart, auto_restart,
pre_paste_delay, pre_paste_delay,

View File

@ -382,6 +382,10 @@ impl Config for LegacyInteropConfig {
fn secure_input_notification(&self) -> bool { fn secure_input_notification(&self) -> bool {
self.config.secure_input_notification self.config.secure_input_notification
} }
fn enable(&self) -> bool {
self.config.enable_active
}
} }
struct LegacyMatchGroup { struct LegacyMatchGroup {