MainView.axaml.cs 7.1 KB

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