Tig Kindel 1 year ago
parent
commit
679ec90106
3 changed files with 72 additions and 18 deletions
  1. 15 5
      Terminal.Gui/View/ViewLayout.cs
  2. 15 7
      Terminal.Gui/Views/Dialog.cs
  3. 42 6
      UICatalog/Scenarios/DimAutoDemo.cs

+ 15 - 5
Terminal.Gui/View/ViewLayout.cs

@@ -741,6 +741,15 @@ namespace Terminal.Gui {
 						newDimension = AutoSize && autosizeDimension > newDimension ? autosizeDimension : newDimension;
 					}
 					newLocation = pos.Anchor (superviewDimension - newDimension);
+#if false
+					if (dim == null) {
+						newDimension = AutoSize ? autosizeDimension : superviewDimension;
+					} else {
+						newDimension = Math.Max (CalculateNewDimension (width, dim, 0, dim.Anchor (superviewDimension), autosizeDimension), 0);
+						newDimension = AutoSize && autosizeDimension > newDimension ? autosizeDimension : newDimension;
+					}
+					newLocation = pos.Anchor (superviewDimension - newDimension);
+#endif
 					break;
 
 				case Pos.PosCombine combine:
@@ -755,8 +764,8 @@ namespace Terminal.Gui {
 					newDimension = Math.Max (CalculateNewDimension (width, dim, newLocation, superviewDimension, autosizeDimension), 0);
 					break;
 
-				case Pos.PosAbsolute:
 				case Pos.PosAnchorEnd:
+				case Pos.PosAbsolute:
 				case Pos.PosFactor:
 				case Pos.PosFunc:
 				case Pos.PosView:
@@ -769,8 +778,10 @@ namespace Terminal.Gui {
 			}
 
 			// Recursively calculates the new dimension (width or height) of the given Dim given:
-			//   the current location (x or y)
-			//   the current dimension (width or height)
+			//   width: true for width, false for height
+			//   location: the current location (x or y)
+			//   dimension: the current dimension (width or height)
+			//   autosize: the size to use if autosize = true
 			int CalculateNewDimension (bool width, Dim d, int location, int dimension, int autosize)
 			{
 				int newDimension;
@@ -796,14 +807,13 @@ namespace Terminal.Gui {
 
 				case Dim.DimAuto auto:
 					var thickness = GetFramesThickness ();
+					newDimension = CalculateNewDimension (width, auto._min, location, dimension, autosize);
 					if (width) {
 						var furthestRight = Subviews.Count == 0 ? 0 : Subviews.Max (v => v.Frame.X + v.Frame.Width);
-						//newDimension = furthestRight + thickness.Left + thickness.Right;
 						newDimension = int.Max (furthestRight + thickness.Left + thickness.Right, auto._min?.Anchor (SuperView?.Bounds.Width ?? 0) ?? 0);
 					} else {
 						var furthestBottom = Subviews.Count == 0 ? 0 : Subviews.Max (v => v.Frame.Y + v.Frame.Height);
 						newDimension = int.Max (furthestBottom + thickness.Top + thickness.Bottom, auto._min?.Anchor (SuperView?.Bounds.Height ?? 0) ?? 0);
-						//newDimension = furthestBottom + thickness.Top + thickness.Bottom;
 					}
 					break;
 

+ 15 - 7
Terminal.Gui/Views/Dialog.cs

@@ -21,7 +21,8 @@ public class Dialog : Window {
 	/// <remarks>
 	/// This property can be set in a Theme.
 	/// </remarks>
-	[SerializableConfigurationProperty (Scope = typeof (ThemeScope))] [JsonConverter (typeof (JsonStringEnumConverter))]
+	[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
+	[JsonConverter (typeof (JsonStringEnumConverter))]
 	public static ButtonAlignments DefaultButtonAlignment { get; set; } = ButtonAlignments.Center;
 
 	// TODO: Reenable once border/borderframe design is settled
@@ -61,8 +62,8 @@ public class Dialog : Window {
 		Y = Pos.Center ();
 		ValidatePosDim = true;
 
-		Width = Dim.Percent (85);
-		Height = Dim.Percent (85);
+		Width = Dim.Percent (85);// Dim.Auto (min: Dim.Percent (10));
+		Height = Dim.Percent (85);//Dim.Auto (min: Dim.Percent (50));
 
 		ColorScheme = Colors.Dialog;
 
@@ -74,11 +75,18 @@ public class Dialog : Window {
 				AddButton (b);
 			}
 		}
+	}
 
-		LayoutComplete += (s, args) => {
-			LayoutButtons ();
-		};
-
+	bool inLayout = false;
+	public override void LayoutSubviews ()
+	{
+		if (inLayout) {
+			return;
+		}
+		inLayout = true;
+		LayoutButtons ();
+		base.LayoutSubviews ();
+		inLayout = false;
 	}
 
 	/// <summary>

+ 42 - 6
UICatalog/Scenarios/DimAutoDemo.cs

@@ -31,12 +31,13 @@ public class DimAutoDemo : Scenario {
 		var resetButton = new Button () {
 			Text = "P_ut Button Back",
 			X = Pos.Center (),
-			Y = Pos.Bottom(label)
+			Y = Pos.Bottom (label)
 		};
 
-		var movingButton = new Button () { Text = "Press to make button move down.", 
-			X = 0, 
-			Y = Pos.Bottom (resetButton), 
+		var movingButton = new Button () {
+			Text = "Press to make button move down.",
+			X = 0,
+			Y = Pos.Bottom (resetButton),
 			Width = 10
 		};
 		movingButton.Clicked += (s, e) => {
@@ -51,12 +52,47 @@ public class DimAutoDemo : Scenario {
 			Title = "Type in the TextField to make View grow.",
 			X = 3,
 			Y = 3,
-			Width = Dim.Auto (min: Dim.Percent(50)),
+			Width = Dim.Auto (min: Dim.Percent (50)),
 			Height = Dim.Auto (min: 10)
 		};
 		view.ValidatePosDim = true;
 		view.Add (textField, label, resetButton, movingButton);
 
-		Application.Top.Add (view);
+		var dlgButton = new Button () {
+			Text = "Open Test _Dialog",
+			X = Pos.Right (view),
+			Y = Pos.Top (view)
+		};
+		dlgButton.Clicked += DlgButton_Clicked;
+
+		Application.Top.Add (view, dlgButton);
+	}
+	
+	private void DlgButton_Clicked (object sender, System.EventArgs e)
+	{
+		var dlg = new Dialog () {
+			Title = "Test Dialog"
+		};
+
+		//var ok = new Button ("Bye") { IsDefault = true };
+		//ok.Clicked += (s, _) => Application.RequestStop (dlg);
+		//dlg.AddButton (ok);
+
+		//var cancel = new Button ("Abort") { };
+		//cancel.Clicked += (s, _) => Application.RequestStop (dlg);
+		//dlg.AddButton (cancel);
+
+		var label = new Label ("This is a label. Press Esc to close.") {
+			X = 0,
+			Y = 2,
+		};
+
+		var btn = new Button ("Button") {
+			X = 0,// Pos.Center (),
+			Y = Pos.AnchorEnd (1)
+		};
+		dlg.Add (btn);
+		dlg.Add (label);
+		Application.Run (dlg);
 	}
 }