Kaynağa Gözat

Fixed a moving window issue. Added OnLoad Action because there are settings that need to be accessed only once. (#375)

* Fixed a moving window issue. Added OnLoad Action because there are settings that need to be accessed only once.

* Fixes a layout issue that does not updated the Pos outside the bounds.

* Fixes a issue with other top-levels.

Co-authored-by: Miguel de Icaza <[email protected]>
BDisp 5 yıl önce
ebeveyn
işleme
df5bc9f0b8
2 değiştirilmiş dosya ile 33 ekleme ve 18 silme
  1. 16 12
      Example/demo.cs
  2. 17 6
      Terminal.Gui/Core.cs

+ 16 - 12
Example/demo.cs

@@ -446,7 +446,6 @@ static class Demo {
 			CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
 
 		//Application.UseSystemConsole = true;
-		Console.WindowHeight = 35;
 
 		Application.Init ();
 
@@ -454,11 +453,13 @@ static class Demo {
 
 		//Open ();
 #if true
+		int margin = 3;
 		var win = new Window ("Hello") {
 			X = 1,
 			Y = 1,
-			Width = Dim.Fill (),
-			Height = Dim.Fill () - 1
+
+			Width = Dim.Fill () - margin,
+			Height = Dim.Fill () - margin
 		};
 #else
 		var tframe = top.Frame;
@@ -528,7 +529,7 @@ static class Demo {
 		ShowEntries (win);
 
 		int count = 0;
-		ml = new Label (new Rect (3, 18, 47, 1), "Mouse: ");
+		ml = new Label (new Rect (3, 17, 47, 1), "Mouse: ");
 		Application.RootMouseEvent += delegate (MouseEvent me) {
 			ml.TextColor = Colors.TopLevel.Normal;
 			ml.Text = $"Mouse: ({me.X},{me.Y}) - {me.Flags} {count++}";
@@ -554,21 +555,24 @@ static class Demo {
 
 		win.Add (drag, dragText);
 #if true
-		// This currently causes a stack overflow, because it is referencing a window that has not had its size allocated yet
+		// FIXED: This currently causes a stack overflow, because it is referencing a window that has not had its size allocated yet
 
-		var bottom = new Label ("This should go on the bottom!");
+		var bottom = new Label ("This should go on the bottom of the same top-level!");
 		win.Add (bottom);
-
-		Application.OnResized = () => {
-			bottom.X = Pos.Left (win);
-			bottom.Y = Pos.Bottom (win);
+		var bottom2 = new Label ("This should go on the bottom of another top-level!");
+		top.Add (bottom2);
+
+		Application.OnLoad = () => {
+			bottom.X = win.X;
+			bottom.Y = Pos.Bottom (win) - Pos.Top (win) - margin;
+			bottom2.X = Pos.Left (win);
+			bottom2.Y = Pos.Bottom (win);
 		};
 #endif
 
-
 		top.Add (win);
 		//top.Add (menu);
-		top.Add (menu, statusBar, ml);
+		top.Add (menu, statusBar);
 		Application.Run ();
 	}
 }

+ 17 - 6
Terminal.Gui/Core.cs

@@ -1248,7 +1248,7 @@ namespace Terminal.Gui {
 		}
 
 		// https://en.wikipedia.org/wiki/Topological_sorting
-		static List<View> TopologicalSort (HashSet<View> nodes, HashSet<(View, View)> edges)
+		List<View> TopologicalSort (HashSet<View> nodes, HashSet<(View, View)> edges)
 		{
 			var result = new List<View> ();
 
@@ -1261,7 +1261,8 @@ namespace Terminal.Gui {
 				S.Remove (n);
 
 				// add n to tail of L
-				result.Add (n);
+				if (n != this?.SuperView)
+					result.Add (n);
 
 				// for each node m with an edge e from n to m do
 				foreach (var e in edges.Where (e => e.Item1.Equals (n)).ToList ()) {
@@ -1271,7 +1272,7 @@ namespace Terminal.Gui {
 					edges.Remove (e);
 
 					// if m has no other incoming edges then
-					if (edges.All (me => me.Item2.Equals (m) == false)) {
+					if (edges.All (me => me.Item2.Equals (m) == false) && m != this?.SuperView) {
 						// insert m into S
 						S.Add (m);
 					}
@@ -1325,11 +1326,15 @@ namespace Terminal.Gui {
 				if (v.LayoutStyle == LayoutStyle.Computed)
 					v.RelativeLayout (Frame);
 
-				if (this?.SuperView != v)
-					v.LayoutSubviews ();
+				v.LayoutSubviews ();
 				v.layoutNeeded = false;
 
 			}
+
+			if (SuperView == Application.Top && layoutNeeded && ordered.Count == 0 && LayoutStyle == LayoutStyle.Computed) {
+				RelativeLayout (Frame);
+			}
+
 			layoutNeeded = false;
 		}
 
@@ -1569,7 +1574,7 @@ namespace Terminal.Gui {
 				}
 				foreach (var view in Subviews) {
 					if (view.Frame.IntersectsWith (region)) {
-						//view.SetNeedsLayout ();
+						view.SetNeedsLayout ();
 						view.SetNeedsDisplay (view.Bounds);
 					}
 				}
@@ -2142,6 +2147,11 @@ namespace Terminal.Gui {
 			}
 		}
 
+		/// <summary>
+		/// Action that is invoked once at beginning.
+		/// </summary>
+		static public Action OnLoad;
+
 		/// <summary>
 		/// Building block API: Prepares the provided toplevel for execution.
 		/// </summary>
@@ -2176,6 +2186,7 @@ namespace Terminal.Gui {
 			if (toplevel.LayoutStyle == LayoutStyle.Computed)
 				toplevel.RelativeLayout (new Rect (0, 0, Driver.Cols, Driver.Rows));
 			toplevel.LayoutSubviews ();
+			OnLoad?.Invoke ();
 			toplevel.WillPresent ();
 			Redraw (toplevel);
 			toplevel.PositionCursor ();