Merge pull request #2835 from BDisp/v1_border-borderBrush-background-fix_2834

Fixes #2834. v1 Border BorderBrush and Background wrongly sets to an invalid Color enum.
This commit is contained in:
Tig
2023-08-30 07:02:21 -06:00
committed by GitHub
3 changed files with 40 additions and 4 deletions

View File

@@ -375,8 +375,10 @@ namespace Terminal.Gui {
public Color BorderBrush {
get => borderBrush != null ? (Color)borderBrush : (Color)(-1);
set {
borderBrush = value;
OnBorderChanged ();
if (Enum.IsDefined (typeof (Color), value)) {
borderBrush = value;
OnBorderChanged ();
}
}
}
@@ -386,8 +388,10 @@ namespace Terminal.Gui {
public Color Background {
get => background != null ? (Color)background : (Color)(-1);
set {
background = value;
OnBorderChanged ();
if (Enum.IsDefined (typeof (Color), value)) {
background = value;
OnBorderChanged ();
}
}
}

View File

@@ -619,5 +619,29 @@ At 0,0
████████████████████
At 0,4 ", output);
}
[Fact]
public void BorderBrush_Background_Only_Is_Set_To_Valid_Color_Enum ()
{
var border = new Border ();
Assert.Equal ((Color)(-1), border.BorderBrush);
Assert.Equal ((Color)(-1), border.Background);
Assert.Null (border.GetFieldValue<string> ("borderBrush"));
Assert.Null (border.GetFieldValue<string> ("background"));
border.BorderBrush = (Color)(-1);
border.Background = (Color)(-1);
Assert.Equal ((Color)(-1), border.BorderBrush);
Assert.Equal ((Color)(-1), border.Background);
Assert.Null (border.GetFieldValue<Color?> ("borderBrush"));
Assert.Null (border.GetFieldValue<Color?> ("background"));
border.BorderBrush = Color.Blue;
border.Background = Color.White;
Assert.Equal (Color.Blue, border.BorderBrush);
Assert.Equal (Color.White, border.Background);
Assert.Equal (Color.Blue, border.GetFieldValue<Color> ("borderBrush"));
Assert.Equal (Color.White, border.GetFieldValue<Color?> ("background"));
}
}
}

View File

@@ -32,4 +32,12 @@ public static class ReflectionTools {
}
return null;
}
public static T GetFieldValue<T> (this object obj, string name)
{
// Set the flags so that private and public fields from instances will be found
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var field = obj.GetType ().GetField (name, bindingFlags);
return (T)field?.GetValue (obj);
}
}