Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Use cross-platform solution for copying to clipboard
Ilya9187bbfb1a05
diff --git a/Cargo.lock b/Cargo.lockindex 295301b02..0197902dc 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -151,6 +151,24 @@ dependencies = ["derive_arbitrary",]+[[package]]+name = "arboard"+version = "3.4.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "df099ccb16cd014ff054ac1bf392c67feeef57164b05c42f037cd40f5d4357f4"+dependencies = [+ "clipboard-win",+ "core-graphics 0.23.2",+ "image 0.25.2",+ "log",+ "objc2",+ "objc2-app-kit",+ "objc2-foundation",+ "parking_lot",+ "windows-sys 0.48.0",+ "x11rb",+]+[[package]]name = "arg_enum_proc_macro"version = "0.3.4"@@ -1078,6 +1096,15 @@ version = "0.4.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "4bfbf56724aa9eca8afa4fcfadeb479e722935bb2a0900c2d37e0cc477af0688"+[[package]]+name = "clipboard-win"+version = "5.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "15efe7a882b08f34e38556b14f2fb3daa98769d06c7f0c1b076dfd0d983bc892"+dependencies = [+ "error-code",+]+[[package]]name = "cocoa"version = "0.20.2"@@ -1716,6 +1743,7 @@ name = "desktop"version = "0.3.0-beta.9.2"dependencies = ["anyhow",+ "arboard","axum","base64 0.22.1","cap-editor",@@ -2065,6 +2093,12 @@ dependencies = ["windows-sys 0.52.0",]+[[package]]+name = "error-code"+version = "3.3.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "a5d9305ccc6942a704f4335694ecd3de2ea531b114ac2d51f5f843750787a92f"+[[package]]name = "event-listener"version = "5.3.1"@@ -2525,6 +2559,16 @@ dependencies = ["version_check",]+[[package]]+name = "gethostname"+version = "0.4.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0176e0459c2e4a1fe232f984bca6890e681076abb9934f6cea7c326f3fc47818"+dependencies = [+ "libc",+ "windows-targets 0.48.5",+]+[[package]]name = "gethostname"version = "0.5.0"@@ -6830,7 +6874,7 @@ version = "2.0.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "fbc5f23a86f37687c7f4fecfdc706b279087bc44f7a46702f7307ff1551ee03a"dependencies = [- "gethostname",+ "gethostname 0.5.0","log","os_info","serde",@@ -8675,6 +8719,23 @@ dependencies = ["pkg-config",]+[[package]]+name = "x11rb"+version = "0.13.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5d91ffca73ee7f68ce055750bf9f6eca0780b8c85eff9bc046a3b0da41755e12"+dependencies = [+ "gethostname 0.4.3",+ "rustix",+ "x11rb-protocol",+]++[[package]]+name = "x11rb-protocol"+version = "0.13.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ec107c4503ea0b4a98ef47356329af139c0a4f7750e621cf2973cd3385ebcb3d"+[[package]]name = "xattr"version = "1.3.1"diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.tomlindex ab0bb32c0..f735e5183 100644--- a/apps/desktop/src-tauri/Cargo.toml+++ b/apps/desktop/src-tauri/Cargo.toml@@ -66,6 +66,7 @@ rand = "0.8.5"cpal.workspace = truekeyed_priority_queue = "0.4.2"sentry.workspace = true+arboard = "3.4.1"cap-utils = { path = "../../../crates/utils" }cap-project = { path = "../../../crates/project" }diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rsindex 70446b91c..ed7f6bf44 100644--- a/apps/desktop/src-tauri/src/lib.rs+++ b/apps/desktop/src-tauri/src/lib.rs@@ -16,6 +16,7 @@ mod upload;mod web_api;mod windows;+use arboard::Clipboard;use audio::AppSounds;use auth::{AuthStore, AuthenticationInvalid};use cap_editor::{EditorInstance, FRAMES_WS_PATH};@@ -650,7 +651,7 @@ async fn copy_screenshot_to_clipboard(app: AppHandle, path: PathBuf) -> Result<(println!("Copying screenshot to clipboard: {:?}", path);- let image_data = match tokio::fs::read(&path).await {+ let image = match image::open(&path) {Ok(data) => data,Err(e) => {println!("Failed to read screenshot file: {}", e);@@ -661,58 +662,27 @@ async fn copy_screenshot_to_clipboard(app: AppHandle, path: PathBuf) -> Result<(return Err(format!("Failed to read screenshot file: {}", e));}};-- #[cfg(target_os = "macos")]- {- use cocoa::appkit::{NSImage, NSPasteboard};- use cocoa::base::{id, nil};- use cocoa::foundation::{NSArray, NSData};- use objc::rc::autoreleasepool;-- let result = unsafe {- autoreleasepool(|| {- let pasteboard: id = NSPasteboard::generalPasteboard(nil);- NSPasteboard::clearContents(pasteboard);-- let ns_data = NSData::dataWithBytes_length_(- nil,- image_data.as_ptr() as *const std::os::raw::c_void,- image_data.len() as u64,- );-- let image = NSImage::initWithData_(NSImage::alloc(nil), ns_data);- if image != nil {- NSPasteboard::writeObjects(pasteboard, NSArray::arrayWithObject(nil, image));- Ok(())- } else {- Err("Failed to create NSImage from data".to_string())- }- })- };-- if let Err(e) = result {+ let data = match image.as_rgba8() {+ Some(data) => data,+ None => {+ println!("Failed to load image as 8bit RGBA");notifications::send_notification(&app,notifications::NotificationType::ScreenshotCopyFailed,);- return Err(e);+ return Err("Failed to load image as 8bit RGBA".into());}+ };- notifications::send_notification(- &app,- notifications::NotificationType::ScreenshotCopiedToClipboard,- );- }-- // TODO(Ilya) (Windows) Add support- #[cfg(not(target_os = "macos"))]- {- notifications::send_notification(- &app,- notifications::NotificationType::ScreenshotCopyFailed,- );- return Err("Clipboard operations are only supported on macOS".to_string());- }+ let _ = app+ .state::<MutableState<'_, Clipboard>>()+ .write()+ .await+ .set_image(arboard::ImageData {+ width: image.width() as usize,+ height: image.height() as usize,+ bytes: std::borrow::Cow::Borrowed(data),+ });Ok(())}@@ -836,66 +806,16 @@ async fn create_editor_instance(#[specta::specta]async fn copy_video_to_clipboard(app: AppHandle, path: String) -> Result<(), String> {println!("copying");-- #[cfg(target_os = "macos")]- {- use cocoa::appkit::NSPasteboard;- use cocoa::base::{id, nil};- use cocoa::foundation::{NSArray, NSString, NSURL};- use objc::rc::autoreleasepool;-- let result: Result<(), String> = unsafe {- autoreleasepool(|| {- let pasteboard: id = NSPasteboard::generalPasteboard(nil);- NSPasteboard::clearContents(pasteboard);-- let url_str = NSString::alloc(nil).init_str(&path);- let url = NSURL::fileURLWithPath_(nil, url_str);-- if url == nil {- return Err("Failed to create NSURL".to_string());- }-- let objects = NSArray::arrayWithObject(nil, url);- if objects == nil {- return Err("Failed to create NSArray".to_string());- }-- #[cfg(target_arch = "x86_64")]- {- let write_result: i8 = NSPasteboard::writeObjects(pasteboard, objects);- if write_result == 0 {- return Err("Failed to write to pasteboard".to_string());- }- }-- #[cfg(target_arch = "aarch64")]- {- let write_result: bool = NSPasteboard::writeObjects(pasteboard, objects);- if !write_result {- return Err("Failed to write to pasteboard".to_string());- }- }-- Ok(())- })- };-- if let Err(e) = result {- println!("Failed to copy to clipboard: {}", e);- notifications::send_notification(- &app,- notifications::NotificationType::VideoCopyFailed,- );- return Err(e);- }- }+ let _ = app+ .state::<MutableState<'_, Clipboard>>()+ .write()+ .await+ .set_text(path);notifications::send_notification(&app,notifications::NotificationType::VideoCopiedToClipboard,);-Ok(())}@@ -1340,11 +1260,13 @@ async fn upload_rendered_video(meta.save_for_project();RecordingMetaChanged { id: video_id }.emit(&app).ok();- NotificationType::ShareableLinkCopied.send(&app);-- #[cfg(target_os = "macos")]- platform::write_string_to_pasteboard(&uploaded_video.link);+ let _ = app+ .state::<MutableState<'_, Clipboard>>()+ .write()+ .await+ .set_text(uploaded_video.link.clone());+ NotificationType::ShareableLinkCopied.send(&app);Ok(UploadResult::Success(uploaded_video.link))}Err(e) => {@@ -1428,8 +1350,11 @@ async fn upload_screenshot(println!("Copying to clipboard: {:?}", share_link);- #[cfg(target_os = "macos")]- platform::write_string_to_pasteboard(&share_link);+ let _ = app+ .state::<MutableState<'_, Clipboard>>()+ .write()+ .await+ .set_text(share_link.clone());// Send notification after successful upload and clipboard copynotifications::send_notification(&app, notifications::NotificationType::ShareableLinkCopied);@@ -2066,6 +1991,10 @@ pub async fn run() {app.manage(FakeWindowBounds(Arc::new(RwLock::new(HashMap::new()))));+ app.manage(Arc::new(RwLock::new(+ Clipboard::new().expect("Failed to create clipboard context"),+ )));+tray::create_tray(&app_handle).unwrap();let app_handle_clone = app_handle.clone();@@ -2442,8 +2371,11 @@ async fn reupload_rendered_video(notifications::NotificationType::ShareableLinkCopied,);- #[cfg(target_os = "macos")]- platform::write_string_to_pasteboard(&uploaded_video.link);+ let _ = app+ .state::<MutableState<'_, Clipboard>>()+ .write()+ .await+ .set_text(uploaded_video.link);Ok(UploadResult::Success(sharing.link)) // Use existing sharing link}diff --git a/apps/desktop/src-tauri/src/platform/macos/mod.rs b/apps/desktop/src-tauri/src/platform/macos/mod.rsindex 8934d9c85..28d368c31 100644--- a/apps/desktop/src-tauri/src/platform/macos/mod.rs+++ b/apps/desktop/src-tauri/src/platform/macos/mod.rs@@ -53,23 +53,6 @@ extern "C" {) -> boolean_t;}-pub fn write_string_to_pasteboard(string: &str) {- use cocoa::appkit::NSPasteboard;- use cocoa::base::{id, nil};- use cocoa::foundation::{NSArray, NSString};- use objc::rc::autoreleasepool;-- unsafe {- autoreleasepool(|| {- let pasteboard: id = NSPasteboard::generalPasteboard(nil);- NSPasteboard::clearContents(pasteboard);- let ns_string = NSString::alloc(nil).init_str(string);- let objects: id = NSArray::arrayWithObject(nil, ns_string);- NSPasteboard::writeObjects(pasteboard, objects);- });- }-}-/// Makes the background of the WKWebView layer transparent./// This differs from Tauri's implementation as it does not change the window background which causes performance performance issues and artifacts when shadows are enabled on the window./// Use Tauri's implementation to make the window itself transparent.diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rsindex 004ff2d26..6ae1c94ec 100644--- a/apps/desktop/src-tauri/src/recording.rs+++ b/apps/desktop/src-tauri/src/recording.rs@@ -275,8 +275,11 @@ pub async fn stop_recording(app: AppHandle, state: MutableState<'_, App>) -> Resif auth.is_upgraded() && settings.auto_create_shareable_link {if let Some(pre_created_video) = state.pre_created_video.take() {// Copy link to clipboard- #[cfg(target_os = "macos")]- platform::write_string_to_pasteboard(&pre_created_video.link);+ let _ = app+ .state::<MutableState<'_, arboard::Clipboard>>()+ .write()+ .await+ .set_text(pre_created_video.link.clone());// Send notification for shareable linknotifications::send_notification(