1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using Avalonia.Controls;
- using Avalonia.Input;
- using Avalonia.Platform.Storage;
- using Avalonia.Threading;
- using PicView.Avalonia.ViewModels;
- using PicView.Core.Config;
- using PicView.Core.Navigation;
- using System.Runtime.InteropServices;
- namespace PicView.Avalonia.Views.UC;
- public partial class ImageViewer : UserControl
- {
- public ImageViewer()
- {
- InitializeComponent();
- PointerWheelChanged += async (_, e) => await Main_OnPointerWheelChanged(e);
- // TODO add visual feedback for drag and drop
- //AddHandler(DragDrop.DragOverEvent, DragOver);
- AddHandler(DragDrop.DropEvent, Drop);
- Loaded += delegate
- {
- if (DataContext is not MainViewModel vm)
- return;
- vm.ImageChanged += (s, e) =>
- {
- if (SettingsHelper.Settings.Zoom.ScrollEnabled)
- {
- Dispatcher.UIThread.InvokeAsync(() =>
- {
- ImageScrollViewer.ScrollToHome();
- }, DispatcherPriority.Normal);
- }
- };
- };
- }
- private void Drop(object? sender, DragEventArgs e)
- {
- if (DataContext is not MainViewModel vm)
- return;
- var data = e.Data.GetFiles();
- if (data == null)
- {
- // TODO Handle URL and folder drops
- return;
- }
- var storageItems = data as IStorageItem[] ?? data.ToArray();
- var firstFile = storageItems.FirstOrDefault();
- var path = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? firstFile.Path.AbsolutePath : firstFile.Path.LocalPath;
- _ = vm.LoadPicFromString(path).ConfigureAwait(false);
- foreach (var file in storageItems.Skip(1))
- {
- // TODO Open each file in a new window if the setting to open in the same window is false
- }
- }
- private async Task Main_OnPointerWheelChanged(PointerWheelEventArgs e)
- {
- if (DataContext is not MainViewModel mainViewModel)
- return;
- if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- // TODO figure out how to do image navigation with gestures
- return;
- }
- if (e.Delta.Y < 0)
- {
- if (SettingsHelper.Settings.Zoom.HorizontalReverseScroll)
- {
- await mainViewModel.LoadNextPic(NavigateTo.Next);
- }
- else
- {
- await mainViewModel.LoadNextPic(NavigateTo.Previous);
- }
- }
- else
- {
- if (SettingsHelper.Settings.Zoom.HorizontalReverseScroll)
- {
- await mainViewModel.LoadNextPic(NavigateTo.Previous);
- }
- else
- {
- await mainViewModel.LoadNextPic(NavigateTo.Next);
- }
- }
- }
- }
|