|
| 1 | +//! Tauri commands for clipboard file operations (copy/cut/paste). |
| 2 | +
|
| 3 | +use std::collections::HashSet; |
| 4 | +use std::path::PathBuf; |
| 5 | + |
| 6 | +#[cfg(target_os = "macos")] |
| 7 | +use crate::file_system::get_paths_at_indices as ops_get_paths_at_indices; |
| 8 | + |
| 9 | +use crate::clipboard; |
| 10 | + |
| 11 | +#[derive(serde::Serialize)] |
| 12 | +#[serde(rename_all = "camelCase")] |
| 13 | +pub struct ClipboardReadResult { |
| 14 | + paths: Vec<String>, |
| 15 | + is_cut: bool, |
| 16 | +} |
| 17 | + |
| 18 | +/// Resolves selected file paths and writes them to the system clipboard. |
| 19 | +/// Clears any existing cut state (this is a copy, not a cut). |
| 20 | +#[cfg(target_os = "macos")] |
| 21 | +#[tauri::command] |
| 22 | +pub async fn copy_files_to_clipboard( |
| 23 | + app: tauri::AppHandle, |
| 24 | + listing_id: String, |
| 25 | + selected_indices: Vec<usize>, |
| 26 | + cursor_index: usize, |
| 27 | + has_parent: bool, |
| 28 | + include_hidden: bool, |
| 29 | +) -> Result<usize, String> { |
| 30 | + let indices = resolve_indices(&selected_indices, cursor_index, has_parent); |
| 31 | + let paths = ops_get_paths_at_indices(&listing_id, &indices, include_hidden, has_parent)?; |
| 32 | + |
| 33 | + if paths.is_empty() { |
| 34 | + return Err("No files to copy".to_string()); |
| 35 | + } |
| 36 | + |
| 37 | + let count = paths.len(); |
| 38 | + |
| 39 | + // Write to pasteboard on the main thread (NSPasteboard requires it) |
| 40 | + let (tx, rx) = std::sync::mpsc::channel(); |
| 41 | + app.run_on_main_thread(move || { |
| 42 | + let result = clipboard::write_file_urls_to_clipboard(&paths); |
| 43 | + let _ = tx.send(result); |
| 44 | + }) |
| 45 | + .map_err(|e| format!("Couldn't run on main thread: {e}"))?; |
| 46 | + |
| 47 | + rx.recv() |
| 48 | + .map_err(|e| format!("Couldn't receive pasteboard result: {e}"))??; |
| 49 | + |
| 50 | + clipboard::clear_cut_state(); |
| 51 | + |
| 52 | + Ok(count) |
| 53 | +} |
| 54 | + |
| 55 | +/// Resolves selected file paths, writes them to the system clipboard, and marks them as cut. |
| 56 | +/// On paste, files will be moved instead of copied. |
| 57 | +#[cfg(target_os = "macos")] |
| 58 | +#[tauri::command] |
| 59 | +pub async fn cut_files_to_clipboard( |
| 60 | + app: tauri::AppHandle, |
| 61 | + listing_id: String, |
| 62 | + selected_indices: Vec<usize>, |
| 63 | + cursor_index: usize, |
| 64 | + has_parent: bool, |
| 65 | + include_hidden: bool, |
| 66 | +) -> Result<usize, String> { |
| 67 | + let indices = resolve_indices(&selected_indices, cursor_index, has_parent); |
| 68 | + let paths = ops_get_paths_at_indices(&listing_id, &indices, include_hidden, has_parent)?; |
| 69 | + |
| 70 | + if paths.is_empty() { |
| 71 | + return Err("No files to cut".to_string()); |
| 72 | + } |
| 73 | + |
| 74 | + let count = paths.len(); |
| 75 | + let cut_paths = paths.clone(); |
| 76 | + |
| 77 | + // Write to pasteboard on the main thread |
| 78 | + let (tx, rx) = std::sync::mpsc::channel(); |
| 79 | + app.run_on_main_thread(move || { |
| 80 | + let result = clipboard::write_file_urls_to_clipboard(&paths); |
| 81 | + let _ = tx.send(result); |
| 82 | + }) |
| 83 | + .map_err(|e| format!("Couldn't run on main thread: {e}"))?; |
| 84 | + |
| 85 | + rx.recv() |
| 86 | + .map_err(|e| format!("Couldn't receive pasteboard result: {e}"))??; |
| 87 | + |
| 88 | + clipboard::set_cut_state(cut_paths); |
| 89 | + |
| 90 | + Ok(count) |
| 91 | +} |
| 92 | + |
| 93 | +/// Reads file URLs from the system clipboard and checks whether they were cut. |
| 94 | +/// |
| 95 | +/// If the clipboard contents no longer match the recorded cut state (the user copied |
| 96 | +/// something else), the stale cut state is automatically cleared. |
| 97 | +#[cfg(target_os = "macos")] |
| 98 | +#[tauri::command] |
| 99 | +pub async fn read_clipboard_files(app: tauri::AppHandle) -> Result<ClipboardReadResult, String> { |
| 100 | + // Read from pasteboard on the main thread |
| 101 | + let (tx, rx) = std::sync::mpsc::channel(); |
| 102 | + app.run_on_main_thread(move || { |
| 103 | + let result = clipboard::read_file_urls_from_clipboard(); |
| 104 | + let _ = tx.send(result); |
| 105 | + }) |
| 106 | + .map_err(|e| format!("Couldn't run on main thread: {e}"))?; |
| 107 | + |
| 108 | + let clipboard_paths = rx |
| 109 | + .recv() |
| 110 | + .map_err(|e| format!("Couldn't receive pasteboard result: {e}"))??; |
| 111 | + |
| 112 | + // Check cut state: if set, verify paths match (order-insensitive) |
| 113 | + let is_cut = if let Some(cut_paths) = clipboard::get_cut_state() { |
| 114 | + let clipboard_set: HashSet<&PathBuf> = clipboard_paths.iter().collect(); |
| 115 | + let cut_set: HashSet<&PathBuf> = cut_paths.iter().collect(); |
| 116 | + |
| 117 | + if clipboard_set == cut_set { |
| 118 | + true |
| 119 | + } else { |
| 120 | + // Clipboard changed since the cut -- clear stale state |
| 121 | + clipboard::clear_cut_state(); |
| 122 | + false |
| 123 | + } |
| 124 | + } else { |
| 125 | + false |
| 126 | + }; |
| 127 | + |
| 128 | + let paths: Vec<String> = clipboard_paths |
| 129 | + .iter() |
| 130 | + .map(|p| p.to_string_lossy().into_owned()) |
| 131 | + .collect(); |
| 132 | + |
| 133 | + Ok(ClipboardReadResult { paths, is_cut }) |
| 134 | +} |
| 135 | + |
| 136 | +/// Clears the in-process cut state without touching the system clipboard. |
| 137 | +#[tauri::command] |
| 138 | +pub fn clear_clipboard_cut_state() { |
| 139 | + clipboard::clear_cut_state(); |
| 140 | +} |
| 141 | + |
| 142 | +// --- Linux stubs --- |
| 143 | + |
| 144 | +#[cfg(not(target_os = "macos"))] |
| 145 | +#[tauri::command] |
| 146 | +pub async fn copy_files_to_clipboard( |
| 147 | + _app: tauri::AppHandle, |
| 148 | + _listing_id: String, |
| 149 | + _selected_indices: Vec<usize>, |
| 150 | + _cursor_index: usize, |
| 151 | + _has_parent: bool, |
| 152 | + _include_hidden: bool, |
| 153 | +) -> Result<usize, String> { |
| 154 | + Err("Clipboard file operations are not yet supported on this platform".to_string()) |
| 155 | +} |
| 156 | + |
| 157 | +#[cfg(not(target_os = "macos"))] |
| 158 | +#[tauri::command] |
| 159 | +pub async fn cut_files_to_clipboard( |
| 160 | + _app: tauri::AppHandle, |
| 161 | + _listing_id: String, |
| 162 | + _selected_indices: Vec<usize>, |
| 163 | + _cursor_index: usize, |
| 164 | + _has_parent: bool, |
| 165 | + _include_hidden: bool, |
| 166 | +) -> Result<usize, String> { |
| 167 | + Err("Clipboard file operations are not yet supported on this platform".to_string()) |
| 168 | +} |
| 169 | + |
| 170 | +#[cfg(not(target_os = "macos"))] |
| 171 | +#[tauri::command] |
| 172 | +pub async fn read_clipboard_files(_app: tauri::AppHandle) -> Result<ClipboardReadResult, String> { |
| 173 | + Err("Clipboard file operations are not yet supported on this platform".to_string()) |
| 174 | +} |
| 175 | + |
| 176 | +// --- Helpers --- |
| 177 | + |
| 178 | +/// When no files are selected, falls back to the cursor index (adjusting for the ".." entry). |
| 179 | +fn resolve_indices(selected_indices: &[usize], cursor_index: usize, has_parent: bool) -> Vec<usize> { |
| 180 | + if !selected_indices.is_empty() { |
| 181 | + return selected_indices.to_vec(); |
| 182 | + } |
| 183 | + |
| 184 | + // Nothing selected -- use the cursor position. |
| 185 | + // If the cursor is on ".." (index 0 with has_parent), skip it. |
| 186 | + if has_parent && cursor_index == 0 { |
| 187 | + return Vec::new(); |
| 188 | + } |
| 189 | + |
| 190 | + vec![cursor_index] |
| 191 | +} |
0 commit comments