[ImageResizer]Use updated images from input when pressing Enter(#26292)

* [Image Resizer] HandleEnterKeyPress event added for image resizer.

* [Image Resizer]
* Comments are added to Button_KeyDown function
* Uncessary spaces are removed.

* [Image Resizer] Workaround reasons are added to function summary.
This commit is contained in:
gokcekantarci
2023-06-11 18:17:59 +03:00
committed by GitHub
parent eddb617484
commit 284a5fb31f
3 changed files with 85 additions and 1 deletions

View File

@@ -18,6 +18,19 @@ namespace ImageResizer.ViewModels
private readonly MainViewModel _mainViewModel;
private readonly IMainView _mainView;
public enum Dimension
{
Width,
Height,
}
public class KeyPressParams
{
public double Value { get; set; }
public Dimension Dimension { get; set; }
}
public InputViewModel(
Settings settings,
MainViewModel mainViewModel,
@@ -37,6 +50,7 @@ namespace ImageResizer.ViewModels
ResizeCommand = new RelayCommand(Resize);
CancelCommand = new RelayCommand(Cancel);
OpenSettingsCommand = new RelayCommand(OpenSettings);
EnterKeyPressedCommand = new RelayCommand<KeyPressParams>(HandleEnterKeyPress);
}
public Settings Settings { get; }
@@ -47,6 +61,8 @@ namespace ImageResizer.ViewModels
public ICommand OpenSettingsCommand { get; }
public ICommand EnterKeyPressedCommand { get; private set; }
public bool TryingToResizeGifFiles
{
get
@@ -67,6 +83,19 @@ namespace ImageResizer.ViewModels
SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.ImageResizer);
}
private void HandleEnterKeyPress(KeyPressParams parameters)
{
switch (parameters.Dimension)
{
case Dimension.Width:
Settings.CustomSize.Width = parameters.Value;
break;
case Dimension.Height:
Settings.CustomSize.Height = parameters.Value;
break;
}
}
public void Cancel()
=> _mainView.Close();
}

View File

@@ -86,6 +86,8 @@
<ui:NumberBox SpinButtonPlacementMode="Compact"
Minimum="0"
Width="102"
Name="WidthNumberBox"
KeyDown="Button_KeyDown"
AutomationProperties.Name="{x:Static p:Resources.Width}"
Margin="8,0,0,0">
<ui:ControlHelper.Header>
@@ -110,6 +112,8 @@
SpinButtonPlacementMode="Compact"
Minimum="0"
Width="102"
Name="HeightNumberBox"
KeyDown="Button_KeyDown"
AutomationProperties.Name="{x:Static p:Resources.Height}"
Visibility="{Binding ElementName=SizeComboBox, Path= SelectedValue.ShowHeight, Converter={StaticResource BoolValueConverter}}">
<ui:ControlHelper.Header>

View File

@@ -3,6 +3,10 @@
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
using System.Windows.Controls;
using System.Windows.Input;
using ImageResizer.ViewModels;
using ModernWpf.Controls;
using static ImageResizer.ViewModels.InputViewModel;
namespace ImageResizer.Views
{
@@ -10,5 +14,52 @@ namespace ImageResizer.Views
{
public InputPage()
=> InitializeComponent();
/// <summary>
/// Pressing Enter key doesn't update value. PropertyChanged is only updated after losing focus to NumberBox.
/// We add this workaround the UI limitations and might need to be revisited or not needed anymore if we upgrade to WinUI3.
/// This function handles the KeyDown event for a NumberBox control.
/// It checks if the key pressed is 'Enter'.
/// According to the NumberBox name, it creates an instance of the KeyPressParams class with the appropriate dimension (Width or Height) and the parsed double value.
/// </summary>
private void Button_KeyDown(object sender, KeyEventArgs e)
{
// Check if the key pressed is the 'Enter' key
if (e.Key == Key.Enter)
{
var numberBox = sender as NumberBox;
var viewModel = (InputViewModel)this.DataContext;
double number;
KeyPressParams keyParams;
if (double.TryParse(((TextBox)e.OriginalSource).Text, out number))
{
// Determine which NumberBox triggered the event based on its name
switch (numberBox.Name)
{
case "WidthNumberBox":
keyParams = new KeyPressParams
{
Value = number,
Dimension = Dimension.Width,
};
break;
case "HeightNumberBox":
keyParams = new KeyPressParams
{
Value = number,
Dimension = Dimension.Height,
};
break;
default:
// Return without EnterKeyPressedCommand executed
return;
}
viewModel.EnterKeyPressedCommand.Execute(keyParams);
}
}
}
}
}