SaveFilePopupViewModel.cs 2.8 KB

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