@steerable/agent-shell-tauri 0.12.0 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steerable/agent-shell-tauri",
3
- "version": "0.12.0",
3
+ "version": "0.12.2",
4
4
  "description": "Reusable Tauri Rust host source for @steerable/agent-shell",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://steerableframework.com/",
package/src/clipboard.rs CHANGED
@@ -82,9 +82,16 @@ pub fn read_clipboard_text() -> String {
82
82
  }
83
83
 
84
84
  fn clipboard_image() -> Option<ClipboardFile> {
85
- let mut clipboard = arboard::Clipboard::new().ok()?;
86
- let image = clipboard.get_image().ok()?;
87
- let png = png_from_rgba(image.width, image.height, image.bytes.as_ref())?;
85
+ clipboard_image_from(pasteboard_png(), encoded_png)
86
+ }
87
+
88
+ /// Builds the pasted image from the pasteboard PNG when there is one, and only
89
+ /// falls back to decoding when the pasteboard holds another format.
90
+ fn clipboard_image_from(
91
+ pasteboard_png: Option<Vec<u8>>,
92
+ encoded: impl FnOnce() -> Option<Vec<u8>>,
93
+ ) -> Option<ClipboardFile> {
94
+ let png = pasteboard_png.or_else(encoded)?;
88
95
  Some(ClipboardFile {
89
96
  name: "image.png".to_string(),
90
97
  path: None,
@@ -93,6 +100,38 @@ fn clipboard_image() -> Option<ClipboardFile> {
93
100
  })
94
101
  }
95
102
 
103
+ /// Pasteboard images that are already PNG.
104
+ ///
105
+ /// Screenshot tools publish `public.png`, so the bytes are copied as they are.
106
+ /// Decoding to RGBA and re-encoding cost ~1s per 2560x1440 screenshot, which
107
+ /// ran inside the Cmd+V menu callback and dropped the next paste.
108
+ #[cfg(target_os = "macos")]
109
+ fn pasteboard_png() -> Option<Vec<u8>> {
110
+ use objc2_app_kit::NSPasteboard;
111
+ use objc2_foundation::ns_string;
112
+
113
+ let pasteboard = NSPasteboard::generalPasteboard();
114
+ let data = pasteboard.dataForType(ns_string!("public.png"))?;
115
+ let bytes = data.to_vec();
116
+ if bytes.is_empty() || bytes.len() > MAX_CLIPBOARD_IMAGE_BYTES {
117
+ return None;
118
+ }
119
+ Some(bytes)
120
+ }
121
+
122
+ #[cfg(not(target_os = "macos"))]
123
+ fn pasteboard_png() -> Option<Vec<u8>> {
124
+ None
125
+ }
126
+
127
+ /// Formats that need decoding first. Only reached when the pasteboard holds
128
+ /// no PNG, for example a TIFF-only image copy from an older application.
129
+ fn encoded_png() -> Option<Vec<u8>> {
130
+ let mut clipboard = arboard::Clipboard::new().ok()?;
131
+ let image = clipboard.get_image().ok()?;
132
+ png_from_rgba(image.width, image.height, image.bytes.as_ref())
133
+ }
134
+
96
135
  pub fn png_from_rgba(width: usize, height: usize, bytes: &[u8]) -> Option<Vec<u8>> {
97
136
  let width = u32::try_from(width).ok()?;
98
137
  let height = u32::try_from(height).ok()?;
@@ -203,4 +242,38 @@ mod tests {
203
242
  assert!(png_from_rgba(2, 2, &[0, 0, 0, 0]).is_none());
204
243
  assert!(png_from_rgba(usize::MAX, usize::MAX, &[]).is_none());
205
244
  }
245
+
246
+ /// A pasteboard PNG is passed through, so a screenshot never pays for a
247
+ /// decode plus re-encode inside the Cmd+V callback.
248
+ #[test]
249
+ fn a_pasteboard_png_skips_the_decode_and_re_encode() {
250
+ let calls = std::cell::Cell::new(0);
251
+ let file = clipboard_image_from(Some(vec![0x89, b'P', b'N', b'G']), || {
252
+ calls.set(calls.get() + 1);
253
+ None
254
+ })
255
+ .expect("a PNG pasteboard yields one file");
256
+ assert_eq!(calls.get(), 0);
257
+ assert_eq!(file.mime.as_deref(), Some("image/png"));
258
+ assert_eq!(
259
+ STANDARD
260
+ .decode(file.data_base64.expect("bytes are attached"))
261
+ .expect("base64 decodes"),
262
+ vec![0x89, b'P', b'N', b'G']
263
+ );
264
+ }
265
+
266
+ /// A TIFF-only copy still reaches the composer through the decoder.
267
+ #[test]
268
+ fn an_image_without_a_pasteboard_png_is_encoded() {
269
+ let file = clipboard_image_from(None, || Some(vec![0x89, b'P', b'N', b'G']))
270
+ .expect("the encoded image yields one file");
271
+ assert_eq!(file.mime.as_deref(), Some("image/png"));
272
+ }
273
+
274
+ /// Nothing on the pasteboard yields no file rather than an empty one.
275
+ #[test]
276
+ fn an_empty_pasteboard_yields_no_image() {
277
+ assert!(clipboard_image_from(None, || None).is_none());
278
+ }
206
279
  }
package/src/commands.rs CHANGED
@@ -109,14 +109,22 @@ pub async fn host_save_text_file(
109
109
  })
110
110
  }
111
111
 
112
+ /// Rich pasteboard read for the composer.
113
+ ///
114
+ /// Runs on the async thread pool, not the main thread: a screenshot read used
115
+ /// to take about a second there and swallow the next Cmd+V.
112
116
  #[tauri::command]
113
- pub fn host_read_clipboard() -> ClipboardContents {
114
- clipboard::read_clipboard()
117
+ pub async fn host_read_clipboard() -> Result<ClipboardContents, String> {
118
+ tauri::async_runtime::spawn_blocking(clipboard::read_clipboard)
119
+ .await
120
+ .map_err(|error| error.to_string())
115
121
  }
116
122
 
117
123
  #[tauri::command]
118
- pub fn host_read_clipboard_text() -> String {
119
- clipboard::read_clipboard_text()
124
+ pub async fn host_read_clipboard_text() -> String {
125
+ tauri::async_runtime::spawn_blocking(clipboard::read_clipboard_text)
126
+ .await
127
+ .unwrap_or_default()
120
128
  }
121
129
 
122
130
  #[tauri::command]
package/src/lib.rs CHANGED
@@ -140,7 +140,13 @@ fn install_menu(app: &tauri::App, data_dir_name: &str) -> tauri::Result<()> {
140
140
  "view-zoom-out" => zoom::zoom_out(app),
141
141
  "view-zoom-reset" => zoom::zoom_reset(app),
142
142
  "edit-paste" => {
143
- let _ = app.emit("menu:paste", clipboard::read_clipboard());
143
+ // The pasteboard read must not run on the main thread: encoding a
144
+ // screenshot takes about a second and any Cmd+V pressed in that
145
+ // window is lost.
146
+ let paste_app = app.clone();
147
+ tauri::async_runtime::spawn_blocking(move || {
148
+ let _ = paste_app.emit("menu:paste", clipboard::read_clipboard());
149
+ });
144
150
  }
145
151
  "app-quit" => app.exit(0),
146
152
  _ => {}