Skip to content

Commit

Permalink
Start simplifying ValueFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed Aug 25, 2024
1 parent 928fc59 commit 2cb286a
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 131 deletions.
150 changes: 31 additions & 119 deletions src/UI/ValueFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,141 +17,39 @@ internal static bool GetNewValue(string propertyName, Design design, Type type,

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(SliderOption<>))
{
var designer = new SliderOptionEditor(type.GetGenericArguments()[0], oldValue);
Application.Run(designer);

if (!designer.Cancelled)
{
newValue = designer.Result;
return true;
}
else
{
// user canceled designing the Option
return false;
}
return RunEditor(new SliderOptionEditor(type.GetGenericArguments()[0], oldValue), out newValue);
}
else
if (type== typeof(ColorScheme))
else if (type == typeof(ColorScheme))
{
return GetNewColorSchemeValue(design, out newValue);
}
else
if (type == typeof(Attribute) ||
type == typeof(Attribute?))
else if (type == typeof(Attribute) || type == typeof(Attribute?))
{
// if its an Attribute or nullableAttribute
var picker = new ColorPicker((Attribute?)oldValue);
Application.Run(picker);

if (!picker.Cancelled)
{
newValue = picker.Result;
return true;
}
else
{
// user cancelled designing the Color
newValue = null;
return false;
}
return RunEditor(new ColorPicker((Attribute?)oldValue), out newValue);
}
else
if (type== typeof(ITextValidateProvider))
else if (type == typeof(Pos))
{
string? oldPattern = oldValue is TextRegexProvider r ? (string?)r.Pattern.ToPrimitive() : null;
if (Modals.GetString("New Validation Pattern", "Regex Pattern", oldPattern, out var newPattern))
{
newValue = string.IsNullOrWhiteSpace(newPattern) ? null : new TextRegexProvider(newPattern);
return true;
}

// user cancelled entering a pattern
newValue = null;
return false;
return RunEditor(new PosEditor(design, (Pos)oldValue ?? throw new Exception("Pos property was unexpectedly null")), out newValue);
}
else
if (type== typeof(Pos))
else if (type == typeof(Size))
{
// user is editing a Pos
var designer = new PosEditor(design, (Pos)oldValue?? throw new Exception("Pos property was unexpectedly null"));

Application.Run(designer);

if (!designer.Cancelled)
{
newValue = designer.Result;
return true;
}
else
{
// user cancelled designing the Pos
newValue = null;
return false;
}
return RunEditor(new SizeEditor((Size)(oldValue ?? throw new Exception($"Property {propertyName} is of Type Size but its current value is null"))), out newValue);
}
else
if (type== typeof(Size))
else if (type == typeof(PointF))
{
// user is editing a Size
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);

if (!designer.Cancelled)
{
newValue = designer.Result;
return true;
}
else
{
// user cancelled designing the Pos
newValue = null;
return false;
}
var oldPointF = (PointF)(oldValue ?? throw new Exception($"Property {propertyName} is of Type PointF but its current value is null"));
return RunEditor(new PointEditor(oldPointF.X, oldPointF.Y), out newValue);
}
else
if (type== typeof(PointF))
else if (type == typeof(Dim))
{
// user is editing a PointF
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);

if (!designer.Cancelled)
{
newValue = new PointF(designer.ResultX, designer.ResultY);
return true;
}
else
{
// user cancelled designing the Pos
newValue = null;
return false;
}
return RunEditor(new DimEditor(design, (Dim)oldValue), out newValue);
}
else
if (type== typeof(Dim))
else if (type == typeof(bool))
{
// user is editing a Dim
var designer = new DimEditor(design, (Dim)oldValue);
Application.Run(designer);

if (!designer.Cancelled)
{
newValue = designer.Result;
return true;
}
else
{
// user cancelled designing the Dim
newValue = null;
return false;
}
int answer = ChoicesDialog.Query(propertyName, $"New value for {type}", "Yes", "No");
newValue = answer == 0 ? true : false;
return answer != -1;
}
else
if (type== typeof(bool))
{
int answer = ChoicesDialog.Query(propertyName, $"New value for {type}", "Yes", "No");
Expand Down Expand Up @@ -316,6 +214,20 @@ internal static bool GetNewValue(string propertyName, Design design, Type type,
newValue = null;
return false;
}

private static bool RunEditor<T>(T editor, out object? result) where T : Dialog, IValueGetterDialog
{
Application.Run(editor);
if (editor.Cancelled)
{
result = null;
return false;
}

result = editor.Result;
return true;
}

internal static bool GetNewValue(Design design, Property property, object? oldValue, out object? newValue)
{
if (property is InstanceOfProperty inst)
Expand Down
4 changes: 2 additions & 2 deletions src/UI/Windows/ColorPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ namespace TerminalGuiDesigner.UI.Windows;
/// <summary>
/// Prompts user to pick a two <see cref="Color"/> to make an <see cref="Attribute"/>.
/// </summary>
public partial class ColorPicker
public partial class ColorPicker : IValueGetterDialog
{
/// <summary>
/// The combination of foreground and background <see cref="Color"/> the user chose.
/// </summary>
public Terminal.Gui.Attribute? Result { get; internal set; }
public object Result { get; internal set; }

/// <summary>
/// True if user closed dialog with cancel.
Expand Down
4 changes: 2 additions & 2 deletions src/UI/Windows/ColorSchemeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ private MutableColorScheme Clone(ColorScheme scheme)

private Terminal.Gui.Attribute PickNewColorsFor(Terminal.Gui.Attribute current)
{
var pick = new ColorPicker(current);
var pick = new Windows.ColorPicker(current);
Application.Run(pick);

return pick.Cancelled ? current : pick.Result ?? current;
return pick.Cancelled ? current : (Attribute?)pick.Result ?? current;
}

private void SetColorPatches()
Expand Down
7 changes: 5 additions & 2 deletions src/UI/Windows/DimEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// </auto-generated>
//------------------------------------------------------------------------------

using JetBrains.Annotations;

namespace TerminalGuiDesigner.UI.Windows;

using Terminal.Gui;
Expand All @@ -16,15 +18,16 @@ namespace TerminalGuiDesigner.UI.Windows;
/// <summary>
/// Editor for the <see cref="Dim"/> class.
/// </summary>
public partial class DimEditor : Dialog
public partial class DimEditor : Dialog, IValueGetterDialog
{
private Design design;

/// <summary>
/// The final <see cref="Dim"/> value user has configured based on
/// radio buttons and text box values.
/// </summary>
public Dim Result { get; private set; }
[CanBeNull]
public object Result { get; private set; }

/// <summary>
/// True if dialog was canceled.
Expand Down
8 changes: 8 additions & 0 deletions src/UI/Windows/IValueGetterDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#nullable disable
namespace TerminalGuiDesigner.UI.Windows;

public interface IValueGetterDialog
{
public object? Result { get; }
public bool Cancelled { get; }
}
7 changes: 6 additions & 1 deletion src/UI/Windows/PointEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
// </auto-generated>
//------------------------------------------------------------------------------

using JetBrains.Annotations;
using TerminalGuiDesigner.UI.Windows;

namespace TerminalGuiDesigner;
using System;
using Terminal.Gui;

/// <summary>
/// Pop-up editor for creating a <see cref="Point"/>.
/// </summary>
public partial class PointEditor {
public partial class PointEditor : IValueGetterDialog {

/// <summary>
/// Gets a value indicating whether user cancelled the dialog before
Expand All @@ -32,6 +35,8 @@ public partial class PointEditor {
/// </summary>
public float ResultY {get;private set;}

[CanBeNull] public object Result => new PointF(ResultX, ResultY);

/// <summary>
/// Creates a new instance of the <see cref="PointEditor"/> class.
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions src/UI/Windows/PosEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// </auto-generated>
//------------------------------------------------------------------------------

using JetBrains.Annotations;

namespace TerminalGuiDesigner.UI.Windows;

using Terminal.Gui;
Expand All @@ -18,15 +20,16 @@ namespace TerminalGuiDesigner.UI.Windows;
/// <summary>
/// Editor for the <see cref="Pos"/> type.
/// </summary>
public partial class PosEditor : Dialog {
public partial class PosEditor : Dialog, IValueGetterDialog {

private Design design;

/// <summary>
/// Users configured <see cref="Pos"/> (assembled from radio button
/// selected and text values entered - offset etc).
/// </summary>
public Pos Result { get; private set; }
[CanBeNull]
public object Result { get; private set; }

/// <summary>
/// True if user cancelled the dialog instead of hitting Ok.
Expand Down
4 changes: 2 additions & 2 deletions src/UI/Windows/SizeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace TerminalGuiDesigner.UI.Windows;
/// <summary>
/// Popup editor for the <see cref="Size"/> class.
/// </summary>
public partial class SizeEditor
public partial class SizeEditor : IValueGetterDialog
{

/// <summary>
/// The users edited <see cref="Size"/>
/// </summary>
public Size Result { get; private set; }
public object? Result { get; private set; }

/// <summary>
/// True if user cancelled the dialog instead of hitting "Ok".
Expand Down
5 changes: 4 additions & 1 deletion src/UI/Windows/SliderOptionEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
// You can make changes to this file and they will not be overwritten when saving.
// </auto-generated>
// -----------------------------------------------------------------------------


namespace TerminalGuiDesigner.UI.Windows {
using System.Reflection;
using System.Text;
using Terminal.Gui;


public partial class SliderOptionEditor {
public partial class SliderOptionEditor : IValueGetterDialog
{
private readonly Type genericTypeArgument;
private readonly Type sliderOptionType;

Expand Down

0 comments on commit 2cb286a

Please sign in to comment.