-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ImageResizer] Fix issues with blank Width and Height controls #37373
Merged
lei9444
merged 3 commits into
microsoft:main
from
daverayment:imageresizer-empty-dimension-fix
Feb 25, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
32 changes: 32 additions & 0 deletions
32
src/modules/imageresizer/ui/Views/NumberBoxValueConverter.cs
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,32 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Windows.Data; | ||
|
||
namespace ImageResizer.Views; | ||
|
||
public class NumberBoxValueConverter : IValueConverter | ||
{ | ||
/// <summary> | ||
/// Converts the underlying double value to a display-friendly format. Ensures that NaN values | ||
/// are not propagated to the UI. | ||
/// </summary> | ||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => | ||
value is double d && double.IsNaN(d) ? 0 : value; | ||
|
||
/// <summary> | ||
/// Converts the user input back to the underlying double value. If the input is not a valid | ||
/// number, 0 is returned. | ||
/// </summary> | ||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => | ||
value switch | ||
{ | ||
null => 0, | ||
double d when double.IsNaN(d) => 0, | ||
string str when !double.TryParse(str, out _) => 0, | ||
_ => value, | ||
}; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/modules/imageresizer/ui/Views/ZeroToEmptyStringNumberFormatter.cs
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,37 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Globalization; | ||
using Wpf.Ui.Controls; | ||
|
||
namespace ImageResizer.Views; | ||
|
||
public class ZeroToEmptyStringNumberFormatter : INumberFormatter, INumberParser | ||
{ | ||
public string FormatDouble(double? value) => value switch | ||
{ | ||
null => string.Empty, | ||
0 => string.Empty, | ||
_ => value.Value.ToString(CultureInfo.CurrentCulture), | ||
}; | ||
|
||
public double? ParseDouble(string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) | ||
{ | ||
return 0; | ||
} | ||
|
||
return double.TryParse(value, NumberStyles.Any, CultureInfo.CurrentCulture, out double result) ? result : 0; | ||
} | ||
|
||
public string FormatInt(int? value) => throw new NotImplementedException(); | ||
|
||
public string FormatUInt(uint? value) => throw new NotImplementedException(); | ||
|
||
public int? ParseInt(string value) => throw new NotImplementedException(); | ||
|
||
public uint? ParseUInt(string value) => throw new NotImplementedException(); | ||
} |
61 changes: 61 additions & 0 deletions
61
src/settings-ui/Settings.UI/Converters/ImageResizerDoubleToAutoConverter.cs
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,61 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Globalization; | ||
using Microsoft.UI.Xaml.Data; | ||
|
||
namespace Microsoft.PowerToys.Settings.UI.Converters; | ||
|
||
/// <summary> | ||
/// Converts between double and string for text-based controls bound to Width or Height fields. | ||
/// Optionally returns localized "Auto" text when the underlying value is 0, letting the UI show, | ||
/// for example "(auto) x 1024 pixels". | ||
/// </summary> | ||
public sealed partial class ImageResizerDoubleToAutoConverter : IValueConverter | ||
{ | ||
private static readonly string AutoText = | ||
Helpers.ResourceLoaderInstance.ResourceLoader.GetString("ImageResizer_AutoText"); | ||
|
||
/// <summary> | ||
/// Converts a double to a string, optionally showing "Auto" for 0 values. NaN values are | ||
/// converted to empty strings. | ||
/// </summary> | ||
/// <param name="value">The value to convert from <see cref="double"/> to | ||
/// <see cref="string"/>.</param> | ||
/// <param name="targetType">The conversion target type. <see cref="string"/> here.</param> | ||
/// <param name="parameter">Set to "Auto" to return the localized "Auto" string if the | ||
/// value is 0.</param> | ||
/// <param name="language">Ignored.</param> | ||
/// <returns>The string representation of the passed-in value.</returns> | ||
public object Convert(object value, Type targetType, object parameter, string language) => | ||
value switch | ||
{ | ||
double d => d switch | ||
{ | ||
double.NaN => "0", | ||
0 => (string)parameter == "Auto" ? AutoText : "0", | ||
_ => d.ToString(CultureInfo.CurrentCulture), | ||
}, | ||
|
||
_ => "0", | ||
}; | ||
|
||
/// <summary> | ||
/// Converts the string representation back to a double, returning 0 if the string is empty, | ||
/// null or not a valid number in the specified culture. | ||
/// </summary> | ||
/// <param name="value">The string value to convert.</param> | ||
/// <param name="targetType">The conversion target type. <see cref="double"/> here.</param> | ||
/// <param name="parameter">Converter parameter. Unused.</param> | ||
/// <param name="language">Ignored.</param> | ||
/// <returns>The corresponding double value.</returns> | ||
public object ConvertBack(object value, Type targetType, object parameter, string language) => | ||
value switch | ||
{ | ||
null or "" => 0.0, | ||
string text when double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, out double result) => result, | ||
_ => 0.0, | ||
}; | ||
} |
31 changes: 31 additions & 0 deletions
31
src/settings-ui/Settings.UI/Converters/ImageResizerNumberBoxValueConverter.cs
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,31 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using Microsoft.UI.Xaml.Data; | ||
|
||
namespace Microsoft.PowerToys.Settings.UI.Converters; | ||
|
||
public partial class ImageResizerNumberBoxValueConverter : IValueConverter | ||
{ | ||
/// <summary> | ||
/// Converts the underlying double value to a display-friendly format. Ensures that NaN values | ||
/// are not propagated to the UI. | ||
/// </summary> | ||
public object Convert(object value, Type targetType, object parameter, string language) => | ||
value is double d && double.IsNaN(d) ? 0.0 : value; | ||
|
||
/// <summary> | ||
/// Converts the user input back to the underlying double value. If the input is not a valid | ||
/// number, a double with value 0 is returned. | ||
/// </summary> | ||
public object ConvertBack(object value, Type targetType, object parameter, string language) => | ||
value switch | ||
{ | ||
null => 0.0, | ||
double d when double.IsNaN(d) => 0.0, | ||
string str when !double.TryParse(str, out _) => 0.0, | ||
_ => value, | ||
}; | ||
} |
39 changes: 39 additions & 0 deletions
39
src/settings-ui/Settings.UI/Converters/ImageResizerZeroToEmptyStringNumberFormatter.cs
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,39 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Globalization; | ||
using Microsoft.UI.Xaml.Controls; | ||
|
||
namespace Microsoft.PowerToys.Settings.UI.Converters; | ||
|
||
public partial class ImageResizerZeroToEmptyStringNumberFormatter | ||
{ | ||
public string Format(long value) => throw new NotImplementedException(); | ||
|
||
public string Format(ulong value) => throw new NotImplementedException(); | ||
|
||
public string Format(double value) => throw new NotImplementedException(); | ||
|
||
public string FormatDouble(double? value) => value switch | ||
{ | ||
null => string.Empty, | ||
0 => string.Empty, | ||
_ => value.Value.ToString(CultureInfo.CurrentCulture), | ||
}; | ||
|
||
public double? ParseDouble(string text) | ||
{ | ||
if (string.IsNullOrWhiteSpace(text)) | ||
{ | ||
return 0.0; | ||
} | ||
|
||
return double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, out double result) ? result : 0.0; | ||
} | ||
|
||
public long? ParseInt(string text) => throw new NotImplementedException(); | ||
|
||
public ulong? ParseUInt(string text) => throw new NotImplementedException(); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/settings-ui/Settings.UI/SettingsXAML/Controls/ImageResizerDimensionsNumberBox.cs
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,38 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
|
||
namespace Microsoft.PowerToys.Settings.UI.Controls; | ||
|
||
public partial class ImageResizerDimensionsNumberBox : NumberBox | ||
{ | ||
public ImageResizerDimensionsNumberBox() | ||
{ | ||
this.Loaded += (_, _) => UpdateDisplayText(); | ||
|
||
this.ValueChanged += (_, _) => UpdateDisplayText(); | ||
|
||
this.GotFocus += (s, e) => | ||
{ | ||
// Show "0" in the UI when focused on the empty value. This ensures that the spinbutton | ||
// controls are usable. | ||
if (Value is double.NaN) | ||
{ | ||
Value = 0.0; | ||
} | ||
}; | ||
|
||
this.LostFocus += (_, _) => UpdateDisplayText(); | ||
} | ||
|
||
private void UpdateDisplayText() | ||
{ | ||
if (FocusState == FocusState.Unfocused && Value == 0) | ||
{ | ||
Text = string.Empty; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check 'Value is double.NaN' will not reliably detect NaN values. Please use double.IsNaN(Value) instead for correct NaN detection.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.