Skip to content

Commit

Permalink
[Avalonia] Window improvements, mouse wheel events, startup, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Aug 6, 2024
1 parent feb760b commit 60c6b3d
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 47 deletions.
1 change: 0 additions & 1 deletion src/PicView.Avalonia/CustomControls/EditableTitlebar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using PicView.Avalonia.ViewModels;
using PicView.Core.FileHandling;
using PicView.Core.ImageDecoding;
using PicView.Core.Localization;

namespace PicView.Avalonia.CustomControls;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reactive.Linq;
using System.Reflection.Metadata.Ecma335;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
Expand Down
12 changes: 7 additions & 5 deletions src/PicView.Avalonia/ImageHandling/ImageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Svg.Skia;
using Avalonia.Threading;
using ImageMagick;
using PicView.Avalonia.Gallery;
using PicView.Avalonia.Navigation;
using PicView.Avalonia.UI;
using PicView.Avalonia.ViewModels;
using PicView.Core.Gallery;
using PicView.Core.ImageDecoding;
using PicView.Core.Navigation;

namespace PicView.Avalonia.ImageHandling;

Expand Down Expand Up @@ -216,4 +211,11 @@ public static void SetImage(object image, Image imageControl, ImageType imageTyp
_ => imageControl.Source
};
}

public static EXIFHelper.EXIFOrientation GetExifOrientation(MainViewModel vm)
{
var magickImage = new MagickImage();
magickImage.Ping(vm.FileInfo);
return EXIFHelper.GetImageOrientation(magickImage);
}
}
50 changes: 30 additions & 20 deletions src/PicView.Avalonia/Navigation/ExifHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public static void SetImageModel(ImageModel imageModel, MainViewModel vm)
switch (imageModel.EXIFOrientation.Value)
{
default:
vm.ScaleX = 1;
vm.RotationAngle = 0;
vm.GetOrientation = string.Empty;
break;

case EXIFHelper.EXIFOrientation.Normal:
vm.ScaleX = 1;
vm.RotationAngle = 0;
vm.GetOrientation = TranslationHelper.Translation.Normal;
Expand Down Expand Up @@ -125,29 +131,33 @@ public static void UpdateExifValues(ImageModel imageModel, MainViewModel vm)
vm.GetBitDepth = (magick.Depth * 3).ToString();
}

if (vm.DpiX == 0)
{
vm.GetPrintSizeCm = vm.GetPrintSizeInch = vm.GetSizeMp = vm.GetResolution = string.Empty;
}
else
if (vm is { PixelWidth: > 0, PixelHeight: > 0 })
{
var inchesWidth = vm.PixelWidth / vm.DpiX;
var inchesHeight = vm.PixelHeight / vm.DpiY;
vm.GetPrintSizeInch =
$"{inchesWidth.ToString("0.##", CultureInfo.CurrentCulture)} x {inchesHeight.ToString("0.##", CultureInfo.CurrentCulture)} {TranslationHelper.GetTranslation("Inches")}";

var cmWidth = vm.PixelWidth / vm.DpiX * 2.54;
var cmHeight = vm.PixelHeight / vm.DpiY * 2.54;
vm.GetPrintSizeCm =
$"{cmWidth.ToString("0.##", CultureInfo.CurrentCulture)} x {cmHeight.ToString("0.##", CultureInfo.CurrentCulture)} {TranslationHelper.GetTranslation("Centimeters")}";
vm.GetSizeMp =
$"{((float)vm.PixelHeight * vm.PixelWidth / 1000000).ToString("0.##", CultureInfo.CurrentCulture)} {TranslationHelper.Translation.MegaPixels}";

vm.GetResolution = $"{vm.DpiX} x {vm.DpiY} {TranslationHelper.GetTranslation("Dpi")}";
if (vm.DpiX == 0)
{
vm.GetPrintSizeCm = vm.GetPrintSizeInch = vm.GetSizeMp = vm.GetResolution = string.Empty;
}
else
{
var inchesWidth = vm.PixelWidth / vm.DpiX;
var inchesHeight = vm.PixelHeight / vm.DpiY;
vm.GetPrintSizeInch =
$"{inchesWidth.ToString("0.##", CultureInfo.CurrentCulture)} x {inchesHeight.ToString("0.##", CultureInfo.CurrentCulture)} {TranslationHelper.GetTranslation("Inches")}";

var cmWidth = vm.PixelWidth / vm.DpiX * 2.54;
var cmHeight = vm.PixelHeight / vm.DpiY * 2.54;
vm.GetPrintSizeCm =
$"{cmWidth.ToString("0.##", CultureInfo.CurrentCulture)} x {cmHeight.ToString("0.##", CultureInfo.CurrentCulture)} {TranslationHelper.GetTranslation("Centimeters")}";
vm.GetSizeMp =
$"{((float)vm.PixelHeight * vm.PixelWidth / 1000000).ToString("0.##", CultureInfo.CurrentCulture)} {TranslationHelper.Translation.MegaPixels}";

vm.GetResolution = $"{vm.DpiX} x {vm.DpiY} {TranslationHelper.GetTranslation("Dpi")}";
}
}

var firstRatio = vm.PixelWidth / TitleHelper.GCD(vm.PixelWidth, vm.PixelHeight);
var secondRatio = vm.PixelHeight / TitleHelper.GCD(vm.PixelWidth, vm.PixelHeight);
var gcd = TitleHelper.GCD(vm.PixelWidth, vm.PixelHeight);
var firstRatio = vm.PixelWidth / gcd;
var secondRatio = vm.PixelHeight / gcd;

if (firstRatio == secondRatio)
{
Expand Down
42 changes: 35 additions & 7 deletions src/PicView.Avalonia/Navigation/QuickLoad.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using PicView.Avalonia.Gallery;
using Avalonia.Threading;
using PicView.Avalonia.Gallery;
using PicView.Avalonia.ImageHandling;
using PicView.Avalonia.UI;
using PicView.Avalonia.ViewModels;
using PicView.Core.Config;
using PicView.Core.FileHandling;
using PicView.Core.ImageDecoding;

namespace PicView.Avalonia.Navigation;

Expand Down Expand Up @@ -44,19 +44,47 @@ async Task Load(bool isDirectory)
}
else
{
vm.FileInfo ??= fileInfo;
var imageModel = await ImageHelper.GetImageModelAsync(fileInfo).ConfigureAwait(false);
vm.ImageSource = imageModel.Image;
vm.ImageType = imageModel.ImageType;
WindowHelper.SetSize(imageModel.PixelWidth, imageModel.PixelHeight, imageModel.Rotation, vm);
vm.IsLoading = false;
imageModel.EXIFOrientation = ImageHelper.GetExifOrientation(vm);
ExifHandling.SetImageModel(imageModel, vm);
var changed = false; // Need to recalculate size if changed
if (vm.ScaleX != 1)
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
vm.ImageViewer.SetScaleX();
});
changed = true;
}
if (vm.RotationAngle != 0)
{
vm.ImageViewer.Rotate(vm.RotationAngle);
await Dispatcher.UIThread.InvokeAsync(() =>
{
vm.ImageViewer.Rotate(vm.RotationAngle);
});
changed = true;
}
vm.IsLoading = false;
if (changed)
{
WindowHelper.SetSize(imageModel.PixelWidth, imageModel.PixelHeight, imageModel.Rotation, vm);
}

ExifHandling.UpdateExifValues(imageModel, vm);
vm.ImageIterator = new ImageIterator(fileInfo, vm);
await vm.ImageIterator.AddAsync(vm.ImageIterator.Index, imageModel).ConfigureAwait(false);
var preloadValue = vm.ImageIterator.PreLoader.Get(vm.ImageIterator.Index, vm.ImageIterator.Pics);
vm.ImageIterator.UpdateSource(preloadValue);

SetTitleHelper.SetTitle(vm, imageModel);
vm.GetIndex = vm.ImageIterator.Index + 1;
if (SettingsHelper.Settings.WindowProperties.KeepCentered)
{
WindowHelper.CenterWindowOnScreen(false);
}

_ = vm.ImageIterator.AddAsync(vm.ImageIterator.Index, imageModel);
_ = vm.ImageIterator.Preload();
}

Expand Down
27 changes: 18 additions & 9 deletions src/PicView.Avalonia/UI/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,12 @@ public static async Task MaximizeRestore()
var vm = desktop.MainWindow.DataContext as MainViewModel;
if (desktop.MainWindow.WindowState is WindowState.Maximized or WindowState.FullScreen)
{
vm.SizeToContent = SettingsHelper.Settings.WindowProperties.AutoFit ?
SizeToContent.WidthAndHeight : SizeToContent.Manual;
// Restore
if (SettingsHelper.Settings.WindowProperties.AutoFit)
{
vm.SizeToContent = SizeToContent.WidthAndHeight;
vm.CanResize = false;
}
await Dispatcher.UIThread.InvokeAsync(() =>
desktop.MainWindow.WindowState = WindowState.Normal);
SettingsHelper.Settings.WindowProperties.Maximized = false;
Expand All @@ -276,12 +280,11 @@ await Dispatcher.UIThread.InvokeAsync(() =>
}
else
{
if (SettingsHelper.Settings.WindowProperties.AutoFit)
// Maximize
if (!SettingsHelper.Settings.WindowProperties.AutoFit)
{
Fullscreen(vm, desktop);
return;
SaveSize(desktop.MainWindow);
}
SaveSize(desktop.MainWindow);
Maximize();
}

Expand All @@ -306,8 +309,14 @@ void Set()
{
return;
}
var vm = desktop.MainWindow.DataContext as MainViewModel;
if (SettingsHelper.Settings.WindowProperties.AutoFit)
{
vm.SizeToContent = SizeToContent.Manual;
vm.CanResize = true;
}

desktop.MainWindow.WindowState = WindowState.FullScreen;
desktop.MainWindow.WindowState = WindowState.Maximized;
SetSize(desktop.MainWindow.DataContext as MainViewModel);
SettingsHelper.Settings.WindowProperties.Maximized = true;
}
Expand Down Expand Up @@ -376,7 +385,7 @@ public static void SetSize(MainViewModel vm)
return;
}
var preloadValue = vm.ImageIterator?.PreLoader.Get(vm.ImageIterator.Index, vm.ImageIterator.Pics);
SetSize(preloadValue?.ImageModel?.PixelWidth ?? (int)vm.ImageWidth, preloadValue?.ImageModel?.PixelHeight ?? (int)vm.ImageHeight, vm.RotationAngle, vm);
SetSize(preloadValue?.ImageModel?.PixelWidth ?? vm.ImageWidth, preloadValue?.ImageModel?.PixelHeight ?? vm.ImageHeight, vm.RotationAngle, vm);
}

public static void SetSize(double width, double height, double rotation, MainViewModel vm)
Expand Down Expand Up @@ -422,7 +431,7 @@ public static void SetSize(double width, double height, double rotation, MainVie
desktopMinHeight,
ImageSizeCalculationHelper.GetInterfaceSize(),
rotation,
0,
padding: SettingsHelper.Settings.ImageScaling.StretchImage ? 2 : 45,
screenSize.Scaling,
vm.TitlebarHeight,
vm.BottombarHeight,
Expand Down
16 changes: 14 additions & 2 deletions src/PicView.Avalonia/Views/ImageViewer.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private void TouchMagnifyEvent(object? sender, PointerDeltaEventArgs e)
ZoomTo(e.GetPosition(this), e.Delta.X > 0);
}

private async Task PreviewOnPointerWheelChanged(object? sender, PointerWheelEventArgs e)
public async Task PreviewOnPointerWheelChanged(object? sender, PointerWheelEventArgs e)
{
e.Handled = true;
await Main_OnPointerWheelChanged(e);
Expand Down Expand Up @@ -472,7 +472,7 @@ public void Rotate(bool clockWise)
}

WindowHelper.SetSize(vm);
MainImage.InvalidateVisual();
//MainImage.InvalidateVisual();
}

public void Rotate(double angle)
Expand Down Expand Up @@ -526,6 +526,18 @@ public void Flip(bool animate)
}
MainImage.InvalidateVisual();
}

public void SetScaleX()
{
if (DataContext is not MainViewModel vm)
return;
if (MainImage.Source is null)
{
return;
}
var flipTransform = new ScaleTransform(vm.ScaleX, 1);
MainImage.RenderTransform = flipTransform;
}

#endregion Rotation and Flip

Expand Down
5 changes: 5 additions & 0 deletions src/PicView.Avalonia/Views/UC/Buttons/AltClose.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public AltClose()
InitializeComponent();
Loaded += delegate
{
if (DataContext is not MainViewModel vm)
{
return;
}
HideInterfaceLogic.AddHoverButtonEvents(this, XButton, DataContext as MainViewModel);
PointerWheelChanged += async (_, e) => await vm.ImageViewer.PreviewOnPointerWheelChanged(this, e);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public ClickArrowLeft()
InitializeComponent();
Loaded += delegate
{
HideInterfaceLogic.AddHoverButtonEvents(this, PolyButton, DataContext as MainViewModel);
if (DataContext is not MainViewModel vm)
{
return;
}
HideInterfaceLogic.AddHoverButtonEvents(this, PolyButton, vm);
PointerWheelChanged += async (_, e) => await vm.ImageViewer.PreviewOnPointerWheelChanged(this, e);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public ClickArrowRight()
InitializeComponent();
Loaded += delegate
{
HideInterfaceLogic.AddHoverButtonEvents(this, PolyButton, DataContext as MainViewModel);
if (DataContext is not MainViewModel vm)
{
return;
}
HideInterfaceLogic.AddHoverButtonEvents(this, PolyButton, vm);
PointerWheelChanged += async (_, e) => await vm.ImageViewer.PreviewOnPointerWheelChanged(this, e);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public GalleryShortcut()
InitializeComponent();
Loaded += delegate
{
if (DataContext is not MainViewModel vm)
{
return;
}
HideInterfaceLogic.AddHoverButtonEvents(this, InnerButton, DataContext as MainViewModel);
PointerWheelChanged += async (_, e) => await vm.ImageViewer.PreviewOnPointerWheelChanged(this, e);
};
}
}

0 comments on commit 60c6b3d

Please sign in to comment.