Skip to content

Commit

Permalink
wip: Refactor ValueFactory to single pathway (fails build)
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed Dec 31, 2023
1 parent 135a55b commit a300227
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 77 deletions.
143 changes: 73 additions & 70 deletions src/UI/ValueFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace TerminalGuiDesigner.UI
{
static class ValueFactory
{
internal static bool GetNewValue(Type type, out object? newValue)
internal static bool GetNewValue(string propertyName, Design design, Type type, object? oldValue, out object? newValue, bool allowMultiLine)
{
newValue = null;

Expand All @@ -32,24 +32,16 @@ internal static bool GetNewValue(Type type, out object? newValue)
}
}
else
if (type== typeof(ColorScheme))
{
// TODO: Handle other cases here
return false;
//toAdd = default;
}
}
internal static bool GetNewValue(Design design, Property property, object? oldValue, out object? newValue)
{
if (property.PropertyInfo.PropertyType == typeof(ColorScheme))
{
return GetNewColorSchemeValue(design, property, out newValue);
return GetNewColorSchemeValue(design, out newValue);
}
else
if (property.PropertyInfo.PropertyType == typeof(Attribute) ||
property.PropertyInfo.PropertyType == typeof(Attribute?))
if (type == typeof(Attribute) ||
type == typeof(Attribute?))
{
// if its an Attribute or nullableAttribute
var picker = new ColorPicker((Attribute?)property.GetValue());
var picker = new ColorPicker((Attribute?)oldValue);
Application.Run(picker);

if (!picker.Cancelled)
Expand All @@ -65,7 +57,7 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa
}
}
else
if (property.PropertyInfo.PropertyType == typeof(ITextValidateProvider))
if (type== typeof(ITextValidateProvider))
{
string? oldPattern = oldValue is TextRegexProvider r ? (string?)r.Pattern.ToPrimitive() : null;
if (Modals.GetString("New Validation Pattern", "Regex Pattern", oldPattern, out var newPattern))
Expand All @@ -79,10 +71,10 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa
return false;
}
else
if (property.PropertyInfo.PropertyType == typeof(Pos))
if (type== typeof(Pos))
{
// user is editing a Pos
var designer = new PosEditor(design, property);
var designer = new PosEditor(design, (Pos)oldValue?? throw new Exception("Pos property was unexpectedly null"));

Application.Run(designer);

Expand All @@ -99,10 +91,10 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa
}
}
else
if (property.PropertyInfo.PropertyType == typeof(Size))
if (type== typeof(Size))
{
// user is editing a Size
var oldSize = (Size)(oldValue ?? throw new Exception($"Property {property.PropertyInfo.Name} is of Type Size but it's current value is null"));
var oldSize = (Size)(oldValue ?? throw new Exception($"Property {propertyName} is of Type Size but it's current value is null"));
var designer = new SizeEditor(oldSize);

Application.Run(designer);
Expand All @@ -120,10 +112,10 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa
}
}
else
if (property.PropertyInfo.PropertyType == typeof(PointF))
if (type== typeof(PointF))
{
// user is editing a PointF
var oldPointF = (PointF)(oldValue ?? throw new Exception($"Property {property.PropertyInfo.Name} is of Type PointF but it's current value is null"));
var oldPointF = (PointF)(oldValue ?? throw new Exception($"Property {propertyName} is of Type PointF but it's current value is null"));
var designer = new PointEditor(oldPointF.X, oldPointF.Y);

Application.Run(designer);
Expand All @@ -141,7 +133,7 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa
}
}
else
if (property.PropertyInfo.PropertyType == typeof(Dim))
if (type== typeof(Dim))
{
// user is editing a Dim
var designer = new DimEditor(design, property);
Expand All @@ -160,48 +152,28 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa
}
}
else
if (property is InstanceOfProperty inst)
{
if (Modals.Get<Type>(
property.PropertyInfo.Name,
"New Value",
typeof(Label).Assembly.GetTypes().Where(inst.MustBeDerrivedFrom.IsAssignableFrom).ToArray(),
inst.GetValue()?.GetType(),
out Type? typeChosen))
{
if (typeChosen == null)
{
newValue = null;
return false;
}

newValue = Activator.CreateInstance(typeChosen);
return true;
}
}
else
if (property.PropertyInfo.PropertyType == typeof(bool))
if (type== typeof(bool))
{
int answer = ChoicesDialog.Query(property.PropertyInfo.Name, $"New value for {property.PropertyInfo.PropertyType}", "Yes", "No");
int answer = ChoicesDialog.Query(propertyName, $"New value for {type}", "Yes", "No");

newValue = answer == 0 ? true : false;
return answer != -1;
}
else
if (
// TODO: I just changed this from IsArray to IList assignable, need to worry about conversions a bit more
property.PropertyInfo.PropertyType.IsAssignableTo(typeof(IList))
type.IsAssignableTo(typeof(IList))
)
{
var elementType = property.GetElementType()
?? throw new Exception($"Property {property.GetHumanReadableName()} was array but had no element type"); ;
?? throw new Exception($"Property {propertyName} was array but had no element type"); ;

if (elementType.IsValueType || elementType == typeof(string))
{
if (Modals.GetArray(
property.PropertyInfo.Name,
propertyName,
"New Array Value",
property.PropertyInfo.PropertyType.GetElementType() ?? throw new Exception("Property was an Array but GetElementType returned null"),
type.GetElementType() ?? throw new Exception("Property was an Array but GetElementType returned null"),
(Array?)oldValue,
out Array? resultArray))
{
Expand Down Expand Up @@ -229,7 +201,7 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa

}
else
if (property.PropertyInfo.PropertyType == typeof(IListDataSource))
if (type== typeof(IListDataSource))
{
// TODO : Make this work with non strings e.g.
// if user types a bunch of numbers in or dates
Expand All @@ -241,7 +213,7 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa
.ToArray();

if (Modals.TryGetArray<string>(
property.PropertyInfo.Name,
propertyName,
"New List Value",
oldValueAsArrayOfStrings,
out Array? resultArray))
Expand All @@ -251,62 +223,62 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa
}
}
else
if (property.PropertyInfo.PropertyType.IsEnum)
if (type.IsEnum)
{
if (Modals.GetEnum(property.PropertyInfo.Name, "New Enum Value", property.PropertyInfo.PropertyType, (Enum?)property.GetValue(), out var resultEnum))
if (Modals.GetEnum(propertyName, "New Enum Value", type, (Enum?)oldValue, out var resultEnum))
{
newValue = resultEnum;
return true;
}
}
else
if (property.PropertyInfo.PropertyType == typeof(int)
|| property.PropertyInfo.PropertyType == typeof(int?)
|| property.PropertyInfo.PropertyType == typeof(uint)
|| property.PropertyInfo.PropertyType == typeof(uint?))
if (type== typeof(int)
|| type== typeof(int?)
|| type== typeof(uint)
|| type== typeof(uint?))
{
// deals with null, int and uint
var v = oldValue == null ? null : (int?)Convert.ToInt32(oldValue);

if (Modals.GetInt(property.PropertyInfo.Name, "New Int Value", v, out var resultInt))
if (Modals.GetInt(propertyName, "New Int Value", v, out var resultInt))
{
// change back to uint/int/null
newValue = resultInt == null ? null : Convert.ChangeType(resultInt, property.PropertyInfo.PropertyType);
newValue = resultInt == null ? null : Convert.ChangeType(resultInt, type);
return true;
}
}
else
if (property.PropertyInfo.PropertyType == typeof(float)
|| property.PropertyInfo.PropertyType == typeof(float?))
if (type== typeof(float)
|| type== typeof(float?))
{
if (Modals.GetFloat(property.PropertyInfo.Name, "New Float Value", (float?)oldValue, out var resultInt))
if (Modals.GetFloat(propertyName, "New Float Value", (float?)oldValue, out var resultInt))
{
newValue = resultInt;
return true;
}
}
else
if (property.PropertyInfo.PropertyType == typeof(char?)
|| property.PropertyInfo.PropertyType == typeof(char))
if (type== typeof(char?)
|| type== typeof(char))
{
if (Modals.GetChar(property.PropertyInfo.Name, "New Single Character", oldValue is null ? null : (char?)oldValue.ToPrimitive() ?? null, out var resultChar))
if (Modals.GetChar(propertyName, "New Single Character", oldValue is null ? null : (char?)oldValue.ToPrimitive() ?? null, out var resultChar))
{
newValue = resultChar;
return true;
}
}
else
if (property.PropertyInfo.PropertyType == typeof(Rune)
|| property.PropertyInfo.PropertyType == typeof(Rune?))
if (type== typeof(Rune)
|| type== typeof(Rune?))
{
if (Modals.GetChar(property.PropertyInfo.Name, "New Single Character", oldValue is null ? null : (char?)oldValue.ToPrimitive() ?? null, out var resultChar))
if (Modals.GetChar(propertyName, "New Single Character", oldValue is null ? null : (char?)oldValue.ToPrimitive() ?? null, out var resultChar))
{
newValue = resultChar == null ? null : new Rune(resultChar.Value);
return true;
}
}
else
if (Modals.GetString(property.PropertyInfo.Name, "New String Value", oldValue?.ToString(), out var result, AllowMultiLine(property)))
if (Modals.GetString(propertyName, "New String Value", oldValue?.ToString(), out var result, allowMultiLine))
{
newValue = result;
return true;
Expand All @@ -315,7 +287,38 @@ internal static bool GetNewValue(Design design, Property property, object? oldVa
newValue = null;
return false;
}
private static bool AllowMultiLine(Property property)
internal static bool GetNewValue(Design design, Property property, object? oldValue, out object? newValue)
{
if (property is InstanceOfProperty inst)
{
if (Modals.Get<Type>(
property.PropertyInfo.Name,
"New Value",
typeof(Label).Assembly.GetTypes().Where(inst.MustBeDerrivedFrom.IsAssignableFrom).ToArray(),
inst.GetValue()?.GetType(),
out Type? typeChosen))
{
if (typeChosen == null)
{
newValue = null;
return false;
}

newValue = Activator.CreateInstance(typeChosen);
return true;
}

// User cancelled dialog
newValue = null;
return false;
}
else
{
return GetNewValue(property.PropertyInfo.Name, design, property.PropertyInfo.PropertyType,oldValue, out newValue, ValueFactory.AllowMultiLine(property));
}

}
public static bool AllowMultiLine(Property property)
{
// for the text editor control let them put multiple lines in
if (property.PropertyInfo.Name.Equals("Text") && property.Design.View is TextView tv && tv.Multiline)
Expand All @@ -326,7 +329,7 @@ private static bool AllowMultiLine(Property property)
return false;
}

private static bool GetNewColorSchemeValue(Design design, Property property, out object? newValue)
private static bool GetNewColorSchemeValue(Design design, out object? newValue)
{
const string custom = "Edit Color Schemes...";
List<object> offer = new();
Expand Down
3 changes: 1 addition & 2 deletions src/UI/Windows/ArrayEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public ArrayEditor(Property property) {

private void BtnAddElement_Clicked(object sender, EventArgs e)
{

if(ValueFactory.GetNewValue(this.elementType,out var newValue))
if(ValueFactory.GetNewValue(property.PropertyInfo.Name, this.property.Design, this.elementType,null, out var newValue, ValueFactory.AllowMultiLine(property)))
{
Result.Add(newValue);
}
Expand Down
8 changes: 3 additions & 5 deletions src/UI/Windows/PosEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ public partial class PosEditor : Dialog {

/// <summary>
/// Prompt user to create a new <see cref="Pos"/> value to populate
/// <paramref name="property"/> on <paramref name="design"/> with.
/// on <paramref name="design"/> with.
/// </summary>
/// <param name="design">What to set the value on.</param>
/// <param name="property">The property to set (must be of type <see cref="Pos"/>).</param>
public PosEditor(Design design, Property property) {
/// <param name="oldValue">The current value for the property.</param>
public PosEditor(Design design, Pos oldValue) {
InitializeComponent();

this.design = design;
this.property = property;


Title = "Pos Designer";
Border.BorderStyle = LineStyle.Double;
Expand Down

0 comments on commit a300227

Please sign in to comment.