|
| 1 | +// ============ 文件对话框相关类型定义 ============ |
| 2 | + |
| 3 | +/** |
| 4 | + * 文件过滤器,用于限制可选择的文件类型 |
| 5 | + */ |
| 6 | +export interface FileFilter { |
| 7 | + /** 过滤器名称,例如 "Images" */ |
| 8 | + name: string |
| 9 | + /** 文件扩展名数组,例如 ["png", "jpg"] */ |
| 10 | + extensions: string[] |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * 打开文件对话框的配置选项 |
| 15 | + */ |
| 16 | +export interface OpenDialogOptions { |
| 17 | + /** 对话框标题 */ |
| 18 | + title?: string |
| 19 | + /** 默认路径 */ |
| 20 | + defaultPath?: string |
| 21 | + /** |
| 22 | + * 确认按钮的自定义标签,留空时使用默认标签 |
| 23 | + */ |
| 24 | + buttonLabel?: string |
| 25 | + /** 文件过滤器数组 */ |
| 26 | + filters?: FileFilter[] |
| 27 | + /** |
| 28 | + * 对话框的功能配置 |
| 29 | + * - openFile: 允许选择文件 |
| 30 | + * - openDirectory: 允许选择文件夹 |
| 31 | + * - multiSelections: 允许多选 |
| 32 | + * - showHiddenFiles: 显示隐藏文件 |
| 33 | + * - createDirectory: 允许创建新目录 (macOS) |
| 34 | + * - promptToCreate: 如果输入的路径不存在,提示创建 (Windows) |
| 35 | + * - noResolveAliases: 禁用自动别名解析 (macOS) |
| 36 | + * - treatPackageAsDirectory: 将包视为目录 (macOS) |
| 37 | + * - dontAddToRecent: 不添加到最近文档 (Windows) |
| 38 | + */ |
| 39 | + properties?: Array< |
| 40 | + | 'openFile' |
| 41 | + | 'openDirectory' |
| 42 | + | 'multiSelections' |
| 43 | + | 'showHiddenFiles' |
| 44 | + | 'createDirectory' |
| 45 | + | 'promptToCreate' |
| 46 | + | 'noResolveAliases' |
| 47 | + | 'treatPackageAsDirectory' |
| 48 | + | 'dontAddToRecent' |
| 49 | + > |
| 50 | + /** |
| 51 | + * 显示在输入框上方的消息 (仅 macOS) |
| 52 | + * @platform darwin |
| 53 | + */ |
| 54 | + message?: string |
| 55 | + /** |
| 56 | + * 创建安全作用域书签 (仅 macOS) |
| 57 | + * @platform darwin,mas |
| 58 | + */ |
| 59 | + securityScopedBookmarks?: boolean |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * 打开文件对话框的返回值 |
| 64 | + */ |
| 65 | +export interface OpenDialogReturnValue { |
| 66 | + /** 对话框是否被取消 */ |
| 67 | + canceled: boolean |
| 68 | + /** 用户选择的文件路径数组。如果对话框被取消,这将是一个空数组 */ |
| 69 | + filePaths: string[] |
| 70 | + /** |
| 71 | + * 与 filePaths 数组匹配的 base64 编码字符串数组,包含安全作用域书签数据 |
| 72 | + * 必须启用 securityScopedBookmarks 才会填充此字段 (仅 macOS) |
| 73 | + * @platform darwin,mas |
| 74 | + */ |
| 75 | + bookmarks?: string[] |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * 保存文件对话框的配置选项 |
| 80 | + */ |
| 81 | +export interface SaveDialogOptions { |
| 82 | + /** |
| 83 | + * 对话框标题。在某些 Linux 桌面环境中无法显示 |
| 84 | + */ |
| 85 | + title?: string |
| 86 | + /** |
| 87 | + * 默认使用的绝对目录路径、绝对文件路径或文件名 |
| 88 | + */ |
| 89 | + defaultPath?: string |
| 90 | + /** |
| 91 | + * 确认按钮的自定义标签,留空时使用默认标签 |
| 92 | + */ |
| 93 | + buttonLabel?: string |
| 94 | + /** 文件过滤器数组 */ |
| 95 | + filters?: FileFilter[] |
| 96 | + /** |
| 97 | + * 显示在文本字段上方的消息 (仅 macOS) |
| 98 | + * @platform darwin |
| 99 | + */ |
| 100 | + message?: string |
| 101 | + /** |
| 102 | + * 显示在文件名文本字段前的自定义标签 (仅 macOS) |
| 103 | + * @platform darwin |
| 104 | + */ |
| 105 | + nameFieldLabel?: string |
| 106 | + /** |
| 107 | + * 显示标签输入框,默认为 true (仅 macOS) |
| 108 | + * @platform darwin |
| 109 | + */ |
| 110 | + showsTagField?: boolean |
| 111 | + /** |
| 112 | + * 对话框的功能配置 |
| 113 | + * - showHiddenFiles: 显示隐藏文件 |
| 114 | + * - createDirectory: 允许创建新目录 (macOS) |
| 115 | + * - treatPackageAsDirectory: 将包视为目录 (macOS) |
| 116 | + * - showOverwriteConfirmation: 如果文件已存在,显示覆盖确认 |
| 117 | + * - dontAddToRecent: 不添加到最近文档 (Windows) |
| 118 | + */ |
| 119 | + properties?: Array< |
| 120 | + | 'showHiddenFiles' |
| 121 | + | 'createDirectory' |
| 122 | + | 'treatPackageAsDirectory' |
| 123 | + | 'showOverwriteConfirmation' |
| 124 | + | 'dontAddToRecent' |
| 125 | + > |
| 126 | + /** |
| 127 | + * 为 Mac App Store 打包时创建安全作用域书签 |
| 128 | + * 如果启用此选项且文件不存在,将在选择的路径创建一个空文件 (仅 macOS) |
| 129 | + * @platform darwin,mas |
| 130 | + */ |
| 131 | + securityScopedBookmarks?: boolean |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * 保存文件对话框的返回值 |
| 136 | + */ |
| 137 | +export interface SaveDialogReturnValue { |
| 138 | + /** 对话框是否被取消 */ |
| 139 | + canceled: boolean |
| 140 | + /** 用户选择的文件路径。如果对话框被取消,这将是一个空字符串 */ |
| 141 | + filePath: string |
| 142 | + /** |
| 143 | + * Base64 编码的字符串,包含已保存文件的安全作用域书签数据 |
| 144 | + * 必须启用 securityScopedBookmarks 才会填充此字段 (仅 macOS) |
| 145 | + * @platform darwin,mas |
| 146 | + */ |
| 147 | + bookmark?: string |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * 显示打开文件对话框 |
| 152 | + * @param options 对话框选项 |
| 153 | + * @returns Promise,包含用户选择的文件路径和取消状态 |
| 154 | + * @example |
| 155 | + * ```typescript |
| 156 | + * const result = await showOpenDialog({ |
| 157 | + * title: '选择文件', |
| 158 | + * filters: [{ name: 'Images', extensions: ['jpg', 'png'] }], |
| 159 | + * properties: ['openFile', 'multiSelections'] |
| 160 | + * }) |
| 161 | + * if (!result.canceled) { |
| 162 | + * console.log('选择的文件:', result.filePaths) |
| 163 | + * } |
| 164 | + * ``` |
| 165 | + */ |
| 166 | +export async function showOpenDialog(options: OpenDialogOptions): Promise<OpenDialogReturnValue> { |
| 167 | + return window.parent.electron.ipcRenderer.invoke('ipc-show-open-dialog', options) |
| 168 | +} |
| 169 | + |
| 170 | +/** |
| 171 | + * 显示保存文件对话框 |
| 172 | + * @param options 对话框选项 |
| 173 | + * @returns Promise,包含用户选择的保存路径和取消状态 |
| 174 | + * @example |
| 175 | + * ```typescript |
| 176 | + * const result = await showSaveDialog({ |
| 177 | + * title: '保存文件', |
| 178 | + * defaultPath: 'untitled.txt', |
| 179 | + * filters: [{ name: 'Text Files', extensions: ['txt'] }] |
| 180 | + * }) |
| 181 | + * if (!result.canceled) { |
| 182 | + * console.log('保存路径:', result.filePath) |
| 183 | + * } |
| 184 | + * ``` |
| 185 | + */ |
| 186 | +export async function showSaveDialog(options: SaveDialogOptions): Promise<SaveDialogReturnValue> { |
| 187 | + return window.parent.electron.ipcRenderer.invoke('ipc-show-save-dialog', options) |
| 188 | +} |
0 commit comments