Skip to content

Commit 1c0f6d6

Browse files
wesbillmanCarlclaude
authored
fix(desktop): copy text through the host clipboard on macOS (#1999)
Signed-off-by: Wes <[email protected]> Co-authored-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 12a4c39 commit 1c0f6d6

37 files changed

Lines changed: 181 additions & 95 deletions

‎biome.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@
9999
],
100100
"plugins": ["./src/shared/avatar-source.grit"]
101101
},
102+
{
103+
"includes": [
104+
"src/**/*.{ts,tsx}",
105+
"!src/**/*.test.{ts,tsx}",
106+
"!src/shared/clipboard.ts"
107+
],
108+
"plugins": ["./src/shared/clipboard.grit"]
109+
},
102110
{
103111
"includes": [
104112
"tests/browser/**",

‎src-tauri/build.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ fn main() {
132132
"media_download",
133133
"file_download",
134134
"media_copy_image",
135+
"clipboard_write_text",
135136
"media_stream_base",
136137
"get_os_idle_seconds",
137138
"workspace_pick_folder",

‎src-tauri/capabilities/default.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
"allow-media-download",
8282
"allow-file-download",
8383
"allow-media-copy-image",
84+
"allow-clipboard-write-text",
8485
"allow-media-stream-base",
8586
"allow-get-os-idle-seconds",
8687
"allow-workspace-pick-folder",

‎src-tauri/capabilities/huddle.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"allow-huddle-window-watch",
88
"allow-huddle-window-action",
99
"allow-media-download",
10+
"allow-clipboard-write-text",
1011
{
1112
"identifier": "opener:allow-open-url",
1213
"allow": [

‎src-tauri/src/browser_permissions_tests.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ fn native_command_permissions_isolate_webviews() {
131131
"media_download",
132132
"file_download",
133133
"media_copy_image",
134+
"clipboard_write_text",
134135
"media_stream_base",
135136
"get_os_idle_seconds",
136137
"plugin_import_folder",
@@ -312,8 +313,8 @@ fn native_command_permissions_isolate_webviews() {
312313
);
313314
assert_eq!(
314315
invoke(huddle.as_ref(), command, local_origin).is_ok(),
315-
command == "media_download",
316-
"huddle must only acquire the bounded download command: {command}"
316+
matches!(command, "media_download" | "clipboard_write_text"),
317+
"huddle must only acquire bounded download and text copy: {command}"
317318
);
318319
assert!(invoke(huddle.as_ref(), command, "https://example.org").is_err());
319320
for origin in [local_origin, "https://example.org", "http://localhost:1430"] {

‎src-tauri/src/clipboard.rs‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//! App-lifetime clipboard ownership keeps copied content available on Linux.
2+
use std::sync::Mutex;
3+
4+
use tauri::Manager as _;
5+
6+
#[derive(Default)]
7+
pub(crate) struct Clipboard(Mutex<Option<arboard::Clipboard>>);
8+
9+
impl Clipboard {
10+
pub(crate) fn release(&self) {
11+
if let Ok(mut clipboard) = self.0.lock() {
12+
clipboard.take();
13+
}
14+
}
15+
}
16+
17+
async fn write<R: tauri::Runtime>(
18+
app: &tauri::AppHandle<R>,
19+
set: impl FnOnce(&mut arboard::Clipboard) -> Result<(), String> + Send + 'static,
20+
) -> Result<(), String> {
21+
let (send, receive) = tokio::sync::oneshot::channel();
22+
let handle = app.clone();
23+
app.run_on_main_thread(move || {
24+
let result = (|| {
25+
let state = handle.state::<Clipboard>();
26+
let mut stored = state
27+
.0
28+
.lock()
29+
.map_err(|_| "Clipboard unavailable".to_owned())?;
30+
if stored.is_none() {
31+
*stored = Some(
32+
arboard::Clipboard::new().map_err(|_| "Clipboard unavailable".to_owned())?,
33+
);
34+
}
35+
set(stored.as_mut().expect("clipboard initialized"))
36+
})();
37+
let _ = send.send(result);
38+
})
39+
.map_err(|_| "Clipboard unavailable".to_owned())?;
40+
receive
41+
.await
42+
.map_err(|_| "Clipboard unavailable".to_owned())?
43+
}
44+
45+
pub(crate) async fn write_image<R: tauri::Runtime>(
46+
app: &tauri::AppHandle<R>,
47+
width: usize,
48+
height: usize,
49+
bytes: Vec<u8>,
50+
) -> Result<(), String> {
51+
write(app, move |clipboard| {
52+
clipboard
53+
.set_image(arboard::ImageData {
54+
width,
55+
height,
56+
bytes: std::borrow::Cow::Owned(bytes),
57+
})
58+
.map_err(|_| "Could not copy image".to_owned())
59+
})
60+
.await
61+
}
62+
63+
/// WebKit rejects `navigator.clipboard` writes without transient user
64+
/// activation, and each host script evaluation (how Tauri delivers events and
65+
/// channel messages) clears it. A click that races host traffic then fails to
66+
/// copy, so macOS text copies go through the host clipboard instead.
67+
#[tauri::command]
68+
pub(crate) async fn clipboard_write_text<R: tauri::Runtime>(
69+
app: tauri::AppHandle<R>,
70+
text: String,
71+
) -> Result<(), String> {
72+
write(&app, move |clipboard| {
73+
clipboard
74+
.set_text(text)
75+
.map_err(|_| "Could not copy text".to_owned())
76+
})
77+
.await
78+
}

‎src-tauri/src/huddle_window.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ mod tests {
764764
"allow-huddle-window-watch",
765765
"allow-huddle-window-action",
766766
"allow-media-download",
767+
"allow-clipboard-write-text",
767768
{ "identifier": "opener:allow-open-url", "allow": [{ "url": "https://*" }, { "url": "http://*" }] }
768769
])
769770
);

‎src-tauri/src/image_clipboard.rs‎

Lines changed: 0 additions & 53 deletions
This file was deleted.

‎src-tauri/src/lib.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use nsec_backup::{
5252
nsec_restore_wait, NsecBackupHost,
5353
};
5454
use nsec_backup_choice::{nsec_backup_choice, nsec_backup_set_choice};
55-
mod image_clipboard;
55+
mod clipboard;
5656
mod jev;
5757
mod keep_awake;
5858
#[cfg(target_os = "macos")]
@@ -649,6 +649,7 @@ fn commands<R: tauri::Runtime>() -> impl Fn(tauri::ipc::Invoke<R>) -> bool + Sen
649649
file_download,
650650
media_download,
651651
media_copy_image,
652+
clipboard::clipboard_write_text,
652653
media_stream_base,
653654
get_os_idle_seconds,
654655
workspace_pick_folder,
@@ -966,7 +967,7 @@ pub fn run() {
966967
None => builder,
967968
};
968969
builder
969-
.manage(image_clipboard::ImageClipboard::default())
970+
.manage(clipboard::Clipboard::default())
970971
.manage(keep_awake::KeepAwake::default())
971972
.manage(identity)
972973
.manage(archive::ArchiveHost::default())
@@ -1066,7 +1067,7 @@ pub(crate) fn shut_down<R: tauri::Runtime>(app: &tauri::AppHandle<R>) {
10661067
app.state::<federated_identity::socket::Sockets>().clear();
10671068
app.state::<relay::Spools>()
10681069
.cancel_all(&app.state::<relay::Uploads>());
1069-
app.state::<image_clipboard::ImageClipboard>().release();
1070+
app.state::<clipboard::Clipboard>().release();
10701071
app.state::<HostProcesses>().shutdown();
10711072
host_command::stop_login_shell_probe();
10721073
app.state::<HostMcpServers>().stop_all();

‎src-tauri/src/relay.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1846,7 +1846,7 @@ pub(crate) async fn media_copy_image<R: tauri::Runtime>(
18461846
let pixels = tauri::async_runtime::spawn_blocking(move || clipboard_pixels(response.body()))
18471847
.await
18481848
.map_err(|_| "Could not decode image".to_owned())??;
1849-
crate::image_clipboard::write_image(&app, pixels.0, pixels.1, pixels.2).await
1849+
crate::clipboard::write_image(&app, pixels.0, pixels.1, pixels.2).await
18501850
}
18511851

18521852
/// Persist an authenticated bounded response without replacing an existing file.

0 commit comments

Comments
 (0)