dialog.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { promisified } from './tauri'
  2. export interface OpenDialogOptions {
  3. filter?: string
  4. defaultPath?: string
  5. multiple?: boolean
  6. directory?: boolean
  7. }
  8. export type SaveDialogOptions = Pick<
  9. OpenDialogOptions,
  10. 'filter' | 'defaultPath'
  11. >
  12. /**
  13. * @name openDialog
  14. * @description Open a file/directory selection dialog
  15. * @param {Object} [options]
  16. * @param {string} [options.filter]
  17. * @param {string} [options.defaultPath]
  18. * @param {boolean} [options.multiple=false]
  19. * @param {boolean} [options.directory=false]
  20. * @returns {Promise<string | string[]>} Promise resolving to the select path(s)
  21. */
  22. async function open(
  23. options: OpenDialogOptions = {}
  24. ): Promise<string | string[]> {
  25. if (typeof options === 'object') {
  26. Object.freeze(options)
  27. }
  28. return await promisified({
  29. cmd: 'openDialog',
  30. options
  31. })
  32. }
  33. /**
  34. * @name save
  35. * @description Open a file/directory save dialog
  36. * @param {Object} [options]
  37. * @param {string} [options.filter]
  38. * @param {string} [options.defaultPath]
  39. * @returns {Promise<string>} Promise resolving to the select path
  40. */
  41. async function save(options: SaveDialogOptions = {}): Promise<string> {
  42. if (typeof options === 'object') {
  43. Object.freeze(options)
  44. }
  45. return await promisified({
  46. cmd: 'saveDialog',
  47. options
  48. })
  49. }
  50. export { open, save }