Skip to content
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

use invariant culture to parse inputs to converters - #10006

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 66 additions & 11 deletions src/DynamoCoreWpf/UI/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1414,15 +1414,29 @@ public object ConvertBack(object value, Type targetType, object parameter, Syste
}
}

//TODO remove(this is not used anywhere) in Dynamo 3.0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saintentropy this is the converter I was talking about .... is this the one you were thinking of? I can't find this particular one used anywhere.

public class ZoomToVisibilityConverter : IValueConverter
{
/// <summary>
/// Returns hidden for small zoom sizes - appears unused.
/// </summary>
/// <param name="value">zoom size</param>
/// <param name="targetType">unused</param>
/// <param name="parameter">unused</param>
/// <param name="culture">unused</param>
/// <returns></returns>
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double zoom = System.Convert.ToDouble(value, culture);

if (zoom < .5)
return Visibility.Hidden;

try
{
double zoom = System.Convert.ToDouble(value, CultureInfo.InvariantCulture);
if (zoom < .5)
return Visibility.Hidden;
}
catch(Exception e)
{
Console.WriteLine($"problem attempting to parse zoomsize or param {value}{ e.Message}");
}
return Visibility.Visible;
}

Expand Down Expand Up @@ -2208,12 +2222,31 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu

public class MenuItemCheckConverter : IValueConverter
{
/// <summary>
/// Converts from a fontsize and param to determine if the two numbers are equal.(ie what is the font set to)
/// </summary>
/// <param name="value">fontSize</param>
/// <param name="targetType">unusued</param>
/// <param name="parameter">target font size</param>
/// <param name="culture">unusued</param>
/// <returns></returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var fontsize = System.Convert.ToDouble(value, culture);
var param = System.Convert.ToDouble(parameter, culture);

return fontsize == param;

//use invariant culture, these strings should always be set via our code.
try
{
var fontsize = System.Convert.ToDouble(value, CultureInfo.InvariantCulture);
var param = System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture);
return fontsize == param;
}

catch(Exception e)
{
Console.WriteLine($"problem attempting to parse fontsize or param {value} {parameter} { e.Message}");
return false;
}

}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Expand Down Expand Up @@ -2265,10 +2298,32 @@ public class GroupFontSizeToEditorEnabledConverter : IMultiValueConverter
{
private const double MinFontFactor = 7.0;

/// <summary>
/// converts a zoom and fontsize to a bool used to determine if group title editor should be enabled.
/// </summary>
/// <param name="values">[0] zoom [1] fontSize - could be strings or doubles</param>
/// <param name="targetType">unused</param>
/// <param name="parameter">unused</param>
/// <param name="culture">unused</param>
/// <returns></returns>
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var zoom = System.Convert.ToDouble(values[0], culture);
var fontsize = System.Convert.ToDouble(values[1], culture);
//defaults
var zoom = 1.0;
var fontsize = 36.0;
try
{
//use invariantCulture -
//fontSize should only be serialized in invariant culture
// and zoom should either come from fallback value or runtime value.
zoom = System.Convert.ToDouble(values[0], CultureInfo.InvariantCulture);
fontsize = System.Convert.ToDouble(values[1], CultureInfo.InvariantCulture);
}
//just use defaults, this will enable the text editor.
catch(Exception e)
{
Console.WriteLine($"problem attempting to parse fontsize or zoom {values[1]} {values[0]}. { e.Message}");
}

var factor = zoom*fontsize;
if (factor < MinFontFactor)
Expand Down
63 changes: 45 additions & 18 deletions test/DynamoCoreWpfTests/ConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,45 +636,72 @@ public void ZoomToVisibilityConverterTest()
[Category("UnitTests")]
public void MenuItemCheckConverterTest()
{
object equal;
object result;
MenuItemCheckConverter converter = new MenuItemCheckConverter();

equal = converter.Convert("1.0", typeof(string), "1.0", new CultureInfo("en-US"));
Assert.AreEqual(equal, true);
result = converter.Convert("1.0", typeof(string), "1.0", new CultureInfo("en-US"));
Assert.AreEqual(result, true);

result = converter.Convert("1,0", typeof(string), "1,0", new CultureInfo("de-DE"));
Assert.AreEqual(result, true);

result = converter.Convert(1.0, typeof(double), 1.0, new CultureInfo("en-US"));
Assert.AreEqual(result, true);

equal = converter.Convert("1,0", typeof(string), "1,0", new CultureInfo("de-DE"));
Assert.AreEqual(equal, true);
result = converter.Convert(1.0, typeof(double), 1.0, new CultureInfo("de-DE"));
Assert.AreEqual(result, true);

equal = converter.Convert(1.0, typeof(double), 1.0, new CultureInfo("en-US"));
Assert.AreEqual(equal, true);
result = converter.Convert(1.0, null, 1.0, null);
Assert.AreEqual(result, true);

equal = converter.Convert(1.0, typeof(double), 1.0, new CultureInfo("de-DE"));
Assert.AreEqual(equal, true);
result = converter.Convert(1.0, null, 100, null);
Assert.AreEqual(result, false);
}

[Test]
[Category("UnitTests")]
public void GroupFontSizeToEditorEnabledConverterTest()
{
object equal;
object result;
GroupFontSizeToEditorEnabledConverter converter = new GroupFontSizeToEditorEnabledConverter();
//obj [1] = zoom, obj [2]= fontsize
string[] stringsUsDefault = { "1.0", "36.0" };
string[] stringsUsDefaultResultInFalse = { "1.0", "2.0" };
string[] stringsUs = { "2.0", "4.0" };
string[] stringsDe = { "2,0", "4,0" };
double[] doubles = { 2.0, 4.0 };
object[] objects = new object[2];
doubles.CopyTo(objects, 0);

equal = converter.Convert(stringsUs , typeof(string), null, new CultureInfo("en-US"));
Assert.AreEqual(equal, true);
result = converter.Convert(stringsUs , typeof(string), null, new CultureInfo("en-US"));
Assert.AreEqual(result, true);

result = converter.Convert(stringsDe, typeof(string), null, new CultureInfo("de-DE"));
Assert.AreEqual(result, true);

result = converter.Convert(objects, typeof(float), null, new CultureInfo("en-US"));
Assert.AreEqual(result, true);

result = converter.Convert(objects, typeof(float), null, new CultureInfo("de-DE"));
Assert.AreEqual(result, true);

result = converter.Convert(stringsDe, typeof(float), null, new CultureInfo("de-DE"));
Assert.AreEqual(result, true);

result = converter.Convert(stringsUsDefault, typeof(float), null, new CultureInfo("de-DE"));
Assert.AreEqual(result, true);

result = converter.Convert(stringsUsDefault, typeof(float), null, new CultureInfo("fr-FR"));
Assert.AreEqual(result, true);

equal = converter.Convert(stringsDe, typeof(string), null, new CultureInfo("de-DE"));
Assert.AreEqual(equal, true);
result = converter.Convert(stringsDe, typeof(float), null, new CultureInfo("fr-FR"));
Assert.AreEqual(result, true);

equal = converter.Convert(objects, typeof(float), null, new CultureInfo("en-US"));
Assert.AreEqual(equal, true);
result = converter.Convert(stringsUsDefault, null, null, null);
Assert.AreEqual(result, true);

equal = converter.Convert(objects, typeof(float), null, new CultureInfo("de-DE"));
Assert.AreEqual(equal, true);
result = converter.Convert(stringsUsDefaultResultInFalse, null, null, null);
Assert.AreEqual(result, false);
}
}
}