Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(desktop-gpui): read and decode the screenshot clipboard image off the UI thread

Khushal Bhardwaj1bef48db8702
diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs
index 61619c069..3a65dfc8a 100644
--- a/apps/desktop-gpui/src/app_windows.rs
+++ b/apps/desktop-gpui/src/app_windows.rs
@@ -6549,10 +6549,14 @@ pub fn screenshot_finished(captured: Option<PathBuf>, cx: &mut App) {
.ok();
let behaviour = crate::store::GeneralSettings::load().post_screenshot_behaviour;
- if behaviour.copies()
- && let Err(error) = platform::copy_image_to_clipboard(&png, cx)
- {
- tracing::error!("copying the screenshot to the clipboard failed: {error}");
+ if behaviour.copies() {
+ let copied = platform::copy_image_to_clipboard(png.clone(), cx);
+ cx.spawn(async move |_| {
+ if let Err(error) = copied.await {
+ tracing::error!("copying the screenshot to the clipboard failed: {error}");
+ }
+ })
+ .detach();
}
if behaviour.opens_editor() {
diff --git a/apps/desktop-gpui/src/main_window.rs b/apps/desktop-gpui/src/main_window.rs
index cabac8bdd..e075602c3 100644
--- a/apps/desktop-gpui/src/main_window.rs
+++ b/apps/desktop-gpui/src/main_window.rs
@@ -4712,13 +4712,14 @@ impl MainWindow {
self.library_action(("lib-ss-copy", index), "icons/copy.svg", {
let png = png.clone();
move |_, _, cx| {
- if let Err(error) = crate::platform::copy_image_to_clipboard(&png, cx) {
- tracing::warn!("copying screenshot failed: {error}");
- cx.spawn(async move |_| {
+ let copied = crate::platform::copy_image_to_clipboard(png.clone(), cx);
+ cx.spawn(async move |_| {
+ if let Err(error) = copied.await {
+ tracing::warn!("copying screenshot failed: {error}");
crate::platform::alert_dialog("Could not copy screenshot", &error);
- })
- .detach();
- }
+ }
+ })
+ .detach();
}
}),
)
diff --git a/apps/desktop-gpui/src/platform.rs b/apps/desktop-gpui/src/platform.rs
index 6189355ea..e71377936 100644
--- a/apps/desktop-gpui/src/platform.rs
+++ b/apps/desktop-gpui/src/platform.rs
@@ -558,10 +558,21 @@ fn clipboard_file_path(path: &std::path::Path) -> Result<std::path::PathBuf, Str
Ok(path)
}
-pub fn copy_image_to_clipboard(path: &std::path::Path, cx: &gpui::App) -> Result<(), String> {
- let bytes = std::fs::read(path)
- .map_err(|error| format!("Could not read the clipboard image: {error}"))?;
- copy_image_bytes_to_clipboard(&bytes, cx)
+pub fn copy_image_to_clipboard(
+ path: std::path::PathBuf,
+ cx: &gpui::App,
+) -> gpui::Task<Result<(), String>> {
+ cx.spawn(async move |cx| {
+ let image = cx
+ .background_executor()
+ .spawn(async move {
+ let bytes = std::fs::read(&path)
+ .map_err(|error| format!("Could not read the clipboard image: {error}"))?;
+ prepare_clipboard_image(bytes)
+ })
+ .await?;
+ cx.update(|cx| write_clipboard_image(image, cx))
+ })
}
#[cfg(any(not(target_os = "macos"), test))]
@@ -2102,7 +2113,13 @@ mod mac {
})
}
- pub fn copy_image_bytes_to_clipboard(bytes: &[u8], _cx: &gpui::App) -> Result<(), String> {
+ pub struct ClipboardImage(Vec<u8>);
+
+ pub fn prepare_clipboard_image(bytes: Vec<u8>) -> Result<ClipboardImage, String> {
+ Ok(ClipboardImage(bytes))
+ }
+
+ pub fn write_clipboard_image(image: ClipboardImage, _cx: &gpui::App) -> Result<(), String> {
use objc2::rc::autoreleasepool;
use objc2::runtime::Bool;
use objc2::{class, msg_send};
@@ -2113,7 +2130,7 @@ mod mac {
if pasteboard.is_null() {
return Err("Clipboard unavailable".into());
}
- let data = NSData::with_bytes(bytes);
+ let data = NSData::with_bytes(&image.0);
let image: *mut AnyObject = msg_send![class!(NSImage), alloc];
let image: *mut AnyObject = msg_send![image, initWithData: &*data];
let image = Id::from_raw(image).ok_or("Failed to load image")?;
@@ -2820,8 +2837,10 @@ mod stub {
.map_err(|error| format!("Could not copy the file to the clipboard: {error}"))
}
- pub fn copy_image_bytes_to_clipboard(bytes: &[u8], cx: &gpui::App) -> Result<(), String> {
- let format = match image::guess_format(bytes).map_err(|error| error.to_string())? {
+ pub struct ClipboardImage(gpui::ClipboardItem);
+
+ pub fn prepare_clipboard_image(bytes: Vec<u8>) -> Result<ClipboardImage, String> {
+ let format = match image::guess_format(&bytes).map_err(|error| error.to_string())? {
image::ImageFormat::Png => gpui::ImageFormat::Png,
image::ImageFormat::Jpeg => gpui::ImageFormat::Jpeg,
image::ImageFormat::WebP => gpui::ImageFormat::Webp,
@@ -2830,8 +2849,14 @@ mod stub {
image::ImageFormat::Tiff => gpui::ImageFormat::Tiff,
_ => return Err("Unsupported clipboard image format".into()),
};
- let image = gpui::Image::from_bytes(format, bytes.to_vec());
- cx.try_write_to_clipboard(gpui::ClipboardItem::new_image(&image))
+ let image = gpui::Image::from_bytes(format, bytes);
+ Ok(ClipboardImage(gpui::ClipboardItem {
+ entries: vec![gpui::ClipboardEntry::Image(image)],
+ }))
+ }
+
+ pub fn write_clipboard_image(image: ClipboardImage, cx: &gpui::App) -> Result<(), String> {
+ cx.try_write_to_clipboard(image.0)
.map_err(|error| format!("Could not copy the image to the clipboard: {error}"))
}
pub fn desktop_picture_path() -> Option<std::path::PathBuf> {
diff --git a/apps/desktop-gpui/src/platform/windows.rs b/apps/desktop-gpui/src/platform/windows.rs
index 60ef53662..ae2e823b8 100644
--- a/apps/desktop-gpui/src/platform/windows.rs
+++ b/apps/desktop-gpui/src/platform/windows.rs
@@ -647,15 +647,23 @@ pub fn copy_file_to_clipboard(path: &Path, _cx: &gpui::App) -> Result<(), String
.map_err(|error| format!("Could not copy the file to the clipboard: {error}"))
}
-pub fn copy_image_bytes_to_clipboard(bytes: &[u8], _cx: &gpui::App) -> Result<(), String> {
+pub struct ClipboardImage(clipboard_rs::RustImageData);
+
+pub fn prepare_clipboard_image(bytes: Vec<u8>) -> Result<ClipboardImage, String> {
+ use clipboard_rs::RustImageData;
use clipboard_rs::common::RustImage;
- use clipboard_rs::{Clipboard, ClipboardContext, RustImageData};
- let image = RustImageData::from_bytes(bytes)
- .map_err(|error| format!("Could not load the clipboard image: {error}"))?;
+ RustImageData::from_bytes(&bytes)
+ .map(ClipboardImage)
+ .map_err(|error| format!("Could not load the clipboard image: {error}"))
+}
+
+pub fn write_clipboard_image(image: ClipboardImage, _cx: &gpui::App) -> Result<(), String> {
+ use clipboard_rs::{Clipboard, ClipboardContext};
+
ClipboardContext::new()
.map_err(|error| format!("Clipboard unavailable: {error}"))?
- .set_image(image)
+ .set_image(image.0)
.map_err(|error| format!("Could not copy the image to the clipboard: {error}"))
}
diff --git a/apps/desktop-gpui/src/screenshot_editor.rs b/apps/desktop-gpui/src/screenshot_editor.rs
index 8e123c378..acac013d4 100644
--- a/apps/desktop-gpui/src/screenshot_editor.rs
+++ b/apps/desktop-gpui/src/screenshot_editor.rs
@@ -1982,8 +1982,14 @@ impl ScreenshotEditorWindow {
match result {
Ok(bytes) => match destination {
ExportDestination::Clipboard => {
+ let image = cx
+ .background_executor()
+ .spawn(async move { crate::platform::prepare_clipboard_image(bytes) })
+ .await;
this.update_in(cx, |this, window, cx| {
- match crate::platform::copy_image_bytes_to_clipboard(&bytes, cx) {
+ match image
+ .and_then(|image| crate::platform::write_clipboard_image(image, cx))
+ {
Ok(()) => {
this.toast_success(
"Screenshot copied to clipboard!",
diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs
index 52d2a2fd3..ef9a410b5 100644
--- a/apps/desktop-gpui/src/settings_window.rs
+++ b/apps/desktop-gpui/src/settings_window.rs
@@ -2375,13 +2375,14 @@ impl SettingsWindow {
{
let png = png.clone();
move |_this, _window, cx| {
- if let Err(error) = crate::platform::copy_image_to_clipboard(&png, cx) {
- tracing::warn!("copying screenshot failed: {error}");
- cx.spawn(async move |_, _| {
+ let copied = crate::platform::copy_image_to_clipboard(png.clone(), cx);
+ cx.spawn(async move |_, _| {
+ if let Err(error) = copied.await {
+ tracing::warn!("copying screenshot failed: {error}");
crate::platform::alert_dialog("Could not copy screenshot", &error);
- })
- .detach();
- }
+ }
+ })
+ .detach();
}
},
))