import { invoke } from "@tauri-apps/api/core"; import { listen, type UnlistenFn } from "@tauri-apps/api/event"; import type { ActionOutcome, AppSettings, CaptureSession, EditorInfo, HotkeyStatus, FileInfo, FileOutputMode, NormalizedRect, RadialContext, RecordingCapabilities, RecordingInfo, RecordingOptions, SharePeer, RuntimeInfo, SwitchState, SwitchUpdate, UpdateProgress, UpdateStatus, WriteCheck, } from "../types"; import type { WedgeOrders } from "./radialOrder"; interface FileActionResultEvent { jobId: string; outcome?: ActionOutcome; error?: string; } interface FileActionProgressEvent { jobId: string; done: number; total: number; packaging: boolean; } async function performFileAction( action: string, paths: string[], target?: string, onProgress?: (done: number, total: number, packaging: boolean) => void, ): Promise { const jobId = typeof crypto.randomUUID === "function" ? crypto.randomUUID() : `${Date.now()}-${Math.random().toString(36).slice(2)}`; let resolveOutcome!: (outcome: ActionOutcome) => void; let rejectOutcome!: (reason?: unknown) => void; const completed = new Promise((resolve, reject) => { resolveOutcome = resolve; rejectOutcome = reject; }); const unlisten: UnlistenFn = await listen( "app://file-action-finished", (event) => { if (event.payload.jobId !== jobId) return; if (event.payload.error) rejectOutcome(new Error(event.payload.error)); else if (event.payload.outcome) resolveOutcome(event.payload.outcome); else rejectOutcome(new Error("File action returned no result.")); }, ); const unlistenProgress = onProgress ? await listen("app://file-action-progress", (event) => { if (event.payload.jobId === jobId) { onProgress(event.payload.done, event.payload.total, event.payload.packaging); } }) : null; try { await invoke("perform_file_action", { jobId, action, paths, target }); return await completed; } finally { unlisten(); unlistenProgress?.(); } } export const api = { inspectFiles: (paths: string[]) => invoke("inspect_files", { paths }), performFileAction, revealFile: (path: string) => invoke("reveal_file", { path }), openFile: (path: string) => invoke("open_file", { path }), readTextFile: (path: string) => invoke("read_text_file", { path }), readImageData: (path: string) => invoke("read_image_data", { path }), saveImageData: (source: string, dataUrl: string, suffix?: string) => invoke("save_image_data", { source, dataUrl, suffix }), renderPdfPages: (path: string) => invoke("render_pdf_pages", { path }), cleanupPdfPages: (paths: string[]) => invoke("cleanup_pdf_pages", { paths }), saveTextNear: (source: string, text: string, suffix?: string) => invoke("save_text_near", { source, text, suffix }), copyFileTo: (source: string, destination: string) => invoke("copy_file_to", { source, destination }), openScreenRecordingSettings: () => invoke("open_screen_recording_settings"), getCaptureSession: () => invoke("get_capture_session"), screenCaptureAccessStatus: () => invoke("screen_capture_access_status"), startCapture: (mode: "screenshot" | "recording") => invoke("start_capture", { mode }), cancelCapture: () => invoke("cancel_capture"), captureCrop: (rect: NormalizedRect) => invoke("capture_crop", { rect }), captureFullscreen: () => invoke("capture_fullscreen"), startRecording: (rect: NormalizedRect, options?: RecordingOptions) => invoke("start_recording", { rect, options }), recordingCapabilities: () => invoke("recording_capabilities"), stopRecording: () => invoke("stop_recording"), recordingStatus: () => invoke("recording_status"), getEditorSession: () => invoke("get_editor_session"), closeEditor: () => invoke("close_editor"), openInEditor: (path: string) => invoke("open_in_editor", { path }), pinImage: (path: string) => invoke("pin_image", { path }), getPinnedImage: () => invoke<{ id: number; path: string; dataUrl: string; width: number; height: number } | null>( "get_pinned_image", ), pinReady: (id: number | null) => invoke("pin_ready", { id }), closePin: () => invoke("close_pin"), getRadialContext: () => invoke("get_radial_context"), hideRadial: () => invoke("hide_radial"), showFilesInRadial: (files: string[], mode?: "actions" | "formats" | "tools") => invoke("show_files_in_radial", { files, mode }), shareAnswer: (transferId: string, accepted: boolean) => invoke("share_answer", { transferId, accepted }), shareRefresh: () => invoke("share_refresh"), shareSend: (peerId: string, paths: string[]) => invoke<{ name: string; size: number; peer: string }[]>("share_send", { peerId, paths }), getSettings: () => invoke("get_app_settings"), setOutputDirectory: (kind: "screenshot" | "recording" | "files", path: string | null) => invoke("set_output_directory", { kind, path }), setFileOutputMode: (mode: FileOutputMode) => invoke("set_file_output_mode", { mode }), setAutoReveal: (enabled: boolean) => invoke("set_auto_reveal", { enabled }), setDragEnabled: (enabled: boolean) => invoke("set_drag_enabled", { enabled }), setShareEnabled: (enabled: boolean) => invoke("set_share_enabled", { enabled }), setAutostart: (enabled: boolean) => invoke("set_autostart", { enabled }), setLanguage: (language: string) => invoke("set_language", { language }), hotkeyStatus: () => invoke("hotkey_status"), quitOtherInstances: () => invoke("quit_other_instances"), reclaimHotkeys: () => invoke("reclaim_hotkeys"), checkOutputAccess: () => invoke("check_output_access"), revealOutputDirectory: (kind: "screenshot" | "recording" | "files") => invoke("reveal_output_directory", { kind }), openFilesPermissionSettings: () => invoke("open_files_permission_settings"), setUpdateCheckEnabled: (enabled: boolean) => invoke("set_update_check_enabled", { enabled }), setSetupReviewed: (reviewed: boolean) => invoke("set_setup_reviewed", { reviewed }), getWedgeOrders: () => invoke("get_wedge_orders"), updateCheck: () => invoke<{ status: UpdateStatus; downloadReady: boolean }>("update_check"), updateStatus: () => invoke("update_status"), updateDownload: () => invoke("update_download"), updateInstall: () => invoke("update_install"), updateClear: () => invoke("update_clear"), onUpdateProgress: (handler: (progress: UpdateProgress) => void) => listen("update://progress", (event) => handler(event.payload)), onUpdateStatus: (handler: (status: UpdateStatus | null) => void) => listen("update://status", (event) => handler(event.payload)), getRuntimeInfo: () => invoke("machine_info"), installRuntimeComponent: (componentId: string) => invoke("install_runtime_component", { componentId }), };