Skip to content

Commit 463d76e

Browse files
author
tznind
committed
Nullable enum support in ValueFactory GetNewValue
1 parent ac6a0a8 commit 463d76e

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/UI/ValueFactory.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ static class ValueFactory
5050
internal static bool GetNewValue(IApplication app, string propertyName, Design design, Type type, object? oldValue, out object? newValue, bool allowMultiLine)
5151
{
5252
newValue = null;
53-
5453
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(LinearRangeOption<>))
5554
{
5655
return RunEditor(app, new SliderOptionEditor(app, type.GetGenericArguments()[0], oldValue), out newValue);
@@ -155,9 +154,9 @@ internal static bool GetNewValue(IApplication app, string propertyName, Design d
155154
}
156155
}
157156
else
158-
if (type.IsEnum)
157+
if (IsEnumOrNullableEnum(type, oldValue, out var enumValue))
159158
{
160-
if (Modals.GetEnum(app, propertyName, "New Enum Value", type, (Enum?)oldValue, out var resultEnum))
159+
if (Modals.GetEnum(app, propertyName, "New Enum Value", type, enumValue, out var resultEnum))
161160
{
162161
newValue = resultEnum;
163162
return true;
@@ -223,6 +222,23 @@ internal static bool GetNewValue(IApplication app, string propertyName, Design d
223222
return false;
224223
}
225224

225+
private static bool IsEnumOrNullableEnum(Type type, object? oldValue, out Enum? enumValue)
226+
{
227+
if(type.IsEnum)
228+
{
229+
enumValue = (Enum?)oldValue;
230+
}
231+
232+
if(Nullable.GetUnderlyingType(type) is Type underlying && underlying.IsEnum)
233+
{
234+
enumValue = (Enum?)oldValue;
235+
return true;
236+
}
237+
238+
enumValue = null;
239+
return false;
240+
}
241+
226242
private static bool RunEditor<T>(IApplication app, T editor, out object? result) where T : Dialog, IValueGetterDialog
227243
{
228244
app.Run(editor);

src/UI/Windows/Modals.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ internal static bool Get<T>(IApplication app, string prompt, string okText, in b
159159

160160
internal static bool GetEnum(IApplication app, string prompt, string okText, Type enumType, Enum? currentValue, out Enum? result)
161161
{
162+
if(Nullable.GetUnderlyingType(enumType) is Type underlyingEnumType)
163+
{
164+
enumType = underlyingEnumType;
165+
}
166+
162167
return Get(app, prompt, okText, true, Enum.GetValues(enumType).Cast<Enum>().ToArray(), o => o?.ToString() ?? "Null", false, currentValue, out result);
163168
}
164169

0 commit comments

Comments
 (0)