dialog.d.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * Extension filters for the file dialog.
  3. *
  4. * @since 1.0.0
  5. */
  6. interface DialogFilter {
  7. /** Filter name. */
  8. name: string;
  9. /**
  10. * Extensions to filter, without a `.` prefix.
  11. * @example
  12. * ```typescript
  13. * extensions: ['svg', 'png']
  14. * ```
  15. */
  16. extensions: string[];
  17. }
  18. /**
  19. * Options for the open dialog.
  20. *
  21. * @since 1.0.0
  22. */
  23. interface OpenDialogOptions {
  24. /** The title of the dialog window. */
  25. title?: string;
  26. /** The filters of the dialog. */
  27. filters?: DialogFilter[];
  28. /** Initial directory or file path. */
  29. defaultPath?: string;
  30. /** Whether the dialog allows multiple selection or not. */
  31. multiple?: boolean;
  32. /** Whether the dialog is a directory selection or not. */
  33. directory?: boolean;
  34. /**
  35. * If `directory` is true, indicates that it will be read recursively later.
  36. * Defines whether subdirectories will be allowed on the scope or not.
  37. */
  38. recursive?: boolean;
  39. }
  40. /**
  41. * Options for the save dialog.
  42. *
  43. * @since 1.0.0
  44. */
  45. interface SaveDialogOptions {
  46. /** The title of the dialog window. */
  47. title?: string;
  48. /** The filters of the dialog. */
  49. filters?: DialogFilter[];
  50. /**
  51. * Initial directory or file path.
  52. * If it's a directory path, the dialog interface will change to that folder.
  53. * If it's not an existing directory, the file name will be set to the dialog's file name input and the dialog will be set to the parent folder.
  54. */
  55. defaultPath?: string;
  56. }
  57. /**
  58. * @since 1.0.0
  59. */
  60. interface MessageDialogOptions {
  61. /** The title of the dialog. Defaults to the app name. */
  62. title?: string;
  63. /** The type of the dialog. Defaults to `info`. */
  64. type?: 'info' | 'warning' | 'error';
  65. /** The label of the confirm button. */
  66. okLabel?: string;
  67. }
  68. interface ConfirmDialogOptions {
  69. /** The title of the dialog. Defaults to the app name. */
  70. title?: string;
  71. /** The type of the dialog. Defaults to `info`. */
  72. type?: 'info' | 'warning' | 'error';
  73. /** The label of the confirm button. */
  74. okLabel?: string;
  75. /** The label of the cancel button. */
  76. cancelLabel?: string;
  77. }
  78. /**
  79. * Open a file/directory selection dialog.
  80. *
  81. * The selected paths are added to the filesystem and asset protocol allowlist scopes.
  82. * When security is more important than the easy of use of this API,
  83. * prefer writing a dedicated command instead.
  84. *
  85. * Note that the allowlist scope change is not persisted, so the values are cleared when the application is restarted.
  86. * You can save it to the filesystem using [tauri-plugin-persisted-scope](https://github.com/tauri-apps/plugins-workspace/tree/v1/plugins/persisted-scope).
  87. * @example
  88. * ```typescript
  89. * import { open } from '@tauri-apps/api/dialog';
  90. * // Open a selection dialog for image files
  91. * const selected = await open({
  92. * multiple: true,
  93. * filters: [{
  94. * name: 'Image',
  95. * extensions: ['png', 'jpeg']
  96. * }]
  97. * });
  98. * if (Array.isArray(selected)) {
  99. * // user selected multiple files
  100. * } else if (selected === null) {
  101. * // user cancelled the selection
  102. * } else {
  103. * // user selected a single file
  104. * }
  105. * ```
  106. *
  107. * @example
  108. * ```typescript
  109. * import { open } from '@tauri-apps/api/dialog';
  110. * import { appDir } from '@tauri-apps/api/path';
  111. * // Open a selection dialog for directories
  112. * const selected = await open({
  113. * directory: true,
  114. * multiple: true,
  115. * defaultPath: await appDir(),
  116. * });
  117. * if (Array.isArray(selected)) {
  118. * // user selected multiple directories
  119. * } else if (selected === null) {
  120. * // user cancelled the selection
  121. * } else {
  122. * // user selected a single directory
  123. * }
  124. * ```
  125. *
  126. * @returns A promise resolving to the selected path(s)
  127. *
  128. * @since 1.0.0
  129. */
  130. declare function open(options?: OpenDialogOptions): Promise<null | string | string[]>;
  131. /**
  132. * Open a file/directory save dialog.
  133. *
  134. * The selected path is added to the filesystem and asset protocol allowlist scopes.
  135. * When security is more important than the easy of use of this API,
  136. * prefer writing a dedicated command instead.
  137. *
  138. * Note that the allowlist scope change is not persisted, so the values are cleared when the application is restarted.
  139. * You can save it to the filesystem using [tauri-plugin-persisted-scope](https://github.com/tauri-apps/plugins-workspace/tree/v1/plugins/persisted-scope).
  140. * @example
  141. * ```typescript
  142. * import { save } from '@tauri-apps/api/dialog';
  143. * const filePath = await save({
  144. * filters: [{
  145. * name: 'Image',
  146. * extensions: ['png', 'jpeg']
  147. * }]
  148. * });
  149. * ```
  150. *
  151. * @returns A promise resolving to the selected path.
  152. *
  153. * @since 1.0.0
  154. */
  155. declare function save(options?: SaveDialogOptions): Promise<string | null>;
  156. /**
  157. * Shows a message dialog with an `Ok` button.
  158. * @example
  159. * ```typescript
  160. * import { message } from '@tauri-apps/api/dialog';
  161. * await message('Tauri is awesome', 'Tauri');
  162. * await message('File not found', { title: 'Tauri', type: 'error' });
  163. * ```
  164. *
  165. * @param message The message to show.
  166. * @param options The dialog's options. If a string, it represents the dialog title.
  167. *
  168. * @returns A promise indicating the success or failure of the operation.
  169. *
  170. * @since 1.0.0
  171. *
  172. */
  173. declare function message(message: string, options?: string | MessageDialogOptions): Promise<void>;
  174. /**
  175. * Shows a question dialog with `Yes` and `No` buttons.
  176. * @example
  177. * ```typescript
  178. * import { ask } from '@tauri-apps/api/dialog';
  179. * const yes = await ask('Are you sure?', 'Tauri');
  180. * const yes2 = await ask('This action cannot be reverted. Are you sure?', { title: 'Tauri', type: 'warning' });
  181. * ```
  182. *
  183. * @param message The message to show.
  184. * @param options The dialog's options. If a string, it represents the dialog title.
  185. *
  186. * @returns A promise resolving to a boolean indicating whether `Yes` was clicked or not.
  187. *
  188. * @since 1.0.0
  189. */
  190. declare function ask(message: string, options?: string | ConfirmDialogOptions): Promise<boolean>;
  191. /**
  192. * Shows a question dialog with `Ok` and `Cancel` buttons.
  193. * @example
  194. * ```typescript
  195. * import { confirm } from '@tauri-apps/api/dialog';
  196. * const confirmed = await confirm('Are you sure?', 'Tauri');
  197. * const confirmed2 = await confirm('This action cannot be reverted. Are you sure?', { title: 'Tauri', type: 'warning' });
  198. * ```
  199. *
  200. * @param message The message to show.
  201. * @param options The dialog's options. If a string, it represents the dialog title.
  202. *
  203. * @returns A promise resolving to a boolean indicating whether `Ok` was clicked or not.
  204. *
  205. * @since 1.0.0
  206. */
  207. declare function confirm(message: string, options?: string | ConfirmDialogOptions): Promise<boolean>;
  208. export type { DialogFilter, OpenDialogOptions, SaveDialogOptions, MessageDialogOptions, ConfirmDialogOptions };
  209. export { open, save, message, ask, confirm };