Browse Source

IDesignable for TableView

Tig 9 months ago
parent
commit
94b254ae16

+ 60 - 1
Terminal.Gui/Views/TableView/TableView.cs

@@ -1,4 +1,5 @@
 using System.Data;
+using System.Globalization;
 
 namespace Terminal.Gui;
 
@@ -16,7 +17,7 @@ public delegate ColorScheme RowColorGetterDelegate (RowColorGetterArgs args);
 ///     View for tabular data based on a <see cref="ITableSource"/>.
 ///     <a href="../docs/tableview.md">See TableView Deep Dive for more information</a>.
 /// </summary>
-public class TableView : View
+public class TableView : View, IDesignable
 {
     /// <summary>
     ///     The default maximum cell width for <see cref="TableView.MaxCellWidth"/> and <see cref="ColumnStyle.MaxWidth"/>
@@ -2307,4 +2308,62 @@ public class TableView : View
         /// <summary>The horizontal position to begin rendering the column at</summary>
         public int X { get; set; }
     }
+
+    bool IDesignable.EnableForDesign ()
+    {
+        var dt = BuildDemoDataTable (5, 5);
+        Table = new DataTableSource (dt);
+        return true;
+    }
+
+    /// <summary>
+    ///     Generates a new demo <see cref="DataTable"/> with the given number of <paramref name="cols"/> (min 5) and
+    ///     <paramref name="rows"/>
+    /// </summary>
+    /// <param name="cols"></param>
+    /// <param name="rows"></param>
+    /// <returns></returns>
+    public static DataTable BuildDemoDataTable (int cols, int rows)
+    {
+        var dt = new DataTable ();
+
+        var explicitCols = 6;
+        dt.Columns.Add (new DataColumn ("StrCol", typeof (string)));
+        dt.Columns.Add (new DataColumn ("DateCol", typeof (DateTime)));
+        dt.Columns.Add (new DataColumn ("IntCol", typeof (int)));
+        dt.Columns.Add (new DataColumn ("DoubleCol", typeof (double)));
+        dt.Columns.Add (new DataColumn ("NullsCol", typeof (string)));
+        dt.Columns.Add (new DataColumn ("Unicode", typeof (string)));
+
+        for (var i = 0; i < cols - explicitCols; i++)
+        {
+            dt.Columns.Add ("Column" + (i + explicitCols));
+        }
+
+        var r = new Random (100);
+
+        for (var i = 0; i < rows; i++)
+        {
+            List<object> row = new ()
+            {
+                $"Demo text in row {i}",
+                new DateTime (2000 + i, 12, 25),
+                r.Next (i),
+                r.NextDouble () * i - 0.5 /*add some negatives to demo styles*/,
+                DBNull.Value,
+                "Les Mise"
+                + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber))
+                + "rables"
+            };
+
+            for (var j = 0; j < cols - explicitCols; j++)
+            {
+                row.Add ("SomeValue" + r.Next (100));
+            }
+
+            dt.Rows.Add (row.ToArray ());
+        }
+
+        return dt;
+    }
 }

+ 1 - 0
UICatalog/Scenarios/Generic.cs

@@ -1,3 +1,4 @@
+#nullable enable
 using Terminal.Gui;
 
 namespace UICatalog.Scenarios;

+ 1 - 52
UICatalog/Scenarios/TableEditor.cs

@@ -344,57 +344,6 @@ public class TableEditor : Scenario
     private ColorScheme _redColorSchemeAlt;
     private TableView _tableView;
 
-    /// <summary>
-    ///     Generates a new demo <see cref="DataTable"/> with the given number of <paramref name="cols"/> (min 5) and
-    ///     <paramref name="rows"/>
-    /// </summary>
-    /// <param name="cols"></param>
-    /// <param name="rows"></param>
-    /// <returns></returns>
-    public static DataTable BuildDemoDataTable (int cols, int rows)
-    {
-        var dt = new DataTable ();
-
-        var explicitCols = 6;
-        dt.Columns.Add (new DataColumn ("StrCol", typeof (string)));
-        dt.Columns.Add (new DataColumn ("DateCol", typeof (DateTime)));
-        dt.Columns.Add (new DataColumn ("IntCol", typeof (int)));
-        dt.Columns.Add (new DataColumn ("DoubleCol", typeof (double)));
-        dt.Columns.Add (new DataColumn ("NullsCol", typeof (string)));
-        dt.Columns.Add (new DataColumn ("Unicode", typeof (string)));
-
-        for (var i = 0; i < cols - explicitCols; i++)
-        {
-            dt.Columns.Add ("Column" + (i + explicitCols));
-        }
-
-        var r = new Random (100);
-
-        for (var i = 0; i < rows; i++)
-        {
-            List<object> row = new ()
-            {
-                "Some long text that is super cool",
-                new DateTime (2000 + i, 12, 25),
-                r.Next (i),
-                r.NextDouble () * i - 0.5 /*add some negatives to demo styles*/,
-                DBNull.Value,
-                "Les Mise"
-                + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber))
-                + "rables"
-            };
-
-            for (var j = 0; j < cols - explicitCols; j++)
-            {
-                row.Add ("SomeValue" + r.Next (100));
-            }
-
-            dt.Rows.Add (row.ToArray ());
-        }
-
-        return dt;
-    }
-
     /// <summary>
     ///     Builds a simple table in which cell values contents are the index of the cell.  This helps testing that
     ///     scrolling etc is working correctly and not skipping out any rows/columns when paging
@@ -1006,7 +955,7 @@ public class TableEditor : Scenario
 
     private void OpenExample (bool big)
     {
-        SetTable (BuildDemoDataTable (big ? 30 : 5, big ? 1000 : 5));
+        SetTable (TableView.BuildDemoDataTable (big ? 30 : 5, big ? 1000 : 5));
         SetDemoTableStyles ();
     }