mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
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:
@@ -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 ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user