Browse Source

Fixed broken unit tests.
Upgraded MessageBoxes scenario.
Tweaked validation logic

Tig 1 year ago
parent
commit
87a76669b7

+ 24 - 21
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -1030,30 +1030,30 @@ public partial class View
     // Diagnostics to highlight when X or Y is read before the view has been initialized
     // Diagnostics to highlight when X or Y is read before the view has been initialized
     private Pos VerifyIsInitialized (Pos pos, string member)
     private Pos VerifyIsInitialized (Pos pos, string member)
     {
     {
-//#if DEBUG
-//        if (pos.ReferencesOtherViews () && !IsInitialized)
-//        {
-//            Debug.WriteLine (
-//                             $"WARNING: {member} = {pos} of {this} is dependent on other views and {member} "
-//                             + $"is being accessed before the View has been initialized. This is likely a bug."
-//                            );
-//        }
-//#endif // DEBUG
+        //#if DEBUG
+        //        if (pos.ReferencesOtherViews () && !IsInitialized)
+        //        {
+        //            Debug.WriteLine (
+        //                             $"WARNING: {member} = {pos} of {this} is dependent on other views and {member} "
+        //                             + $"is being accessed before the View has been initialized. This is likely a bug."
+        //                            );
+        //        }
+        //#endif // DEBUG
         return pos;
         return pos;
     }
     }
 
 
     // Diagnostics to highlight when Width or Height is read before the view has been initialized
     // Diagnostics to highlight when Width or Height is read before the view has been initialized
     private Dim VerifyIsInitialized (Dim dim, string member)
     private Dim VerifyIsInitialized (Dim dim, string member)
     {
     {
-//#if DEBUG
-//        if (dim.ReferencesOtherViews () && !IsInitialized)
-//        {
-//            Debug.WriteLine (
-//                             $"WARNING: {member} = {dim} of {this} is dependent on other views and {member} "
-//                             + $"is being accessed before the View has been initialized. This is likely a bug."
-//                            );
-//        }
-//#endif // DEBUG
+        //#if DEBUG
+        //        if (dim.ReferencesOtherViews () && !IsInitialized)
+        //        {
+        //            Debug.WriteLine (
+        //                             $"WARNING: {member} = {dim} of {this} is dependent on other views and {member} "
+        //                             + $"is being accessed before the View has been initialized. This is likely a bug."
+        //                            );
+        //        }
+        //#endif // DEBUG
         return dim;
         return dim;
     }
     }
 
 
@@ -1075,21 +1075,24 @@ public partial class View
     /// <exception cref="InvalidOperationException"></exception>
     /// <exception cref="InvalidOperationException"></exception>
     private void CheckDimAuto ()
     private void CheckDimAuto ()
     {
     {
-        if (!ValidatePosDim || !IsInitialized || (Width is not DimAuto && Height is not DimAuto))
+        if (!ValidatePosDim || !IsInitialized)
         {
         {
             return;
             return;
         }
         }
 
 
+        DimAuto? widthAuto = Width as DimAuto;
+        DimAuto? heightAuto = Height as DimAuto;
+
         // Verify none of the subviews are using Dim objects that depend on the SuperView's dimensions.
         // Verify none of the subviews are using Dim objects that depend on the SuperView's dimensions.
         foreach (View view in Subviews)
         foreach (View view in Subviews)
         {
         {
-            if (Width is DimAuto { MinimumContentDim: null })
+            if (widthAuto is { } && widthAuto.Style.HasFlag (DimAutoStyle.Content) && _contentSize is null)
             {
             {
                 ThrowInvalid (view, view.Width, nameof (view.Width));
                 ThrowInvalid (view, view.Width, nameof (view.Width));
                 ThrowInvalid (view, view.X, nameof (view.X));
                 ThrowInvalid (view, view.X, nameof (view.X));
             }
             }
 
 
-            if (Height is DimAuto { MinimumContentDim: null })
+            if (heightAuto is { } && heightAuto.Style.HasFlag (DimAutoStyle.Content) && _contentSize is null)
             {
             {
                 ThrowInvalid (view, view.Height, nameof (view.Height));
                 ThrowInvalid (view, view.Height, nameof (view.Height));
                 ThrowInvalid (view, view.Y, nameof (view.Y));
                 ThrowInvalid (view, view.Y, nameof (view.Y));

+ 1 - 1
Terminal.Gui/Views/Dialog.cs

@@ -59,7 +59,7 @@ public class Dialog : Window
         Arrangement = ViewArrangement.Movable;
         Arrangement = ViewArrangement.Movable;
         X = Pos.Center ();
         X = Pos.Center ();
         Y = Pos.Center ();
         Y = Pos.Center ();
-        ValidatePosDim = true;
+        //ValidatePosDim = true;
 
 
         Width = Dim.Percent (85); 
         Width = Dim.Percent (85); 
         Height = Dim.Percent (85);
         Height = Dim.Percent (85);

+ 40 - 40
UICatalog/Scenarios/MessageBoxes.cs

@@ -9,12 +9,28 @@ namespace UICatalog.Scenarios;
 [ScenarioCategory ("Dialogs")]
 [ScenarioCategory ("Dialogs")]
 public class MessageBoxes : Scenario
 public class MessageBoxes : Scenario
 {
 {
-    public override void Setup ()
+    public override void Main ()
     {
     {
-        var frame = new FrameView { X = Pos.Center (), Y = 1, Width = Dim.Percent (75), Title = "MessageBox Options" };
-        Win.Add (frame);
+        Application.Init ();
 
 
-        var label = new Label { X = 0, Y = 0, TextAlignment = TextAlignment.Right, Text = "Width:" };
+        Window app = new ()
+        {
+            Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
+        };
+
+        var frame = new FrameView
+        {
+            X = Pos.Center (),
+            Y = 1,
+            Width = Dim.Percent (75),
+            Height = Dim.Auto (DimAutoStyle.Content),
+            Title = "MessageBox Options"
+
+        };
+        app.Add (frame);
+
+        // TODO: Use Pos.Align her to demo aligning labels and fields
+        var label = new Label { X = 0, Y = 0, Width = 15, TextAlignment = TextAlignment.Right, Text = "Width:" };
         frame.Add (label);
         frame.Add (label);
 
 
         var widthEdit = new TextField
         var widthEdit = new TextField
@@ -27,7 +43,7 @@ public class MessageBoxes : Scenario
         };
         };
         frame.Add (widthEdit);
         frame.Add (widthEdit);
 
 
-        label = new()
+        label = new ()
         {
         {
             X = 0,
             X = 0,
             Y = Pos.Bottom (label),
             Y = Pos.Bottom (label),
@@ -62,7 +78,7 @@ public class MessageBoxes : Scenario
                    }
                    }
                   );
                   );
 
 
-        label = new()
+        label = new ()
         {
         {
             X = 0,
             X = 0,
             Y = Pos.Bottom (label),
             Y = Pos.Bottom (label),
@@ -84,7 +100,7 @@ public class MessageBoxes : Scenario
         };
         };
         frame.Add (titleEdit);
         frame.Add (titleEdit);
 
 
-        label = new()
+        label = new ()
         {
         {
             X = 0,
             X = 0,
             Y = Pos.Bottom (label),
             Y = Pos.Bottom (label),
@@ -106,7 +122,7 @@ public class MessageBoxes : Scenario
         };
         };
         frame.Add (messageEdit);
         frame.Add (messageEdit);
 
 
-        label = new()
+        label = new ()
         {
         {
             X = 0,
             X = 0,
             Y = Pos.Bottom (messageEdit),
             Y = Pos.Bottom (messageEdit),
@@ -128,7 +144,7 @@ public class MessageBoxes : Scenario
         };
         };
         frame.Add (numButtonsEdit);
         frame.Add (numButtonsEdit);
 
 
-        label = new()
+        label = new ()
         {
         {
             X = 0,
             X = 0,
             Y = Pos.Bottom (label),
             Y = Pos.Bottom (label),
@@ -150,7 +166,7 @@ public class MessageBoxes : Scenario
         };
         };
         frame.Add (defaultButtonEdit);
         frame.Add (defaultButtonEdit);
 
 
-        label = new()
+        label = new ()
         {
         {
             X = 0,
             X = 0,
             Y = Pos.Bottom (label),
             Y = Pos.Bottom (label),
@@ -174,30 +190,13 @@ public class MessageBoxes : Scenario
         };
         };
         frame.Add (ckbWrapMessage);
         frame.Add (ckbWrapMessage);
 
 
-        frame.ValidatePosDim = true;
+        //frame.ValidatePosDim = true;
 
 
-        void Top_LayoutComplete (object sender, EventArgs args)
-        {
-            frame.Height =
-                widthEdit.Frame.Height
-                + heightEdit.Frame.Height
-                + titleEdit.Frame.Height
-                + messageEdit.Frame.Height
-                + numButtonsEdit.Frame.Height
-                + defaultButtonEdit.Frame.Height
-                + styleRadioGroup.Frame.Height
-                + ckbWrapMessage.Frame.Height
-                + frame.GetAdornmentsThickness ().Vertical;
-            Top.Loaded -= Top_LayoutComplete;
-        }
-
-        Top.LayoutComplete += Top_LayoutComplete;
-
-        label = new()
+        label = new ()
         {
         {
             X = Pos.Center (), Y = Pos.Bottom (frame) + 2, TextAlignment = TextAlignment.Right, Text = "Button Pressed:"
             X = Pos.Center (), Y = Pos.Bottom (frame) + 2, TextAlignment = TextAlignment.Right, Text = "Button Pressed:"
         };
         };
-        Win.Add (label);
+        app.Add (label);
 
 
         var buttonPressedLabel = new Label
         var buttonPressedLabel = new Label
         {
         {
@@ -235,8 +234,7 @@ public class MessageBoxes : Scenario
                                                if (styleRadioGroup.SelectedItem == 0)
                                                if (styleRadioGroup.SelectedItem == 0)
                                                {
                                                {
                                                    buttonPressedLabel.Text =
                                                    buttonPressedLabel.Text =
-                                                       $"{
-                                                           MessageBox.Query (
+                                                       $"{MessageBox.Query (
                                                                              width,
                                                                              width,
                                                                              height,
                                                                              height,
                                                                              titleEdit.Text,
                                                                              titleEdit.Text,
@@ -244,14 +242,12 @@ public class MessageBoxes : Scenario
                                                                              defaultButton,
                                                                              defaultButton,
                                                                              (bool)ckbWrapMessage.Checked,
                                                                              (bool)ckbWrapMessage.Checked,
                                                                              btns.ToArray ()
                                                                              btns.ToArray ()
-                                                                            )
-                                                       }";
+                                                                            )}";
                                                }
                                                }
                                                else
                                                else
                                                {
                                                {
                                                    buttonPressedLabel.Text =
                                                    buttonPressedLabel.Text =
-                                                       $"{
-                                                           MessageBox.ErrorQuery (
+                                                       $"{MessageBox.ErrorQuery (
                                                                                   width,
                                                                                   width,
                                                                                   height,
                                                                                   height,
                                                                                   titleEdit.Text,
                                                                                   titleEdit.Text,
@@ -259,8 +255,7 @@ public class MessageBoxes : Scenario
                                                                                   defaultButton,
                                                                                   defaultButton,
                                                                                   (bool)ckbWrapMessage.Checked,
                                                                                   (bool)ckbWrapMessage.Checked,
                                                                                   btns.ToArray ()
                                                                                   btns.ToArray ()
-                                                                                 )
-                                                       }";
+                                                                                 )}";
                                                }
                                                }
                                            }
                                            }
                                            catch (FormatException)
                                            catch (FormatException)
@@ -268,8 +263,13 @@ public class MessageBoxes : Scenario
                                                buttonPressedLabel.Text = "Invalid Options";
                                                buttonPressedLabel.Text = "Invalid Options";
                                            }
                                            }
                                        };
                                        };
-        Win.Add (showMessageBoxButton);
+        app.Add (showMessageBoxButton);
+
+        app.Add (buttonPressedLabel);
+
+        Application.Run (app);
+        app.Dispose ();
 
 
-        Win.Add (buttonPressedLabel);
+        Application.Shutdown ();
     }
     }
 }
 }

+ 3 - 3
UnitTests/Views/LineViewTests.cs

@@ -10,7 +10,7 @@ public class LineViewTests
 
 
         Assert.Equal (Orientation.Horizontal, horizontal.Orientation);
         Assert.Equal (Orientation.Horizontal, horizontal.Orientation);
         Assert.Equal (Dim.Fill (), horizontal.Width);
         Assert.Equal (Dim.Fill (), horizontal.Width);
-        Assert.Equal (1, horizontal.Height);
+        Assert.Equal (1, horizontal.Frame.Height);
     }
     }
 
 
     [Fact]
     [Fact]
@@ -21,7 +21,7 @@ public class LineViewTests
 
 
         Assert.Equal (Orientation.Horizontal, horizontal.Orientation);
         Assert.Equal (Orientation.Horizontal, horizontal.Orientation);
         Assert.Equal (Dim.Fill (), horizontal.Width);
         Assert.Equal (Dim.Fill (), horizontal.Width);
-        Assert.Equal (1, horizontal.Height);
+        Assert.Equal (1, horizontal.Frame.Height);
     }
     }
 
 
     [Fact]
     [Fact]
@@ -32,6 +32,6 @@ public class LineViewTests
 
 
         Assert.Equal (Orientation.Vertical, vert.Orientation);
         Assert.Equal (Orientation.Vertical, vert.Orientation);
         Assert.Equal (Dim.Fill (), vert.Height);
         Assert.Equal (Dim.Fill (), vert.Height);
-        Assert.Equal (1, vert.Width);
+        Assert.Equal (1, vert.Frame.Width);
     }
     }
 }
 }

+ 2 - 2
UnitTests/Views/SpinnerViewTests.cs

@@ -104,8 +104,8 @@ public class SpinnerViewTests
         top.Add (view);
         top.Add (view);
         Application.Begin (top);
         Application.Begin (top);
 
 
-        Assert.Equal (1, view.Width);
-        Assert.Equal (1, view.Height);
+        Assert.Equal (1, view.Frame.Width);
+        Assert.Equal (1, view.Frame.Height);
 
 
         return view;
         return view;
     }
     }