Forráskód Böngészése

Merged @bdisp's latest and code cleanup

Tig 1 éve
szülő
commit
66f5fb826d
3 módosított fájl, 58 hozzáadás és 74 törlés
  1. 14 25
      Terminal.Gui/Application.cs
  2. 43 48
      UICatalog/Scenario.cs
  3. 1 1
      UICatalog/UICatalog.cs

+ 14 - 25
Terminal.Gui/Application.cs

@@ -548,7 +548,8 @@ public static partial class Application
     }
 
     /// <summary>
-    ///     Runs the application by creating a <see cref="Toplevel"/> object and calling <see cref="Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>.
+    ///     Runs the application by creating a <see cref="Toplevel"/> object and calling
+    ///     <see cref="Run(Toplevel, Func{Exception, bool}, ConsoleDriver)"/>.
     /// </summary>
     /// <remarks>
     ///     <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
@@ -585,7 +586,7 @@ public static partial class Application
     /// </param>
     /// <returns>The created T object. The caller is responsible for disposing this object.</returns>
     public static T Run<T> (Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null)
-        where T : Toplevel, new ()
+        where T : Toplevel, new()
     {
         var top = new T ();
 
@@ -636,12 +637,8 @@ public static partial class Application
     /// </param>
     public static void Run (Toplevel view, Func<Exception, bool> errorHandler = null, ConsoleDriver driver = null)
     {
-        // Validate that Init has been called and that the Toplevel is valid.
-        if (view is null)
-        {
-            throw new ArgumentException ($"{view.GetType ().Name} must be derived from TopLevel");
-        }
-
+        ArgumentNullException.ThrowIfNull (view);
+        
         if (_initialized)
         {
             if (Driver is null)
@@ -682,6 +679,7 @@ public static partial class Application
                 Debug.Assert (_topLevels.Count == 0);
 #endif
                 runState.Dispose ();
+
                 return;
             }
 
@@ -784,15 +782,8 @@ public static partial class Application
     /// <param name="state">The state returned by the <see cref="Begin(Toplevel)"/> method.</param>
     public static void RunLoop (RunState state)
     {
-        if (state is null)
-        {
-            throw new ArgumentNullException (nameof (state));
-        }
-
-        if (state.Toplevel is null)
-        {
-            throw new ObjectDisposedException ("state");
-        }
+        ArgumentNullException.ThrowIfNull (state);
+        ObjectDisposedException.ThrowIf (state.Toplevel is null, "state");
 
         var firstIteration = true;
 
@@ -1033,10 +1024,7 @@ public static partial class Application
     /// <param name="runState">The <see cref="RunState"/> returned by the <see cref="Begin(Toplevel)"/> method.</param>
     public static void End (RunState runState)
     {
-        if (runState is null)
-        {
-            throw new ArgumentNullException (nameof (runState));
-        }
+        ArgumentNullException.ThrowIfNull (runState);
 
         if (OverlappedTop is { })
         {
@@ -1125,11 +1113,12 @@ public static partial class Application
     public static Toplevel Top { get; private set; }
 
     /// <summary>
-    ///     The current <see cref="Toplevel"/> object. This is updated in <see cref="Application.Begin"/> enters and leaves to point to the current
+    ///     The current <see cref="Toplevel"/> object. This is updated in <see cref="Application.Begin"/> enters and leaves to
+    ///     point to the current
     ///     <see cref="Toplevel"/> .
     /// </summary>
     /// <remarks>
-    /// Only relevant in scenarios where <see cref="Toplevel.IsOverlappedContainer"/> is <see langword="true"/>.
+    ///     Only relevant in scenarios where <see cref="Toplevel.IsOverlappedContainer"/> is <see langword="true"/>.
     /// </remarks>
     /// <value>The current.</value>
     public static Toplevel Current { get; private set; }
@@ -1545,7 +1534,7 @@ public static partial class Application
         {
             Point frameLoc = adornment.ScreenToFrame (a.MouseEvent.X, a.MouseEvent.Y);
 
-            me = new()
+            me = new ()
             {
                 X = frameLoc.X,
                 Y = frameLoc.Y,
@@ -1558,7 +1547,7 @@ public static partial class Application
         {
             Point boundsPoint = view.ScreenToBounds (a.MouseEvent.X, a.MouseEvent.Y);
 
-            me = new()
+            me = new ()
             {
                 X = boundsPoint.X,
                 Y = boundsPoint.Y,

+ 43 - 48
UICatalog/Scenario.cs

@@ -116,6 +116,26 @@ public class Scenario : IDisposable
         return objects.OrderBy (s => s.GetName ()).ToList ();
     }
 
+    /// <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>
+    /// </remarks>
+    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
@@ -142,7 +162,7 @@ public class Scenario : IDisposable
 
         Top = new ();
 
-        Win = new()
+        Win = new ()
         {
             Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
             X = 0,
@@ -154,25 +174,6 @@ public class Scenario : IDisposable
         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`.
@@ -196,6 +197,7 @@ public class Scenario : IDisposable
     /// <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; }
 
@@ -207,10 +209,12 @@ public class Scenario : IDisposable
     ///     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
+    #region IDispose
+
     public void Dispose ()
     {
         // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
@@ -231,26 +235,25 @@ public class Scenario : IDisposable
             _disposedValue = true;
         }
     }
-#endregion IDispose
+
+    #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 ()
     {
         List<string> categories = new ();
 
-        foreach (Type type in typeof (Scenario).Assembly.GetTypes ()
-                                               .Where (
-                                                       myType => myType.IsClass
-                                                                 && !myType.IsAbstract
-                                                                 && myType.IsSubclassOf (typeof (Scenario))
-                                                      ))
-        {
-            List<System.Attribute> attrs = System.Attribute.GetCustomAttributes (type).ToList ();
-
-            categories = categories
-                         .Union (attrs.Where (a => a is ScenarioCategory).Select (a => ((ScenarioCategory)a).Name))
-                         .ToList ();
-        }
+        categories = typeof (Scenario).Assembly.GetTypes ()
+                                      .Where (
+                                              myType => myType.IsClass
+                                                        && !myType.IsAbstract
+                                                        && myType.IsSubclassOf (typeof (Scenario)))
+                                      .Select (type => System.Attribute.GetCustomAttributes (type).ToList ())
+                                      .Aggregate (
+                                                  categories,
+                                                  (current, attrs) => current
+                                                                      .Union (attrs.Where (a => a is ScenarioCategory).Select (a => ((ScenarioCategory)a).Name))
+                                                                      .ToList ());
 
         // Sort
         categories = categories.OrderBy (c => c).ToList ();
@@ -263,10 +266,8 @@ public class Scenario : IDisposable
 
     /// <summary>Defines the category names used to catagorize a <see cref="Scenario"/></summary>
     [AttributeUsage (AttributeTargets.Class, AllowMultiple = true)]
-    public class ScenarioCategory : System.Attribute
+    public class ScenarioCategory (string Name) : System.Attribute
     {
-        public ScenarioCategory (string Name) { this.Name = Name; }
-
         /// <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>
@@ -285,21 +286,15 @@ public class Scenario : IDisposable
         public static string GetName (Type t) { return ((ScenarioCategory)GetCustomAttributes (t) [0]).Name; }
 
         /// <summary>Category Name</summary>
-        public string Name { get; set; }
+        public string Name { get; set; } = Name;
     }
 
     /// <summary>Defines the metadata (Name and Description) for a <see cref="Scenario"/></summary>
     [AttributeUsage (AttributeTargets.Class)]
-    public class ScenarioMetadata : System.Attribute
+    public class ScenarioMetadata (string Name, string Description) : System.Attribute
     {
-        public ScenarioMetadata (string Name, string Description)
-        {
-            this.Name = Name;
-            this.Description = Description;
-        }
-
         /// <summary><see cref="Scenario"/> Description</summary>
-        public string Description { get; set; }
+        public string Description { get; set; } = Description;
 
         /// <summary>Static helper function to get the <see cref="Scenario"/> Description given a Type</summary>
         /// <param name="t"></param>
@@ -312,6 +307,6 @@ public class Scenario : IDisposable
         public static string GetName (Type t) { return ((ScenarioMetadata)GetCustomAttributes (t) [0]).Name; }
 
         /// <summary><see cref="Scenario"/> Name</summary>
-        public string Name { get; set; }
+        public string Name { get; set; } = Name;
     }
 }

+ 1 - 1
UICatalog/UICatalog.cs

@@ -199,7 +199,7 @@ internal class UICatalogApp
             Apply ();
         }
 
-        Application.Run<UICatalogTopLevel> ();
+        Application.Run<UICatalogTopLevel> ().Dispose ();
         Application.Shutdown ();
 
         return _selectedScenario!;