Add configurable Paste shortcuts. Helps to fix #130

This commit is contained in:
Federico Terzi 2019-11-29 22:09:02 +01:00
parent 615d60b17c
commit 182d44580a
6 changed files with 73 additions and 15 deletions

View File

@ -26,6 +26,7 @@ use std::fs::{File, create_dir_all};
use std::io::Read; use std::io::Read;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::event::KeyModifier; use crate::event::KeyModifier;
use crate::keyboard::PasteShortcut;
use std::collections::{HashSet, HashMap}; use std::collections::{HashSet, HashMap};
use log::{error}; use log::{error};
use std::fmt; use std::fmt;
@ -97,6 +98,9 @@ pub struct Configs {
#[serde(default = "default_toggle_interval")] #[serde(default = "default_toggle_interval")]
pub toggle_interval: u32, pub toggle_interval: u32,
#[serde(default)]
pub paste_shortcut: PasteShortcut,
#[serde(default = "default_backspace_limit")] #[serde(default = "default_backspace_limit")]
pub backspace_limit: i32, pub backspace_limit: i32,

View File

@ -206,7 +206,7 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
}, },
BackendType::Clipboard => { BackendType::Clipboard => {
self.clipboard_manager.set_clipboard(&target_string); self.clipboard_manager.set_clipboard(&target_string);
self.keyboard_manager.trigger_paste(); self.keyboard_manager.trigger_paste(&config.paste_shortcut);
}, },
} }
@ -221,7 +221,7 @@ impl <'a, S: KeyboardManager, C: ClipboardManager, M: ConfigManager<'a>, U: UIMa
// Make sure the image exist beforehand // Make sure the image exist beforehand
if content.path.exists() { if content.path.exists() {
self.clipboard_manager.set_clipboard_image(&content.path); self.clipboard_manager.set_clipboard_image(&content.path);
self.keyboard_manager.trigger_paste(); self.keyboard_manager.trigger_paste(&config.paste_shortcut);
}else{ }else{
error!("Image not found in path: {:?}", content.path); error!("Image not found in path: {:?}", content.path);
} }

View File

@ -19,6 +19,8 @@
use std::ffi::CString; use std::ffi::CString;
use crate::bridge::linux::*; use crate::bridge::linux::*;
use super::PasteShortcut;
use log::error;
pub struct LinuxKeyboardManager { pub struct LinuxKeyboardManager {
} }
@ -36,8 +38,10 @@ impl super::KeyboardManager for LinuxKeyboardManager {
// On linux this is not needed, so NOOP // On linux this is not needed, so NOOP
} }
fn trigger_paste(&self) { fn trigger_paste(&self, shortcut: &PasteShortcut) {
unsafe { unsafe {
match shortcut {
PasteShortcut::Default => {
let is_terminal = is_current_window_terminal(); let is_terminal = is_current_window_terminal();
// Terminals use a different keyboard combination to paste from clipboard, // Terminals use a different keyboard combination to paste from clipboard,
@ -47,6 +51,17 @@ impl super::KeyboardManager for LinuxKeyboardManager {
}else{ }else{
trigger_terminal_paste(); trigger_terminal_paste();
} }
},
PasteShortcut::CtrlV => {
trigger_paste();
},
PasteShortcut::CtrlShiftV => {
trigger_terminal_paste();
}
_ => {
error!("Linux backend does not support this Paste Shortcut, please open an issue on GitHub if you need it.")
}
}
} }
} }

View File

@ -19,6 +19,8 @@
use std::ffi::CString; use std::ffi::CString;
use crate::bridge::macos::*; use crate::bridge::macos::*;
use super::PasteShortcut;
use log::error;
pub struct MacKeyboardManager { pub struct MacKeyboardManager {
} }
@ -39,10 +41,19 @@ impl super::KeyboardManager for MacKeyboardManager {
} }
} }
fn trigger_paste(&self) { fn trigger_paste(&self, shortcut: &PasteShortcut) {
unsafe {
match shortcut {
PasteShortcut::Default => {
unsafe { unsafe {
trigger_paste(); trigger_paste();
} }
},
_ => {
error!("MacOS backend does not support this Paste Shortcut, please open an issue on GitHub if you need it.")
}
}
}
} }
fn delete_string(&self, count: i32) { fn delete_string(&self, count: i32) {

View File

@ -17,6 +17,8 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>. * along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/ */
use serde::{Serialize, Deserialize, Deserializer};
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
mod windows; mod windows;
@ -29,11 +31,26 @@ mod macos;
pub trait KeyboardManager { pub trait KeyboardManager {
fn send_string(&self, s: &str); fn send_string(&self, s: &str);
fn send_enter(&self); fn send_enter(&self);
fn trigger_paste(&self); fn trigger_paste(&self, shortcut: &PasteShortcut);
fn delete_string(&self, count: i32); fn delete_string(&self, count: i32);
fn move_cursor_left(&self, count: i32); fn move_cursor_left(&self, count: i32);
} }
#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum PasteShortcut {
Default, // Default one for the current system
CtrlV, // Classic Ctrl+V shortcut
CtrlShiftV, // Could be used to paste without formatting in many applications
ShiftInsert, // Often used in Linux systems
MetaV, // Corresponding to Win+V on Windows and Linux, CMD+V on macOS
}
impl Default for PasteShortcut{
fn default() -> Self {
PasteShortcut::Default
}
}
// WINDOWS IMPLEMENTATION // WINDOWS IMPLEMENTATION
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub fn get_manager() -> impl KeyboardManager { pub fn get_manager() -> impl KeyboardManager {

View File

@ -19,6 +19,8 @@
use widestring::{U16CString}; use widestring::{U16CString};
use crate::bridge::windows::*; use crate::bridge::windows::*;
use super::PasteShortcut;
use log::error;
pub struct WindowsKeyboardManager { pub struct WindowsKeyboardManager {
} }
@ -44,10 +46,19 @@ impl super::KeyboardManager for WindowsKeyboardManager {
} }
} }
fn trigger_paste(&self) { fn trigger_paste(&self, shortcut: &PasteShortcut) {
unsafe {
match shortcut {
PasteShortcut::Default => {
unsafe { unsafe {
trigger_paste(); trigger_paste();
} }
},
_ => {
error!("Windows backend does not support this Paste Shortcut, please open an issue on GitHub if you need it.")
}
}
}
} }
fn delete_string(&self, count: i32) { fn delete_string(&self, count: i32) {