use std::ffi::{c_char, c_void}; use std::ptr; use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; use std::time::Instant; use color_eyre::eyre::{Result, eyre}; use objc2::rc::Retained; use objc2::runtime::ProtocolObject; use objc2_app_kit::{NSPasteboard, NSPasteboardItem, NSPasteboardTypeString, NSPasteboardWriting}; use objc2_foundation::{NSArray, NSData, NSString}; use crate::keyboard; use crate::suppression::InputActivity; pub struct Paster { clipboard: Retained, activity: InputActivity, continuation: Option, clipboard_restore: Arc>, prepared_clipboard: Option, } #[derive(Clone, Copy)] pub enum PasteMode { Continue, Send, Standalone, } struct PreparedClipboard { change_count: isize, snapshot: ClipboardSnapshot, capture_ms: u128, } struct Continuation { revision: u64, inserted: String, } #[derive(Default)] struct ClipboardRestore { generation: u64, original: Option, last_change_count: Option, } impl ClipboardRestore { fn register( &mut self, previous: ClipboardSnapshot, previous_change_count: isize, inserted_change_count: isize, ) -> u64 { self.generation = self.generation.wrapping_add(1); if self.last_change_count != Some(previous_change_count) { self.original = Some(previous); } self.last_change_count = Some(inserted_change_count); self.generation } } #[derive(Clone, Debug, Eq, PartialEq)] struct ClipboardSnapshot { items: Vec>, } #[derive(Clone, Debug, Eq, PartialEq)] struct ClipboardFlavor { data_type: String, data: Vec, flags: u32, } type PasteboardRef = *const c_void; type PasteboardItemId = *mut c_void; type CfTypeRef = *const c_void; type CfStringRef = *const c_void; type CfArrayRef = *const c_void; type CfDataRef = *const c_void; const CF_STRING_ENCODING_UTF8: u32 = 0x0800_0100; const BAD_PASTEBOARD_FLAVOR: i32 = -25133; const DUPLICATE_PASTEBOARD_FLAVOR: i32 = -25134; const PASTE_SETTLE_DELAY: Duration = Duration::from_millis(100); // Chrome 155 and 156 on macOS can drop a ⌘V posted right after a pasteboard // write (#136, fixed upstream by Chromium CL 8503629, expected in Chrome 157). const CHROMIUM_PRE_PASTE_DELAY: Duration = Duration::from_millis(150); // Matched case-insensitively at a dot boundary, so release channels // (`com.google.Chrome.beta`) and installed web apps (`com.google.Chrome.app.…`) // share their browser's delay. const CHROMIUM_BUNDLE_FAMILIES: &[&str] = &[ "com.google.Chrome", "com.brave.Browser", "company.thebrowser.Browser", "company.thebrowser.dia", "com.microsoft.edgemac", "com.vivaldi.Vivaldi", "com.operasoftware.Opera", "com.operasoftware.OperaGX", "org.chromium.Chromium", ]; const SYSTEM_TRANSLATED_FLAVOR: u32 = 1 << 8; #[link(name = "ApplicationServices", kind = "framework")] unsafe extern "C" { fn PasteboardCreate(name: CfStringRef, pasteboard: *mut PasteboardRef) -> i32; fn PasteboardSynchronize(pasteboard: PasteboardRef) -> u32; fn PasteboardClear(pasteboard: PasteboardRef) -> i32; fn PasteboardGetItemCount(pasteboard: PasteboardRef, count: *mut usize) -> i32; fn PasteboardGetItemIdentifier( pasteboard: PasteboardRef, index: isize, item: *mut PasteboardItemId, ) -> i32; fn PasteboardCopyItemFlavors( pasteboard: PasteboardRef, item: PasteboardItemId, flavors: *mut CfArrayRef, ) -> i32; fn PasteboardGetItemFlavorFlags( pasteboard: PasteboardRef, item: PasteboardItemId, flavor: CfStringRef, flags: *mut u32, ) -> i32; fn PasteboardCopyItemFlavorData( pasteboard: PasteboardRef, item: PasteboardItemId, flavor: CfStringRef, data: *mut CfDataRef, ) -> i32; fn PasteboardPutItemFlavor( pasteboard: PasteboardRef, item: PasteboardItemId, flavor: CfStringRef, data: CfDataRef, flags: u32, ) -> i32; } #[link(name = "CoreFoundation", kind = "framework")] unsafe extern "C" { fn CFArrayGetCount(array: CfArrayRef) -> isize; fn CFArrayGetValueAtIndex(array: CfArrayRef, index: isize) -> *const c_void; fn CFDataCreate(allocator: *const c_void, bytes: *const u8, length: isize) -> CfDataRef; fn CFDataGetBytePtr(data: CfDataRef) -> *const u8; fn CFDataGetLength(data: CfDataRef) -> isize; fn CFStringGetLength(value: CfStringRef) -> isize; fn CFStringGetMaximumSizeForEncoding(length: isize, encoding: u32) -> isize; fn CFStringGetCString( value: CfStringRef, buffer: *mut c_char, buffer_size: isize, encoding: u32, ) -> bool; fn CFStringCreateWithBytes( allocator: *const c_void, bytes: *const u8, length: isize, encoding: u32, is_external_representation: u8, ) -> CfStringRef; fn CFRelease(value: CfTypeRef); } struct PasteboardHandle(PasteboardRef); impl Drop for PasteboardHandle { fn drop(&mut self) { unsafe { CFRelease(self.0) }; } } impl Paster { pub fn new(activity: InputActivity) -> Self { Self { clipboard: NSPasteboard::generalPasteboard(), activity, continuation: None, clipboard_restore: Arc::new(Mutex::new(ClipboardRestore::default())), prepared_clipboard: None, } } pub fn prepare(&mut self) { let change_count = self.clipboard.changeCount(); if self .prepared_clipboard .as_ref() .is_some_and(|prepared| prepared.change_count == change_count) { return; } self.prepared_clipboard = None; let _restore = self .clipboard_restore .lock() .unwrap_or_else(|error| error.into_inner()); let started = Instant::now(); match capture_clipboard(&self.clipboard) { Ok(snapshot) => { self.prepared_clipboard = Some(PreparedClipboard { change_count, snapshot, capture_ms: started.elapsed().as_millis(), }); } Err(error) => tracing::debug!(%error, "eager clipboard capture failed"), } } /// Commit only after clipboard preparation, immediately before the first write. /// A rejected commit leaves both the clipboard and continuation unchanged. pub fn paste( &mut self, text: &str, mode: PasteMode, commit: impl FnOnce() -> bool, ) -> Result<()> { if matches!(mode, PasteMode::Send) { send_after_paste(|| self.paste_text(text, mode, commit), keyboard::post_enter)?; self.continuation = None; Ok(()) } else { self.paste_text(text, mode, commit) } } fn paste_text( &mut self, text: &str, mode: PasteMode, commit: impl FnOnce() -> bool, ) -> Result<()> { let revision = self.activity.revision(); let text = self .continuation .as_ref() .filter(|continuation| { !matches!(mode, PasteMode::Standalone) && continuation.revision == revision }) .map_or_else( || text.to_string(), |continuation| join(&continuation.inserted, text), ); let generation = commit_prepared_paste( || { let restore = self .clipboard_restore .lock() .unwrap_or_else(|error| error.into_inner()); let previous_change_count = self.clipboard.changeCount(); let prepared = self.prepared_clipboard.take(); let previous = if let Some(prepared) = prepared && prepared.change_count == previous_change_count { tracing::debug!( clipboard_capture_ms = prepared.capture_ms, "used eagerly captured clipboard" ); prepared.snapshot } else { let started = Instant::now(); let snapshot = capture_clipboard(&self.clipboard)?; tracing::debug!( clipboard_capture_ms = started.elapsed().as_millis(), "captured clipboard at paste time" ); snapshot }; if self.clipboard.changeCount() != previous_change_count { return Err(eyre!("clipboard changed while HEX was preserving it")); } Ok((restore, previous, previous_change_count)) }, commit, |(mut restore, previous, previous_change_count)| { if matches!(mode, PasteMode::Standalone) { self.continuation = None; } if let Err(error) = write_clipboard_text(&self.clipboard, &text) { if let Err(restore_error) = restore_clipboard(&self.clipboard, &previous) { tracing::error!(%restore_error, "could not recover the clipboard after a failed write"); } return Err(error); } let inserted_change_count = self.clipboard.changeCount(); Ok(restore.register(previous, previous_change_count, inserted_change_count)) }, )?; let delay = pre_paste_delay(crate::context::frontmost_bundle_identifier().as_deref()); if !delay.is_zero() { tracing::debug!( delay_ms = delay.as_millis(), "delaying paste for a Chromium browser" ); } let clipboard_restore = self.clipboard_restore.clone(); complete_paste( delay, || keyboard::post_command('v'), move || { thread::spawn(move || { thread::sleep(Duration::from_millis(500)); let mut restore = clipboard_restore .lock() .unwrap_or_else(|error| error.into_inner()); if restore.generation != generation { return; } let clipboard = NSPasteboard::generalPasteboard(); if restore.last_change_count != Some(clipboard.changeCount()) { restore.original = None; restore.last_change_count = None; return; } let Some(previous) = restore.original.take() else { return; }; let result = restore_clipboard(&clipboard, &previous); restore.last_change_count = None; if let Err(error) = result { tracing::warn!(%error, "could not restore clipboard after paste"); } }); }, thread::sleep, )?; self.continuation = (!matches!(mode, PasteMode::Standalone)).then_some(Continuation { revision, inserted: text, }); Ok(()) } } pub(crate) fn commit_prepared_paste( prepare: impl FnOnce() -> Result

, commit: impl FnOnce() -> bool, paste: impl FnOnce(P) -> Result, ) -> Result { let prepared = prepare()?; // Waiting for restoration and materializing promised clipboard data remain cancellable. // Once committed, even a failed first write may have changed the clipboard. if !commit() { return Err(eyre!("paste was cancelled")); } paste(prepared) } fn pre_paste_delay(bundle_id: Option<&str>) -> Duration { if bundle_id.is_some_and(is_chromium_browser) { CHROMIUM_PRE_PASTE_DELAY } else { Duration::ZERO } } fn is_chromium_browser(bundle_id: &str) -> bool { CHROMIUM_BUNDLE_FAMILIES.iter().any(|family| { bundle_id .get(..family.len()) .is_some_and(|prefix| prefix.eq_ignore_ascii_case(family)) && matches!(bundle_id.as_bytes().get(family.len()), None | Some(b'.')) }) } fn complete_paste( pre_paste_delay: Duration, post_paste: impl FnOnce() -> Result<()>, schedule_restore: impl FnOnce(), mut wait: impl FnMut(Duration), ) -> Result<()> { if !pre_paste_delay.is_zero() { wait(pre_paste_delay); } let posted = post_paste(); schedule_restore(); posted?; // Keep the clipboard stable for the target's 100 ms consumption window. // This is not an acknowledgment; longer OS or application stalls can still lose a paste. wait(PASTE_SETTLE_DELAY); Ok(()) } fn send_after_paste( paste: impl FnOnce() -> Result<()>, post_enter: impl FnOnce() -> Result<()>, ) -> Result<()> { paste()?; post_enter() } fn capture_clipboard(clipboard: &NSPasteboard) -> Result { let capture_started = Instant::now(); let pasteboard = create_pasteboard(&clipboard.name())?; unsafe { PasteboardSynchronize(pasteboard.0) }; let mut item_count = 0; check_status( unsafe { PasteboardGetItemCount(pasteboard.0, &mut item_count) }, "count clipboard items", )?; let mut items = Vec::with_capacity(item_count); for index in 1..=item_count { let mut item = ptr::null_mut(); check_status( unsafe { PasteboardGetItemIdentifier(pasteboard.0, index as isize, &mut item) }, "identify a clipboard item", )?; let mut flavors = ptr::null(); check_status( unsafe { PasteboardCopyItemFlavors(pasteboard.0, item, &mut flavors) }, "list clipboard formats", )?; if flavors.is_null() { return Err(eyre!("could not list clipboard formats")); } let flavor_count = unsafe { CFArrayGetCount(flavors) }; let result = (0..flavor_count) .map(|flavor_index| { let flavor = unsafe { CFArrayGetValueAtIndex(flavors, flavor_index) }; if flavor.is_null() { return Err(eyre!("clipboard format was unavailable")); } let mut flags = 0; check_status( unsafe { PasteboardGetItemFlavorFlags(pasteboard.0, item, flavor, &mut flags) }, "inspect clipboard format", )?; Ok((flavor, flags)) }) .collect::>>() .and_then(|flavor_entries| { let has_authoritative_flavor = flavor_entries .iter() .any(|&(_, flags)| flags & SYSTEM_TRANSLATED_FLAVOR == 0); flavor_entries .into_iter() .map(|(flavor, flags)| { let data_type = cf_string(flavor)?; if !should_preserve_flavor(flags, has_authoritative_flavor) { tracing::debug!(%data_type, "skipping synthesized clipboard format"); return Ok(None); } let mut data = ptr::null(); let flavor_started = Instant::now(); let status = unsafe { PasteboardCopyItemFlavorData(pasteboard.0, item, flavor, &mut data) }; let flavor_ms = flavor_started.elapsed().as_millis(); if flavor_ms >= 100 { tracing::warn!( %data_type, flavor_ms, "clipboard format was slow to materialize" ); } if status == BAD_PASTEBOARD_FLAVOR { tracing::debug!(%data_type, "skipping unavailable clipboard format"); return Ok(None); } check_status(status, "preserve clipboard format")?; if data.is_null() { return Err(eyre!("clipboard format data was unavailable")); } let bytes = cf_data(data); unsafe { CFRelease(data) }; Ok(Some(ClipboardFlavor { data_type, data: bytes?, flags: flags & 0x0f, })) }) .collect::>>() }); unsafe { CFRelease(flavors) }; items.push(result?.into_iter().flatten().collect()); } let capture_ms = capture_started.elapsed().as_millis(); if capture_ms >= 100 { tracing::warn!(capture_ms, item_count, "clipboard snapshot was slow"); } Ok(ClipboardSnapshot { items }) } fn should_preserve_flavor(flags: u32, has_authoritative_flavor: bool) -> bool { !has_authoritative_flavor || flags & SYSTEM_TRANSLATED_FLAVOR == 0 } /// nspasteboard.org markers asking clipboard managers not to record a write. /// The transcript is only on the clipboard for the duration of the paste. const CLIPBOARD_MANAGER_MARKERS: [&str; 2] = [ "org.nspasteboard.TransientType", "org.nspasteboard.AutoGeneratedType", ]; fn write_clipboard_text(clipboard: &NSPasteboard, text: &str) -> Result<()> { // One item carrying the text and its markers, published by a single write, // so a polling clipboard manager never observes the text without them. let item = NSPasteboardItem::new(); let marker_data = NSData::new(); let markers_written = CLIPBOARD_MANAGER_MARKERS .iter() .all(|marker| item.setData_forType(&marker_data, &NSString::from_str(marker))); let text_written = item.setString_forType(&NSString::from_str(text), unsafe { NSPasteboardTypeString }); if !(markers_written && text_written) { return Err(eyre!("could not prepare the transcript for the clipboard")); } clipboard.clearContents(); write_items(clipboard, &[item]) } fn restore_clipboard(clipboard: &NSPasteboard, snapshot: &ClipboardSnapshot) -> Result<()> { let pasteboard = create_pasteboard(&clipboard.name())?; unsafe { PasteboardSynchronize(pasteboard.0) }; check_status( unsafe { PasteboardClear(pasteboard.0) }, "clear the clipboard", )?; for (item_index, item) in snapshot.items.iter().enumerate() { let item_id = (item_index + 1) as PasteboardItemId; for flavor in item { let data_type = cf_string_create(&flavor.data_type)?; let data = unsafe { CFDataCreate( ptr::null(), flavor.data.as_ptr(), flavor.data.len() as isize, ) }; if data.is_null() { unsafe { CFRelease(data_type) }; return Err(eyre!("could not encode a clipboard format")); } let status = unsafe { PasteboardPutItemFlavor(pasteboard.0, item_id, data_type, data, flavor.flags) }; unsafe { CFRelease(data); CFRelease(data_type); } if status == DUPLICATE_PASTEBOARD_FLAVOR { tracing::debug!(data_type = %flavor.data_type, "skipping synthesized clipboard format"); continue; } check_status(status, "restore clipboard format")?; } } Ok(()) } fn write_items(clipboard: &NSPasteboard, items: &[Retained]) -> Result<()> { let objects = items .iter() .map(|item| ProtocolObject::from_ref(&**item)) .collect::>>(); let objects = NSArray::from_slice(&objects); clipboard .writeObjects(&objects) .then_some(()) .ok_or_else(|| eyre!("could not write the clipboard")) } fn create_pasteboard(name: &NSString) -> Result { let mut pasteboard = ptr::null(); check_status( unsafe { PasteboardCreate((name as *const NSString).cast(), &mut pasteboard) }, "open the clipboard", )?; (!pasteboard.is_null()) .then_some(PasteboardHandle(pasteboard)) .ok_or_else(|| eyre!("could not open the clipboard")) } fn check_status(status: i32, action: &str) -> Result<()> { (status == 0) .then_some(()) .ok_or_else(|| eyre!("could not {action} (status {status})")) } fn cf_string_create(value: &str) -> Result { let value = unsafe { CFStringCreateWithBytes( ptr::null(), value.as_ptr(), value.len() as isize, CF_STRING_ENCODING_UTF8, 0, ) }; (!value.is_null()) .then_some(value) .ok_or_else(|| eyre!("could not encode a clipboard format name")) } fn cf_string(value: CfStringRef) -> Result { let length = unsafe { CFStringGetLength(value) }; let capacity = unsafe { CFStringGetMaximumSizeForEncoding(length, CF_STRING_ENCODING_UTF8) } .saturating_add(1); let mut bytes = vec![0; capacity as usize]; if !unsafe { CFStringGetCString( value, bytes.as_mut_ptr().cast(), capacity, CF_STRING_ENCODING_UTF8, ) } { return Err(eyre!("could not decode a clipboard format name")); } let length = bytes .iter() .position(|byte| *byte == 0) .unwrap_or(bytes.len()); bytes.truncate(length); String::from_utf8(bytes).map_err(Into::into) } fn cf_data(value: CfDataRef) -> Result> { let length = unsafe { CFDataGetLength(value) }; if length < 0 { return Err(eyre!("clipboard format had an invalid length")); } if length == 0 { return Ok(Vec::new()); } let bytes = unsafe { CFDataGetBytePtr(value) }; if bytes.is_null() { return Err(eyre!("clipboard format data was unavailable")); } Ok(unsafe { std::slice::from_raw_parts(bytes, length as usize) }.to_vec()) } fn join(previous: &str, next: &str) -> String { let sentence_start = ends_sentence(previous); let mut next = set_initial_case(next, sentence_start); let needs_space = previous .chars() .next_back() .is_some_and(|character| !character.is_whitespace() && !is_opening(character)) && next .chars() .next() .is_some_and(|character| !character.is_whitespace() && !is_closing(character)); if needs_space { next.insert(0, ' '); } next } fn ends_sentence(text: &str) -> bool { text.trim_end() .trim_end_matches(['\'', '"', ')', ']', '}']) .ends_with(['.', '?', '!']) } fn is_opening(character: char) -> bool { matches!(character, '(' | '[' | '{') } fn is_closing(character: char) -> bool { matches!( character, ',' | '.' | '?' | '!' | ';' | ':' | ')' | ']' | '}' ) } fn set_initial_case(text: &str, uppercase: bool) -> String { let Some((index, character)) = text .char_indices() .find(|(_, character)| character.is_alphabetic()) else { return text.to_string(); }; if uppercase && character.is_lowercase() { return replace_character(text, index, character.to_uppercase()); } if !uppercase && character.is_uppercase() && sentence_initial_word(text, index) { return replace_character(text, index, character.to_lowercase()); } text.to_string() } fn sentence_initial_word(text: &str, start: usize) -> bool { let word = text[start..] .split(|character: char| !character.is_alphabetic() && character != '\'') .next() .unwrap_or_default(); matches!( word.to_ascii_lowercase().as_str(), "a" | "an" | "and" | "as" | "because" | "but" | "for" | "if" | "nor" | "or" | "so" | "the" | "then" | "though" | "to" | "when" | "while" | "yet" ) } fn replace_character(text: &str, index: usize, replacement: impl Iterator) -> String { let character_length = text[index..].chars().next().unwrap().len_utf8(); let mut output = String::with_capacity(text.len()); output.push_str(&text[..index]); output.extend(replacement); output.push_str(&text[index + character_length..]); output } #[cfg(test)] mod tests { use std::cell::{Cell, RefCell}; use super::*; static PASTEBOARD_TEST: Mutex<()> = Mutex::new(()); #[test] fn rejected_commit_does_not_write_paste_restore_or_send() { let steps = RefCell::new(Vec::new()); let result = send_after_paste( || { commit_prepared_paste( || { steps.borrow_mut().push("prepare"); Ok(()) }, || { steps.borrow_mut().push("commit"); false }, |()| { steps.borrow_mut().push("write"); complete_paste( Duration::ZERO, || { steps.borrow_mut().push("post"); Ok(()) }, || steps.borrow_mut().push("restore"), |_| steps.borrow_mut().push("settle"), ) }, ) }, || { steps.borrow_mut().push("enter"); Ok(()) }, ); assert_eq!(result.unwrap_err().to_string(), "paste was cancelled"); assert_eq!(steps.into_inner(), ["prepare", "commit"]); } #[test] fn failed_preparation_does_not_commit_or_write() { let result = commit_prepared_paste( || Err::<(), _>(eyre!("snapshot failed")), || panic!("failed preparation must remain cancellable"), |()| -> Result<()> { panic!("failed preparation must not mutate the clipboard") }, ); assert_eq!(result.unwrap_err().to_string(), "snapshot failed"); } #[test] fn sequential_pastes_settle_before_overwrite_and_enter() { let clipboard = Cell::new(""); let pending = Cell::new(false); let consumed = RefCell::new(Vec::new()); let steps = RefCell::new(Vec::new()); let paste = |text| { clipboard.set(text); complete_paste( Duration::ZERO, || { pending.set(true); steps.borrow_mut().push("post"); Ok(()) }, || steps.borrow_mut().push("restore"), |delay| { assert_eq!(delay, PASTE_SETTLE_DELAY); assert!(pending.replace(false)); consumed.borrow_mut().push(clipboard.get()); steps.borrow_mut().push("settle"); }, ) }; paste("first").unwrap(); assert_eq!(*consumed.borrow(), ["first"]); send_after_paste( || paste("second"), || { assert_eq!(*consumed.borrow(), ["first", "second"]); steps.borrow_mut().push("enter"); Ok(()) }, ) .unwrap(); assert_eq!( steps.into_inner(), [ "post", "restore", "settle", "post", "restore", "settle", "enter" ] ); } #[test] fn restoration_is_scheduled_after_a_stalled_post_completes() { let elapsed = Cell::new(Duration::ZERO); let restore_scheduled_at = Cell::new(None); complete_paste( Duration::ZERO, || { elapsed.set(Duration::from_secs(1)); assert_eq!(restore_scheduled_at.get(), None); Ok(()) }, || restore_scheduled_at.set(Some(elapsed.get())), |delay| elapsed.set(elapsed.get() + delay), ) .unwrap(); assert_eq!(restore_scheduled_at.get(), Some(Duration::from_secs(1))); assert_eq!(elapsed.get(), Duration::from_secs(1) + PASTE_SETTLE_DELAY); } #[test] fn failed_paste_still_schedules_restore_without_settling_or_sending() { let steps = RefCell::new(Vec::new()); let result = send_after_paste( || { complete_paste( Duration::ZERO, || { steps.borrow_mut().push("post"); Err(eyre!("post failed")) }, || steps.borrow_mut().push("restore"), |_| steps.borrow_mut().push("settle"), ) }, || { steps.borrow_mut().push("enter"); Ok(()) }, ); assert_eq!(result.unwrap_err().to_string(), "post failed"); assert_eq!(steps.into_inner(), ["post", "restore"]); } #[test] fn only_chromium_browsers_delay_the_paste_shortcut() { for bundle_id in [ "com.google.Chrome", "com.google.Chrome.beta", "com.google.Chrome.canary", "com.google.Chrome.app.kjgfgldnnfoeklkmfkjfagphfepbbdan", "com.brave.Browser", "com.brave.Browser.nightly", "company.thebrowser.Browser", "company.thebrowser.dia", "com.microsoft.edgemac", "com.microsoft.edgemac.Dev", "com.vivaldi.Vivaldi", "com.operasoftware.Opera", "com.operasoftware.OperaGX", "org.chromium.Chromium", "COM.GOOGLE.CHROME", ] { assert_eq!( pre_paste_delay(Some(bundle_id)), CHROMIUM_PRE_PASTE_DELAY, "{bundle_id}" ); } for bundle_id in [ None, Some(""), Some("com.apple.Safari"), Some("org.mozilla.firefox"), Some("com.mitchellh.ghostty"), Some("com.google.Chromecast"), Some("com.google"), Some("com.brave.Browserish"), Some("çom.google.Chrome"), ] { assert_eq!(pre_paste_delay(bundle_id), Duration::ZERO, "{bundle_id:?}"); } } #[test] fn chromium_delay_runs_between_the_clipboard_write_and_the_paste_shortcut() { let steps = RefCell::new(Vec::new()); complete_paste( CHROMIUM_PRE_PASTE_DELAY, || { steps.borrow_mut().push("post".to_string()); Ok(()) }, || steps.borrow_mut().push("restore".to_string()), |delay| { steps .borrow_mut() .push(format!("wait {}", delay.as_millis())) }, ) .unwrap(); assert_eq!( steps.into_inner(), ["wait 150", "post", "restore", "wait 100"] ); } #[test] fn joins_contiguous_dictation_with_sentence_aware_spacing() { assert_eq!( join("Because if I don't do that,", "And if you see it."), " and if you see it." ); assert_eq!( join("And if you see it.", "well, that works."), " Well, that works." ); assert_eq!(join("Already spaced. ", "Next sentence."), "Next sentence."); assert_eq!(join("Hello", ", world."), ", world."); } #[test] fn preserves_likely_proper_nouns_mid_sentence() { assert_eq!(join("Open", "GitHub next."), " GitHub next."); assert_eq!(join("Message", "Slack now."), " Slack now."); } #[test] fn rapid_pastes_keep_the_original_clipboard_for_the_latest_restore() { let mut restore = ClipboardRestore::default(); let original = ClipboardSnapshot { items: vec![vec![ClipboardFlavor { data_type: "public.png".into(), data: vec![1, 2, 3], flags: 0, }]], }; let first_insert = ClipboardSnapshot { items: vec![vec![ClipboardFlavor { data_type: "public.utf8-plain-text".into(), data: b"first".to_vec(), flags: 0, }]], }; let first = restore.register(original.clone(), 10, 12); let second = restore.register(first_insert, 12, 14); assert_ne!(first, second); assert_eq!(restore.original, Some(original)); assert_eq!(restore.last_change_count, Some(14)); } #[test] fn clipboard_snapshot_preserves_multiple_items_and_formats() { let _test = PASTEBOARD_TEST.lock().unwrap(); let clipboard = NSPasteboard::pasteboardWithUniqueName(); let expected = [ vec![ ("com.hex.fixture.text", b"text".to_vec()), ("com.hex.fixture.rich", b"rich text".to_vec()), ], vec![("com.hex.fixture.image", vec![1, 2, 3, 4])], ]; let items = expected .iter() .map(|types| { let item = NSPasteboardItem::new(); for (data_type, bytes) in types { assert!(item.setData_forType( &NSData::with_bytes(bytes), &NSString::from_str(data_type) )); } item }) .collect::>(); write_items(&clipboard, &items).unwrap(); let snapshot = capture_clipboard(&clipboard).unwrap(); clipboard.clearContents(); restore_clipboard(&clipboard, &snapshot).unwrap(); assert_eq!(capture_clipboard(&clipboard).unwrap(), snapshot); } #[test] fn eager_clipboard_capture_refreshes_a_stale_snapshot() { let _test = PASTEBOARD_TEST.lock().unwrap(); let clipboard = NSPasteboard::pasteboardWithUniqueName(); let item = NSPasteboardItem::new(); assert!(item.setData_forType( &NSData::with_bytes(b"first"), &NSString::from_str("com.hex.fixture.first") )); clipboard.clearContents(); write_items(&clipboard, &[item]).unwrap(); let mut paster = Paster { clipboard: clipboard.clone(), activity: InputActivity::default(), continuation: None, clipboard_restore: Arc::new(Mutex::new(ClipboardRestore::default())), prepared_clipboard: None, }; paster.prepare(); let first_change_count = paster.prepared_clipboard.as_ref().unwrap().change_count; let item = NSPasteboardItem::new(); assert!(item.setData_forType( &NSData::with_bytes(b"second"), &NSString::from_str("com.hex.fixture.second") )); clipboard.clearContents(); write_items(&clipboard, &[item]).unwrap(); paster.prepare(); let prepared = paster.prepared_clipboard.as_ref().unwrap(); assert_ne!(prepared.change_count, first_change_count); assert_eq!(prepared.snapshot, capture_clipboard(&clipboard).unwrap()); } #[test] fn transcript_write_is_marked_for_clipboard_managers() { let _test = PASTEBOARD_TEST.lock().unwrap(); let clipboard = NSPasteboard::pasteboardWithUniqueName(); write_clipboard_text(&clipboard, "dictated text").unwrap(); let snapshot = capture_clipboard(&clipboard).unwrap(); assert_eq!(snapshot.items.len(), 1); let types = snapshot.items[0] .iter() .map(|flavor| flavor.data_type.as_str()) .collect::>(); for marker in CLIPBOARD_MANAGER_MARKERS { assert!(types.contains(&marker), "{marker} missing from {types:?}"); } assert_eq!( clipboard .stringForType(unsafe { NSPasteboardTypeString }) .unwrap() .to_string(), "dictated text" ); } #[test] fn skips_regenerable_system_translations() { assert!(!should_preserve_flavor(SYSTEM_TRANSLATED_FLAVOR, true)); assert!(should_preserve_flavor(0, true)); assert!(should_preserve_flavor(SYSTEM_TRANSLATED_FLAVOR, false)); } #[test] fn image_snapshot_keeps_its_authoritative_format() { let _test = PASTEBOARD_TEST.lock().unwrap(); let clipboard = NSPasteboard::pasteboardWithUniqueName(); let item = NSPasteboardItem::new(); assert!(item.setData_forType( &NSData::with_bytes(include_bytes!("../app/AppIcon.icon/Assets/Image.png")), &NSString::from_str("public.png") )); clipboard.clearContents(); write_items(&clipboard, &[item]).unwrap(); let snapshot = capture_clipboard(&clipboard).unwrap(); assert_eq!( snapshot .items .iter() .flatten() .map(|flavor| flavor.data_type.as_str()) .collect::>(), vec!["public.png"] ); clipboard.clearContents(); restore_clipboard(&clipboard, &snapshot).unwrap(); assert_eq!(capture_clipboard(&clipboard).unwrap(), snapshot); } }