-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resizing and file conversion updates
- Loading branch information
Showing
9 changed files
with
664 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Avalonia.Input; | ||
|
||
namespace PicView.Avalonia.Input; | ||
|
||
public static class InputHelper | ||
{ | ||
public static async Task OnKeyDownVerifyInput(KeyEventArgs e, Func<bool> focus, Task saveImage) | ||
{ | ||
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: // Handle Enter key for function | ||
|
||
await saveImage.ConfigureAwait(false); | ||
return; | ||
|
||
default: | ||
e.Handled = true; // Block all other inputs | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,6 @@ | |
|
||
<ItemGroup> | ||
<Folder Include="Assets\" /> | ||
<Folder Include="Resizing\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Globalization; | ||
using Avalonia.Controls; | ||
using PicView.Avalonia.ViewModels; | ||
using PicView.Core.Extensions; | ||
|
||
namespace PicView.Avalonia.Resizing; | ||
|
||
public static class AspectRatioHelper | ||
{ | ||
public static void SetAspectRatioForTextBox(TextBox widthTextBox, TextBox heightTextBox, bool isWidth, double aspectRatio, MainViewModel vm) | ||
{ | ||
var percentage = isWidth ? widthTextBox.Text.GetPercentage() : heightTextBox.Text.GetPercentage(); | ||
if (percentage > 0) | ||
{ | ||
var newWidth = vm.PixelWidth * (percentage / 100); | ||
var newHeight = vm.PixelHeight * (percentage / 100); | ||
|
||
widthTextBox.Text = newWidth.ToString("# ", CultureInfo.CurrentCulture); | ||
heightTextBox.Text = newHeight.ToString("# ", CultureInfo.CurrentCulture); | ||
|
||
if (isWidth) | ||
{ | ||
heightTextBox.Text = newHeight.ToString(CultureInfo.CurrentCulture); | ||
} | ||
else | ||
{ | ||
widthTextBox.Text = newWidth.ToString(CultureInfo.CurrentCulture); | ||
} | ||
} | ||
else | ||
{ | ||
if (!uint.TryParse(widthTextBox.Text, out var width) || !uint.TryParse(heightTextBox.Text, out var height)) | ||
{ | ||
// Invalid input, delete last character | ||
try | ||
{ | ||
if (isWidth && widthTextBox.Text.Length > 1) | ||
{ | ||
widthTextBox.Text = widthTextBox.Text[..^1]; | ||
} | ||
else if (heightTextBox.Text.Length > 1) | ||
{ | ||
heightTextBox.Text = heightTextBox.Text[..^1]; | ||
} | ||
|
||
} | ||
catch (Exception e) | ||
{ | ||
#if DEBUG | ||
Console.WriteLine(e); | ||
#endif | ||
} | ||
return; | ||
} | ||
if (isWidth) | ||
{ | ||
var newHeight = Math.Round(width / aspectRatio); | ||
heightTextBox.Text = newHeight.ToString(CultureInfo.CurrentCulture); | ||
} | ||
else | ||
{ | ||
var newWidth = Math.Round(height * aspectRatio); | ||
widthTextBox.Text = newWidth.ToString(CultureInfo.CurrentCulture); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.