ViewportWindowViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.ComponentModel;
  2. using System.Threading.Tasks;
  3. using Avalonia.Threading;
  4. using CommunityToolkit.Mvvm.Input;
  5. using PixiDocks.Core.Docking;
  6. using PixiDocks.Core.Docking.Events;
  7. using PixiEditor.AvaloniaUI.Helpers.UI;
  8. using PixiEditor.AvaloniaUI.Models.DocumentModels;
  9. using PixiEditor.AvaloniaUI.ViewModels.Dock;
  10. using PixiEditor.AvaloniaUI.ViewModels.Document;
  11. using PixiEditor.AvaloniaUI.Views.Visuals;
  12. using PixiEditor.DrawingApi.Core.Numerics;
  13. using PixiEditor.Numerics;
  14. namespace PixiEditor.AvaloniaUI.ViewModels.SubViewModels;
  15. #nullable enable
  16. internal class ViewportWindowViewModel : SubViewModel<WindowViewModel>, IDockableContent, IDockableCloseEvents, IDockableSelectionEvents
  17. {
  18. public DocumentViewModel Document { get; }
  19. public ExecutionTrigger<VecI> CenterViewportTrigger { get; } = new ExecutionTrigger<VecI>();
  20. public ExecutionTrigger<double> ZoomViewportTrigger { get; } = new ExecutionTrigger<double>();
  21. public string Index => _index;
  22. public string Id => id;
  23. public string Title => $"{Document.FileName}{Index}";
  24. public bool CanFloat => true;
  25. public bool CanClose => true;
  26. public DocumentTabCustomizationSettings TabCustomizationSettings { get; } = new DocumentTabCustomizationSettings(showCloseButton: true);
  27. TabCustomizationSettings IDockableContent.TabCustomizationSettings => TabCustomizationSettings;
  28. private bool _closeRequested;
  29. private string _index = "";
  30. private bool _flipX;
  31. private string id = Guid.NewGuid().ToString();
  32. public bool FlipX
  33. {
  34. get => _flipX;
  35. set
  36. {
  37. _flipX = value;
  38. OnPropertyChanged(nameof(FlipX));
  39. }
  40. }
  41. private bool _flipY;
  42. public bool FlipY
  43. {
  44. get => _flipY;
  45. set
  46. {
  47. _flipY = value;
  48. OnPropertyChanged(nameof(FlipY));
  49. }
  50. }
  51. private ViewportColorChannels _channels = ViewportColorChannels.Default;
  52. public ViewportColorChannels Channels
  53. {
  54. get => _channels;
  55. set => SetProperty(ref _channels, value);
  56. }
  57. public void IndexChanged()
  58. {
  59. _index = Owner.CalculateViewportIndex(this) ?? "";
  60. OnPropertyChanged(nameof(Index));
  61. OnPropertyChanged(nameof(Title));
  62. OnPropertyChanged(nameof(Id));
  63. }
  64. public ViewportWindowViewModel(WindowViewModel owner, DocumentViewModel document) : base(owner)
  65. {
  66. Document = document;
  67. Document.SizeChanged += DocumentOnSizeChanged;
  68. Document.PropertyChanged += DocumentOnPropertyChanged;
  69. TabCustomizationSettings.Icon = new SurfaceImage(Document.PreviewSurface);
  70. }
  71. private void DocumentOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
  72. {
  73. if (e.PropertyName == nameof(DocumentViewModel.FileName))
  74. {
  75. OnPropertyChanged(nameof(Title));
  76. }
  77. else if (e.PropertyName == nameof(DocumentViewModel.PreviewSurface))
  78. {
  79. TabCustomizationSettings.Icon = new SurfaceImage(Document.PreviewSurface);
  80. }
  81. else if (e.PropertyName == nameof(DocumentViewModel.AllChangesSaved))
  82. {
  83. TabCustomizationSettings.IsSaved = Document.AllChangesSaved;
  84. }
  85. }
  86. ~ViewportWindowViewModel()
  87. {
  88. Document.SizeChanged -= DocumentOnSizeChanged;
  89. Document.PropertyChanged -= DocumentOnPropertyChanged;
  90. }
  91. private void DocumentOnSizeChanged(object? sender, DocumentSizeChangedEventArgs e)
  92. {
  93. TabCustomizationSettings.Icon = new SurfaceImage(Document.PreviewSurface);
  94. OnPropertyChanged(nameof(TabCustomizationSettings));
  95. }
  96. bool IDockableCloseEvents.OnClose()
  97. {
  98. if (!_closeRequested)
  99. {
  100. Task.Run(async () =>
  101. {
  102. await Dispatcher.UIThread.InvokeAsync(async () =>
  103. {
  104. _closeRequested =
  105. await Owner.OnViewportWindowCloseButtonPressed(this);
  106. });
  107. });
  108. }
  109. return _closeRequested;
  110. }
  111. void IDockableSelectionEvents.OnSelected()
  112. {
  113. Owner.ActiveWindow = this;
  114. }
  115. void IDockableSelectionEvents.OnDeselected()
  116. {
  117. }
  118. }