1
0

dialogs.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "github.com/wailsapp/wails/v3/pkg/application"
  4. )
  5. // dialogs implements app.Dialogs with Wails' native file dialogs.
  6. type dialogs struct {
  7. a *application.App
  8. }
  9. // cancelled maps a dialog the user closed to "", nil. On Windows Wails
  10. // returns an error with this text instead of an empty path.
  11. func cancelled(path string, err error) (string, error) {
  12. if err != nil && err.Error() == "cancelled by user" {
  13. return "", nil
  14. }
  15. return path, err
  16. }
  17. func (d *dialogs) OpenWAV() (string, error) {
  18. return cancelled(d.a.Dialog.OpenFile().
  19. SetTitle("Choose a WAV file").
  20. CanChooseFiles(true).
  21. AddFilter("WAV files", "*.wav").
  22. PromptForSingleSelection())
  23. }
  24. func (d *dialogs) OpenProject() (string, error) {
  25. return cancelled(d.a.Dialog.OpenFile().
  26. SetTitle("Load a project").
  27. CanChooseFiles(true).
  28. AddFilter("MPC Sample projects", "*.xpj").
  29. PromptForSingleSelection())
  30. }
  31. func (d *dialogs) ChooseFolder() (string, error) {
  32. return cancelled(d.a.Dialog.OpenFile().
  33. SetTitle("Export to folder").
  34. CanChooseFiles(false).
  35. CanChooseDirectories(true).
  36. CanCreateDirectories(true).
  37. PromptForSingleSelection())
  38. }
  39. func (d *dialogs) SaveZip(defaultName string) (string, error) {
  40. return cancelled(d.a.Dialog.SaveFile().
  41. SetFilename(defaultName).
  42. AddFilter("Zip files", "*.zip").
  43. PromptForSingleSelection())
  44. }