MainView.axaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System.Runtime.InteropServices;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Input;
  5. using Avalonia.Media;
  6. using Avalonia.Threading;
  7. using PicView.Avalonia.Converters;
  8. using PicView.Avalonia.Crop;
  9. using PicView.Avalonia.DragAndDrop;
  10. using PicView.Avalonia.Functions;
  11. using PicView.Avalonia.Input;
  12. using PicView.Avalonia.UI;
  13. using PicView.Avalonia.UI.FileHistory;
  14. using PicView.Avalonia.ViewModels;
  15. using PicView.Avalonia.WindowBehavior;
  16. using PicView.Core.FileHistory;
  17. namespace PicView.Avalonia.Views;
  18. public partial class MainView : UserControl
  19. {
  20. public FileHistoryMenuController? FileHistoryMenuController;
  21. public MainView()
  22. {
  23. InitializeComponent();
  24. if (!Settings.Theme.Dark && !Settings.Theme.GlassTheme)
  25. {
  26. if (!Application.Current.TryGetResource("MainTextColor",
  27. Application.Current.RequestedThemeVariant, out var mainTextColor) ||
  28. !Application.Current.TryGetResource("SecondaryTextColor", Application.Current.RequestedThemeVariant, out var secondaryTextColor))
  29. {
  30. return;
  31. }
  32. if (mainTextColor is not Color color || secondaryTextColor is not Color secondaryColor)
  33. {
  34. return;
  35. }
  36. var brush = new SolidColorBrush(color);
  37. var secondaryBrush = new SolidColorBrush(secondaryColor);
  38. HistoryClearButton.PointerEntered += delegate
  39. {
  40. HistoryClearTextBlock.Foreground = secondaryBrush;
  41. HistoryClearPath.Fill = secondaryBrush;
  42. };
  43. HistoryClearButton.PointerExited += delegate
  44. {
  45. HistoryClearTextBlock.Foreground = brush;
  46. HistoryClearPath.Fill = brush;
  47. };
  48. HistoryFileButton.PointerEntered += delegate
  49. {
  50. HistoryFileNameTextBlock.Foreground = secondaryBrush;
  51. HistoryFileButtonPath.Fill = secondaryBrush;
  52. };
  53. HistoryFileButton.PointerExited += delegate
  54. {
  55. HistoryFileNameTextBlock.Foreground = brush;
  56. HistoryFileButtonPath.Fill = brush;
  57. };
  58. }
  59. Loaded += delegate
  60. {
  61. AddHandler(DragDrop.DragEnterEvent, DragEnter);
  62. AddHandler(DragDrop.DragLeaveEvent, DragLeave);
  63. AddHandler(DragDrop.DropEvent, Drop);
  64. GotFocus += CloseTitlebarIfOpen;
  65. LostFocus += HandleLostFocus;
  66. PointerPressed += PointerPressedBehavior;
  67. MainContextMenu.Opened += async (sender, args) => await OnMainContextMenuOpened(sender, args);
  68. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  69. {
  70. WallpaperMenuItem.IsVisible = false;
  71. OpenWithMenuItem.IsVisible = false;
  72. }
  73. else
  74. {
  75. MacOSWallpaperMenuItem.IsVisible = false;
  76. }
  77. if (!FileHistoryManager.IsSortingDescending)
  78. {
  79. if (Application.Current.TryGetResource("SortAscImage",
  80. Application.Current.RequestedThemeVariant, out var sortAscImage))
  81. {
  82. HistorySortButton.Icon = sortAscImage as DrawingImage;
  83. }
  84. }
  85. if (DataContext is not MainViewModel vm)
  86. {
  87. return;
  88. }
  89. // Initialize the history menu controller
  90. // TODO: rewrite FileHistory to MVVM
  91. FileHistoryMenuController = new FileHistoryMenuController(RecentFilesCM, HistorySortButton, HistoryClearButton, vm);
  92. HistoryFileButton.Click += async delegate
  93. {
  94. await FunctionsMapper.ShowRecentHistoryFile();
  95. };
  96. HideInterfaceLogic.AddHoverButtonEvents(AltButtonsPanel, vm);
  97. PointerWheelChanged += async (_, e) => await vm.ImageViewer.PreviewOnPointerWheelChanged(this, e);
  98. };
  99. }
  100. private void PointerPressedBehavior(object? sender, PointerPressedEventArgs e)
  101. {
  102. CloseTitlebarIfOpen(sender, e);
  103. if (MainKeyboardShortcuts.ShiftDown && !CropFunctions.IsCropping)
  104. {
  105. var hostWindow = (Window)VisualRoot!;
  106. WindowFunctions.WindowDragBehavior(hostWindow, e);
  107. }
  108. DragAndDropHelper.RemoveDragDropView();
  109. }
  110. private void CloseTitlebarIfOpen(object? sender, EventArgs e)
  111. {
  112. if (DataContext is not MainViewModel vm)
  113. {
  114. return;
  115. }
  116. if (!vm.IsEditableTitlebarOpen)
  117. {
  118. return;
  119. }
  120. vm.IsEditableTitlebarOpen = false;
  121. MainKeyboardShortcuts.IsKeysEnabled = true;
  122. Focus();
  123. }
  124. private static void HandleLostFocus(object? sender, EventArgs e)
  125. {
  126. DragAndDropHelper.RemoveDragDropView();
  127. }
  128. private async Task OnMainContextMenuOpened(object? sender, EventArgs e)
  129. {
  130. if (DataContext is not MainViewModel vm)
  131. {
  132. return;
  133. }
  134. await Dispatcher.UIThread.InvokeAsync(() =>
  135. {
  136. CropMenuItem.IsEnabled = CropFunctions.DetermineIfShouldBeEnabled(vm);
  137. ConversionHelper.DetermineIfOptimizeImageShouldBeEnabled(vm);
  138. // Set source for ChangeCtrlZoomImage
  139. if (!Application.Current.TryGetResource("ScanEyeImage", Application.Current.RequestedThemeVariant, out var scanEyeImage))
  140. {
  141. return;
  142. }
  143. if (!Application.Current.TryGetResource("LeftRightArrowsImage", Application.Current.RequestedThemeVariant, out var leftRightArrowsImage))
  144. {
  145. return;
  146. }
  147. var isNavigatingWithCtrl = Settings.Zoom.CtrlZoom;
  148. vm.ChangeCtrlZoomImage = isNavigatingWithCtrl ? leftRightArrowsImage as DrawingImage : scanEyeImage as DrawingImage;
  149. });
  150. // Update file history menu items in Dispatcher with low priority to avoid slowdown
  151. await Dispatcher.UIThread.InvokeAsync(() => FileHistoryMenuController?.UpdateFileHistoryMenu());
  152. }
  153. private async Task Drop(object? sender, DragEventArgs e)
  154. {
  155. if (DataContext is not MainViewModel vm)
  156. {
  157. return;
  158. }
  159. await DragAndDropHelper.Drop(e, vm);
  160. }
  161. private async Task DragEnter(object? sender, DragEventArgs e)
  162. {
  163. if (DataContext is not MainViewModel vm)
  164. return;
  165. await DragAndDropHelper.DragEnter(e, vm, this);
  166. }
  167. private void DragLeave(object? sender, DragEventArgs e)
  168. {
  169. DragAndDropHelper.DragLeave(e, this);
  170. }
  171. }