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