From b90259349e03e7475a92776c2811e1019f82e8a3 Mon Sep 17 00:00:00 2001 From: Tigger Kindel Date: Wed, 29 Mar 2023 20:45:41 -0600 Subject: [PATCH] partial impl of fixing #2465 --- Terminal.Gui/Core/View.cs | 138 +++++++---- UnitTests/Core/AbsoluteLayoutTests.cs | 327 ++++++++++++++++++++++++++ 2 files changed, 425 insertions(+), 40 deletions(-) create mode 100644 UnitTests/Core/AbsoluteLayoutTests.cs diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 8ea963ab18..cf9bc886e8 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -446,70 +446,102 @@ public override bool CanFocus { public virtual bool WantContinuousButtonPressed { get; set; } /// - /// Gets or sets the frame for the view. The frame is relative to the view's container (). + /// Gets or sets the location and size of the view, relative to its . /// - /// The frame. + /// The rectangle describing the location and size of the view, in coordinates relative to the . /// /// - /// Change the Frame when using the layout style to move or resize views. + /// Change the Frame when using the layout style to move or resize views. /// /// - /// Altering the Frame of a view will trigger the redrawing of the - /// view as well as the redrawing of the affected regions of the . + /// If is , altering the Frame will + /// change the layout style to and , , and + /// will be set to the values of the Frame (using and ). + /// + /// + /// Altering the Frame will eventually (when the view is next drawn) cause the and methods to be called. /// /// public virtual Rect Frame { get => frame; set { frame = new Rect (value.X, value.Y, Math.Max (value.Width, 0), Math.Max (value.Height, 0)); + X = frame.X; + Y = frame.Y; + Width = frame.Width; + Height = frame.Height; TextFormatter.Size = GetSizeNeededForTextAndHotKey (); SetNeedsLayout (); SetNeedsDisplay (); } } - ///// - ///// Gets an enumerator that enumerates the subviews in this view. - ///// - ///// The enumerator. - //public IEnumerator GetEnumerator () - //{ - // foreach (var v in InternalSubviews) - // yield return v; - //} - LayoutStyle layoutStyle; /// - /// Controls how the View's is computed during the LayoutSubviews method, if the style is set to - /// , - /// LayoutSubviews does not change the . If the style is - /// the is updated using - /// the , , , and properties. + /// Gets or sets what the view is using. /// + /// + /// + /// Setting this property to will cause to determine the size and position of the view. and will be + /// set to using . + /// + /// + /// Setting this property to will cause the view to use the method to + /// size and position of the view. If either of the and properties are `null` they will be set to using + /// the current value of . + /// If either of the and properties are `null` they will be set to using . + /// + /// /// The layout style. public LayoutStyle LayoutStyle { - get => layoutStyle; + get { + if ((X == null || X is Pos.PosAbsolute) && (Y == null || Y is Pos.PosAbsolute) && + (Width == null || Width is Dim.DimAbsolute) && (Height == null || Height is Dim.DimAbsolute)) { + return LayoutStyle.Absolute; + } + return LayoutStyle.Computed; + } set { - layoutStyle = value; + switch (value) { + case LayoutStyle.Absolute: + X = Frame.X; + Y = Frame.Y; + Width = Frame.Width; + Height = Frame.Height; + break; + + case LayoutStyle.Computed: + if (X == null) X = Frame.X; + if (Y == null) Y = Frame.Y; + if (Width == null) Width = Frame.Width; + if (Height == null) Height = Frame.Height; + break; + } + SetNeedsLayout (); } } /// - /// The bounds represent the View-relative rectangle used for this view; the area inside of the view. + /// The bounds represent the View-relative rectangle used for this view; the area inside of the view where subviews and content are presented. /// - /// The bounds. + /// The rectangle describing the location and size of the area where the views' subviews and content are drawn. /// /// - /// Updates to the Bounds update the , - /// and has the same side effects as updating the . + /// If is the value of Bounds is indterminate until the + /// view has been laid out (see has been called. + /// + /// + /// Updates to the Bounds updates , and has the same side effects as updating the . + /// + /// + /// Altering the Bounds will eventually (when the view is next drawn) cause the and methods to be called. /// /// /// Because coordinates are relative to the upper-left corner of the , /// the coordinates of the upper-left corner of the rectangle returned by this property are (0,0). - /// Use this property to obtain the size and coordinates of the client area of the - /// control for tasks such as drawing on the surface of the control. + /// Use this property to obtain the size of the area of the view for tasks such as drawing the view's contents. /// /// public Rect Bounds { @@ -520,11 +552,17 @@ public Rect Bounds { Pos x, y; /// - /// Gets or sets the X position for the view (the column). Only used if the is . + /// Gets or sets the X position for the view (the column). /// /// The X Position. /// - /// If is changing this property has no effect and its value is indeterminate. + /// + /// Changing this property will eventually (when the view is next drawn) cause the and methods to be called. + /// + /// + /// If is changing this property will cause the to be updated. If + /// the new value is not of type the will change to . + /// /// public Pos X { get => x; @@ -540,11 +578,17 @@ public Pos X { } /// - /// Gets or sets the Y position for the view (the row). Only used if the is . + /// Gets or sets the Y position for the view (the row). /// - /// The y position (line). + /// The Y Position. /// - /// If is changing this property has no effect and its value is indeterminate. + /// + /// Changing this property will eventually (when the view is next drawn) cause the and methods to be called. + /// + /// + /// If is changing this property will cause the to be updated. If + /// the new value is not of type the will change to . + /// /// public Pos Y { get => y; @@ -561,11 +605,17 @@ public Pos Y { Dim width, height; /// - /// Gets or sets the width of the view. Only used the is . + /// Gets or sets the width of the view. /// - /// The width. + /// The width of the view (the number of columns). /// - /// If is changing this property has no effect and its value is indeterminate. + /// + /// Changing this property will eventually (when the view is next drawn) cause the and methods to be called. + /// + /// + /// If is changing this property will cause the to be updated. If + /// the new value is not of type the will change to . + /// /// public Dim Width { get => width; @@ -588,10 +638,18 @@ public Dim Width { } /// - /// Gets or sets the height of the view. Only used the is . + /// Gets or sets the height of the view. /// - /// The height. - /// If is changing this property has no effect and its value is indeterminate. + /// The width of the view (the number of rows). + /// + /// + /// Changing this property will eventually (when the view is next drawn) cause the and methods to be called. + /// + /// + /// If is changing this property will cause the to be updated. If + /// the new value is not of type the will change to . + /// + /// public Dim Height { get => height; set { @@ -827,7 +885,7 @@ void SetInitialProperties (ustring text, Rect rect, LayoutStyle layoutStyle = La Text = text; - OnResizeNeeded (); + //OnResizeNeeded (); } /// diff --git a/UnitTests/Core/AbsoluteLayoutTests.cs b/UnitTests/Core/AbsoluteLayoutTests.cs new file mode 100644 index 0000000000..2670fe8136 --- /dev/null +++ b/UnitTests/Core/AbsoluteLayoutTests.cs @@ -0,0 +1,327 @@ +using NStack; +using System; +using System.Collections.Generic; +using System.Xml.Linq; +using Terminal.Gui.Graphs; +using Xunit; +using Xunit.Abstractions; +//using GraphViewTests = Terminal.Gui.Views.GraphViewTests; + +// Alias Console to MockConsole so we don't accidentally use Console +using Console = Terminal.Gui.FakeConsole; + +namespace Terminal.Gui.CoreTests { + public class AbsoluteLayoutTests { + readonly ITestOutputHelper output; + + public AbsoluteLayoutTests (ITestOutputHelper output) + { + this.output = output; + } + + [Fact] + public void AbsoluteLayout_Constructor () + { + var frame = new Rect (1, 2, 3, 4); + var v = new View (frame); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + Assert.Equal (frame, v.Frame); + Assert.Equal (new Rect(0, 0, frame.Width, frame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout + Assert.Equal ($"Absolute(0)", v.X.ToString ()); + Assert.Null (v.X); + Assert.Null (v.Y); + Assert.Null (v.Height); + Assert.Null (v.Width); + + v = new View (frame, "v"); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + Assert.Equal (frame, v.Frame); + Assert.Equal (new Rect (0, 0, frame.Width, frame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout + Assert.Null (v.X); + Assert.Null (v.Y); + Assert.Null (v.Height); + Assert.Null (v.Width); + + v = new View (frame.X, frame.Y, "v"); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + // BUGBUG: v2 - I think the default size should be 0,0 + Assert.Equal (new Rect(frame.X, frame.Y, 1, 1), v.Frame); + Assert.Equal (new Rect (0, 0, 1, 1), v.Bounds); // With Absolute Bounds *is* deterministic before Layout + Assert.Null (v.X); + Assert.Null (v.Y); + Assert.Null (v.Height); + Assert.Null (v.Width); + } + + + [Fact] + public void AbsoluteLayout_Change_Frame () + { + var frame = new Rect (1, 2, 3, 4); + var newFrame = new Rect (1, 2, 30, 40); + + var v = new View (frame); + v.Frame = newFrame; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + Assert.Equal (newFrame, v.Frame); + Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout + Assert.Null (v.X); + Assert.Null (v.Y); + Assert.Null (v.Height); + Assert.Null (v.Width); + + v = new View (frame.X, frame.Y, "v"); + v.Frame = newFrame; + Assert.Equal (newFrame, v.Frame); + Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout + Assert.Null (v.X); + Assert.Null (v.Y); + Assert.Null (v.Height); + Assert.Null (v.Width); + + newFrame = new Rect (10, 20, 30, 40); + v = new View (frame); + v.Frame = newFrame; + Assert.Equal (newFrame, v.Frame); + Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout + Assert.Null (v.X); + Assert.Null (v.Y); + Assert.Null (v.Height); + Assert.Null (v.Width); + + v = new View (frame.X, frame.Y, "v"); + v.Frame = newFrame; + Assert.Equal (newFrame, v.Frame); + Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout + Assert.Null (v.X); + Assert.Null (v.Y); + Assert.Null (v.Height); + Assert.Null (v.Width); + + } + + + [Fact] + public void AbsoluteLayout_Change_Height_or_Width_Absolute () + { + var frame = new Rect (1, 2, 3, 4); + var newFrame = new Rect (1, 2, 30, 40); + + var v = new View (frame); + v.Height = newFrame.Height; + v.Width = newFrame.Width; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + Assert.Equal (newFrame, v.Frame); + Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout + Assert.Null (v.X); + Assert.Null (v.Y); + Assert.Equal ($"Absolute({newFrame.Height})", v.Height.ToString()); + Assert.Equal ($"Absolute({newFrame.Width})", v.Width.ToString ()); + } + + [Fact] + public void AbsoluteLayout_Change_Height_or_Width_NotAbsolute () + { + var v = new View (Rect.Empty); + v.Height = Dim.Fill (); + v.Width = Dim.Fill (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + } + + [Fact] + public void AbsoluteLayout_Change_Height_or_Width_Null () + { + var v = new View (Rect.Empty); + v.Height = null; + v.Width = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + } + + [Fact] + public void AbsoluteLayout_Change_X_or_Y_Absolute () + { + var frame = new Rect (1, 2, 3, 4); + var newFrame = new Rect (10, 20, 3, 4); + + var v = new View (frame); + v.X = newFrame.X; + v.Y = newFrame.Y; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + Assert.Equal (newFrame, v.Frame); + Assert.Equal (new Rect (0, 0, newFrame.Width, newFrame.Height), v.Bounds); // With Absolute Bounds *is* deterministic before Layout + Assert.Equal ($"Absolute({newFrame.X})", v.X.ToString ()); + Assert.Equal ($"Absolute({newFrame.Y})", v.Y.ToString ()); + Assert.Null (v.Height); + Assert.Null (v.Width); + } + + [Fact] + public void AbsoluteLayout_Change_X_or_Y_NotAbsolute () + { + var v = new View (Rect.Empty); + v.X = Pos.Center (); + v.Y = Pos.Center (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + } + + [Fact] + public void AbsoluteLayout_Change_X_or_Y_Null () + { + var v = new View (Rect.Empty); + v.X = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + + v = new View (Rect.Empty); + v.X = Pos.Center (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + + v.X = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + + v = new View (Rect.Empty); + v.Y = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + + v = new View (Rect.Empty); + v.Y = Pos.Center (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + + v.Y = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + } + + [Fact] + public void AbsoluteLayout_Change_X_Y_Height_Width_Absolute () + { + var v = new View (Rect.Empty); + v.X = 1; + v.Y = 2; + v.Height = 3; + v.Width = 4; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + + v = new View (Rect.Empty); + v.X = Pos.Center (); + v.Y = Pos.Center (); + v.Width = Dim.Fill (); + v.Height = Dim.Fill (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + + // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte? + v.X = null; + v.Y = null; + v.Height = null; + v.Width = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed?? + + v = new View (Rect.Empty); + v.X = Pos.Center (); + v.Y = Pos.Center (); + v.Width = Dim.Fill (); + v.Height = Dim.Fill (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + + // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte? + v.X = 1; + v.Y = null; + v.Height = null; + v.Width = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed?? + + v = new View (Rect.Empty); + v.X = Pos.Center (); + v.Y = Pos.Center (); + v.Width = Dim.Fill (); + v.Height = Dim.Fill (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + + // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte? + v.X = null; + v.Y = 2; + v.Height = null; + v.Width = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed?? + + v = new View (Rect.Empty); + v.X = Pos.Center (); + v.Y = Pos.Center (); + v.Width = Dim.Fill (); + v.Height = Dim.Fill (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + + // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte? + v.X = null; + v.Y = null; + v.Height = 3; + v.Width = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed?? + + v = new View (Rect.Empty); + v.X = Pos.Center (); + v.Y = Pos.Center (); + v.Width = Dim.Fill (); + v.Height = Dim.Fill (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + + // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte? + v.X = null; + v.Y = null; + v.Height = null; + v.Width = 4; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed?? + } + + [Fact] + public void AbsoluteLayout_Change_X_Y_Height_Width_Null () + { + var v = new View (Rect.Empty); + v.X = null; + v.Y = null; + v.Height = null; + v.Width = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); + + v = new View (Rect.Empty); + v.X = Pos.Center (); + v.Y = Pos.Center (); + v.Width = Dim.Fill (); + v.Height = Dim.Fill (); + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // BUGBUG: v2 - Changing the Height or Width should change the LayoutStyle + + // BUGBUG: v2 - If all of X, Y, Width, and Height are null or Absolute(n), isn't that the same as LayoutStyle.Absoulte? + v.X = null; + v.Y = null; + v.Height = null; + v.Width = null; + Assert.True (v.LayoutStyle == LayoutStyle.Absolute); // We never automatically change to Absolute from Computed?? + } + + [Fact] + public void AbsoluteLayout_Layout () + { + var superRect = new Rect (0, 0, 100, 100); + var super = new View (superRect, "super"); + Assert.True (super.LayoutStyle == LayoutStyle.Absolute); + var v1 = new View () { + X = 0, + Y = 0, + Width = 10, + Height = 10 + }; + Assert.True (v1.LayoutStyle == LayoutStyle.Absolute); + + var v2 = new View () { + X = 10, + Y = 10, + Width = 10, + Height = 10 + }; + Assert.True (v2.LayoutStyle == LayoutStyle.Absolute); + + super.Add (v1, v2); + super.LayoutSubviews (); + Assert.Equal (new Rect (0, 0, 10, 10), v1.Frame); + Assert.Equal (new Rect (10, 10, 10, 10), v2.Frame); + } + } +}