Skip to content

Commit

Permalink
Aspect ratio calculations fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Dec 1, 2024
1 parent ce41ed0 commit 57d7839
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 173 deletions.
75 changes: 75 additions & 0 deletions src/PicView.Avalonia/CustomControls/NumTextBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Avalonia.Input;

namespace PicView.Avalonia.CustomControls;

public class NumTextBox : FuncTextBox
{
public NumTextBox()
{
KeyDown += (_, e) => OnKeyDownVerifyInput(e);
}

private void OnKeyDownVerifyInput(KeyEventArgs e)
{
switch (e.Key)
{
case Key.D0:
case Key.D1:
case Key.D2:
case Key.D3:
case Key.D4:
case Key.D5:
case Key.D6:
case Key.D7:
case Key.D8:
case Key.D9:
case Key.NumPad0:
case Key.NumPad1:
case Key.NumPad2:
case Key.NumPad3:
case Key.NumPad4:
case Key.NumPad5:
case Key.NumPad6:
case Key.NumPad7:
case Key.NumPad8:
case Key.NumPad9:
case Key.Back:
case Key.Delete:
break; // Allow numbers and basic operations

case Key.Left:
case Key.Right:
case Key.Tab:
case Key.OemBackTab:
break; // Allow navigation keys

case Key.A:
case Key.C:
case Key.X:
case Key.V:
if (e.KeyModifiers == KeyModifiers.Control)
{
// Allow Ctrl + A, Ctrl + C, Ctrl + X, and Ctrl + V (paste)
break;
}

e.Handled = true; // Only allow with Ctrl
return;

case Key.Oem5: // Key for `%` symbol (may vary based on layout)
break; // Allow the percentage symbol (%)

case Key.Escape: // Handle Escape key
Focus();
e.Handled = true;
return;

case Key.Enter:
return;

default:
e.Handled = true; // Block all other inputs
return;
}
}
}
23 changes: 2 additions & 21 deletions src/PicView.Avalonia/Navigation/ExifHandling.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using Avalonia.Media.Imaging;
using Avalonia.Media.Imaging;
using ImageMagick;
using PicView.Avalonia.ImageHandling;
using PicView.Avalonia.Resizing;
Expand Down Expand Up @@ -72,9 +71,6 @@ public static void UpdateExifValues(MainViewModel vm)
};

var meter = TranslationHelper.Translation.Meter;
var square = TranslationHelper.Translation.Square;
var landscape = TranslationHelper.Translation.Landscape;
var portrait = TranslationHelper.Translation.Portrait;

if (string.IsNullOrEmpty(vm.GetBitDepth))
{
Expand All @@ -99,22 +95,7 @@ public static void UpdateExifValues(MainViewModel vm)
var gcd = ImageTitleFormatter.GCD(vm.PixelWidth, vm.PixelHeight);
if (gcd != 0) // Check for zero before division
{
var firstRatio = vm.PixelWidth / gcd;
var secondRatio = vm.PixelHeight / gcd;

if (firstRatio == secondRatio)
{
vm.GetAspectRatio = $"{firstRatio}:{secondRatio} ({square})";
}
else if (firstRatio > secondRatio)
{
vm.GetAspectRatio =
$"{firstRatio}:{secondRatio} ({landscape})";
}
else
{
vm.GetAspectRatio = $"{firstRatio}:{secondRatio} ({portrait})";
}
vm.GetAspectRatio = AspectRatioHelper.GetFormattedAspectRatio(gcd, vm.PixelWidth, vm.PixelHeight);
}
else
{
Expand Down
18 changes: 18 additions & 0 deletions src/PicView.Avalonia/Resizing/AspectRatioHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ public static PrintSizes GetPrintSizes(int pixelWidth, int pixelHeight, double d
return new PrintSizes(printSizeCm, printSizeInch, sizeMp);

}

public static string GetFormattedAspectRatio(int gcd, int width, int height)
{
var square = TranslationHelper.Translation.Square;
var landscape = TranslationHelper.Translation.Landscape;
var portrait = TranslationHelper.Translation.Portrait;

var firstRatio = width / gcd;
var secondRatio = height / gcd;

if (firstRatio == secondRatio)
{
return $"{firstRatio}:{secondRatio} ({square})";
}
return firstRatio > secondRatio ?
$"{firstRatio}:{secondRatio} ({landscape})" :
$"{firstRatio}:{secondRatio} ({portrait})";
}

public readonly struct PrintSizes(string printSizeCm, string printSizeInch, string sizeMp)
{
Expand Down
9 changes: 4 additions & 5 deletions src/PicView.Avalonia/Views/BatchResizeView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
xmlns:viewModels="clr-namespace:PicView.Avalonia.ViewModels;assembly=PicView.Avalonia"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>

<converters:IndexBiggerThanBooleanConverter x:Key="IndexBiggerThanBooleanConverter" />
</UserControl.Resources>
<StackPanel>
Expand Down Expand Up @@ -185,7 +184,7 @@
Width="130"
x:Name="WidthTextBlock" />

<customControls:FuncTextBox
<customControls:NumTextBox
Classes="hover TStyle"
Margin="11,0,10,0"
Text="{CompiledBinding PixelWidth,
Expand All @@ -206,7 +205,7 @@
Width="130"
x:Name="HeightTextBlock" />

<customControls:FuncTextBox
<customControls:NumTextBox
Classes="hover TStyle"
Margin="11,0,10,0"
Text="{CompiledBinding PixelHeight,
Expand All @@ -227,7 +226,7 @@
Width="130"
x:Name="WidthAndHeightWidthTextBlock" />

<customControls:FuncTextBox
<customControls:NumTextBox
Classes="hover TStyle"
Margin="11,0,10,0"
Text="{CompiledBinding PixelWidth,
Expand Down Expand Up @@ -361,7 +360,7 @@
Width="130"
x:Name="WidthAndHeightHeightTextBlock" />

<customControls:FuncTextBox
<customControls:NumTextBox
Classes="hover TStyle"
Margin="11,0,10,0"
Text="{CompiledBinding PixelHeight,
Expand Down
1 change: 0 additions & 1 deletion src/PicView.Avalonia/Views/BatchResizeView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public BatchResizeView()
SourceFolderTextBox.Text = vm.FileInfo?.DirectoryName ?? string.Empty;

StartButton.Click += async (_, _) => await StartBatchResize();

};
}

Expand Down
4 changes: 2 additions & 2 deletions src/PicView.Avalonia/Views/ExifView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
Text="{CompiledBinding Width,
Mode=OneWay}"
Width="100" />
<customControls:FuncTextBox
<customControls:NumTextBox
Background="{DynamicResource SecondaryBackgroundColor}"
Classes="hover TStyle x"
Foreground="{StaticResource SecondaryTextColor}"
Expand Down Expand Up @@ -278,7 +278,7 @@
Text="{CompiledBinding Height,
Mode=OneWay}"
Width="100" />
<customControls:FuncTextBox
<customControls:NumTextBox
Background="{DynamicResource SecondaryBackgroundColor}"
Classes="hover TStyle x"
Foreground="{StaticResource SecondaryTextColor}"
Expand Down
93 changes: 22 additions & 71 deletions src/PicView.Avalonia/Views/ExifView.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using System.Reactive.Linq;
using System.Reactive.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
Expand All @@ -9,6 +8,7 @@
using PicView.Avalonia.Resizing;
using PicView.Avalonia.ViewModels;
using PicView.Core.ImageDecoding;
using PicView.Core.Navigation;
using ReactiveUI;

namespace PicView.Avalonia.Views;
Expand Down Expand Up @@ -43,8 +43,8 @@ public ExifView()
}
};

PixelWidthTextBox.KeyDown += async (s, e) => await OnKeyDownVerifyInput(s,e);
PixelHeightTextBox.KeyDown += async (s, e) => await OnKeyDownVerifyInput(s,e);
PixelWidthTextBox.KeyDown += async (s, e) => await SaveImageOnEnter(s,e);
PixelHeightTextBox.KeyDown += async (s, e) => await SaveImageOnEnter(s,e);

PixelWidthTextBox.KeyUp += delegate { AdjustAspectRatio(PixelWidthTextBox); };
PixelHeightTextBox.KeyUp += delegate { AdjustAspectRatio(PixelHeightTextBox); };
Expand Down Expand Up @@ -123,18 +123,26 @@ private void AdjustAspectRatio(TextBox sender)
{
return;
}
var aspectRatio = (double)vm.PixelWidth / vm.PixelHeight;
AspectRatioHelper.SetAspectRatioForTextBox(PixelWidthTextBox, PixelHeightTextBox, sender == PixelWidthTextBox,
aspectRatio, DataContext as MainViewModel);
AspectRatioTextBox.Text = aspectRatio.ToString(CultureInfo.CurrentCulture);
if (!int.TryParse(PixelWidthTextBox.Text, out var width) || !int.TryParse(PixelHeightTextBox.Text, out var height))
{
return;
}

if (width <=0 || height <= 0)
{
return;
}
var printSizes = AspectRatioHelper.GetPrintSizes(width, height, vm.DpiX, vm.DpiY);
PrintSizeInchTextBox.Text = printSizes.PrintSizeInch;
PrintSizeCmTextBox.Text = printSizes.PrintSizeCm;
SizeMpTextBox.Text = printSizes.SizeMp;

var gcd = ImageTitleFormatter.GCD(width, height);
var aspectRatio = (double)vm.PixelWidth / vm.PixelHeight;
AspectRatioTextBox.Text = AspectRatioHelper.GetFormattedAspectRatio(gcd, vm.PixelWidth, vm.PixelHeight);

AspectRatioHelper.SetAspectRatioForTextBox(PixelWidthTextBox, PixelHeightTextBox, sender == PixelWidthTextBox,
aspectRatio, DataContext as MainViewModel);
}

private static async Task DoResize(MainViewModel vm, bool isWidth, object width, object height)
Expand Down Expand Up @@ -175,73 +183,16 @@ private static async Task DoResize(MainViewModel vm, bool isWidth, object width,
}
}

private async Task OnKeyDownVerifyInput(object? sender, KeyEventArgs? e)
private async Task SaveImageOnEnter(object? sender, KeyEventArgs e)
{
switch (e.Key)
if (e.Key == Key.Enter)
{
case Key.D0:
case Key.D1:
case Key.D2:
case Key.D3:
case Key.D4:
case Key.D5:
case Key.D6:
case Key.D7:
case Key.D8:
case Key.D9:
case Key.NumPad0:
case Key.NumPad1:
case Key.NumPad2:
case Key.NumPad3:
case Key.NumPad4:
case Key.NumPad5:
case Key.NumPad6:
case Key.NumPad7:
case Key.NumPad8:
case Key.NumPad9:
case Key.Back:
case Key.Delete:
break; // Allow numbers and basic operations

case Key.Left:
case Key.Right:
case Key.Tab:
case Key.OemBackTab:
break; // Allow navigation keys

case Key.A:
case Key.C:
case Key.X:
case Key.V:
if (e.KeyModifiers == KeyModifiers.Control)
{
// Allow Ctrl + A, Ctrl + C, Ctrl + X, and Ctrl + V (paste)
break;
}

e.Handled = true; // Only allow with Ctrl
return;

case Key.Oem5: // Key for `%` symbol (may vary based on layout)
break; // Allow the percentage symbol (%)

case Key.Escape: // Handle Escape key
Focus();
e.Handled = true;
if (DataContext is not MainViewModel vm)
{
return;

case Key.Enter: // Handle Enter key for saving
if (DataContext is not MainViewModel vm)
{
return;
}
}

await DoResize(vm, Equals(sender, PixelWidthTextBox), PixelWidthTextBox.Text, PixelHeightTextBox.Text).ConfigureAwait(false);
return;

default:
e.Handled = true; // Block all other inputs
return;
await DoResize(vm, Equals(sender, PixelWidthTextBox), PixelWidthTextBox.Text, PixelHeightTextBox.Text).ConfigureAwait(false);
}
}
}
25 changes: 17 additions & 8 deletions src/PicView.Avalonia/Views/SingleImageResizeView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
Margin="4,0,0,5"
Text="{CompiledBinding Width,
Mode=OneWay}" />
<customControls:FuncTextBox
<customControls:NumTextBox
Background="{DynamicResource SecondaryBackgroundColor}"
Classes="hover TStyle"
CornerRadius="4"
Expand All @@ -42,16 +42,25 @@
</StackPanel>

<Button
x:Name="LinkChainButton"
Foreground="{StaticResource SecondaryTextColor}"
Background="Transparent"
Classes="altHover"
Height="35"
Foreground="{StaticResource SecondaryTextColor}"
Height="35"
Margin="5,20,5,0"
Width="38">
Width="38"
x:Name="LinkChainButton">
<Panel>
<Image x:Name="LinkChainImage" Source="{StaticResource LinkChainImage}" Width="21" Height="21" />
<Image x:Name="UnlinkChainImage" IsVisible="False" Source="{StaticResource UnlinkChainImage}" Width="21" Height="21" />
<Image
Height="21"
Source="{StaticResource LinkChainImage}"
Width="21"
x:Name="LinkChainImage" />
<Image
Height="21"
IsVisible="False"
Source="{StaticResource UnlinkChainImage}"
Width="21"
x:Name="UnlinkChainImage" />
</Panel>
</Button>

Expand All @@ -64,7 +73,7 @@
Margin="4,0,0,5"
Text="{CompiledBinding Height,
Mode=OneWay}" />
<customControls:FuncTextBox
<customControls:NumTextBox
Background="{DynamicResource SecondaryBackgroundColor}"
Classes="hover TStyle"
CornerRadius="4"
Expand Down
Loading

0 comments on commit 57d7839

Please sign in to comment.