dialog.ts 1.2 KB

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