Skip to content

Commit

Permalink
fix primary clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed May 6, 2024
1 parent 0ec1adf commit fda6b1c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions crates/ferrite-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions crates/ferrite-core/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ pub fn set_contents(text: impl Into<String>) {

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}"),
}
}

Expand All @@ -58,10 +59,13 @@ pub fn get_contents() -> String {
pub fn set_primary(text: impl Into<String>) {
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}");
}
}
}

Expand Down

0 comments on commit fda6b1c

Please sign in to comment.