Skip to content

Commit

Permalink
use invariant culture to parse inputs to converters - (DynamoDS#10006)
Browse files Browse the repository at this point in the history
* use invariant culture to parse inputs -
we were passing the culture before but this was usually set to en-us EVEN in localized enviornments
unless something set it explicitly
wrap with try catch and return some default conversion

* Update src/DynamoCoreWpf/UI/Converters.cs

* Update src/DynamoCoreWpf/UI/Converters.cs

(cherry picked from commit 3dc4202)
  • Loading branch information
mjkkirschner committed Sep 25, 2019
1 parent 10d7ef2 commit 43aa512
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 29 deletions.
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
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);
}
}
}

0 comments on commit 43aa512

Please sign in to comment.