img clipboard

This commit is contained in:
babayaga 2025-09-21 21:46:24 +02:00
parent 33897e7024
commit b03d74661a
5 changed files with 57 additions and 29 deletions

Binary file not shown.

View File

@ -57,31 +57,42 @@ async fn parse_clipboard_images(
let candidate_format = candidate_format.unwrap_or_else(|| "png".to_string());
let temp_dir = temp_dir.unwrap_or_else(|| std::env::temp_dir());
// Try to get file list first (drag and drop from explorer/finder)
if let Ok(file_list) = clipboard.get().file_list() {
info!("[parse_clipboard_images] File list: {:?}", file_list);
for file in file_list {
let path_str = file.to_string_lossy().to_string();
// Filter for image files only
if path_str.to_lowercase().ends_with(".png")
|| path_str.to_lowercase().ends_with(".jpg")
|| path_str.to_lowercase().ends_with(".jpeg")
|| path_str.to_lowercase().ends_with(".gif")
|| path_str.to_lowercase().ends_with(".webp") {
result.push(path_str);
// Try to get image data first (screenshot, copied image)
match clipboard.get().image() {
Ok(image_data) => {
info!(
"[parse_clipboard_images] Found image data: {}x{}, {} bytes",
image_data.width,
image_data.height,
image_data.bytes.len()
);
let temp_path = save_rgba_image_to_temp(&image_data, &candidate_format, &temp_dir)?;
result.push(temp_path);
}
Err(e) => {
info!("[parse_clipboard_images] No image data found: {}", e);
// Try to get file list (drag and drop from explorer/finder)
match clipboard.get().file_list() {
Ok(file_list) => {
info!("[parse_clipboard_images] Found file list: {:?}", file_list);
for file in file_list {
let path_str = file.to_string_lossy().to_string();
// Filter for image files only
if path_str.to_lowercase().ends_with(".png")
|| path_str.to_lowercase().ends_with(".jpg")
|| path_str.to_lowercase().ends_with(".jpeg")
|| path_str.to_lowercase().ends_with(".gif")
|| path_str.to_lowercase().ends_with(".webp") {
result.push(path_str);
}
}
}
Err(e2) => {
info!("[parse_clipboard_images] No file list found: {}", e2);
}
}
}
}
// If no files, try to get image data (screenshot, copied image)
else if let Ok(image_data) = clipboard.get().image() {
info!(
"[parse_clipboard_images] Image data: {}x{}, {} bytes",
image_data.width,
image_data.height,
image_data.bytes.len()
);
let temp_path = save_rgba_image_to_temp(&image_data, &candidate_format, &temp_dir)?;
result.push(temp_path);
}
Ok(result)

View File

@ -361,10 +361,27 @@ function App() {
// Update destination directory
const firstPath = uniqueNewPaths[0];
const lastSeparatorIndex = Math.max(firstPath.lastIndexOf('/'), firstPath.lastIndexOf('\\'));
const newDir = firstPath.substring(0, lastSeparatorIndex);
// Check if this is a clipboard image (from temp directory)
const isClipboardImage = firstPath.includes('kbot_clipboard_') ||
firstPath.includes(process.platform === 'win32' ? '\\Temp\\' : '/tmp/') ||
firstPath.includes('AppData\\Local\\Temp') ||
firstPath.includes('/var/folders/'); // macOS temp
let newDir: string;
if (isClipboardImage) {
// For clipboard images, use current working directory or user's Documents folder
newDir = './'; // Use current working directory
log.info('📋 Clipboard image detected, using current working directory for output');
} else {
// For regular files, use the directory of the first file
const lastSeparatorIndex = Math.max(firstPath.lastIndexOf('/'), firstPath.lastIndexOf('\\'));
newDir = firstPath.substring(0, lastSeparatorIndex);
}
const currentFilename = dst.split(/[/\\]/).pop() || generateDefaultDst(1, firstPath);
const newDst = `${newDir}${firstPath.includes('\\') ? '\\' : '/'}${currentFilename}`;
const separator = firstPath.includes('\\') ? '\\' : '/';
const newDst = newDir === './' ? currentFilename : `${newDir}${separator}${currentFilename}`;
setDst(newDst);
// Read files

View File

@ -126,7 +126,7 @@ const PromptForm: React.FC<PromptFormProps> = ({
log.info('📋 Paste event detected, checking for images...');
// Try to get images from clipboard using our Tauri command
const result = await tauriApi.parseClipboardImages('png');
const result = await tauriApi.parseClipboardImages('png', '');
if (result?.success && result.paths && result.paths.length > 0) {
log.info(`📋 Found ${result.paths.length} image(s) in clipboard`, {

View File

@ -239,7 +239,7 @@ export const tauriApi = {
parseClipboardImages: (candidateFormat: string = 'png', tempDir?: string) =>
safeInvoke<{ success: boolean; paths?: string[]; error?: string }>('ipc_parse_clipboard_images', {
candidate_format: candidateFormat,
temp_dir: tempDir || ''
candidateFormat: candidateFormat,
tempDir: tempDir || ''
}),
};