Skip to content

Commit

Permalink
Fixed more bad code
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Mar 25, 2024
1 parent 9c0fb02 commit 6ebee8d
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 189 deletions.
180 changes: 84 additions & 96 deletions Terminal.Gui/Application.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Dialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Terminal.Gui;
/// </summary>
/// <remarks>
/// To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to
/// <see cref="Application.Run(Func{Exception, bool})"/>. This will execute the dialog until it terminates via the
/// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>. This will execute the dialog until it terminates via the
/// [ESC] or [CTRL-Q] key, or when one of the views or buttons added to the dialog calls
/// <see cref="Application.RequestStop"/>.
/// </remarks>
Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/Views/FileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public bool AllowsMultipleSelection

/// <summary>
/// Gets all files/directories selected or an empty collection <see cref="AllowsMultipleSelection"/> is
/// <see langword="false"/> or <see cref="Canceled"/>.
/// <see langword="false"/> or <see cref="CancelSearch"/>.
/// </summary>
/// <remarks>If selecting only a single file/directory then you should use <see cref="Path"/> instead.</remarks>
public IReadOnlyList<string> MultiSelected { get; private set; }
Expand All @@ -359,7 +359,7 @@ public bool AllowsMultipleSelection

/// <summary>
/// Gets or Sets the selected path in the dialog. This is the result that should be used if
/// <see cref="AllowsMultipleSelection"/> is off and <see cref="Canceled"/> is true.
/// <see cref="AllowsMultipleSelection"/> is off and <see cref="CancelSearch"/> is true.
/// </summary>
public string Path
{
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/OpenDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public enum OpenMode
/// </para>
/// <para>
/// To use, create an instance of <see cref="OpenDialog"/>, and pass it to
/// <see cref="Application.Run(Func{Exception, bool})"/>. This will run the dialog modally, and when this returns,
/// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>. This will run the dialog modally, and when this returns,
/// the list of files will be available on the <see cref="FilePaths"/> property.
/// </para>
/// <para>To select more than one file, users can use the spacebar, or control-t.</para>
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/SaveDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Terminal.Gui;
/// <remarks>
/// <para>
/// To use, create an instance of <see cref="SaveDialog"/>, and pass it to
/// <see cref="Application.Run(Func{Exception, bool})"/>. This will run the dialog modally, and when this returns,
/// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>. This will run the dialog modally, and when this returns,
/// the <see cref="FileName"/>property will contain the selected file name or null if the user canceled.
/// </para>
/// </remarks>
Expand Down
6 changes: 3 additions & 3 deletions Terminal.Gui/Views/Toplevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace Terminal.Gui;
/// <remarks>
/// <para>
/// Toplevels can run as modal (popup) views, started by calling
/// <see cref="Application.Run(Toplevel, Func{Exception,bool})"/>. They return control to the caller when
/// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>. They return control to the caller when
/// <see cref="Application.RequestStop(Toplevel)"/> has been called (which sets the <see cref="Toplevel.Running"/>
/// property to <c>false</c>).
/// </para>
/// <para>
/// A Toplevel is created when an application initializes Terminal.Gui by calling <see cref="Application.Init"/>.
/// The application Toplevel can be accessed via <see cref="Application.Top"/>. Additional Toplevels can be created
/// and run (e.g. <see cref="Dialog"/>s. To run a Toplevel, create the <see cref="Toplevel"/> and call
/// <see cref="Application.Run(Toplevel, Func{Exception,bool})"/>.
/// <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>.
/// </para>
/// </remarks>
public partial class Toplevel : View
Expand Down Expand Up @@ -452,7 +452,7 @@ out StatusBar sb
/// <inheritdoc/>
public override void Remove (View view)
{
if (this is Toplevel Toplevel && Toplevel.MenuBar is { })
if (this is Toplevel { MenuBar: { } })
{
RemoveMenuStatusBar (view);
}
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Wizard/Wizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public WizardStep CurrentStep
/// <description>Add the Wizard to a containing view with <see cref="View.Add(View)"/>.</description>
/// </item>
/// </list>
/// If a non-Modal Wizard is added to the application after <see cref="Application.Run(Func{Exception, bool})"/> has
/// If a non-Modal Wizard is added to the application after <see cref="Application.Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/> has
/// been called the first step must be explicitly set by setting <see cref="CurrentStep"/> to
/// <see cref="GetNextStep()"/>:
/// <code>
Expand Down
77 changes: 47 additions & 30 deletions UICatalog/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,6 @@ public class Scenario : IDisposable
public string TopLevelColorScheme = "Base";
private bool _disposedValue;

/// <summary>
/// The Toplevel for the <see cref="Scenario"/>. This should be set to <see cref="Terminal.Gui.Application.Top"/>.
/// </summary>
public Toplevel Top { get; set; }

/// <summary>
/// The Window for the <see cref="Scenario"/>. This should be set to <see cref="Terminal.Gui.Application.Top"/> in
/// most cases.
/// </summary>
public Window Win { get; set; }


/// <summary>
/// Helper function to get the list of categories a <see cref="Scenario"/> belongs to (defined in
/// <see cref="ScenarioCategory"/>)
Expand Down Expand Up @@ -128,17 +116,9 @@ public static List<Scenario> GetScenarios ()
return objects.OrderBy (s => s.GetName ()).ToList ();
}


public virtual void Main ()
{
Init ();
Setup ();
Run ();
}


/// <summary>
/// Helper that calls <see cref="Application.Init"/> and creates the default <see cref="Terminal.Gui.Window"/> implementation with a frame and label
/// Helper that calls <see cref="Application.Init"/> and creates the default <see cref="Terminal.Gui.Window"/>
/// implementation with a frame and label
/// showing the name of the <see cref="Scenario"/> and logic to exit back to the Scenario picker UI. Override
/// <see cref="Init"/> to provide any <see cref="Terminal.Gui.Toplevel"/> behavior needed.
/// </summary>
Expand All @@ -152,6 +132,7 @@ public virtual void Main ()
/// creating any views or calling other Terminal.Gui APIs.
/// </para>
/// </remarks>
[ObsoleteAttribute ("This method is obsolete and will be removed in v2. Use Main instead.", false)]
public virtual void Init ()
{
Application.Init ();
Expand All @@ -161,7 +142,7 @@ public virtual void Init ()

Top = new ();

Win = new Window
Win = new()
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
X = 0,
Expand All @@ -173,6 +154,25 @@ public virtual void Init ()
Top.Add (Win);
}

/// <summary>
/// Called by UI Catalog to run the <see cref="Scenario"/>. This is the main entry point for the <see cref="Scenario"/>
/// .
/// </summary>
/// <remarks>
/// <para>
/// Scenario developers are encouraged to override this method as the primary way of authoring a new
/// scenario.
/// </para>
/// <para>
/// The base implementation calls <see cref="Init"/>, <see cref="Setup"/>, and <see cref="Run"/>.
/// </para>
public virtual void Main ()
{
Init ();
Setup ();
Run ();
}

/// <summary>
/// Runs the <see cref="Scenario"/>. Override to start the <see cref="Scenario"/> using a <see cref="Toplevel"/>
/// different than `Top`.
Expand All @@ -181,6 +181,7 @@ public virtual void Init ()
/// Overrides that do not call the base.<see cref="Run"/>, must call <see cref="Application.Shutdown"/> before
/// returning.
/// </remarks>
[ObsoleteAttribute ("This method is obsolete and will be removed in v2. Use Main instead.", false)]
public virtual void Run ()
{
// Must explicitly call Application.Shutdown method to shutdown.
Expand All @@ -189,12 +190,27 @@ public virtual void Run ()

/// <summary>Override this to implement the <see cref="Scenario"/> setup logic (create controls, etc...).</summary>
/// <remarks>This is typically the best place to put scenario logic code.</remarks>
[ObsoleteAttribute ("This method is obsolete and will be removed in v2. Use Main instead.", false)]
public virtual void Setup () { }

/// <summary>
/// The Toplevel for the <see cref="Scenario"/>. This should be set to <see cref="Terminal.Gui.Application.Top"/>.
/// </summary>
//[ObsoleteAttribute ("This property is obsolete and will be removed in v2. Use Main instead.", false)]
public Toplevel Top { get; set; }

/// <summary>Gets the Scenario Name + Description with the Description padded based on the longest known Scenario name.</summary>
/// <returns></returns>
public override string ToString () { return $"{GetName ().PadRight (_maxScenarioNameLen)}{GetDescription ()}"; }


/// <summary>
/// The Window for the <see cref="Scenario"/>. This should be set to <see cref="Terminal.Gui.Application.Top"/> in
/// most cases.
/// </summary>
//[ObsoleteAttribute ("This property is obsolete and will be removed in v2. Use Main instead.", false)]
public Window Win { get; set; }

#region IDispose
public void Dispose ()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Expand All @@ -215,6 +231,7 @@ protected virtual void Dispose (bool disposing)
_disposedValue = true;
}
}
#endregion IDispose

/// <summary>Returns a list of all Categories set by all of the <see cref="Scenario"/>s defined in the project.</summary>
internal static List<string> GetAllCategories ()
Expand Down Expand Up @@ -250,9 +267,6 @@ public class ScenarioCategory : System.Attribute
{
public ScenarioCategory (string Name) { this.Name = Name; }

/// <summary>Category Name</summary>
public string Name { get; set; }

/// <summary>Static helper function to get the <see cref="Scenario"/> Categories given a Type</summary>
/// <param name="t"></param>
/// <returns>list of category names</returns>
Expand All @@ -269,6 +283,9 @@ public static List<string> GetCategories (Type t)
/// <param name="t"></param>
/// <returns>Name of the category</returns>
public static string GetName (Type t) { return ((ScenarioCategory)GetCustomAttributes (t) [0]).Name; }

/// <summary>Category Name</summary>
public string Name { get; set; }
}

/// <summary>Defines the metadata (Name and Description) for a <see cref="Scenario"/></summary>
Expand All @@ -284,9 +301,6 @@ public ScenarioMetadata (string Name, string Description)
/// <summary><see cref="Scenario"/> Description</summary>
public string Description { get; set; }

/// <summary><see cref="Scenario"/> Name</summary>
public string Name { get; set; }

/// <summary>Static helper function to get the <see cref="Scenario"/> Description given a Type</summary>
/// <param name="t"></param>
/// <returns></returns>
Expand All @@ -296,5 +310,8 @@ public ScenarioMetadata (string Name, string Description)
/// <param name="t"></param>
/// <returns></returns>
public static string GetName (Type t) { return ((ScenarioMetadata)GetCustomAttributes (t) [0]).Name; }

/// <summary><see cref="Scenario"/> Name</summary>
public string Name { get; set; }
}
}
15 changes: 10 additions & 5 deletions UnitTests/Application/ApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -974,15 +974,20 @@ public void Run_Creates_Top_Without_Init ()
Assert.NotNull (Application.Top);
Application.RequestStop ();
};
Application.Run (null, driver);
var top = Application.Run (null, driver);
#if DEBUG_IDISPOSABLE
Assert.False (Application.Top.WasDisposed);
Assert.Equal(top, Application.Top);
Assert.False (top.WasDisposed);
var exception = Record.Exception (() => Application.Shutdown ());
Assert.NotNull (exception);
Assert.False (Application.Top.WasDisposed);
Assert.False (top.WasDisposed);
#endif

// It's up to caller to dispose it
Application.Top.Dispose ();
Assert.True (Application.Top.WasDisposed);
top.Dispose ();

#if DEBUG_IDISPOSABLE
Assert.True (top.WasDisposed);
#endif
Assert.NotNull (Application.Top);

Expand Down
4 changes: 2 additions & 2 deletions UnitTests/Application/SynchronizatonContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void SynchronizationContext_Post ()
);

// blocks here until the RequestStop is processed at the end of the test
Application.Run ();
Application.Run ().Dispose ();
Assert.True (success);
}

Expand Down Expand Up @@ -79,7 +79,7 @@ public void SynchronizationContext_Send ()
);

// blocks here until the RequestStop is processed at the end of the test
Application.Run ();
Application.Run ().Dispose ();
Assert.True (success);
}
}
3 changes: 1 addition & 2 deletions UnitTests/Dialogs/DialogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,7 @@ public void Dialog_Opened_From_Another_Dialog ()
}
};

Run ();
Top.Dispose ();
Run ().Dispose ();
Shutdown ();

Assert.Equal (4, iterations);
Expand Down
18 changes: 9 additions & 9 deletions UnitTests/Dialogs/MessageBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void KeyBindings_Enter_Causes_Focused_Button_Click ()
break;
}
};
Application.Run ();
Application.Run ().Dispose ();

Assert.Equal (1, result);
}
Expand Down Expand Up @@ -77,7 +77,7 @@ public void KeyBindings_Esc_Closes ()
break;
}
};
Application.Run ();
Application.Run ().Dispose ();

Assert.Equal (-1, result);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public void KeyBindings_Space_Causes_Focused_Button_Click ()
break;
}
};
Application.Run ();
Application.Run ().Dispose ();

Assert.Equal (1, result);
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public void Location_Default ()
}
};

Application.Run ();
Application.Run ().Dispose ();
}

[Theory]
Expand Down Expand Up @@ -233,7 +233,7 @@ int linesLength
}
};

Application.Run ();
Application.Run ().Dispose ();
}

[Fact]
Expand Down Expand Up @@ -604,7 +604,7 @@ public void Size_Default ()
}
};

Application.Run ();
Application.Run ().Dispose ();
}

[Fact]
Expand Down Expand Up @@ -655,7 +655,7 @@ public void Size_JustBigEnough_Fixed_Size ()
}
};

Application.Run ();
Application.Run ().Dispose ();
}

[Fact]
Expand Down Expand Up @@ -756,7 +756,7 @@ public void Size_None_No_Buttons ()
}
};

Application.Run ();
Application.Run ().Dispose ();
}

[Theory]
Expand Down Expand Up @@ -905,6 +905,6 @@ public void Size_Tiny_Fixed_Size ()
}
};

Application.Run ();
Application.Run ().Dispose ();
}
}
Loading

0 comments on commit 6ebee8d

Please sign in to comment.