SaveFilePopupViewModel.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Microsoft.Win32;
  2. using PixiEditor.Helpers;
  3. using PixiEditor.Models.Enums;
  4. using PixiEditor.Models.IO;
  5. using System.IO;
  6. using System.Windows;
  7. namespace PixiEditor.ViewModels
  8. {
  9. internal class SaveFilePopupViewModel : ViewModelBase
  10. {
  11. private string _filePath;
  12. private FileType _chosenFormat;
  13. public SaveFilePopupViewModel()
  14. {
  15. CloseButtonCommand = new RelayCommand(CloseWindow);
  16. DragMoveCommand = new RelayCommand(MoveWindow);
  17. OkCommand = new RelayCommand(OkButton);
  18. }
  19. public RelayCommand CloseButtonCommand { get; set; }
  20. public RelayCommand DragMoveCommand { get; set; }
  21. public RelayCommand OkCommand { get; set; }
  22. public string FilePath
  23. {
  24. get => _filePath;
  25. set
  26. {
  27. if (_filePath != value)
  28. {
  29. _filePath = value;
  30. RaisePropertyChanged(nameof(FilePath));
  31. }
  32. }
  33. }
  34. public FileType ChosenFormat
  35. {
  36. get => _chosenFormat;
  37. set
  38. {
  39. if (_chosenFormat != value)
  40. {
  41. _chosenFormat = value;
  42. RaisePropertyChanged(nameof(ChosenFormat));
  43. }
  44. }
  45. }
  46. /// <summary>
  47. /// Command that handles Path choosing to save file
  48. /// </summary>
  49. private string ChoosePath()
  50. {
  51. SaveFileDialog path = new SaveFileDialog
  52. {
  53. Title = "Export path",
  54. CheckPathExists = true,
  55. Filter = SupportedFilesHelper.BuildSaveFilter(false),
  56. FilterIndex = 0
  57. };
  58. if (path.ShowDialog() == true)
  59. {
  60. if (string.IsNullOrEmpty(path.FileName) == false)
  61. {
  62. ChosenFormat = Exporter.ParseImageFormat(Path.GetExtension(path.SafeFileName));
  63. return path.FileName;
  64. }
  65. }
  66. return null;
  67. }
  68. private void CloseWindow(object parameter)
  69. {
  70. ((Window)parameter).DialogResult = false;
  71. CloseButton(parameter);
  72. }
  73. private void MoveWindow(object parameter)
  74. {
  75. DragMove(parameter);
  76. }
  77. private void OkButton(object parameter)
  78. {
  79. string path = ChoosePath();
  80. if (path == null)
  81. return;
  82. FilePath = path;
  83. ((Window)parameter).DialogResult = true;
  84. CloseButton(parameter);
  85. }
  86. }
  87. }