MainView.axaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System.Diagnostics;
  2. using System.Runtime.InteropServices;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Input;
  6. using Avalonia.Interactivity;
  7. using Avalonia.Media;
  8. using PicView.Avalonia.Crop;
  9. using PicView.Avalonia.DragAndDrop;
  10. using PicView.Avalonia.Input;
  11. using PicView.Avalonia.Navigation;
  12. using PicView.Avalonia.UI;
  13. using PicView.Avalonia.ViewModels;
  14. using PicView.Avalonia.WindowBehavior;
  15. using PicView.Core.Config;
  16. using PicView.Core.Extensions;
  17. namespace PicView.Avalonia.Views;
  18. public partial class MainView : UserControl
  19. {
  20. public MainView()
  21. {
  22. InitializeComponent();
  23. Loaded += delegate
  24. {
  25. AddHandler(DragDrop.DragEnterEvent, DragEnter);
  26. AddHandler(DragDrop.DragLeaveEvent, DragLeave);
  27. AddHandler(DragDrop.DropEvent, Drop);
  28. GotFocus += CloseTitlebarIfOpen;
  29. LostFocus += HandleLostFocus;
  30. PointerPressed += PointerPressedBehavior;
  31. MainContextMenu.Opened += OnMainContextMenuOpened;
  32. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  33. {
  34. // TODO Implement setting as wallpaper for macOS
  35. WallpaperMenuItem.IsEnabled = false;
  36. }
  37. if (DataContext is not MainViewModel vm)
  38. {
  39. return;
  40. }
  41. HideInterfaceLogic.AddHoverButtonEvents(AltButtonsPanel, vm);
  42. PointerWheelChanged += async (_, e) => await vm.ImageViewer.PreviewOnPointerWheelChanged(this, e);
  43. };
  44. }
  45. private void PointerPressedBehavior(object? sender, PointerPressedEventArgs e)
  46. {
  47. CloseTitlebarIfOpen(sender, e);
  48. if (MainKeyboardShortcuts.ShiftDown && !CropFunctions.IsCropping)
  49. {
  50. var hostWindow = (Window)VisualRoot!;
  51. WindowFunctions.WindowDragBehavior(hostWindow, e);
  52. }
  53. DragAndDropHelper.RemoveDragDropView();
  54. }
  55. private void CloseTitlebarIfOpen(object? sender, EventArgs e)
  56. {
  57. if (DataContext is not MainViewModel vm)
  58. {
  59. return;
  60. }
  61. if (vm.IsEditableTitlebarOpen)
  62. {
  63. vm.IsEditableTitlebarOpen = false;
  64. }
  65. }
  66. private void HandleLostFocus(object? sender, EventArgs e)
  67. {
  68. DragAndDropHelper.RemoveDragDropView();
  69. }
  70. private void OnMainContextMenuOpened(object? sender, EventArgs e)
  71. {
  72. if (DataContext is not MainViewModel vm)
  73. {
  74. return;
  75. }
  76. CropMenuItem.IsEnabled = CropFunctions.DetermineIfShouldBeEnabled(vm);
  77. // Set source for ChangeCtrlZoomImage
  78. // TODO should probably be refactored inside a command (It doesn't update the UI in the zoom view, so should be made into a command)
  79. if (!Application.Current.TryGetResource("ScanEyeImage", Application.Current.RequestedThemeVariant, out var scanEyeImage ))
  80. {
  81. return;
  82. }
  83. if (!Application.Current.TryGetResource("LeftRightArrowsImage", Application.Current.RequestedThemeVariant, out var leftRightArrowsImage ))
  84. {
  85. return;
  86. }
  87. var isNavigatingWithCtrl = SettingsHelper.Settings.Zoom.CtrlZoom;
  88. vm.ChangeCtrlZoomImage = isNavigatingWithCtrl ? leftRightArrowsImage as DrawingImage : scanEyeImage as DrawingImage;
  89. // Update file history
  90. var count = FileHistoryNavigation.GetCount();
  91. if (RecentFilesCM.Items.Count < count)
  92. {
  93. for (var i = RecentFilesCM.Items.Count; i < count; i++)
  94. {
  95. AddOrReplaceMenuItem(i, vm, isReplace: false);
  96. }
  97. }
  98. else
  99. {
  100. for (var i = 0; i < count; i++)
  101. {
  102. AddOrReplaceMenuItem(i, vm, isReplace: true);
  103. }
  104. }
  105. }
  106. private void AddOrReplaceMenuItem(int index, MainViewModel vm, bool isReplace)
  107. {
  108. if (!Application.Current.TryGetResource("SecondaryAccentColor", Application.Current.RequestedThemeVariant, out var secondaryAccentColor))
  109. {
  110. return;
  111. }
  112. try
  113. {
  114. #if DEBUG
  115. Debug.Assert(secondaryAccentColor != null, nameof(secondaryAccentColor) + " != null");
  116. #endif
  117. var secondaryAccentBrush = (SolidColorBrush)secondaryAccentColor;
  118. var fileLocation = FileHistoryNavigation.GetFileLocation(index);
  119. var selected = vm.ImageIterator?.CurrentIndex == vm.ImageIterator?.ImagePaths.IndexOf(fileLocation);
  120. var header = Path.GetFileNameWithoutExtension(fileLocation);
  121. header = header.Length > 60 ? header.Shorten(60) : header;
  122. var item = new MenuItem
  123. {
  124. Header = header,
  125. };
  126. if (selected)
  127. {
  128. item.Foreground = secondaryAccentBrush;
  129. }
  130. item.Click += async delegate
  131. {
  132. await NavigationHelper.LoadPicFromStringAsync(fileLocation, vm).ConfigureAwait(false);
  133. };
  134. ToolTip.SetTip(item, fileLocation);
  135. if (isReplace)
  136. {
  137. RecentFilesCM.Items[index] = item;
  138. }
  139. else
  140. {
  141. RecentFilesCM.Items.Insert(index, item);
  142. }
  143. }
  144. #if DEBUG
  145. catch (Exception e)
  146. {
  147. Console.WriteLine(e);
  148. }
  149. #else
  150. catch (Exception){}
  151. #endif
  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. private void SetWallpaperClick(object? sender, RoutedEventArgs e)
  172. {
  173. Task.Run(FunctionsHelper.SetAsWallpaper);
  174. MainContextMenu.Close();
  175. }
  176. }