Skip to content

Commit 6e011bc

Browse files
echobtfactorydroid
andauthored
fix(tui): improve clipboard handling for Linux and Windows (#493)
This fixes clipboard issues in the login screen where: - On Linux: clipboard content was dropped when the Clipboard object went out of scope before the clipboard manager received the data - On Windows: clipboard content could be released too quickly Changes: 1. login_screen.rs: Use the existing safe_clipboard_copy() function instead of directly using arboard::Clipboard. This centralizes clipboard logic. 2. terminal.rs: Improve safe_clipboard_copy() with platform-specific handling: - Linux: Already uses SetExtLinux::wait() to ensure clipboard manager receives data before the Clipboard object is dropped - Windows: Added a small delay (10ms) after set_text() to ensure the clipboard operation completes before the object is dropped - Other platforms: Standard behavior The safe_clipboard_copy function now properly handles clipboard operations across all major platforms. Co-authored-by: Droid Agent <droid@factory.ai>
1 parent b1b54bf commit 6e011bc

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

cortex-tui/src/runner/login_screen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,10 @@ impl LoginScreen {
502502
self.async_rx = None;
503503
}
504504
KeyCode::Char('c') | KeyCode::Char('C') => {
505-
// Copy URL to clipboard
505+
// Copy URL to clipboard using the safe clipboard function
506+
// This properly handles Linux (with wait()) and Windows clipboard behavior
506507
let url = self.get_direct_url();
507-
if let Ok(mut clipboard) = arboard::Clipboard::new() {
508-
let _ = clipboard.set_text(&url);
508+
if super::terminal::safe_clipboard_copy(&url) {
509509
self.copied_notification = Some(Instant::now());
510510
}
511511
}

cortex-tui/src/runner/terminal.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,8 @@ pub fn safe_clipboard_copy(text: &str) -> bool {
863863
{
864864
use arboard::SetExtLinux;
865865
// On Linux, use wait() to ensure the clipboard manager receives the data
866+
// before the Clipboard object is dropped. This is critical because X11/Wayland
867+
// clipboards require the source application to remain available.
866868
match clipboard.set().wait().text(text) {
867869
Ok(_) => true,
868870
Err(e) => {
@@ -871,7 +873,24 @@ pub fn safe_clipboard_copy(text: &str) -> bool {
871873
}
872874
}
873875
}
874-
#[cfg(not(target_os = "linux"))]
876+
#[cfg(target_os = "windows")]
877+
{
878+
// On Windows, clipboard content persists after the Clipboard object is dropped,
879+
// but we need to ensure the set operation completes successfully.
880+
// Small delay helps ensure clipboard is fully populated before returning.
881+
match clipboard.set_text(text) {
882+
Ok(_) => {
883+
// Give Windows a moment to finalize the clipboard operation
884+
std::thread::sleep(std::time::Duration::from_millis(10));
885+
true
886+
}
887+
Err(e) => {
888+
tracing::warn!("Clipboard copy failed: {}", e);
889+
false
890+
}
891+
}
892+
}
893+
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
875894
{
876895
match clipboard.set_text(text) {
877896
Ok(_) => true,

0 commit comments

Comments
 (0)