From fda6b1c410acd29dda76c08be58d284cc1853176 Mon Sep 17 00:00:00 2001 From: Axel Kappel <69117984+Kl4rry@users.noreply.github.com> Date: Mon, 6 May 2024 19:49:59 +0200 Subject: [PATCH] fix primary clipboard --- crates/ferrite-core/src/buffer.rs | 9 +++++++++ crates/ferrite-core/src/clipboard.rs | 12 ++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/ferrite-core/src/buffer.rs b/crates/ferrite-core/src/buffer.rs index 535f94c..316abb8 100644 --- a/crates/ferrite-core/src/buffer.rs +++ b/crates/ferrite-core/src/buffer.rs @@ -749,6 +749,9 @@ impl Buffer { } pub fn insert_text(&mut self, text: &str) { + if text.is_empty() { + return; + } // TODO collect multiple words/whitespace chars into single undo step self.history.begin(self.cursor, self.dirty); @@ -1312,8 +1315,10 @@ impl Buffer { self.clicks_in_a_row += 1; if self.clicks_in_a_row == 1 { self.select_word(); + self.copy_selection_to_primary(); } else if self.clicks_in_a_row == 2 { self.select_line(); + self.copy_selection_to_primary(); } else { self.clicks_in_a_row = 0; } @@ -1363,6 +1368,10 @@ impl Buffer { self.set_cursor_pos(cursor.column, cursor.line); self.set_anchor_pos(anchor.column, anchor.line); + self.copy_selection_to_primary(); + } + + pub fn copy_selection_to_primary(&mut self) { #[cfg(target_os = "linux")] { let start = self.cursor.position.min(self.cursor.anchor); diff --git a/crates/ferrite-core/src/clipboard.rs b/crates/ferrite-core/src/clipboard.rs index 42ecfbd..4e6ce23 100644 --- a/crates/ferrite-core/src/clipboard.rs +++ b/crates/ferrite-core/src/clipboard.rs @@ -31,8 +31,9 @@ pub fn set_contents(text: impl Into) { let mut clipboard = CLIPBOARD.lock().unwrap(); if let Some(clipboard) = &mut *clipboard { - if clipboard.set_text(&text).is_ok() { - return; + match clipboard.set_text(&text) { + Ok(_) => return, + Err(err) => tracing::error!("{err}"), } } @@ -58,10 +59,13 @@ pub fn get_contents() -> String { pub fn set_primary(text: impl Into) { use arboard::{LinuxClipboardKind, SetExtLinux}; if let Some(clipboard) = CLIPBOARD.lock().unwrap().as_mut() { - let _ = clipboard + if let Err(err) = clipboard .set() .clipboard(LinuxClipboardKind::Primary) - .text(text.into()); + .text(text.into()) + { + tracing::error!("{err}"); + } } }