MainWindow.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using Microsoft.Extensions.DependencyInjection;
  2. using PixiEditor.Helpers.Extensions;
  3. using PixiEditor.Models.DataHolders;
  4. using PixiEditor.Models.UserPreferences;
  5. using PixiEditor.ViewModels;
  6. using PixiEditor.Views.Dialogs;
  7. using System;
  8. using System.ComponentModel;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Windows;
  12. using System.Windows.Input;
  13. using System.Windows.Interop;
  14. using System.Windows.Media.Imaging;
  15. namespace PixiEditor
  16. {
  17. /// <summary>
  18. /// Interaction logic for MainWindow.xaml.
  19. /// </summary>
  20. public partial class MainWindow : Window
  21. {
  22. private static WriteableBitmap pixiEditorLogo;
  23. private readonly IPreferences preferences;
  24. public new ViewModelMain DataContext { get => (ViewModelMain)base.DataContext; set => base.DataContext = value; }
  25. public MainWindow()
  26. {
  27. IServiceProvider services = new ServiceCollection()
  28. .AddPixiEditor()
  29. .BuildServiceProvider();
  30. preferences = services.GetRequiredService<IPreferences>();
  31. DataContext = services.GetRequiredService<ViewModelMain>();
  32. DataContext.Setup(services);
  33. InitializeComponent();
  34. pixiEditorLogo = BitmapFactory.FromResource(@"/Images/PixiEditorLogo.png");
  35. UpdateWindowChromeBorderThickness();
  36. StateChanged += MainWindow_StateChanged;
  37. Activated += MainWindow_Activated;
  38. DataContext.CloseAction = Close;
  39. Application.Current.ShutdownMode = ShutdownMode.OnMainWindowClose;
  40. DataContext.BitmapManager.DocumentChanged += BitmapManager_DocumentChanged;
  41. preferences.AddCallback<bool>("ImagePreviewInTaskbar", x =>
  42. {
  43. if (x)
  44. {
  45. UpdateTaskbarIcon(DataContext.BitmapManager.ActiveDocument);
  46. }
  47. else
  48. {
  49. UpdateTaskbarIcon(null);
  50. }
  51. });
  52. }
  53. protected override void OnClosing(CancelEventArgs e)
  54. {
  55. DataContext.CloseWindow(e);
  56. DataContext.DiscordViewModel.Dispose();
  57. }
  58. protected override void OnSourceInitialized(EventArgs e)
  59. {
  60. base.OnSourceInitialized(e);
  61. ((HwndSource)PresentationSource.FromVisual(this)).AddHook(Helpers.WindowSizeHelper.SetMaxSizeHook);
  62. }
  63. [Conditional("RELEASE")]
  64. private static void CloseHelloThereIfRelease()
  65. {
  66. Application.Current.Windows.OfType<HelloTherePopup>().ToList().ForEach(x => { if (!x.IsClosing) x.Close(); });
  67. }
  68. private void BitmapManager_DocumentChanged(object sender, Models.Events.DocumentChangedEventArgs e)
  69. {
  70. if (preferences.GetPreference("ImagePreviewInTaskbar", false))
  71. {
  72. UpdateTaskbarIcon(e.NewDocument);
  73. }
  74. }
  75. private void UpdateTaskbarIcon(Document document)
  76. {
  77. if (document?.PreviewImage == null)
  78. {
  79. Icon = pixiEditorLogo;
  80. return;
  81. }
  82. var previewCopy = document.PreviewImage.Clone()
  83. .Resize(512, 512, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
  84. previewCopy.Blit(new Rect(256, 256, 256, 256), pixiEditorLogo, new Rect(0, 0, 512, 512));
  85. Icon = previewCopy;
  86. }
  87. private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  88. {
  89. e.CanExecute = true;
  90. }
  91. private void CommandBinding_Executed_Minimize(object sender, ExecutedRoutedEventArgs e)
  92. {
  93. SystemCommands.MinimizeWindow(this);
  94. }
  95. private void CommandBinding_Executed_Maximize(object sender, ExecutedRoutedEventArgs e)
  96. {
  97. SystemCommands.MaximizeWindow(this);
  98. }
  99. private void CommandBinding_Executed_Restore(object sender, ExecutedRoutedEventArgs e)
  100. {
  101. SystemCommands.RestoreWindow(this);
  102. }
  103. private void CommandBinding_Executed_Close(object sender, ExecutedRoutedEventArgs e)
  104. {
  105. SystemCommands.CloseWindow(this);
  106. }
  107. private void MainWindow_Activated(object sender, EventArgs e)
  108. {
  109. CloseHelloThereIfRelease();
  110. }
  111. private void UpdateWindowChromeBorderThickness()
  112. {
  113. if (WindowState == WindowState.Maximized)
  114. {
  115. windowsChrome.ResizeBorderThickness = new Thickness(0, 0, 0, 0);
  116. }
  117. else
  118. {
  119. windowsChrome.ResizeBorderThickness = new Thickness(5, 5, 5, 5);
  120. }
  121. }
  122. private void MainWindow_StateChanged(object sender, EventArgs e)
  123. {
  124. UpdateWindowChromeBorderThickness();
  125. if (WindowState == WindowState.Maximized)
  126. {
  127. RestoreButton.Visibility = Visibility.Visible;
  128. MaximizeButton.Visibility = Visibility.Collapsed;
  129. }
  130. else
  131. {
  132. RestoreButton.Visibility = Visibility.Collapsed;
  133. MaximizeButton.Visibility = Visibility.Visible;
  134. }
  135. }
  136. private void MainWindow_Initialized(object sender, EventArgs e)
  137. {
  138. AppDomain.CurrentDomain.UnhandledException += (sender, e) => Helpers.CrashHelper.SaveCrashInfo((Exception)e.ExceptionObject);
  139. }
  140. private void MainWindow_Drop(object sender, DragEventArgs e)
  141. {
  142. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  143. {
  144. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  145. DataContext.FileSubViewModel.Open(files[0]);
  146. }
  147. }
  148. }
  149. }