diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-05-20 02:51:59 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-05-20 02:51:59 +0300 |
| commit | ed478be9835e984c42bc1e30fe84b5fb01a8c5d4 (patch) | |
| tree | 353b6ea5c7368a9f3b477bc8f1bbcd248e377577 /src/platform/macos.rs | |
| parent | 2b97799b6715fd6c355db9c225029765971915e6 (diff) | |
fix: bridge remote clipboard image paste
refs #205
Diffstat (limited to 'src/platform/macos.rs')
| -rw-r--r-- | src/platform/macos.rs | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/src/platform/macos.rs b/src/platform/macos.rs index f085ee91..d0cf43c8 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use std::sync::OnceLock; -use super::{ClipboardCommand, ForegroundJob, ForegroundProcess, Signal}; +use super::{ClipboardCommand, ClipboardImage, ForegroundJob, ForegroundProcess, Signal}; const PROC_PGRP_ONLY: u32 = 2; @@ -202,6 +202,47 @@ pub fn write_clipboard(bytes: &[u8]) -> bool { ) } +pub fn read_clipboard_image() -> Option<ClipboardImage> { + let path = std::env::temp_dir().join(format!( + "herdr-clipboard-image-{}-{}.png", + std::process::id(), + unique_timestamp_nanos() + )); + let script = format!( + "set png_data to (the clipboard as «class PNGf»)\nset fp to open for access POSIX file \"{}\" with write permission\nwrite png_data to fp\nclose access fp", + path.display() + ); + + let status = Command::new("osascript") + .arg("-e") + .arg(script) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .ok()?; + + if !status.success() { + let _ = std::fs::remove_file(&path); + return None; + } + + let bytes = std::fs::read(&path).ok(); + let _ = std::fs::remove_file(&path); + let bytes = bytes?; + (!bytes.is_empty()).then_some(ClipboardImage { + bytes, + extension: "png", + }) +} + +fn unique_timestamp_nanos() -> u128 { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|duration| duration.as_nanos()) + .unwrap_or(0) +} + /// Show a native macOS notification. /// /// Prefer `terminal-notifier` when it is installed because it can activate the |
