Address Mac build failures

This commit is contained in:
Skyler Lehmkuhl 2026-03-09 14:36:54 -04:00
parent 0066dffc81
commit 0ae97f9562
1 changed files with 17 additions and 21 deletions

View File

@ -44,35 +44,31 @@ pub fn get(preferred: &[&str]) -> Option<(String, Vec<u8>)> {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
mod platform_impl { mod platform_impl {
use objc2::rc::Retained;
use objc2_app_kit::NSPasteboard; use objc2_app_kit::NSPasteboard;
use objc2_foundation::{NSData, NSString}; use objc2_foundation::{NSData, NSString};
pub fn set(entries: &[(&str, &[u8])]) { pub fn set(entries: &[(&str, &[u8])]) {
// SAFETY: must be called from the main thread (same as ClipboardManager). let pb = NSPasteboard::generalPasteboard();
unsafe { for &(mime, data) in entries {
let pb = NSPasteboard::generalPasteboard(); let ns_type = NSString::from_str(mime);
for &(mime, data) in entries { let ns_data = NSData::with_bytes(data);
let ns_type: Retained<NSString> = NSString::from_str(mime); // setData:forType: appends to the current clipboard contents
let ns_data: Retained<NSData> = NSData::with_bytes(data); // (arboard already called clearContents, so no double-clear needed).
// setData:forType: appends to the current clipboard contents pb.setData_forType(Some(&ns_data), &ns_type);
// (arboard already called clearContents, so no double-clear needed).
pb.setData_forType(Some(&ns_data), &ns_type);
}
} }
} }
pub fn get(preferred: &[&str]) -> Option<(String, Vec<u8>)> { pub fn get(preferred: &[&str]) -> Option<(String, Vec<u8>)> {
// SAFETY: must be called from the main thread. let pb = NSPasteboard::generalPasteboard();
unsafe { for &mime in preferred {
let pb = NSPasteboard::generalPasteboard(); let ns_type = NSString::from_str(mime);
for &mime in preferred { if let Some(ns_data) = pb.dataForType(&ns_type) {
let ns_type: Retained<NSString> = NSString::from_str(mime); let len = ns_data.length();
if let Some(ns_data) = pb.dataForType(&ns_type) { // SAFETY: bytes() is valid for length() bytes per NSData contract.
// NSData implements AsRef<[u8]> in objc2-foundation. let bytes = unsafe {
let bytes = AsRef::<[u8]>::as_ref(&ns_data).to_vec(); std::slice::from_raw_parts(ns_data.bytes() as *const u8, len).to_vec()
return Some((mime.to_string(), bytes)); };
} return Some((mime.to_string(), bytes));
} }
} }
None None