123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System.Runtime.InteropServices;
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Input;
- using Avalonia.Layout;
- using Avalonia.Media;
- using Avalonia.Threading;
- using PicView.Avalonia.Converters;
- using PicView.Avalonia.Crop;
- using PicView.Avalonia.DragAndDrop;
- using PicView.Avalonia.Functions;
- using PicView.Avalonia.Input;
- using PicView.Avalonia.UI;
- using PicView.Avalonia.UI.FileHistory;
- using PicView.Avalonia.ViewModels;
- using PicView.Avalonia.WindowBehavior;
- using PicView.Core.FileHistory;
- namespace PicView.Avalonia.Views;
- public partial class MainView : UserControl
- {
- public FileHistoryMenuController? FileHistoryMenuController;
-
- public MainView()
- {
- InitializeComponent();
- if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {
- // TODO: Add macOS support
- PrintMenuItem.IsVisible = false;
-
- // Move alt hover to left side on macOS and switch button order
- AltButtonsPanel.HorizontalAlignment = HorizontalAlignment.Left;
- AltButtonsPanel.Children.Move(AltButtonsPanel.Children.IndexOf(AltClose),0);
- AltButtonsPanel.Children.Move(AltButtonsPanel.Children.IndexOf(AltMinimize),2);
- AltMinimize.RenderTransform = new ScaleTransform{ScaleX = -1};
- }
- if (!Settings.Theme.Dark && !Settings.Theme.GlassTheme)
- {
- if (!Application.Current.TryGetResource("MainTextColor",
- Application.Current.RequestedThemeVariant, out var mainTextColor) ||
- !Application.Current.TryGetResource("SecondaryTextColor", Application.Current.RequestedThemeVariant, out var secondaryTextColor))
- {
- return;
- }
- if (mainTextColor is not Color color || secondaryTextColor is not Color secondaryColor)
- {
- return;
- }
- var brush = new SolidColorBrush(color);
- var secondaryBrush = new SolidColorBrush(secondaryColor);
- HistoryClearButton.PointerEntered += delegate
- {
- HistoryClearTextBlock.Foreground = secondaryBrush;
- HistoryClearPath.Fill = secondaryBrush;
- };
- HistoryClearButton.PointerExited += delegate
- {
- HistoryClearTextBlock.Foreground = brush;
- HistoryClearPath.Fill = brush;
- };
- HistoryFileButton.PointerEntered += delegate
- {
- HistoryFileNameTextBlock.Foreground = secondaryBrush;
- };
- HistoryFileButton.PointerExited += delegate
- {
- HistoryFileNameTextBlock.Foreground = brush;
- };
- }
- Loaded += delegate
- {
- AddHandler(DragDrop.DragEnterEvent, DragEnter);
- AddHandler(DragDrop.DragLeaveEvent, DragLeave);
- AddHandler(DragDrop.DropEvent, Drop);
- GotFocus += CloseTitlebarIfOpen;
- LostFocus += HandleLostFocus;
- PointerPressed += PointerPressedBehavior;
- MainContextMenu.Opened += async (sender, args) => await OnMainContextMenuOpened(sender, args);
-
- if (!FileHistoryManager.IsSortingDescending)
- {
- if (Application.Current.TryGetResource("SortAscImage",
- Application.Current.RequestedThemeVariant, out var sortAscImage))
- {
- HistorySortButton.Icon = sortAscImage as DrawingImage;
- }
- }
-
- if (DataContext is not MainViewModel vm)
- {
- return;
- }
- // Initialize the history menu controller
- // TODO: rewrite FileHistory to MVVM
- FileHistoryMenuController = new FileHistoryMenuController(RecentFilesCM, HistorySortButton, HistoryClearButton, HistoryFileButton, vm);
- HistoryFileButton.Click += async delegate
- {
- await FunctionsMapper.ShowRecentHistoryFile();
- };
- // Setup hover fade buttons
- _ = new HoverFadeButtonHandler(ClickArrowRight, vm, ClickArrowRight.PolyButton);
- _ = new HoverFadeButtonHandler(ClickArrowLeft, vm, ClickArrowLeft.PolyButton);
- _ = new HoverFadeButtonHandler(AltButtonsPanel, vm);
-
- PointerWheelChanged += async (_, e) => await ImageViewer.PreviewOnPointerWheelChanged(this, e);
- };
- }
- private void PointerPressedBehavior(object? sender, PointerPressedEventArgs e)
- {
- CloseTitlebarIfOpen(sender, e);
- if (MainKeyboardShortcuts.ShiftDown && !CropFunctions.IsCropping)
- {
- var hostWindow = (Window)VisualRoot!;
- WindowFunctions.WindowDragBehavior(hostWindow, e);
- }
-
- DragAndDropHelper.RemoveDragDropView();
- }
-
- private void CloseTitlebarIfOpen(object? sender, EventArgs e)
- {
- if (DataContext is not MainViewModel vm)
- {
- return;
- }
- if (!vm.MainWindow.IsEditableTitlebarOpen.Value)
- {
- return;
- }
- vm.MainWindow.IsEditableTitlebarOpen.Value = false;
- MainKeyboardShortcuts.IsKeysEnabled = true;
- Focus();
- }
-
- private static void HandleLostFocus(object? sender, EventArgs e)
- {
- DragAndDropHelper.RemoveDragDropView();
- }
- private async Task OnMainContextMenuOpened(object? sender, EventArgs e)
- {
- if (DataContext is not MainViewModel vm)
- {
- return;
- }
-
- await Dispatcher.UIThread.InvokeAsync(() =>
- {
- CropMenuItem.IsEnabled = CropFunctions.DetermineIfShouldBeEnabled(vm);
- ConversionHelper.DetermineIfOptimizeImageShouldBeEnabled(vm);
- // Set source for ChangeCtrlZoomImage
- if (!Application.Current.TryGetResource("ScanEyeImage", Application.Current.RequestedThemeVariant, out var scanEyeImage))
- {
- return;
- }
- if (!Application.Current.TryGetResource("LeftRightArrowsImage", Application.Current.RequestedThemeVariant, out var leftRightArrowsImage))
- {
- return;
- }
- var isNavigatingWithCtrl = Settings.Zoom.CtrlZoom;
- vm.MainWindow.ChangeCtrlZoomImage.Value = isNavigatingWithCtrl ? leftRightArrowsImage as DrawingImage : scanEyeImage as DrawingImage;
- });
-
- // Update file history menu items in Dispatcher with low priority to avoid slowdown
- await Dispatcher.UIThread.InvokeAsync(() => FileHistoryMenuController?.UpdateFileHistoryMenu());
- }
- private async Task Drop(object? sender, DragEventArgs e)
- {
- if (DataContext is not MainViewModel vm)
- {
- return;
- }
- await DragAndDropHelper.Drop(e, vm);
- }
-
- private async Task DragEnter(object? sender, DragEventArgs e)
- {
- if (DataContext is not MainViewModel vm)
- return;
- await DragAndDropHelper.DragEnter(e, vm, this);
- }
-
- private void DragLeave(object? sender, DragEventArgs e)
- {
- DragAndDropHelper.DragLeave(e, this);
- }
- }
|