feat(core): wire up alternative x11 xclip clipboard backend and create patch for gedit. Fix #882

This commit is contained in:
Federico Terzi 2021-11-21 19:40:53 +01:00
parent 42cbb6e3de
commit 08853451c0
8 changed files with 109 additions and 13 deletions

View File

@ -25,7 +25,10 @@ use espanso_config::{
};
use espanso_info::{AppInfo, AppInfoProvider};
use super::builtin::is_builtin_match;
use super::{
builtin::is_builtin_match,
engine::process::middleware::render::extension::clipboard::ClipboardOperationOptionsProvider,
};
pub struct ConfigManager<'a> {
config_store: &'a dyn ConfigStore,
@ -139,6 +142,16 @@ impl<'a> super::engine::dispatch::executor::clipboard_injector::ClipboardParamsP
disable_x11_fast_inject: active.disable_x11_fast_inject(),
restore_clipboard: active.preserve_clipboard(),
restore_clipboard_delay: active.restore_clipboard_delay(),
x11_use_xclip_backend: active.x11_use_xclip_backend(),
}
}
}
impl<'a> ClipboardOperationOptionsProvider for ConfigManager<'a> {
fn get_operation_options(&self) -> espanso_clipboard::ClipboardOperationOptions {
let active = self.active();
espanso_clipboard::ClipboardOperationOptions {
use_xclip_backend: active.x11_use_xclip_backend(),
}
}
}

View File

@ -19,7 +19,7 @@
use std::{convert::TryInto, path::PathBuf};
use espanso_clipboard::Clipboard;
use espanso_clipboard::{Clipboard, ClipboardOperationOptions};
use espanso_inject::{keys::Key, InjectionOptions, Injector};
use log::error;
@ -39,6 +39,7 @@ pub struct ClipboardParams {
pub disable_x11_fast_inject: bool,
pub restore_clipboard: bool,
pub restore_clipboard_delay: usize,
pub x11_use_xclip_backend: bool,
}
pub struct ClipboardInjectorAdapter<'a> {
@ -103,11 +104,19 @@ impl<'a> ClipboardInjectorAdapter<'a> {
Some(ClipboardRestoreGuard::lock(
self.clipboard,
params.restore_clipboard_delay.try_into().unwrap(),
self.get_operation_options(),
))
} else {
None
}
}
fn get_operation_options(&self) -> ClipboardOperationOptions {
let params = self.params_provider.get();
ClipboardOperationOptions {
use_xclip_backend: params.x11_use_xclip_backend,
}
}
}
impl<'a> TextInjector for ClipboardInjectorAdapter<'a> {
@ -118,7 +127,9 @@ impl<'a> TextInjector for ClipboardInjectorAdapter<'a> {
fn inject_text(&self, text: &str) -> anyhow::Result<()> {
let _guard = self.restore_clipboard_guard();
self.clipboard.set_text(text)?;
self
.clipboard
.set_text(text, &self.get_operation_options())?;
self.send_paste_combination()?;
@ -130,7 +141,9 @@ impl<'a> HtmlInjector for ClipboardInjectorAdapter<'a> {
fn inject_html(&self, html: &str, fallback_text: &str) -> anyhow::Result<()> {
let _guard = self.restore_clipboard_guard();
self.clipboard.set_html(html, Some(fallback_text))?;
self
.clipboard
.set_html(html, Some(fallback_text), &self.get_operation_options())?;
self.send_paste_combination()?;
@ -153,7 +166,9 @@ impl<'a> ImageInjector for ClipboardInjectorAdapter<'a> {
let _guard = self.restore_clipboard_guard();
self.clipboard.set_image(&path)?;
self
.clipboard
.set_image(&path, &self.get_operation_options())?;
self.send_paste_combination()?;
@ -165,16 +180,22 @@ struct ClipboardRestoreGuard<'a> {
clipboard: &'a dyn Clipboard,
content: Option<String>,
restore_delay: u64,
clipboard_operation_options: ClipboardOperationOptions,
}
impl<'a> ClipboardRestoreGuard<'a> {
pub fn lock(clipboard: &'a dyn Clipboard, restore_delay: u64) -> Self {
let clipboard_content = clipboard.get_text();
pub fn lock(
clipboard: &'a dyn Clipboard,
restore_delay: u64,
clipboard_operation_options: ClipboardOperationOptions,
) -> Self {
let clipboard_content = clipboard.get_text(&clipboard_operation_options);
Self {
clipboard,
content: clipboard_content,
restore_delay,
clipboard_operation_options,
}
}
}
@ -186,7 +207,10 @@ impl<'a> Drop for ClipboardRestoreGuard<'a> {
// A delay is needed to mitigate the problem
std::thread::sleep(std::time::Duration::from_millis(self.restore_delay));
if let Err(error) = self.clipboard.set_text(&content) {
if let Err(error) = self
.clipboard
.set_text(&content, &self.clipboard_operation_options)
{
error!(
"unable to restore clipboard content after expansion: {}",
error

View File

@ -181,7 +181,7 @@ pub fn initialize_and_spawn(
let clipboard = espanso_clipboard::get_clipboard(Default::default())
.expect("failed to initialize clipboard module"); // TODO: handle options
let clipboard_adapter = ClipboardAdapter::new(&*clipboard);
let clipboard_adapter = ClipboardAdapter::new(&*clipboard, &config_manager);
let clipboard_extension =
espanso_render::extension::clipboard::ClipboardExtension::new(&clipboard_adapter);
let date_extension = espanso_render::extension::date::DateExtension::new();

View File

@ -17,21 +17,36 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use espanso_clipboard::Clipboard;
use espanso_clipboard::{Clipboard, ClipboardOperationOptions};
use espanso_render::extension::clipboard::ClipboardProvider;
pub trait ClipboardOperationOptionsProvider {
fn get_operation_options(&self) -> ClipboardOperationOptions;
}
pub struct ClipboardAdapter<'a> {
clipboard: &'a dyn Clipboard,
clipboard_operation_options_provider: &'a dyn ClipboardOperationOptionsProvider,
}
impl<'a> ClipboardAdapter<'a> {
pub fn new(clipboard: &'a dyn Clipboard) -> Self {
Self { clipboard }
pub fn new(
clipboard: &'a dyn Clipboard,
clipboard_operation_options_provider: &'a dyn ClipboardOperationOptionsProvider,
) -> Self {
Self {
clipboard,
clipboard_operation_options_provider,
}
}
}
impl<'a> ClipboardProvider for ClipboardAdapter<'a> {
fn get_text(&self) -> Option<String> {
self.clipboard.get_text()
self.clipboard.get_text(
&self
.clipboard_operation_options_provider
.get_operation_options(),
)
}
}

View File

@ -42,6 +42,7 @@ fn get_builtin_patches() -> Vec<PatchDefinition> {
return vec![
patches::linux::alacritty_terminal_x11::patch(),
patches::linux::emacs_x11::patch(),
patches::linux::gedit_x11::patch(),
patches::linux::generic_terminal_x11::patch(),
patches::linux::kitty_terminal_x11::patch(),
patches::linux::konsole_terminal_x11::patch(),

View File

@ -0,0 +1,41 @@
/*
* This file is part of espanso.
*
* Copyright (C) 2019-2021 Federico Terzi
*
* espanso is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* espanso is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
use std::sync::Arc;
use crate::patch::patches::{PatchedConfig, Patches};
use crate::patch::PatchDefinition;
pub fn patch() -> PatchDefinition {
PatchDefinition {
name: module_path!().split(':').last().unwrap_or("unknown"),
is_enabled: || cfg!(target_os = "linux") && !super::util::is_wayland(),
should_patch: |app| app.class.unwrap_or_default().contains("Gedit"),
apply: |base, name| {
Arc::new(PatchedConfig::patch(
base,
name,
Patches {
x11_use_xclip_backend: Some(true),
..Default::default()
},
))
},
}
}

View File

@ -19,6 +19,7 @@
pub mod alacritty_terminal_x11;
pub mod emacs_x11;
pub mod gedit_x11;
pub mod generic_terminal_x11;
pub mod kitty_terminal_x11;
pub mod konsole_terminal_x11;

View File

@ -50,5 +50,6 @@ generate_patchable_config!(
undo_backspace -> bool,
win32_exclude_orphan_events -> bool,
win32_keyboard_layout_cache_interval -> i64,
x11_use_xclip_backend -> bool,
keyboard_layout -> Option<RMLVOConfig>
);