Skip to content

Commit

Permalink
[Avalonia] Side by side scrolling calculation fixes #89
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Sep 6, 2024
1 parent 34329bc commit 80c01c1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
5 changes: 5 additions & 0 deletions src/PicView.Avalonia.Win32/Views/WinMainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ private void Control_OnSizeChanged(object? sender, SizeChangedEventArgs e)
{
return;
}

if (SettingsHelper.Settings.WindowProperties.AutoFit)
{
return;
}
var wm = (MainViewModel)DataContext;
WindowHelper.SetSize(wm);
}
Expand Down
1 change: 0 additions & 1 deletion src/PicView.Avalonia/UI/UIHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public static async Task ShowSideBySide(MainViewModel vm)
preloadValue.ImageModel.PixelHeight, vm.RotationAngle, vm);
}
}
await Dispatcher.UIThread.InvokeAsync(() => { vm.ImageViewer.MainImage.InvalidateVisual(); });
await SettingsHelper.SaveSettingsAsync();
}

Expand Down
64 changes: 41 additions & 23 deletions src/PicView.Core/Calculations/ImageSizeCalculationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public static double GetInterfaceSize()
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 165 : 228;
}


public static ImageSize GetImageSize(double width,
double height,
double monitorWidth,
Expand All @@ -29,7 +28,7 @@ public static ImageSize GetImageSize(double width,
{
if (width <= 0 || height <= 0 || rotationAngle > 360 || rotationAngle < 0)
{
return new ImageSize(0, 0, 0, 0, 0, 0,0,0);
return new ImageSize(0, 0, 0, 0, 0, 0, 0, 0);
}

double aspectRatio;
Expand All @@ -49,7 +48,7 @@ public static ImageSize GetImageSize(double width,
{
workAreaWidth -= SizeDefaults.ScrollbarSize * dpiScaling;
containerWidth -= SizeDefaults.ScrollbarSize * dpiScaling;

maxWidth = workAreaWidth - padding;
maxHeight = height;
}
Expand Down Expand Up @@ -124,24 +123,24 @@ public static ImageSize GetImageSize(double width,
{
xWidth = maxWidth - SizeDefaults.ScrollbarSize - 10;
xHeight = maxWidth * height / width;

scrollWidth = maxWidth;
scrollHeight = containerHeight - padding - 8;
}
else
{
scrollWidth = containerWidth + SizeDefaults.ScrollbarSize;
scrollHeight = containerHeight;

xWidth = containerWidth - SizeDefaults.ScrollbarSize + 10;
xHeight = height / width * xWidth;
}
}
else
{
{
scrollWidth = double.NaN;
scrollHeight = double.NaN;

xWidth = width * aspectRatio;
xHeight = height * aspectRatio;
}
Expand Down Expand Up @@ -173,17 +172,19 @@ public static ImageSize GetImageSize(double width,
if (width <= 0 || height <= 0 || secondaryWidth <= 0 || secondaryHeight <= 0 || rotationAngle > 360 ||
rotationAngle < 0)
{
return new ImageSize(0, 0, 0, 0, 0,0, 0,0);
return new ImageSize(0, 0, 0, 0, 0, 0, 0, 0);
}

// Get sizes for both images
var firstSize = GetImageSize(width, height, monitorWidth, monitorHeight, monitorMinWidth, monitorMinHeight,
interfaceSize, rotationAngle, padding, dpiScaling, uiTopSize, uiBottomSize, galleryHeight, containerWidth,
interfaceSize, rotationAngle, padding, dpiScaling, uiTopSize, uiBottomSize, galleryHeight,
containerWidth,
containerHeight);
var secondSize = GetImageSize(secondaryWidth, secondaryHeight, monitorWidth, monitorHeight, monitorMinWidth,
monitorMinHeight, interfaceSize, rotationAngle, padding, dpiScaling, uiTopSize, uiBottomSize, galleryHeight,
monitorMinHeight, interfaceSize, rotationAngle, padding, dpiScaling, uiTopSize, uiBottomSize,
galleryHeight,
containerWidth, containerHeight);

// Determine maximum height for both images
var xHeight = Math.Max(firstSize.Height, secondSize.Height);

Expand All @@ -206,7 +207,7 @@ public static ImageSize GetImageSize(double width,
xWidth1 *= scaleFactor;
xWidth2 *= scaleFactor;
xHeight *= scaleFactor;

combinedWidth = xWidth1 + xWidth2;
}
}
Expand All @@ -218,19 +219,26 @@ public static ImageSize GetImageSize(double width,
xWidth1 *= scaleFactor;
xWidth2 *= scaleFactor;
xHeight *= scaleFactor;

combinedWidth = xWidth1 + xWidth2;
}
}

double scrollWidth, scrollHeight;
if (SettingsHelper.Settings.Zoom.ScrollEnabled)
{
if (SettingsHelper.Settings.WindowProperties.AutoFit)
{
combinedWidth -= SizeDefaults.ScrollbarSize;
scrollWidth = combinedWidth + SizeDefaults.ScrollbarSize + 8;
scrollHeight = containerHeight - padding - 8;

var fullscreen = SettingsHelper.Settings.WindowProperties.Fullscreen ||
SettingsHelper.Settings.WindowProperties.Maximized;
var borderSpaceHeight = fullscreen ? 0 : uiTopSize + uiBottomSize + galleryHeight;
var workAreaHeight = monitorHeight * dpiScaling - borderSpaceHeight;
scrollHeight = SettingsHelper.Settings.ImageScaling.StretchImage
? workAreaHeight
: workAreaHeight - padding;
}
else
{
Expand All @@ -245,10 +253,12 @@ public static ImageSize GetImageSize(double width,
scrollHeight = double.NaN;
}

var titleMaxWidth = GetTitleMaxWidth(rotationAngle, combinedWidth, xHeight, monitorMinWidth, monitorMinHeight, interfaceSize, containerWidth);
var titleMaxWidth = GetTitleMaxWidth(rotationAngle, combinedWidth, xHeight, monitorMinWidth,
monitorMinHeight, interfaceSize, containerWidth);

var margin = firstSize.Height > secondSize.Height ? firstSize.Margin : secondSize.Margin;
return new ImageSize(combinedWidth, xHeight, xWidth2, scrollWidth, scrollHeight, titleMaxWidth, margin, firstSize.AspectRatio);
return new ImageSize(combinedWidth, xHeight, xWidth2, scrollWidth, scrollHeight, titleMaxWidth, margin,
firstSize.AspectRatio);
}


Expand All @@ -268,7 +278,7 @@ public static double GetTitleMaxWidth(double rotationAngle, double width, double
titleMaxWidth = titleMaxWidth - interfaceSize < interfaceSize
? interfaceSize
: titleMaxWidth - interfaceSize;

if (SettingsHelper.Settings.Zoom.ScrollEnabled)
{
titleMaxWidth += SizeDefaults.ScrollbarSize + 4;
Expand All @@ -283,17 +293,25 @@ public static double GetTitleMaxWidth(double rotationAngle, double width, double
return titleMaxWidth;
}

public struct ImageSize(double width, double height, double secondaryWidth, double scrollViewerWidth, double scrollViewerHeight, double titleMaxWidth, double margin, double aspectRatio)
public readonly struct ImageSize(
double width,
double height,
double secondaryWidth,
double scrollViewerWidth,
double scrollViewerHeight,
double titleMaxWidth,
double margin,
double aspectRatio)
{
public double TitleMaxWidth { get; private set; } = titleMaxWidth;
public double TitleMaxWidth { get; } = titleMaxWidth;
public double Width { get; } = width;
public double Height { get; } = height;

public double ScrollViewerWidth { get; } = scrollViewerWidth;
public double ScrollViewerHeight { get; } = scrollViewerHeight;

public double SecondaryWidth { get; } = secondaryWidth;
public double Margin { get; private set; } = margin;
public double Margin { get; } = margin;

public double AspectRatio { get; } = aspectRatio;
}
Expand Down

0 comments on commit 80c01c1

Please sign in to comment.