ImageViewer.axaml.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.Platform.Storage;
  4. using Avalonia.Threading;
  5. using PicView.Avalonia.ViewModels;
  6. using PicView.Core.Config;
  7. using PicView.Core.Navigation;
  8. using System.Runtime.InteropServices;
  9. namespace PicView.Avalonia.Views.UC;
  10. public partial class ImageViewer : UserControl
  11. {
  12. public ImageViewer()
  13. {
  14. InitializeComponent();
  15. PointerWheelChanged += async (_, e) => await Main_OnPointerWheelChanged(e);
  16. // TODO add visual feedback for drag and drop
  17. //AddHandler(DragDrop.DragOverEvent, DragOver);
  18. AddHandler(DragDrop.DropEvent, Drop);
  19. Loaded += delegate
  20. {
  21. if (DataContext is not MainViewModel vm)
  22. return;
  23. vm.ImageChanged += (s, e) =>
  24. {
  25. if (SettingsHelper.Settings.Zoom.ScrollEnabled)
  26. {
  27. Dispatcher.UIThread.InvokeAsync(() =>
  28. {
  29. ImageScrollViewer.ScrollToHome();
  30. }, DispatcherPriority.Normal);
  31. }
  32. };
  33. };
  34. }
  35. private void Drop(object? sender, DragEventArgs e)
  36. {
  37. if (DataContext is not MainViewModel vm)
  38. return;
  39. var data = e.Data.GetFiles();
  40. if (data == null)
  41. {
  42. // TODO Handle URL and folder drops
  43. return;
  44. }
  45. var storageItems = data as IStorageItem[] ?? data.ToArray();
  46. var firstFile = storageItems.FirstOrDefault();
  47. var path = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? firstFile.Path.AbsolutePath : firstFile.Path.LocalPath;
  48. _ = vm.LoadPicFromString(path).ConfigureAwait(false);
  49. foreach (var file in storageItems.Skip(1))
  50. {
  51. // TODO Open each file in a new window if the setting to open in the same window is false
  52. }
  53. }
  54. private async Task Main_OnPointerWheelChanged(PointerWheelEventArgs e)
  55. {
  56. if (DataContext is not MainViewModel mainViewModel)
  57. return;
  58. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  59. {
  60. // TODO figure out how to do image navigation with gestures
  61. return;
  62. }
  63. if (e.Delta.Y < 0)
  64. {
  65. if (SettingsHelper.Settings.Zoom.HorizontalReverseScroll)
  66. {
  67. await mainViewModel.LoadNextPic(NavigateTo.Next);
  68. }
  69. else
  70. {
  71. await mainViewModel.LoadNextPic(NavigateTo.Previous);
  72. }
  73. }
  74. else
  75. {
  76. if (SettingsHelper.Settings.Zoom.HorizontalReverseScroll)
  77. {
  78. await mainViewModel.LoadNextPic(NavigateTo.Previous);
  79. }
  80. else
  81. {
  82. await mainViewModel.LoadNextPic(NavigateTo.Next);
  83. }
  84. }
  85. }
  86. }