From df5bc9f0b8696978f635c71f7512e2936a8d4c5b Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 18 Apr 2020 18:37:50 +0100 Subject: [PATCH] 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 --- Example/demo.cs | 26 +++++++++++++++----------- Terminal.Gui/Core.cs | 23 +++++++++++++++++------ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Example/demo.cs b/Example/demo.cs index c44b23ae7..0ec5cc9ae 100644 --- a/Example/demo.cs +++ b/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); + var bottom2 = new Label ("This should go on the bottom of another top-level!"); + top.Add (bottom2); - Application.OnResized = () => { - bottom.X = Pos.Left (win); - bottom.Y = Pos.Bottom (win); + 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 (); } } diff --git a/Terminal.Gui/Core.cs b/Terminal.Gui/Core.cs index c93af8908..36b18410e 100644 --- a/Terminal.Gui/Core.cs +++ b/Terminal.Gui/Core.cs @@ -1248,7 +1248,7 @@ namespace Terminal.Gui { } // https://en.wikipedia.org/wiki/Topological_sorting - static List TopologicalSort (HashSet nodes, HashSet<(View, View)> edges) + List TopologicalSort (HashSet nodes, HashSet<(View, View)> edges) { var result = new List (); @@ -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 { } } + /// + /// Action that is invoked once at beginning. + /// + static public Action OnLoad; + /// /// Building block API: Prepares the provided toplevel for execution. /// @@ -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 ();